Skip to content

Commit

Permalink
fixing the toBytes when converting to renvm tx
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Mar 23, 2022
1 parent 1d724ab commit 630fdf4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 24 deletions.
8 changes: 1 addition & 7 deletions watcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,12 @@ func (fetcher ethFetcher) fetchLogBurnToChain(ctx context.Context, asset multich
continue
}

// Decode the target address
toBytes, err := fetcher.bindings.DecodeAddress(targetChain, multichain.Address(iter.Event.RecipientAddress))
if err != nil {
continue
}

event := EventInfo{
Asset: asset,
TargetChain: targetChain,
Txid: iter.Event.Raw.TxHash.Bytes(),
Amount: pack.NewU256FromInt(iter.Event.Amount),
ToBytes: toBytes,
ToBytes: []byte(iter.Event.RecipientChain),
Nonce: nonceBytes,
BlockNumber: pack.NewU64(iter.Event.Raw.BlockNumber),
}
Expand Down
17 changes: 3 additions & 14 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,9 @@ func (watcher Watcher) burnToParams(eventLog EventInfo) (jsonrpc.ParamsSubmitTx,

var to multichain.Address
var toDecoded []byte
var err error
if !isBurnAndMint {
var err error
to, toDecoded, err = watcher.decodeToAddress(eventLog.Asset, eventLog.ToBytes)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}
} else {
to, err = watcher.bindings.EncodeAddress(eventLog.TargetChain, eventLog.ToBytes)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}

toDecoded = eventLog.ToBytes
to, toDecoded, err := watcher.decodeToAddress(eventLog.Asset, eventLog.ToBytes)
if err != nil {
return jsonrpc.ParamsSubmitTx{}, err
}

watcher.opts.Logger.Infof("[watcher] %v burn parameters (to=%v, amount=%v, nonce=%v)", selector, string(to), eventLog.Amount, eventLog.Nonce)
Expand Down
72 changes: 69 additions & 3 deletions watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"net/http"
"strings"
"testing/quick"
"time"

Expand Down Expand Up @@ -200,6 +201,53 @@ var _ = Describe("Watcher", func() {
Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})

It("should convert the burn log with an address of all small letters", func() {
redisClient := initRedis()
fetcher := &MockFetcher{}
bindings := initBindings()
resovler := &jsonrpcresolver.Callbacks{}
options := watcher.DefaultOptions().
WithNetwork(multichain.NetworkTestnet).
WithChain(multichain.Ethereum).
WithAssets([]multichain.Asset{multichain.BTC})
w := watcher.NewWatcher(options, fetcher, bindings, resovler, redisClient)

latestBlock := uint64(0)
test := func() bool {
// Make sure we are only pulling once
ctx, cancel := context.WithCancel(context.Background())
cancel()

// Simulate a burn event
event := RandomEventInfo()
if event.TargetChain.IsAccountBased() {
event.ToBytes = []byte(strings.ToLower(string(event.ToBytes)))
}
fetcher.handleFetchBurnLogs = func(ctx context.Context, from, to uint64) ([]watcher.EventInfo, error) {
return []watcher.EventInfo{event}, nil
}
fetcher.handleLatestBlockHeight = func(ctx context.Context) (uint64, error) {
latestBlock += 10
return latestBlock, nil
}

resovler.SubmitTxHandler = func(ctx context.Context, i interface{}, tx *jsonrpc.ParamsSubmitTx, request *http.Request) jsonrpc.Response {
var input engine.LockMintBurnReleaseInput
Expect(pack.Decode(&input, tx.Tx.Input)).Should(Succeed())
Expect(input.Txid).Should(Equal(event.Txid))
Expect(input.Amount.Equal(event.Amount)).Should(BeTrue())
Expect(input.To).Should(Equal(pack.String(event.ToBytes)))
Expect(input.Nonce).Should(Equal(event.Nonce))
return jsonrpc.NewResponse(i, map[string]bool{"ok": true}, nil)
}
// Verify the burn tx sent to resolver
w.Run(ctx)
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})

It("should convert the burn log to a renvm tx and submit to the resolver for Solana", func() {
redisClient := initRedis()
fetcher := &MockFetcher{}
Expand Down Expand Up @@ -286,12 +334,30 @@ var _ = Describe("Watcher", func() {

func RandomEventInfo() watcher.EventInfo {
r := rand.New(rand.NewSource(time.Now().Unix()))
chains := []multichain.Chain{
multichain.Bitcoin,
multichain.DigiByte,
multichain.Dogecoin,
multichain.BitcoinCash,
multichain.Zcash,
multichain.Avalanche,
multichain.Ethereum,
multichain.Goerli,
multichain.BinanceSmartChain,
multichain.Fantom,
multichain.Polygon,
multichain.Arbitrum,
multichain.Filecoin,
multichain.Terra,
multichain.Solana,
}
chain := chains[rand.Intn(len(chains))]
addr := RandomGoodAddress(chain, multichain.NetworkTestnet)
txid := RandomBytes(r, 20)
addr := RandomGoodAddress(multichain.Bitcoin, multichain.NetworkTestnet)

return watcher.EventInfo{
Asset: multichain.BTC,
TargetChain: multichain.Bitcoin,
Asset: chain.NativeAsset(),
TargetChain: chain,
Txid: txid,
Amount: pack.U256{}.Generate(r, 100).Interface().(pack.U256),
ToBytes: []byte(addr),
Expand Down

0 comments on commit 630fdf4

Please sign in to comment.