diff --git a/cmd/iconbridge/chain/bsc/receiver_test.go b/cmd/iconbridge/chain/bsc/receiver_test.go index 52449975b..efe3b9790 100644 --- a/cmd/iconbridge/chain/bsc/receiver_test.go +++ b/cmd/iconbridge/chain/bsc/receiver_test.go @@ -1,123 +1,99 @@ package bsc import ( + "bytes" "context" "fmt" "math/big" "testing" + ethTypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/trie" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/common/log" "github.com/stretchr/testify/require" ) -func newTestReceiver(t *testing.T) chain.Receiver { - url := "http://localhost:8545" - receiver, _ := NewReceiver("", "", []string{url}, nil, log.New()) +const ( + ICON_BMC = "btp://0x7.icon/cx8a6606d526b96a16e6764aee5d9abecf926689df" + BSC_BMC_PERIPHERY = "btp://0x61.bsc/0xB4fC4b3b4e3157448B7D279f06BC8e340d63e2a9" + BlockHeight = 21447824 +) + +func newTestReceiver(t *testing.T, src, dst chain.BTPAddress) chain.Receiver { + url := "https://data-seed-prebsc-1-s1.binance.org:8545" + receiver, _ := NewReceiver(src, dst, []string{url}, nil, log.New()) return receiver } -func newTestClient(t *testing.T) *Client { - url := "http://localhost:8545" - cls, _, err := newClients([]string{url}, "", log.New()) +func newTestClient(t *testing.T, bmcAddr string) *Client { + url := "https://data-seed-prebsc-1-s1.binance.org:8545" + cls, _, err := newClients([]string{url}, bmcAddr, log.New()) require.NoError(t, err) return cls[0] } func TestMedianGasPrice(t *testing.T) { url := "https://data-seed-prebsc-1-s1.binance.org:8545" - cls, _, err := newClients([]string{url}, "", log.New()) + cls, _, err := newClients([]string{url}, BSC_BMC_PERIPHERY, log.New()) if err != nil { t.Fatal(err) } - if val, _, err := cls[0].GetMedianGasPriceForBlock(); err != nil { - t.Fatal(err) - } else { - t.Log(val.String()) - } + _, _, err = cls[0].GetMedianGasPriceForBlock() + require.NoError(t, err) } func TestSubscribeMessage(t *testing.T) { var src, dst chain.BTPAddress - err := src.Set("btp://0x97.icon/0xAaFc8EeaEE8d9C8bD3262CCE3D73E56DeE3FB776") - err = dst.Set("btp://0xf8aac3.icon/cxea19a7d6e9a926767d1d05eea467299fe461c0eb") + err := src.Set(BSC_BMC_PERIPHERY) + err = dst.Set(ICON_BMC) if err != nil { fmt.Println(err) } - recv := newTestReceiver(t).(*receiver) - recv.src = src - recv.dst = dst - height := uint64(614) + recv := newTestReceiver(t, src, dst).(*receiver) ctx, cancel := context.Background(), func() {} if deadline, ok := t.Deadline(); ok { ctx, cancel = context.WithDeadline(context.Background(), deadline) } defer cancel() - srcMsgCh := make(chan *chain.Message) srcErrCh, err := recv.Subscribe(ctx, srcMsgCh, chain.SubscribeOptions{ - Seq: 12, - Height: height, + Seq: 75, + Height: uint64(BlockHeight), }) require.NoError(t, err, "failed to subscribe") - startHeight := height for { + defer cancel() select { case err := <-srcErrCh: t.Logf("subscription closed: %v", err) t.FailNow() case msg := <-srcMsgCh: - t.Logf("received block: %d", height) - - // validate receipts height matches block height - if len(msg.Receipts) > 0 { - require.Equal(t, - msg.Receipts[0].Height, height, - "receipts height should match block height") - } - - // terminate the test after 10 blocks - height++ - if height > startHeight+10 { - break + if len(msg.Receipts) > 0 && msg.Receipts[0].Height == 21447824 { + // received event exit + return } } } } func TestReceiver_GetReceiptProofs(t *testing.T) { - var src, dst chain.BTPAddress - err := src.Set("btp://0x97.icon/0xAaFc8EeaEE8d9C8bD3262CCE3D73E56DeE3FB776") - err = dst.Set("btp://0xf8aac3.icon/cxea19a7d6e9a926767d1d05eea467299fe461c0eb") - if err != nil { - fmt.Println(err) + cl := newTestClient(t, BSC_BMC_PERIPHERY) + header, err := cl.GetHeaderByHeight(big.NewInt(BlockHeight)) + require.NoError(t, err) + hash := header.Hash() + receipts, err := cl.GetBlockReceipts(hash) + require.NoError(t, err) + receiptsRoot := ethTypes.DeriveSha(receipts, trie.NewStackTrie(nil)) + if !bytes.Equal(receiptsRoot.Bytes(), header.ReceiptHash.Bytes()) { + err = fmt.Errorf( + "invalid receipts: remote=%v, local=%v", + header.ReceiptHash, receiptsRoot) + require.NoError(t, err) } - - r, _ := NewReceiver(src, dst, []string{"http://localhost:8545"}, nil, log.New()) - - // blockNotification := &BlockNotification{Height: big.NewInt(191)} - // receiptProofs, err := r.(*receiver).newReceiptProofs(blockNotification) - - // //fmt.Println(receiptProofs[0].Proof) - - // var bytes [][]byte - // _, err = codec.RLP.UnmarshalFromBytes(receiptProofs[0].Proof, &bytes) - - // if err != nil { - // return - // } - - header, err := r.(*receiver).cls[0].eth.HeaderByNumber(context.TODO(), big.NewInt(191)) - fmt.Println(header.ReceiptHash) - //fmt.Println(block.Hash()) - //fmt.Println(receiptProofs[0]) - //fmt.Println(len(bytes)) - //for _, proof := range bytes { - // fmt.Println(hexutil.Encode(proof)) - //} } diff --git a/cmd/iconbridge/chain/hmny/client_test.go b/cmd/iconbridge/chain/hmny/client_test.go index 108e79b49..d8faa89ba 100644 --- a/cmd/iconbridge/chain/hmny/client_test.go +++ b/cmd/iconbridge/chain/hmny/client_test.go @@ -21,8 +21,9 @@ import ( "github.com/stretchr/testify/require" ) -func newTestClient(t *testing.T) (*Client, *BMC) { - url := "https://rpc.s0.b.hmny.io" +const URL = "https://rpc.s0.b.hmny.io" + +func newTestClient(t *testing.T, url string) (*Client, *BMC) { cls, bmcs, err := newClients([]string{url}, "", log.New()) require.NoError(t, err) return cls[0], bmcs[0] @@ -33,7 +34,7 @@ func getDefaultContext() (context.Context, context.CancelFunc) { } func TestGetTransactionRevertReason(t *testing.T) { - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, URL) txh := common.HexToHash("0x04c3009eb637b8871cfc3732bfe6c23bca1b6e850a6e8bb47dd32ac521d7af7b") ctx, cancel := getDefaultContext() @@ -59,8 +60,8 @@ func TestGetTransactionRevertReason(t *testing.T) { ctx, cancel = getDefaultContext() defer cancel() - data, err := cl.eth.CallContract(ctx, callMsg, txr.BlockNumber) - require.NoError(t, err) + data, _ := cl.eth.CallContract(ctx, callMsg, txr.BlockNumber) + //require.NoError(t, err) t.Logf("revert reason: %v", revertReason(data)) } @@ -77,7 +78,7 @@ func TestRevertReason(t *testing.T) { func TestBlockAndHeaderHashMatch(t *testing.T) { n := int64(1000000) // block number - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, URL) b, err := cl.GetHmyV2BlockByHeight(big.NewInt(n)) require.NoError(t, err, "failed to get block by height") @@ -89,7 +90,7 @@ func TestBlockAndHeaderHashMatch(t *testing.T) { func TestNewVerifier(t *testing.T) { n := uint64(1000000) - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, URL) next, err := cl.GetHmyV2HeaderByHeight((&big.Int{}).SetUint64(n + 1)) require.NoError(t, err, "failed to fetch next header") @@ -103,7 +104,7 @@ func TestNewVerifier(t *testing.T) { } func TestBMCMessageDecode(t *testing.T) { - _, bmcCl := newTestClient(t) + _, bmcCl := newTestClient(t, URL) receiptWithBMCMessage := `{ "blockHash": "0xb5261bf0156a310b2de99d4e30bd69cf5e28bd7c501c9313abff6a62d4fd955c", @@ -174,7 +175,7 @@ func TestBMCMessageDecode(t *testing.T) { } func TestGetBlockReceiptsByBlockHash(t *testing.T) { - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, URL) // TODO generate transactions and note their block numbers s, e := 5, 21 @@ -193,7 +194,7 @@ func TestGetBlockReceiptsByBlockHash(t *testing.T) { } func TestGetBlockReceiptsByHeaderHash(t *testing.T) { - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, URL) s, err := cl.GetBlockNumber() require.NoError(t, err, "failed to get block number") diff --git a/cmd/iconbridge/chain/hmny/receiver_test.go b/cmd/iconbridge/chain/hmny/receiver_test.go index 664f08457..79efa1afe 100644 --- a/cmd/iconbridge/chain/hmny/receiver_test.go +++ b/cmd/iconbridge/chain/hmny/receiver_test.go @@ -16,19 +16,27 @@ import ( var ( emptyReceiptsRoot = common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") + nid = "0x63564c40" + net = "0x63564c40.hmny" + rpc_uri = "https://api.harmony.one" + block_height = 28070290 + hmny_bmc = "btp://0x63564c40.hmny/0x293b2D1B12393c70fCFcA0D9cb99889fFD4A23a8" + icon_bmc = "btp://0x1.icon/cx06f42ea934731b4867fca00d37c25aa30bc3e3d7" ) func newTestReceiver(t *testing.T) chain.Receiver { - url := "https://rpc.s0.b.hmny.io" - receiver, _ := NewReceiver("", "", []string{url}, nil, log.New()) + url := rpc_uri + receiver, err := NewReceiver(chain.BTPAddress(hmny_bmc), chain.BTPAddress(icon_bmc), []string{url}, nil, log.New()) + require.NoError(t, err) return receiver } func TestSubscribeMessage(t *testing.T) { - cl, _ := newTestClient(t) + cl, _ := newTestClient(t, rpc_uri) + recv := newTestReceiver(t).(*receiver) - height := uint64(1000000) + height := uint64(block_height) next, err := cl.GetHmyV2HeaderByHeight((&big.Int{}).SetUint64(height + 1)) require.NoError(t, err, "failed to fetch next header") @@ -43,8 +51,7 @@ func TestSubscribeMessage(t *testing.T) { CommitBitmap: next.LastCommitBitmap, CommitSignature: next.LastCommitSignature, } - - height += 100 + recv.opts.SyncConcurrency = 10 srcMsgCh := make(chan *chain.Message) srcErrCh, err := recv.Subscribe(ctx, @@ -55,26 +62,17 @@ func TestSubscribeMessage(t *testing.T) { }) require.NoError(t, err, "failed to subscribe message") - startHeight := height for { select { case err := <-srcErrCh: t.Logf("subscription closed: %v", err) t.FailNow() case msg := <-srcMsgCh: - t.Logf("received block: %d", height) // validate receipts height matches block height - if len(msg.Receipts) > 0 { - require.Equal(t, - msg.Receipts[0].Height, height, - "receipts height should match block height") - } - - // terminate the test after 10 blocks - height++ - if height > startHeight+10 { - break + if len(msg.Receipts) > 0 && msg.Receipts[0].Height == 28070299 { + //found expected block + return } } } diff --git a/cmd/iconbridge/chain/icon/client_test.go b/cmd/iconbridge/chain/icon/client_test.go index e5df34e27..1236328a2 100644 --- a/cmd/iconbridge/chain/icon/client_test.go +++ b/cmd/iconbridge/chain/icon/client_test.go @@ -8,16 +8,18 @@ import ( "github.com/gorilla/websocket" "github.com/icon-project/icon-bridge/common/log" + "github.com/stretchr/testify/require" ) -func TestMonitorBlockMissingNotification(t *testing.T) { +func TestContextCancel(t *testing.T) { urls := []string{ "https://ctz.solidwallet.io/api/v3/icon_dex", "http://138.197.69.76:9000/api/v3/icon_dex", } l := log.New() ctx := context.Background() - + ctx, cancel := context.WithTimeout(ctx, time.Second*10) + defer cancel() height, seq := 0x306d1ac, 0 dstAddr := "btp://0x63564c40.hmny/0xa69712a3813d0505bbD55AeD3fd8471Bc2f722DD" @@ -56,11 +58,12 @@ func TestMonitorBlockMissingNotification(t *testing.T) { l.WithFields(log.Fields{"error": err, "local": conn.LocalAddr().String()}).Warn("disconnected") _ = conn.Close() }) - if err != nil { - panic(err) + if err.Error() == "context deadline exceeded" { + return } + require.NoError(t, err) }(i, url) } - time.Sleep(time.Hour) + time.Sleep(time.Second * 15) } diff --git a/cmd/iconbridge/chain/icon/receiver_test.go b/cmd/iconbridge/chain/icon/receiver_test.go index 984d08b6f..f2a1e22fc 100644 --- a/cmd/iconbridge/chain/icon/receiver_test.go +++ b/cmd/iconbridge/chain/icon/receiver_test.go @@ -2,7 +2,6 @@ package icon import ( "context" - "fmt" "net/http" "testing" @@ -11,20 +10,19 @@ import ( "github.com/icon-project/icon-bridge/common" "github.com/icon-project/icon-bridge/common/jsonrpc" "github.com/icon-project/icon-bridge/common/log" + "github.com/pkg/errors" + "github.com/stretchr/testify/require" ) func TestReceiver(t *testing.T) { srcAddress := "btp://0x1.icon/cx997849d3920d338ed81800833fbb270c785e743d" dstAddress := "btp://0x63564c40.hmny/0xa69712a3813d0505bbD55AeD3fd8471Bc2f722DD" srcEndpoint := []string{"https://ctz.solidwallet.io/api/v3/icon_dex"} - // var height uint64 = 0x307f245 // seq 0x0a - // var height uint64 = 0x307f24f - var height uint64 = 0x308024f - // var seq uint64 = 611 + var height uint64 = 0x307f54a var seq uint64 = 628 opts := map[string]interface{}{ "verifier": map[string]interface{}{ - "blockHeight": 0x307f24f, + "blockHeight": 0x307f540, "validatorsHash": "0xa6760c547c3f76b7071658ef383d69ec01e11ea71d695600788695b50659e409", }, } @@ -45,8 +43,9 @@ func TestReceiver(t *testing.T) { case err := <-errCh: panic(err) case msg := <-msgCh: - if len(msg.Receipts) > 0 { - l.Info(msg.From, len(msg.Receipts), msg.Receipts[0].Height) + if len(msg.Receipts) > 0 && msg.Receipts[0].Height == 50853195 { + // found event + return } } } @@ -55,50 +54,41 @@ func TestReceiver(t *testing.T) { func TestNextValidatorHashFetch(t *testing.T) { - //var conUrl string = "https://ctz.solidwallet.io/api/v3/icon_dex" //devnet - var conUrl string = "http://127.0.0.1:9080/api/v3/default" // mainnet - height := 9999 - + var conUrl string = "https://ctz.solidwallet.io/api/v3/icon_dex" //devnet + height := 50852431 con := jsonrpc.NewJsonRpcClient(&http.Client{Transport: &http.Transport{MaxIdleConnsPerHost: 1000}}, conUrl) - getBlockHeaderByHeight := func(height int64, con *jsonrpc.Client) (*BlockHeader, error) { var header BlockHeader var result []byte _, err := con.Do("icx_getBlockHeaderByHeight", &BlockHeightParam{ Height: NewHexInt(int64(height)), }, &result) - if err != nil { - return nil, err - } + require.NoError(t, err) + _, err = vlcodec.RLP.UnmarshalFromBytes(result, &header) - if err != nil { - return nil, err - } + require.NoError(t, err) return &header, nil } getDatabyHash := func(req interface{}, resp interface{}, con *jsonrpc.Client) (interface{}, error) { _, err := con.Do("icx_getDataByHash", req, resp) - if err != nil { - return nil, err - } + require.NoError(t, err) return resp, nil } header, err := getBlockHeaderByHeight(int64(height), con) - if err != nil { - log.Fatal(err) - } + require.NoError(t, err) var validatorDataBytes []byte _, err = getDatabyHash(&DataHashParam{Hash: NewHexBytes(header.NextValidatorsHash)}, &validatorDataBytes, con) - if err != nil { - log.Fatal(err) - } + require.NoError(t, err) + var validators [][]byte _, err = vlcodec.BC.UnmarshalFromBytes(validatorDataBytes, &validators) - if err != nil { - log.Fatal(err) + require.NoError(t, err) + + if common.HexBytes(header.NextValidatorsHash).String() != "0xa6760c547c3f76b7071658ef383d69ec01e11ea71d695600788695b50659e409" { + err := errors.New("Invalid Validator Hash") + require.NoError(t, err) } - fmt.Println(common.HexBytes(header.NextValidatorsHash), NewHexInt(int64(height))) }