Skip to content

Commit

Permalink
unit tests for relay-#143
Browse files Browse the repository at this point in the history
  • Loading branch information
Manish committed Aug 4, 2022
1 parent c734aec commit 14e23de
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 127 deletions.
102 changes: 39 additions & 63 deletions cmd/iconbridge/chain/bsc/receiver_test.go
Original file line number Diff line number Diff line change
@@ -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))
//}
}
21 changes: 11 additions & 10 deletions cmd/iconbridge/chain/hmny/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand All @@ -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))
}
Expand All @@ -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")

Expand All @@ -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")
Expand All @@ -103,7 +104,7 @@ func TestNewVerifier(t *testing.T) {
}

func TestBMCMessageDecode(t *testing.T) {
_, bmcCl := newTestClient(t)
_, bmcCl := newTestClient(t, URL)

receiptWithBMCMessage := `{
"blockHash": "0xb5261bf0156a310b2de99d4e30bd69cf5e28bd7c501c9313abff6a62d4fd955c",
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
34 changes: 16 additions & 18 deletions cmd/iconbridge/chain/hmny/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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,
Expand All @@ -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
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/iconbridge/chain/icon/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Loading

0 comments on commit 14e23de

Please sign in to comment.