Skip to content

Commit

Permalink
fix(compat): fix burning
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanswrt committed Jul 14, 2021
1 parent de8eadd commit 5c6358d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
46 changes: 20 additions & 26 deletions compat/v0/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,25 +456,34 @@ func V1TxParamsFromTx(ctx context.Context, params ParamsSubmitTx, bindings *bind
return jsonrpc.ParamsSubmitTx{}, err
}

// Calculate tx hash for v0 tx
txHash, err := V0TxHashFromTx(params.Tx)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
var txHash B32

submitVersion := tx.Version0
// Convert the v0 tx to v1 transaction
if IsShiftIn(params.Tx.To) {
v1Tx, txHash, err = V1TxFromV0Mint(ctx, params.Tx, bindings, pubkey)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
} else {
v1Tx, err = V1TxFromV0Burn(ctx, params.Tx, bindings, network)
}
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}

submitVersion = tx.Version1

// Calculate tx hash for v0 tx
txHash, err = V0TxHashFromTx(params.Tx)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
}

copy(params.Tx.Hash[:], txHash[:])

h, err := tx.NewTxHash(tx.Version0, v1Tx.Selector, v1Tx.Input)
// calculate the new tx format hash
h, err := tx.NewTxHash(submitVersion, v1Tx.Selector, v1Tx.Input)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
Expand Down Expand Up @@ -568,7 +577,7 @@ func V1TxFromV0Mint(ctx context.Context, v0tx Tx, bindings *binding.Binding, pub
selector := tx.Selector(fmt.Sprintf("%s/toEthereum", v0tx.To[0:3]))
utxo := v0tx.In.Get("utxo").Value.(ExtBtcCompatUTXO)
vout := utxo.VOut.Int.Uint64()
txidB, err := utxo.TxHash.MarshalBinary() //chainhash.NewHashFromStr(hex.EncodeToString(utxo.TxHash[:]))
txidB, err := utxo.TxHash.MarshalBinary()
if err != nil {
return tx.Tx{}, B32{}, err
}
Expand Down Expand Up @@ -724,23 +733,8 @@ func V1TxFromV0Burn(ctx context.Context, v0tx Tx, bindings *binding.Binding, net

func V0TxHashFromTx(tx Tx) (B32, error) {
var hash B32
// Incorrect - we need to use the nhash, not the nonce
if IsShiftIn(tx.To) {
return hash, fmt.Errorf("cannot handle shift-ins")
// payload := pack.NewBytes(tx.In.Get("p").Value.(ExtEthCompatPayload).Value[:])
// phash := engine.Phash(payload)
// tokenAddr := tx.In.Get("token").Value.(ExtEthCompatAddress)
// to := tx.In.Get("to").Value.(ExtEthCompatAddress)
// n := tx.In.Get("n").Value.(B32)
// var nonce pack.Bytes32
// copy(nonce[:], n[:])

// ghash, err := engine.V0Ghash(tokenAddr[:], phash, to[:], nonce)
// if err != nil {
// return B32{}, err
// }
// utxo := tx.In.Get("utxo").Value.(ExtBtcCompatUTXO)
// copy(hash[:], crypto.Keccak256([]byte(fmt.Sprintf("txHash_%s_%s_%s_%d", tx.To, ghash, utxo.TxHash, utxo.VOut.Int.Int64()))))
panic("cannot handle shift-ins")
} else {
ref := tx.In.Get("ref").Value.(U64)
copy(hash[:], crypto.Keccak256([]byte(fmt.Sprintf("txHash_%s_%d", tx.To, ref.Int.Int64()))))
Expand Down
27 changes: 16 additions & 11 deletions compat/v0/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var _ = Describe("Compat V0", func() {
Expect(shardsResponse.Shards[0].Gateways[0].PubKey).Should(Equal("Akwn5WEMcB2Ff_E0ZOoVks9uZRvG_eFD99AysymOc5fm"))
})

It("should convert a v0 BTC Burn ParamsSubmitTx into an empty v1 ParamsSubmitTx", func() {
It("should convert a v0 BTC Burn ParamsSubmitTx into an v1 ParamsSubmitTx", func() {
params := testutils.MockBurnParamSubmitTxV0BTC()
store, _, bindings, pubkey := init(params, false)
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -170,8 +170,10 @@ var _ = Describe("Compat V0", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(storedHash).Should(Equal(hash))

// v0 hash should have a mapping in the store
v0Hash, err := v0.V0TxHashFromTx(params.Tx)
ghash := v1.Tx.Input.Get("ghash").(pack.Bytes32)
txid := v1.Tx.Input.Get("txid").(pack.Bytes)
txindex := v1.Tx.Input.Get("txindex").(pack.U32)
v0Hash := v0.MintTxHash(v1.Tx.Selector, ghash, txid, txindex)
Expect(err).ShouldNot(HaveOccurred())

// btc txhash mapping
Expand All @@ -180,7 +182,7 @@ var _ = Describe("Compat V0", func() {
Expect(keys).Should(ContainElement(v0Hash.String()))

// v1 hash should be correct
v1Hash, err := tx.NewTxHash(tx.Version1, v1.Tx.Selector, v1.Tx.Input)
v1Hash, err := tx.NewTxHash(tx.Version0, v1.Tx.Selector, v1.Tx.Input)
Expect(err).ShouldNot(HaveOccurred())
Expect(hash).To(Equal(v1Hash.String()))
})
Expand All @@ -206,8 +208,10 @@ var _ = Describe("Compat V0", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(storedHash).Should(Equal(hash))

// v0 hash should have a mapping in the store
v0Hash, err := v0.V0TxHashFromTx(params.Tx)
ghash := v1.Tx.Input.Get("ghash").(pack.Bytes32)
txid := v1.Tx.Input.Get("txid").(pack.Bytes)
txindex := v1.Tx.Input.Get("txindex").(pack.U32)
v0Hash := v0.MintTxHash(v1.Tx.Selector, ghash, txid, txindex)
Expect(err).ShouldNot(HaveOccurred())

// btc txhash mapping
Expand All @@ -216,7 +220,7 @@ var _ = Describe("Compat V0", func() {
Expect(keys).Should(ContainElement(v0Hash.String()))

// v1 hash should be correct
v1Hash, err := tx.NewTxHash(tx.Version1, v1.Tx.Selector, v1.Tx.Input)
v1Hash, err := tx.NewTxHash(tx.Version0, v1.Tx.Selector, v1.Tx.Input)
Expect(err).ShouldNot(HaveOccurred())
Expect(hash).To(Equal(v1Hash.String()))
})
Expand All @@ -242,17 +246,18 @@ var _ = Describe("Compat V0", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(hash).Should(Equal(storedHash))

// v0 hash should have a mapping in the store
v0Hash, err := v0.V0TxHashFromTx(params.Tx)
Expect(err).ShouldNot(HaveOccurred())
ghash := v1.Tx.Input.Get("ghash").(pack.Bytes32)
txid := v1.Tx.Input.Get("txid").(pack.Bytes)
txindex := v1.Tx.Input.Get("txindex").(pack.U32)
v0Hash := v0.MintTxHash(v1.Tx.Selector, ghash, txid, txindex)

// btc txhash mapping
keys, err = client.Keys("*").Result()
Expect(err).ShouldNot(HaveOccurred())
Expect(keys).Should(ContainElement(v0Hash.String()))

// v1 hash should be correct
v1Hash, err := tx.NewTxHash(tx.Version1, v1.Tx.Selector, v1.Tx.Input)
v1Hash, err := tx.NewTxHash(tx.Version0, v1.Tx.Selector, v1.Tx.Input)
Expect(err).ShouldNot(HaveOccurred())
Expect(hash).To(Equal(v1Hash.String()))
})
Expand Down
17 changes: 8 additions & 9 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ func (resolver *Resolver) QueryBlocks(ctx context.Context, id interface{}, param
func (resolver *Resolver) SubmitTx(ctx context.Context, id interface{}, params *jsonrpc.ParamsSubmitTx, req *http.Request) jsonrpc.Response {
// Check if the tx is a v1 tx or v0 tx.
txVersion := params.Tx.Version
// The comment below is incorrect, we need to maintain the tx version to create the correct hashes on the darknode
// The darknode has a faulty implementation of tx version limiting, so we can still submit v0 txes
//
// if params.Tx.Version == tx.Version0{
// // We need to always make sure the tx to submit is a v1 tx as
// // darknode won't accept v0 tx.
// params.Tx.Version = tx.Version1
// }

if params.Tx.Version == tx.Version0 && params.Tx.Selector.IsBurn() {
// When burning, we need to always make sure the tx to submit is a v1 tx as
// darknode won't accept v0 tx.
params.Tx.Version = tx.Version1
}
response := resolver.handleMessage(ctx, id, jsonrpc.MethodSubmitTx, *params, req, true)

if txVersion != tx.Version0 {
Expand All @@ -92,10 +90,11 @@ func (resolver *Resolver) SubmitTx(ctx context.Context, id interface{}, params *
if response.Error != nil {
return response
}

v0tx, err := v0.TxFromV1Tx(params.Tx, false, resolver.bindings)
if err != nil {
resolver.logger.Errorf("[responder] cannot convert v1 tx to v0, %v", err)
jsonErr := jsonrpc.NewError(jsonrpc.ErrorCodeInternal, "fail to convert v1 tx to v0", nil)
jsonErr := jsonrpc.NewError(jsonrpc.ErrorCodeInternal, "failed to convert v1 tx to v0", nil)
return jsonrpc.NewResponse(id, nil, &jsonErr)
}

Expand Down
9 changes: 7 additions & 2 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,13 @@ var _ = Describe("Resolver", func() {
paramsJSON, err := json.Marshal(params)
Expect(err).ShouldNot(HaveOccurred())

v0Hash, err := v0.V0TxHashFromTx(params.Tx)
// It's a bit of a pain to make this robustly calculatable, so lets use the mock
// v0 tx's v0 hash directly
v0HashString := "fEwRnmZAjz6uzPZFGwYSa4OK8xtHVl2nsncCHvV0aKE="
v0HashBytes, err := base64.StdEncoding.DecodeString(v0HashString)
Expect(err).ShouldNot(HaveOccurred())
v0Hash := [32]byte{}
copy(v0Hash[:], v0HashBytes[:])

req, resp := validator.ValidateRequest(innerCtx, &http.Request{}, jsonrpc.Request{
Version: "2.0",
Expand All @@ -197,7 +202,7 @@ var _ = Describe("Resolver", func() {
resp = resolver.SubmitTx(ctx, nil, (req).(*jsonrpc.ParamsSubmitTx), nil)
Expect(resp.Error).Should(BeNil())

hashS, err := client.Get(v0Hash.String()).Result()
hashS, err := client.Get(v0HashString).Result()
Expect(err).ShouldNot(HaveOccurred())
Expect(hashS).ShouldNot(Equal(nil))

Expand Down
1 change: 0 additions & 1 deletion resolver/txChecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func (tc *txchecker) Run() {
err := tc.verifier.VerifyTx(ctx, params.Tx)
cancel()
if err != nil {
tc.logger.Errorf("lightnode validation fail %+v", params.Tx)
req.RespondWithErr(jsonrpc.ErrorCodeInvalidParams, err)
continue
}
Expand Down
1 change: 0 additions & 1 deletion resolver/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (validator *LightnodeValidator) ValidateRequest(ctx context.Context, r *htt
Message: fmt.Sprintf("invalid params: %v", err),
})
}
validator.logger.Printf("cast params {%+v}", castParams)
raw, err := json.Marshal(castParams)
if err != nil {
return nil, jsonrpc.NewResponse(req.ID, nil, &jsonrpc.Error{
Expand Down

0 comments on commit 5c6358d

Please sign in to comment.