Skip to content

Commit

Permalink
Merge branch 'fix/submitTxResponse' into fix/decodeBurnEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Feb 16, 2021
2 parents f26cb2e + dcfc4b9 commit 34b185d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
61 changes: 53 additions & 8 deletions compat/v0/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,49 @@ func ShardsResponseFromState(state jsonrpc.ResponseQueryState) (ResponseQuerySha
return resp, nil
}

// TxFromV1Tx takes a V1 Tx and converts it to a V0 Tx, the given tx has to be a mint tx.
func BurnTxFromV1Tx(t tx.Tx, bindings txengine.Bindings) (Tx, error) {
tx := Tx{}

//nonce is ref in byte format
nonce := t.Input.Get("nonce").(pack.Bytes32)
ref := pack.NewU256(nonce)

tx.Hash = BurnTxHash(t.Selector, ref)

tx.To = Address(ToFromV1Selector(t.Selector))

tx.In.Set(Arg{
Name: "ref",
Type: "u64",
Value: U64{Int: ref.Int()},
})

to := t.Input.Get("to").(pack.String)

tx.In.Set(Arg{
Name: "to",
Type: "b",
Value: B(to),
})

inamount := t.Input.Get("amount").(pack.U256)
castamount := U256{Int: inamount.Int()}

tx.In.Set(Arg{
Name: "amount",
Type: "u256",
Value: castamount,
})

return tx, nil
}

// TxFromV1Tx takes a V1 Tx and converts it to a V0 Tx.
func TxFromV1Tx(t tx.Tx, hasOut bool, bindings txengine.Bindings) (Tx, error) {
if t.Selector.IsBurn() || t.Selector.IsRelease() {
return BurnTxFromV1Tx(t, bindings)
}

tx := Tx{}

phash := t.Input.Get("phash").(pack.Bytes32)
Expand Down Expand Up @@ -145,12 +186,6 @@ func TxFromV1Tx(t tx.Tx, hasOut bool, bindings txengine.Bindings) (Tx, error) {
Value: B32(nonce),
})

// rest of compat won't work beyond this point, so return early
// in theory burns only need to check the status anyhow
if t.Selector.IsBurn() || t.Selector.IsRelease() {
return tx, nil
}

to := t.Input.Get("to").(pack.String)
toAddr, err := ExtEthCompatAddressFromHex(to.String())
if err != nil {
Expand Down Expand Up @@ -221,6 +256,16 @@ func TxFromV1Tx(t tx.Tx, hasOut bool, bindings txengine.Bindings) (Tx, error) {
})
}

if t.Output.Get("revert") != nil {
reason := t.Output.Get("revert").(pack.String)

tx.Out.Set(Arg{
Name: "revert",
Type: "str",
Value: Str(reason),
})
}

if t.Output.Get("sig") != nil {
sig := t.Output.Get("sig").(pack.Bytes65)
r := [32]byte{}
Expand Down Expand Up @@ -251,7 +296,7 @@ func TxFromV1Tx(t tx.Tx, hasOut bool, bindings txengine.Bindings) (Tx, error) {

tx.To = Address(ToFromV1Selector(t.Selector))
v0hash := MintTxHash(t.Selector, ghash, btcTxHash, btcTxIndex)
copy(tx.Hash[:], v0hash[:])
copy(tx.Hash[:], v0hash[:])

return tx, nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/onsi/gomega v1.10.1
github.com/renproject/aw v0.4.0-9
github.com/renproject/darknode v0.5.3-0.20210215032435-5ec527109d99
github.com/renproject/darknode v0.5.3-0.20210216011743-e88be13521f3
github.com/renproject/id v0.4.2
github.com/renproject/kv v1.1.2
github.com/renproject/mercury v0.3.16
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1430,10 +1430,10 @@ github.com/renproject/aw v0.4.0-9 h1:O3rFYg4arz+kuu+qqn/+FYtWDhsstD/R8/+RkOTrOCE
github.com/renproject/aw v0.4.0-9/go.mod h1:iuELPkEjsWV9dSyZ9TE5SKsTr+jv2I9naHDhsodH3Z0=
github.com/renproject/darknode v0.5.3-0.20210202054456-20290a7a369a h1:uuQKoJ4qZD/JNLo6EeJJ3ZSWqJHPAKk8wg6chSceTqM=
github.com/renproject/darknode v0.5.3-0.20210202054456-20290a7a369a/go.mod h1:3mJXcyPi6/bzX00URx5lNKW3fivJyRj/+uyP2tWxv+k=
github.com/renproject/darknode v0.5.3-0.20210214233843-2fb53abe8430 h1:ocVBd8dhKdLIxjomKKVI8xri6uHVdSL7RIbW7IWYmg0=
github.com/renproject/darknode v0.5.3-0.20210214233843-2fb53abe8430/go.mod h1:3mJXcyPi6/bzX00URx5lNKW3fivJyRj/+uyP2tWxv+k=
github.com/renproject/darknode v0.5.3-0.20210215032435-5ec527109d99 h1:Zvp1bkReg6SYUkofEn5cYw4cLKzpz67C3945dl2WJY4=
github.com/renproject/darknode v0.5.3-0.20210215032435-5ec527109d99/go.mod h1:3mJXcyPi6/bzX00URx5lNKW3fivJyRj/+uyP2tWxv+k=
github.com/renproject/darknode v0.5.3-0.20210216011743-e88be13521f3 h1:YICqnrpjNJq3zbX3/ZX0MnYtRgvT3a1m5nJZEklEX2Y=
github.com/renproject/darknode v0.5.3-0.20210216011743-e88be13521f3/go.mod h1:3mJXcyPi6/bzX00URx5lNKW3fivJyRj/+uyP2tWxv+k=
github.com/renproject/id v0.4.2 h1:XseNDPPCJtsZjIWR7Qgf+zxy0Gt5xsLrfwpQxJt5wFQ=
github.com/renproject/id v0.4.2/go.mod h1:bCzV4zZkyWetf0GvhJxMT9HQNnGUwzQpImtXOUXqq0k=
github.com/renproject/kv v1.1.2 h1:P18yHdDVJTEZ9yeyx6o82ICY1m6f+VdtAt/ouZez+AU=
Expand Down
50 changes: 41 additions & 9 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/renproject/aw/wire"
"github.com/renproject/darknode/jsonrpc"
"github.com/renproject/darknode/tx"
"github.com/renproject/darknode/tx/txutil"
"github.com/renproject/darknode/txengine/txenginebindings"
"github.com/renproject/darknode/txengine/txengineutil"
Expand All @@ -39,7 +40,7 @@ import (
)

var _ = Describe("Resolver", func() {
init := func(ctx context.Context) (*Resolver, jsonrpc.Validator) {
init := func(ctx context.Context) (*Resolver, jsonrpc.Validator, *redis.Client) {
logger := logrus.New()

table := kv.NewTable(kv.NewMemDB(kv.JSONCodec), "addresses")
Expand Down Expand Up @@ -119,7 +120,7 @@ var _ = Describe("Resolver", func() {
validator := NewValidator(bindings, (*id.PubKey)(pubkey), compatStore, logger)
resolver := New(logger, cacher, multiaddrStore, verifier, database, jsonrpc.Options{}, compatStore, bindings)

return resolver, validator
return resolver, validator, client
}

cleanup := func() {
Expand All @@ -130,7 +131,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _ := init(ctx)
resolver, _, _ := init(ctx)
defer cleanup()
offset := pack.NewU32(1)

Expand Down Expand Up @@ -167,7 +168,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, validator := init(ctx)
resolver, validator, _ := init(ctx)
defer cleanup()

innerCtx, innerCancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down Expand Up @@ -206,11 +207,42 @@ var _ = Describe("Resolver", func() {
Expect(resp).ShouldNot(Equal(jsonrpc.Response{}))
})

It("should handle queryTx to a v0 burn tx", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _, client := init(ctx)
defer cleanup()

// Use a v1 burn tx, as it will be persisted
r := rand.New(rand.NewSource(GinkgoRandomSeed()))
mocktx := txutil.RandomGoodTx(r)
mocktx.Selector = tx.Selector("BTC/fromEthereum")

// Submit tx to ensure that it can be queried against
params := jsonrpc.ParamsSubmitTx{
Tx: mocktx,
}

// hacky
// manually set an entry in redis so that we think a v1 burn
client.Set(mocktx.Hash.String(), mocktx.Hash.String(), 0)

// Submit so that it gets persisted in db
resp := resolver.SubmitTx(ctx, nil, &params, nil)

resp = resolver.QueryTx(ctx, nil, &jsonrpc.ParamsQueryTx{
TxHash: mocktx.Hash,
}, nil)

Expect(resp).ShouldNot(Equal(jsonrpc.Response{}))
})

It("should handle a request witout a specified ID", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _ := init(ctx)
resolver, _, _ := init(ctx)
defer cleanup()

urlI, err := url.Parse("http://localhost/")
Expand All @@ -232,7 +264,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _ := init(ctx)
resolver, _, _ := init(ctx)
defer cleanup()

urlI, err := url.Parse("http://localhost/?id=123")
Expand All @@ -254,7 +286,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _ := init(ctx)
resolver, _, _ := init(ctx)
defer cleanup()

urlI, err := url.Parse("http://localhost/?id=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
Expand All @@ -276,7 +308,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, validator := init(ctx)
resolver, validator, _ := init(ctx)
defer cleanup()

params := testutils.MockBurnParamSubmitTxV0BTC()
Expand Down Expand Up @@ -305,7 +337,7 @@ var _ = Describe("Resolver", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resolver, _ := init(ctx)
resolver, _, _ := init(ctx)
defer cleanup()

r := rand.New(rand.NewSource(GinkgoRandomSeed()))
Expand Down

0 comments on commit 34b185d

Please sign in to comment.