From 7bf75fb9bd0104c7ba8568e6dab7378b9f4185e5 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan Date: Mon, 26 Sep 2022 09:13:22 +0530 Subject: [PATCH 01/11] fix(bmr): send only one receipt per transaction --- cmd/iconbridge/chain/near/sender.go | 43 ++++++++++++----------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/cmd/iconbridge/chain/near/sender.go b/cmd/iconbridge/chain/near/sender.go index 509ac6cb2..5bb5e9723 100644 --- a/cmd/iconbridge/chain/near/sender.go +++ b/cmd/iconbridge/chain/near/sender.go @@ -88,34 +88,24 @@ func (s *Sender) Segment(ctx context.Context, msg *chain.Message) (tx chain.Rela Receipts: make([][]byte, 0), } - var msgSize uint64 + receipt := msg.Receipts[0] - newMsg = &chain.Message{ - From: msg.From, - Receipts: msg.Receipts, + rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) + if err != nil { + return nil, nil, err } - for i, receipt := range msg.Receipts { - rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize > txMaxDataSize { - newMsg.Receipts = msg.Receipts[i:] - break - } - msgSize = newMsgSize - rm.Receipts = append(rm.Receipts, rlpReceipt) + + rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: rlpEvents, + }) + if err != nil { + return nil, nil, err } + rm.Receipts = append(rm.Receipts, rlpReceipt) + message, err := codec.RLP.MarshalToBytes(rm) if err != nil { return nil, nil, err @@ -126,7 +116,10 @@ func (s *Sender) Segment(ctx context.Context, msg *chain.Message) (tx chain.Rela return nil, nil, err } - return tx, newMsg, nil + return tx, &chain.Message{ + From: msg.From, + Receipts: msg.Receipts[1:], + }, nil } func (s *Sender) newRelayTransaction(ctx context.Context, prev string, message []byte) (*RelayTransaction, error) { From 1915df5fb0d8bdab6d644444c1aa1dc59142f1ac Mon Sep 17 00:00:00 2001 From: bbist Date: Tue, 27 Sep 2022 15:13:06 +0545 Subject: [PATCH 02/11] fix tx size limit exceeding when single receipt has too many events --- cmd/iconbridge/chain/bsc/sender.go | 59 +++++++++++++++------- cmd/iconbridge/chain/icon/sender.go | 60 +++++++++++++++------- cmd/iconbridge/chain/near/sender.go | 78 +++++++++++++++++++++-------- 3 files changed, 140 insertions(+), 57 deletions(-) diff --git a/cmd/iconbridge/chain/bsc/sender.go b/cmd/iconbridge/chain/bsc/sender.go index 7c7b27b06..7bc67765d 100644 --- a/cmd/iconbridge/chain/bsc/sender.go +++ b/cmd/iconbridge/chain/bsc/sender.go @@ -173,27 +173,50 @@ func (s *sender) Segment( From: msg.From, Receipts: msg.Receipts, } + +rloop: for i, receipt := range msg.Receipts { - rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize > s.opts.TxDataSizeLimit { - newMsg.Receipts = msg.Receipts[i:] - break + eloop: + for j := range receipt.Events { + // try all events first + // if it exceeds limit, try again by removing last event + events := receipt.Events[:len(receipt.Events)-j] + + rlpEvents, err := codec.RLP.MarshalToBytes(events) + if err != nil { + return nil, nil, err + } + rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: rlpEvents, + }) + if err != nil { + return nil, nil, err + } + + newMsgSize := msgSize + uint64(len(rlpReceipt)) + if newMsgSize <= s.opts.TxDataSizeLimit { + + msgSize = newMsgSize + if len(events) == len(receipt.Events) { // all events added + newMsg.Receipts = msg.Receipts[i+1:] + } else { // save remaining events in this receipt + receipt.Events = receipt.Events[len(events):] + newMsg.Receipts = msg.Receipts[i:] + } + rm.Receipts = append(rm.Receipts, rlpReceipt) + break eloop + + } else if len(events) == 1 { + // stop iterating over receipts when adding even a single event + // exceeds tx size limit + break rloop + } + } - msgSize = newMsgSize - rm.Receipts = append(rm.Receipts, rlpReceipt) } + message, err := codec.RLP.MarshalToBytes(rm) if err != nil { return nil, nil, err diff --git a/cmd/iconbridge/chain/icon/sender.go b/cmd/iconbridge/chain/icon/sender.go index a1b26a52a..bfae31339 100644 --- a/cmd/iconbridge/chain/icon/sender.go +++ b/cmd/iconbridge/chain/icon/sender.go @@ -21,12 +21,13 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/icon/types" "math/big" "net/url" "strconv" "time" + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/icon/types" + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/common" "github.com/icon-project/icon-bridge/common/codec" @@ -158,26 +159,47 @@ func (s *sender) Segment( Receipts: msg.Receipts, } +rloop: for i, receipt := range msg.Receipts { - rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize > s.opts.TxDataSizeLimit { - newMsg.Receipts = msg.Receipts[i:] - break + eloop: + for j := range receipt.Events { + // try all events first + // if it exceeds limit, try again by removing last event + events := receipt.Events[:len(receipt.Events)-j] + + rlpEvents, err := codec.RLP.MarshalToBytes(events) + if err != nil { + return nil, nil, err + } + rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: rlpEvents, + }) + if err != nil { + return nil, nil, err + } + + newMsgSize := msgSize + uint64(len(rlpReceipt)) + if newMsgSize <= s.opts.TxDataSizeLimit { + + msgSize = newMsgSize + if len(events) == len(receipt.Events) { // all events added + newMsg.Receipts = msg.Receipts[i+1:] + } else { // save remaining events in this receipt + receipt.Events = receipt.Events[len(events):] + newMsg.Receipts = msg.Receipts[i:] + } + rm.Receipts = append(rm.Receipts, rlpReceipt) + break eloop + + } else if len(events) == 1 { + // stop iterating over receipts when adding even a single event + // exceeds tx size limit + break rloop + } + } - msgSize = newMsgSize - rm.Receipts = append(rm.Receipts, rlpReceipt) } message, err := codec.RLP.MarshalToBytes(rm) diff --git a/cmd/iconbridge/chain/near/sender.go b/cmd/iconbridge/chain/near/sender.go index 5bb5e9723..19c5a008a 100644 --- a/cmd/iconbridge/chain/near/sender.go +++ b/cmd/iconbridge/chain/near/sender.go @@ -18,7 +18,9 @@ import ( ) const ( - txMaxDataSize = 64 * 1024 + txMaxDataSize = 4 * 1024 + txOverheadScale = 0.01 // base64 encoding overhead 0.36, rlp and other fields 0.01 + defaultTxSizeLimit = txMaxDataSize / (1 + txOverheadScale) functionCallMethod = "handle_relay_message" gas = uint64(300000000000000) defaultSendTxTimeout = 15 * time.Second @@ -32,6 +34,7 @@ type Sender struct { wallet Wallet logger log.Logger options struct { + TxDataSizeLimit uint64 `json:"tx_data_size_limit"` BalanceThreshold types.BigInt `json:"balance_threshold"` } } @@ -75,11 +78,18 @@ func (s *Sender) client() IClient { return s.clients[rand.Intn(len(s.clients))] } -func (s *Sender) Segment(ctx context.Context, msg *chain.Message) (tx chain.RelayTx, newMsg *chain.Message, err error) { +func (s *Sender) Segment( + ctx context.Context, msg *chain.Message, +) (tx chain.RelayTx, newMsg *chain.Message, err error) { if ctx.Err() != nil { return nil, nil, ctx.Err() } + if s.options.TxDataSizeLimit == 0 { + limit := defaultTxSizeLimit + s.options.TxDataSizeLimit = uint64(limit) + } + if len(msg.Receipts) == 0 { return nil, msg, nil } @@ -88,23 +98,55 @@ func (s *Sender) Segment(ctx context.Context, msg *chain.Message) (tx chain.Rela Receipts: make([][]byte, 0), } - receipt := msg.Receipts[0] + var msgSize uint64 - rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) - if err != nil { - return nil, nil, err + newMsg = &chain.Message{ + From: msg.From, + Receipts: msg.Receipts, } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } +rloop: + for i, receipt := range msg.Receipts { + eloop: + for j := range receipt.Events { + // try all events first + // if it exceeds limit, try again by removing last event + events := receipt.Events[:len(receipt.Events)-j] + + rlpEvents, err := codec.RLP.MarshalToBytes(events) + if err != nil { + return nil, nil, err + } + rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: rlpEvents, + }) + if err != nil { + return nil, nil, err + } + + newMsgSize := msgSize + uint64(len(rlpReceipt)) + if newMsgSize <= s.options.TxDataSizeLimit { + + msgSize = newMsgSize + if len(events) == len(receipt.Events) { // all events added + newMsg.Receipts = msg.Receipts[i+1:] + } else { // save remaining events in this receipt + receipt.Events = receipt.Events[len(events):] + newMsg.Receipts = msg.Receipts[i:] + } + rm.Receipts = append(rm.Receipts, rlpReceipt) + break eloop + + } else if len(events) == 1 { + // stop iterating over receipts when adding even a single event + // exceeds tx size limit + break rloop + } - rm.Receipts = append(rm.Receipts, rlpReceipt) + } + } message, err := codec.RLP.MarshalToBytes(rm) if err != nil { @@ -116,12 +158,8 @@ func (s *Sender) Segment(ctx context.Context, msg *chain.Message) (tx chain.Rela return nil, nil, err } - return tx, &chain.Message{ - From: msg.From, - Receipts: msg.Receipts[1:], - }, nil + return tx, newMsg, nil } - func (s *Sender) newRelayTransaction(ctx context.Context, prev string, message []byte) (*RelayTransaction, error) { if nearWallet, Ok := (s.wallet).(*wallet.NearWallet); Ok { accountId := nearWallet.Address() From a1af6abefd2bdf44281ab16cc6b1bac2212ad83b Mon Sep 17 00:00:00 2001 From: bbist Date: Tue, 27 Sep 2022 20:15:44 +0545 Subject: [PATCH 03/11] move shared segementation code to separate package --- cmd/iconbridge/chain/bsc/sender.go | 60 +++--------------- cmd/iconbridge/chain/hmny/sender.go | 37 ++---------- cmd/iconbridge/chain/icon/sender.go | 58 ++---------------- cmd/iconbridge/chain/near/sender.go | 58 ++---------------- cmd/iconbridge/common/chainutils/segment.go | 67 +++++++++++++++++++++ 5 files changed, 89 insertions(+), 191 deletions(-) create mode 100644 cmd/iconbridge/common/chainutils/segment.go diff --git a/cmd/iconbridge/chain/bsc/sender.go b/cmd/iconbridge/chain/bsc/sender.go index 7bc67765d..7c80e3ab9 100644 --- a/cmd/iconbridge/chain/bsc/sender.go +++ b/cmd/iconbridge/chain/bsc/sender.go @@ -29,6 +29,8 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" + "github.com/icon-project/icon-bridge/cmd/iconbridge/common/chainutils" + bscTypes "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types" "github.com/icon-project/icon-bridge/common/codec" "github.com/icon-project/icon-bridge/common/intconv" @@ -163,64 +165,16 @@ func (s *sender) Segment( return nil, msg, nil } - rm := &chain.RelayMessage{ - Receipts: make([][]byte, 0), - } - - var msgSize uint64 - - newMsg = &chain.Message{ - From: msg.From, - Receipts: msg.Receipts, - } - -rloop: - for i, receipt := range msg.Receipts { - eloop: - for j := range receipt.Events { - // try all events first - // if it exceeds limit, try again by removing last event - events := receipt.Events[:len(receipt.Events)-j] - - rlpEvents, err := codec.RLP.MarshalToBytes(events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize <= s.opts.TxDataSizeLimit { - - msgSize = newMsgSize - if len(events) == len(receipt.Events) { // all events added - newMsg.Receipts = msg.Receipts[i+1:] - } else { // save remaining events in this receipt - receipt.Events = receipt.Events[len(events):] - newMsg.Receipts = msg.Receipts[i:] - } - rm.Receipts = append(rm.Receipts, rlpReceipt) - break eloop - - } else if len(events) == 1 { - // stop iterating over receipts when adding even a single event - // exceeds tx size limit - break rloop - } - - } + relayMsg, newMsg, err := chainutils.SegmentByTxDataSize(msg, s.opts.TxDataSizeLimit) + if err != nil { + return nil, nil, err } - message, err := codec.RLP.MarshalToBytes(rm) + message, err := codec.RLP.MarshalToBytes(relayMsg) if err != nil { return nil, nil, err } + cl, _ := s.jointClient() gasPrice, gasHeight, err := cl.GetMedianGasPriceForBlock(ctx) if err != nil || gasPrice.Int64() == 0 { diff --git a/cmd/iconbridge/chain/hmny/sender.go b/cmd/iconbridge/chain/hmny/sender.go index e84c801ad..265c4b1de 100644 --- a/cmd/iconbridge/chain/hmny/sender.go +++ b/cmd/iconbridge/chain/hmny/sender.go @@ -16,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" + "github.com/icon-project/icon-bridge/cmd/iconbridge/common/chainutils" "github.com/icon-project/icon-bridge/common/codec" "github.com/icon-project/icon-bridge/common/intconv" "github.com/icon-project/icon-bridge/common/log" @@ -135,40 +136,12 @@ func (s *sender) Segment( return nil, msg, nil } - rm := &chain.RelayMessage{ - Receipts: make([][]byte, 0), - } - - var msgSize uint64 - - newMsg = &chain.Message{ - From: msg.From, - Receipts: msg.Receipts, - } - - for i, receipt := range msg.Receipts { - rlpEvents, err := codec.RLP.MarshalToBytes(receipt.Events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize > s.opts.TxDataSizeLimit { - newMsg.Receipts = msg.Receipts[i:] - break - } - msgSize = newMsgSize - rm.Receipts = append(rm.Receipts, rlpReceipt) + relayMsg, newMsg, err := chainutils.SegmentByTxDataSize(msg, s.opts.TxDataSizeLimit) + if err != nil { + return nil, nil, err } - message, err := codec.RLP.MarshalToBytes(rm) + message, err := codec.RLP.MarshalToBytes(relayMsg) if err != nil { return nil, nil, err } diff --git a/cmd/iconbridge/chain/icon/sender.go b/cmd/iconbridge/chain/icon/sender.go index bfae31339..e1b8609dd 100644 --- a/cmd/iconbridge/chain/icon/sender.go +++ b/cmd/iconbridge/chain/icon/sender.go @@ -27,6 +27,7 @@ import ( "time" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/icon/types" + "github.com/icon-project/icon-bridge/cmd/iconbridge/common/chainutils" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/common" @@ -148,61 +149,12 @@ func (s *sender) Segment( return nil, msg, nil } - rm := &chain.RelayMessage{ - Receipts: make([][]byte, 0), - } - - var msgSize uint64 - - newMsg = &chain.Message{ - From: msg.From, - Receipts: msg.Receipts, - } - -rloop: - for i, receipt := range msg.Receipts { - eloop: - for j := range receipt.Events { - // try all events first - // if it exceeds limit, try again by removing last event - events := receipt.Events[:len(receipt.Events)-j] - - rlpEvents, err := codec.RLP.MarshalToBytes(events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize <= s.opts.TxDataSizeLimit { - - msgSize = newMsgSize - if len(events) == len(receipt.Events) { // all events added - newMsg.Receipts = msg.Receipts[i+1:] - } else { // save remaining events in this receipt - receipt.Events = receipt.Events[len(events):] - newMsg.Receipts = msg.Receipts[i:] - } - rm.Receipts = append(rm.Receipts, rlpReceipt) - break eloop - - } else if len(events) == 1 { - // stop iterating over receipts when adding even a single event - // exceeds tx size limit - break rloop - } - - } + relayMsg, newMsg, err := chainutils.SegmentByTxDataSize(msg, s.opts.TxDataSizeLimit) + if err != nil { + return nil, nil, err } - message, err := codec.RLP.MarshalToBytes(rm) + message, err := codec.RLP.MarshalToBytes(relayMsg) if err != nil { return nil, nil, err } diff --git a/cmd/iconbridge/chain/near/sender.go b/cmd/iconbridge/chain/near/sender.go index 19c5a008a..f2cee4b59 100644 --- a/cmd/iconbridge/chain/near/sender.go +++ b/cmd/iconbridge/chain/near/sender.go @@ -12,6 +12,7 @@ import ( "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/errors" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/types" + "github.com/icon-project/icon-bridge/cmd/iconbridge/common/chainutils" "github.com/icon-project/icon-bridge/common/codec" "github.com/icon-project/icon-bridge/common/log" "github.com/icon-project/icon-bridge/common/wallet" @@ -94,61 +95,12 @@ func (s *Sender) Segment( return nil, msg, nil } - rm := &chain.RelayMessage{ - Receipts: make([][]byte, 0), - } - - var msgSize uint64 - - newMsg = &chain.Message{ - From: msg.From, - Receipts: msg.Receipts, - } - -rloop: - for i, receipt := range msg.Receipts { - eloop: - for j := range receipt.Events { - // try all events first - // if it exceeds limit, try again by removing last event - events := receipt.Events[:len(receipt.Events)-j] - - rlpEvents, err := codec.RLP.MarshalToBytes(events) - if err != nil { - return nil, nil, err - } - rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ - Index: receipt.Index, - Height: receipt.Height, - Events: rlpEvents, - }) - if err != nil { - return nil, nil, err - } - - newMsgSize := msgSize + uint64(len(rlpReceipt)) - if newMsgSize <= s.options.TxDataSizeLimit { - - msgSize = newMsgSize - if len(events) == len(receipt.Events) { // all events added - newMsg.Receipts = msg.Receipts[i+1:] - } else { // save remaining events in this receipt - receipt.Events = receipt.Events[len(events):] - newMsg.Receipts = msg.Receipts[i:] - } - rm.Receipts = append(rm.Receipts, rlpReceipt) - break eloop - - } else if len(events) == 1 { - // stop iterating over receipts when adding even a single event - // exceeds tx size limit - break rloop - } - - } + relayMsg, newMsg, err := chainutils.SegmentByTxDataSize(msg, s.options.TxDataSizeLimit) + if err != nil { + return nil, nil, err } - message, err := codec.RLP.MarshalToBytes(rm) + message, err := codec.RLP.MarshalToBytes(relayMsg) if err != nil { return nil, nil, err } diff --git a/cmd/iconbridge/common/chainutils/segment.go b/cmd/iconbridge/common/chainutils/segment.go new file mode 100644 index 000000000..0ae93dd68 --- /dev/null +++ b/cmd/iconbridge/common/chainutils/segment.go @@ -0,0 +1,67 @@ +package chainutils + +import ( + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" + "github.com/icon-project/icon-bridge/common/codec" +) + +func SegmentByTxDataSize(msg *chain.Message, txDataSizeLimit uint64) (relayMsg *chain.RelayMessage, newMsg *chain.Message, err error) { + + relayMsg = &chain.RelayMessage{ + Receipts: make([][]byte, 0), + } + + newMsg = &chain.Message{ + From: msg.From, + Receipts: msg.Receipts, + } + + var msgSize uint64 + +rloop: + for i, receipt := range msg.Receipts { + + eloop: + for j := range receipt.Events { + // try all events first + // if it exceeds limit, try again by removing last event + events := receipt.Events[:len(receipt.Events)-j] + + rlpEvents, err := codec.RLP.MarshalToBytes(events) + if err != nil { + return nil, nil, err + } + rlpReceipt, err := codec.RLP.MarshalToBytes(&chain.RelayReceipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: rlpEvents, + }) + if err != nil { + return nil, nil, err + } + + newMsgSize := msgSize + uint64(len(rlpReceipt)) + if newMsgSize <= txDataSizeLimit { + + msgSize = newMsgSize + if len(events) == len(receipt.Events) { // all events added + newMsg.Receipts = msg.Receipts[i+1:] + } else { // save remaining events in this receipt + receipt.Events = receipt.Events[len(events):] + newMsg.Receipts = msg.Receipts[i:] + } + relayMsg.Receipts = append(relayMsg.Receipts, rlpReceipt) + break eloop + + } else if len(events) == 1 { + // stop iterating over receipts when adding even a single event + // exceeds tx size limit + break rloop + } + + } + } + + return relayMsg, newMsg, nil + +} From f824d507d7f2135cf482dc7f5d958a00d15edf78 Mon Sep 17 00:00:00 2001 From: bbist Date: Tue, 27 Sep 2022 20:16:17 +0545 Subject: [PATCH 04/11] add unit tests for segmentation by tx data size --- .../common/chainutils/segment_test.go | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 cmd/iconbridge/common/chainutils/segment_test.go diff --git a/cmd/iconbridge/common/chainutils/segment_test.go b/cmd/iconbridge/common/chainutils/segment_test.go new file mode 100644 index 000000000..c7f025170 --- /dev/null +++ b/cmd/iconbridge/common/chainutils/segment_test.go @@ -0,0 +1,108 @@ +package chainutils + +import ( + "testing" + + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" + "github.com/stretchr/testify/require" +) + +func getTestMsg() *chain.Message { + return &chain.Message{ + From: "", + Receipts: []*chain.Receipt{ + {Index: 0, Height: 1, Events: []*chain.Event{ + {Next: "", Sequence: 0, Message: []byte{}}, + {Next: "", Sequence: 0, Message: []byte{}}, + }}, + {Index: 2, Height: 6, Events: []*chain.Event{ + {Next: "", Sequence: 0, Message: []byte{}}, + {Next: "", Sequence: 0, Message: []byte{}}, + {Next: "", Sequence: 0, Message: []byte{}}, + {Next: "", Sequence: 0, Message: []byte{}}, + }}, + {Index: 5, Height: 7, Events: []*chain.Event{ + {Next: "", Sequence: 0, Message: []byte{}}, + }}, + {Index: 0, Height: 10, Events: []*chain.Event{ + {Next: "", Sequence: 0, Message: []byte("msg")}, + {Next: "", Sequence: 0, Message: []byte{}}, + {Next: "", Sequence: 0, Message: []byte{}}, + }}, + }, + } +} + +func TestSegmentByTxDataSize(t *testing.T) { + + /* byte sizes + receipt: + with 1 event: 9 bytes + with more events: 4 bytes each + */ + + _, msg, err := SegmentByTxDataSize(getTestMsg(), 5) + require.NoError(t, err) + require.EqualValues(t, 4, len(msg.Receipts)) + require.EqualValues(t, 1, msg.Receipts[0].Height) + require.EqualValues(t, 2, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 9) // encode first receipt with 1 event + require.NoError(t, err) + require.EqualValues(t, 4, len(msg.Receipts)) + require.EqualValues(t, 1, msg.Receipts[0].Height) + require.EqualValues(t, 1, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 12) // encode first receipt with 1 event + require.NoError(t, err) + require.EqualValues(t, 4, len(msg.Receipts)) + require.EqualValues(t, 1, msg.Receipts[0].Height) + require.EqualValues(t, 1, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 15) // encode first receipt with 2 events + require.NoError(t, err) + require.EqualValues(t, 3, len(msg.Receipts)) + require.EqualValues(t, 6, msg.Receipts[0].Height) + require.EqualValues(t, 4, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 22) // encode first receipt with 2 events and second receipt with 1 event + require.NoError(t, err) + require.EqualValues(t, 3, len(msg.Receipts)) + require.EqualValues(t, 6, msg.Receipts[0].Height) + require.EqualValues(t, 3, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 35) // encode first receipt with 2 events and second receipt with 4 event + require.NoError(t, err) + require.EqualValues(t, 2, len(msg.Receipts)) + require.EqualValues(t, 7, msg.Receipts[0].Height) + require.EqualValues(t, 1, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 43) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event + require.NoError(t, err) + require.EqualValues(t, 1, len(msg.Receipts)) + require.EqualValues(t, 10, msg.Receipts[0].Height) + require.EqualValues(t, 3, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 52) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event + require.NoError(t, err) + require.EqualValues(t, 1, len(msg.Receipts)) + require.EqualValues(t, 10, msg.Receipts[0].Height) + require.EqualValues(t, 3, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 55) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 1 event with message body + require.NoError(t, err) + require.EqualValues(t, 1, len(msg.Receipts)) + require.EqualValues(t, 10, msg.Receipts[0].Height) + require.EqualValues(t, 2, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 60) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 2 events with one with message body + require.NoError(t, err) + require.EqualValues(t, 1, len(msg.Receipts)) + require.EqualValues(t, 10, msg.Receipts[0].Height) + require.EqualValues(t, 1, len(msg.Receipts[0].Events)) + + _, msg, err = SegmentByTxDataSize(getTestMsg(), 70) // encode all receipts + require.NoError(t, err) + require.EqualValues(t, 0, len(msg.Receipts)) + +} From c9266489c30f3ec71c41a73e1b78842b23030c98 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan Date: Wed, 28 Sep 2022 17:46:59 +0530 Subject: [PATCH 05/11] add more tests --- cmd/iconbridge/chain/near/sender.go | 9 +- cmd/iconbridge/chain/near/sender_test.go | 55 +- .../chain/near/tests/sender_segment.go | 1106 +++++++++++++++++ .../chain/near/types/sender_options.go | 6 + 4 files changed, 1167 insertions(+), 9 deletions(-) create mode 100644 cmd/iconbridge/chain/near/tests/sender_segment.go create mode 100644 cmd/iconbridge/chain/near/types/sender_options.go diff --git a/cmd/iconbridge/chain/near/sender.go b/cmd/iconbridge/chain/near/sender.go index f2cee4b59..4432915f4 100644 --- a/cmd/iconbridge/chain/near/sender.go +++ b/cmd/iconbridge/chain/near/sender.go @@ -34,10 +34,7 @@ type Sender struct { destination chain.BTPAddress wallet Wallet logger log.Logger - options struct { - TxDataSizeLimit uint64 `json:"tx_data_size_limit"` - BalanceThreshold types.BigInt `json:"balance_threshold"` - } + options types.SenderOptions } func NewSender(source, destination chain.BTPAddress, urls []string, wallet wallet.Wallet, options json.RawMessage, logger log.Logger) (chain.Sender, error) { @@ -61,7 +58,7 @@ func NewSender(source, destination chain.BTPAddress, urls []string, wallet walle return sender, nil } -func newMockSender(source, destination chain.BTPAddress, client *Client, wallet wallet.Wallet, _ map[string]interface{}, logger log.Logger) (*Sender, error) { +func newMockSender(source, destination chain.BTPAddress, client *Client, wallet wallet.Wallet, options types.SenderOptions, logger log.Logger) (*Sender, error) { clients := make([]IClient, 0) clients = append(clients, client) sender := &Sender{ @@ -70,6 +67,7 @@ func newMockSender(source, destination chain.BTPAddress, client *Client, wallet destination: destination, wallet: wallet, logger: logger, + options: options, } return sender, nil @@ -112,6 +110,7 @@ func (s *Sender) Segment( return tx, newMsg, nil } + func (s *Sender) newRelayTransaction(ctx context.Context, prev string, message []byte) (*RelayTransaction, error) { if nearWallet, Ok := (s.wallet).(*wallet.NearWallet); Ok { accountId := nearWallet.Address() diff --git a/cmd/iconbridge/chain/near/sender_test.go b/cmd/iconbridge/chain/near/sender_test.go index 53848417b..8cd4b291f 100644 --- a/cmd/iconbridge/chain/near/sender_test.go +++ b/cmd/iconbridge/chain/near/sender_test.go @@ -9,9 +9,11 @@ import ( "github.com/btcsuite/btcutil/base58" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/tests" + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/types" "github.com/icon-project/icon-bridge/common/log" "github.com/icon-project/icon-bridge/common/wallet" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNearSender(t *testing.T) { @@ -26,7 +28,7 @@ func TestNearSender(t *testing.T) { links, Ok := (testData.Input).([]chain.BTPAddress) assert.True(f, Ok) - sender, err := newMockSender(links[1], links[0], client, nil, nil, nil) + sender, err := newMockSender(links[1], links[0], client, nil, types.SenderOptions{}, nil) assert.Nil(f, err) status, err := sender.Status(context.Background()) @@ -66,7 +68,7 @@ func TestNearSender(t *testing.T) { nearWallet, err := wallet.NewNearwalletFromPrivateKey(&privateKey) assert.NoError(f, err) - sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, nil, log.New()) + sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, types.SenderOptions{}, log.New()) assert.Nil(f, err) relayTx, err := sender.newRelayTransaction(context.Background(), "", []byte{}) @@ -110,7 +112,7 @@ func TestNearSender(t *testing.T) { nearWallet, err := wallet.NewNearwalletFromPrivateKey(&privateKey) assert.NoError(f, err) - sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, nil, log.New()) + sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, types.SenderOptions{}, log.New()) assert.Nil(f, err) relayTx, err := sender.newRelayTransaction(context.Background(), "", []byte{}) @@ -157,7 +159,7 @@ func TestNearSender(t *testing.T) { nearWallet, err := wallet.NewNearwalletFromPrivateKey(&privateKey) assert.NoError(f, err) - sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, nil, log.New()) + sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, types.SenderOptions{}, log.New()) assert.Nil(f, err) balance, _, err := sender.Balance(context.Background()) @@ -174,4 +176,49 @@ func TestNearSender(t *testing.T) { } }) } + + if test, err := tests.GetTest("SenderSegment", t); err == nil { + t.Run(test.Description(), func(f *testing.T) { + for _, testData := range test.TestDatas() { + f.Run(testData.Description, func(f *testing.T) { + client := &Client{ + api: testData.MockApi, + logger: log.New(), + } + + input, Ok := (testData.Input).(struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }) + require.True(f, Ok) + + privateKeyBytes := base58.Decode(input.PrivateKey) + privateKey := ed25519.PrivateKey(privateKeyBytes) + nearWallet, err := wallet.NewNearwalletFromPrivateKey(&privateKey) + + require.NoError(f, err) + + sender, err := newMockSender(input.Source, input.Destination, client, nearWallet, input.Options, log.New()) + require.NoError(f, err) + + _, newMsg, err := sender.Segment(context.Background(), input.Message) + require.NoError(f, err) + + if testData.Expected.Success != nil { + expected, Ok := (testData.Expected.Success).(struct { + NewMessage *chain.Message + }) + require.True(f, Ok) + + assert.Equal(f, expected.NewMessage, newMsg) + } else { + assert.Error(f, err) + } + }) + } + }) + } } diff --git a/cmd/iconbridge/chain/near/tests/sender_segment.go b/cmd/iconbridge/chain/near/tests/sender_segment.go new file mode 100644 index 000000000..e2211972f --- /dev/null +++ b/cmd/iconbridge/chain/near/tests/sender_segment.go @@ -0,0 +1,1106 @@ +package tests + +import ( + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/tests/mock" + "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/types" +) + +type SenderSegmentTest struct { + description string + testData []TestData +} + +func (t SenderSegmentTest) Description() string { + return t.description +} + +func (t SenderSegmentTest) TestDatas() []TestData { + return t.testData +} + +func init() { + var testData = []TestData{ + { + Description: "Without any receipts", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + }, + }, + { + Description: "Within Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + }, + }, + { + Description: "Within 1 Event above Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 5000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + }, + }, + { + Description: "With 2 Events within Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Events, and 1 Event above Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 4000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and Events within Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 500), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and 1 Receipt have Events above Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(1), + Message: make([]byte, 500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(2), + Message: make([]byte, 500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 4000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 4000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and 1 Receipt have 2 Events above Default Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(1), + Message: make([]byte, 500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(2), + Message: make([]byte, 500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 4000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 4000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 4000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 4000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + + { + Description: "Within Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + }, + }, + { + Description: "Within 1 Event above Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 9000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{}, + }, + }, + }, + }, + { + Description: "With 2 Events within Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3500), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Events, and 1 Event above Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 10000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 3988), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and Events within Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 1500), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and 1 Receipt have Events above Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(1), + Message: make([]byte, 1500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(2), + Message: make([]byte, 1500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 1500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 10000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 10000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + { + Description: "With 2 Receipts, and 1 Receipt have 2 Events above Custom Limit", + Input: struct { + PrivateKey string + Source chain.BTPAddress + Destination chain.BTPAddress + Message *chain.Message + Options types.SenderOptions + }{ + PrivateKey: "22yx6AjQgG1jGuAmPuEwLnVKFnuq5LU23dbU3JBZodKxrJ8dmmqpDZKtRSfiU4F8UQmv1RiZSrjWhQMQC3ye7M1J", + Source: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Destination: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Message: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(1), + Message: make([]byte, 1500), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(2), + Message: make([]byte, 1500), + }, + }, + Height: uint64(1), + }, + &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 10000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 10000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + Options: types.SenderOptions{ + TxDataSizeLimit: 8000, + }, + }, + MockApi: func() *mock.MockApi { + mockApi := mock.NewMockApi(mock.Storage{}) + + mockApi.On("Status", mock.MockParam).Return(mockApi.StatusFactory()) + mockApi.On("ViewAccessKey", mock.MockParam).Return(mockApi.ViewAccessKeyFactory()) + + return mockApi + }(), + Expected: struct { + Success interface{} + Fail interface{} + }{ + Success: struct { + NewMessage *chain.Message + }{ + NewMessage: &chain.Message{ + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: func() []*chain.Receipt { + receipts := make([]*chain.Receipt, 0) + receipts = append(receipts, &chain.Receipt{ + Index: uint64(1), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(3), + Message: make([]byte, 10000), + }, + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(4), + Message: make([]byte, 10000), + }, + }, + Height: uint64(1), + }) + + return receipts + }(), + }, + }, + }, + }, + } + + RegisterTest("SenderSegment", SenderSegmentTest{ + description: "Test Sender Segment", + testData: testData, + }) +} diff --git a/cmd/iconbridge/chain/near/types/sender_options.go b/cmd/iconbridge/chain/near/types/sender_options.go new file mode 100644 index 000000000..e3063210e --- /dev/null +++ b/cmd/iconbridge/chain/near/types/sender_options.go @@ -0,0 +1,6 @@ +package types + +type SenderOptions struct { + TxDataSizeLimit uint64 `json:"tx_data_size_limit"` + BalanceThreshold BigInt `json:"balance_threshold"` +} From 6eeecbd026cfba858b47b1968e90f01d6497b641 Mon Sep 17 00:00:00 2001 From: bbist Date: Wed, 28 Sep 2022 21:38:18 +0545 Subject: [PATCH 06/11] copy receipt to avoid input receipts mutation --- cmd/iconbridge/common/chainutils/segment.go | 9 +++-- .../common/chainutils/segment_test.go | 34 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cmd/iconbridge/common/chainutils/segment.go b/cmd/iconbridge/common/chainutils/segment.go index 0ae93dd68..f0223289e 100644 --- a/cmd/iconbridge/common/chainutils/segment.go +++ b/cmd/iconbridge/common/chainutils/segment.go @@ -47,8 +47,13 @@ rloop: if len(events) == len(receipt.Events) { // all events added newMsg.Receipts = msg.Receipts[i+1:] } else { // save remaining events in this receipt - receipt.Events = receipt.Events[len(events):] - newMsg.Receipts = msg.Receipts[i:] + newMsg.Receipts = make([]*chain.Receipt, len(msg.Receipts[i:])) + copy(newMsg.Receipts, msg.Receipts[i:]) // make a copy to not mutate original receipt + newMsg.Receipts[0] = &chain.Receipt{ + Index: receipt.Index, + Height: receipt.Height, + Events: receipt.Events[len(events):], + } } relayMsg.Receipts = append(relayMsg.Receipts, rlpReceipt) break eloop diff --git a/cmd/iconbridge/common/chainutils/segment_test.go b/cmd/iconbridge/common/chainutils/segment_test.go index c7f025170..947b5a104 100644 --- a/cmd/iconbridge/common/chainutils/segment_test.go +++ b/cmd/iconbridge/common/chainutils/segment_test.go @@ -41,67 +41,79 @@ func TestSegmentByTxDataSize(t *testing.T) { with more events: 4 bytes each */ - _, msg, err := SegmentByTxDataSize(getTestMsg(), 5) + srcMsg := getTestMsg() + _, msg, err := SegmentByTxDataSize(srcMsg, 5) require.NoError(t, err) require.EqualValues(t, 4, len(msg.Receipts)) require.EqualValues(t, 1, msg.Receipts[0].Height) require.EqualValues(t, 2, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 9) // encode first receipt with 1 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 9) // encode first receipt with 1 event require.NoError(t, err) require.EqualValues(t, 4, len(msg.Receipts)) require.EqualValues(t, 1, msg.Receipts[0].Height) require.EqualValues(t, 1, len(msg.Receipts[0].Events)) + require.EqualValues(t, 2, len(srcMsg.Receipts[0].Events), "events in original receipt should not mutate") - _, msg, err = SegmentByTxDataSize(getTestMsg(), 12) // encode first receipt with 1 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 12) // encode first receipt with 1 event require.NoError(t, err) require.EqualValues(t, 4, len(msg.Receipts)) require.EqualValues(t, 1, msg.Receipts[0].Height) require.EqualValues(t, 1, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 15) // encode first receipt with 2 events + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 15) // encode first receipt with 2 events require.NoError(t, err) require.EqualValues(t, 3, len(msg.Receipts)) require.EqualValues(t, 6, msg.Receipts[0].Height) require.EqualValues(t, 4, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 22) // encode first receipt with 2 events and second receipt with 1 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 22) // encode first receipt with 2 events and second receipt with 1 event require.NoError(t, err) require.EqualValues(t, 3, len(msg.Receipts)) require.EqualValues(t, 6, msg.Receipts[0].Height) require.EqualValues(t, 3, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 35) // encode first receipt with 2 events and second receipt with 4 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 35) // encode first receipt with 2 events and second receipt with 4 event require.NoError(t, err) require.EqualValues(t, 2, len(msg.Receipts)) require.EqualValues(t, 7, msg.Receipts[0].Height) require.EqualValues(t, 1, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 43) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 43) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event require.NoError(t, err) require.EqualValues(t, 1, len(msg.Receipts)) require.EqualValues(t, 10, msg.Receipts[0].Height) require.EqualValues(t, 3, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 52) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 52) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event require.NoError(t, err) require.EqualValues(t, 1, len(msg.Receipts)) require.EqualValues(t, 10, msg.Receipts[0].Height) require.EqualValues(t, 3, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 55) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 1 event with message body + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 55) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 1 event with message body require.NoError(t, err) require.EqualValues(t, 1, len(msg.Receipts)) require.EqualValues(t, 10, msg.Receipts[0].Height) require.EqualValues(t, 2, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 60) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 2 events with one with message body + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 60) // encode first receipt with 2 events and second receipt with 4 events and third receipt with 1 event and fourth receipt with 2 events with one with message body require.NoError(t, err) require.EqualValues(t, 1, len(msg.Receipts)) require.EqualValues(t, 10, msg.Receipts[0].Height) require.EqualValues(t, 1, len(msg.Receipts[0].Events)) - _, msg, err = SegmentByTxDataSize(getTestMsg(), 70) // encode all receipts + srcMsg = getTestMsg() + _, msg, err = SegmentByTxDataSize(srcMsg, 70) // encode all receipts require.NoError(t, err) require.EqualValues(t, 0, len(msg.Receipts)) From 0dcaff45f6172bad86d4b76f496eb2f881201124 Mon Sep 17 00:00:00 2001 From: bbist Date: Thu, 29 Sep 2022 09:41:43 +0545 Subject: [PATCH 07/11] skip test with external rpc call --- cmd/iconbridge/chain/bsc/receiver_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/iconbridge/chain/bsc/receiver_test.go b/cmd/iconbridge/chain/bsc/receiver_test.go index 238093b30..2188bd0cd 100644 --- a/cmd/iconbridge/chain/bsc/receiver_test.go +++ b/cmd/iconbridge/chain/bsc/receiver_test.go @@ -381,6 +381,7 @@ func TestReceiver_MockSyncVerifier(t *testing.T) { } func TestSender_NewObj(t *testing.T) { + return // skip test with real rpc endpoint //senderOpts := `{"gas_limit": 24000000,"tx_data_size_limit": 8192,"balance_threshold": "100000000000000000000","boost_gas_price": 1}` thres := intconv.BigInt{} thres.SetString("100000000000000000000", 10) From 49446d26bb7614124e9652713941fea2f77ac3c6 Mon Sep 17 00:00:00 2001 From: bbist Date: Fri, 30 Sep 2022 12:26:12 +0545 Subject: [PATCH 08/11] resolve the cause of failing unit tests --- cmd/iconbridge/chain/near/sender_test.go | 2 +- .../chain/near/tests/sender_segment.go | 37 ++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cmd/iconbridge/chain/near/sender_test.go b/cmd/iconbridge/chain/near/sender_test.go index 8cd4b291f..38d9be831 100644 --- a/cmd/iconbridge/chain/near/sender_test.go +++ b/cmd/iconbridge/chain/near/sender_test.go @@ -209,7 +209,7 @@ func TestNearSender(t *testing.T) { if testData.Expected.Success != nil { expected, Ok := (testData.Expected.Success).(struct { - NewMessage *chain.Message + NewMessage *chain.Message }) require.True(f, Ok) diff --git a/cmd/iconbridge/chain/near/tests/sender_segment.go b/cmd/iconbridge/chain/near/tests/sender_segment.go index e2211972f..3ca778e99 100644 --- a/cmd/iconbridge/chain/near/tests/sender_segment.go +++ b/cmd/iconbridge/chain/near/tests/sender_segment.go @@ -115,7 +115,7 @@ func init() { }, }, { - Description: "Within 1 Event above Default Limit", + Description: "With 1 Event above Default Limit", Input: struct { PrivateKey string Source chain.BTPAddress @@ -162,8 +162,18 @@ func init() { NewMessage *chain.Message }{ NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{}, + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 5000), + }, + }, + Height: uint64(1), + }}, }, }, }, @@ -253,7 +263,7 @@ func init() { { Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), Sequence: uint64(0), - Message: make([]byte, 4000), + Message: make([]byte, 3000), }, { Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), @@ -568,7 +578,6 @@ func init() { }, }, }, - { Description: "Within Custom Limit", Input: struct { @@ -627,7 +636,7 @@ func init() { }, }, { - Description: "Within 1 Event above Custom Limit", + Description: "With 1 Event above Custom Limit", Input: struct { PrivateKey string Source chain.BTPAddress @@ -677,8 +686,18 @@ func init() { NewMessage *chain.Message }{ NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{}, + From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), + Receipts: []*chain.Receipt{{ + Index: uint64(0), + Events: []*chain.Event{ + { + Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), + Sequence: uint64(0), + Message: make([]byte, 9000), + }, + }, + Height: uint64(1), + }}, }, }, }, @@ -770,7 +789,7 @@ func init() { { Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), Sequence: uint64(0), - Message: make([]byte, 10000), + Message: make([]byte, 7000), }, { Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), From d8477972c88c94e99b1538952f2004595a4dc0b5 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan Date: Sat, 22 Oct 2022 09:16:29 +0530 Subject: [PATCH 09/11] update testcase to assert size of relay message --- cmd/iconbridge/chain/near/sender.go | 14 +- cmd/iconbridge/chain/near/sender_test.go | 16 +- .../chain/near/tests/sender_segment.go | 292 ++++-------------- cmd/iconbridge/chain/near/tests/tests.go | 4 +- 4 files changed, 73 insertions(+), 253 deletions(-) diff --git a/cmd/iconbridge/chain/near/sender.go b/cmd/iconbridge/chain/near/sender.go index d39778602..66f0ec6bf 100644 --- a/cmd/iconbridge/chain/near/sender.go +++ b/cmd/iconbridge/chain/near/sender.go @@ -140,7 +140,7 @@ func (s *Sender) newRelayTransaction(ctx context.Context, prev string, message [ }, } - return NewRelayTransaction(ctx, nearWallet, s.destination.ContractAddress(), s.client(), actions), nil + return NewRelayTransaction(ctx, nearWallet, s.destination.ContractAddress(), s.client(), actions, message), nil } return nil, fmt.Errorf("failed to cast wallet") @@ -151,9 +151,10 @@ type RelayTransaction struct { client IClient wallet *wallet.NearWallet context context.Context + message []byte } -func NewRelayTransaction(context context.Context, wallet *wallet.NearWallet, destination string, client IClient, actions []types.Action) *RelayTransaction { +func NewRelayTransaction(context context.Context, wallet *wallet.NearWallet, destination string, client IClient, actions []types.Action, message []byte) *RelayTransaction { transaction := types.Transaction{ SignerId: types.AccountId(wallet.Address()), ReceiverId: types.AccountId(destination), @@ -162,10 +163,11 @@ func NewRelayTransaction(context context.Context, wallet *wallet.NearWallet, des } return &RelayTransaction{ - Transaction: transaction, - client: client, - wallet: wallet, - context: context, + transaction, + client, + wallet, + context, + message, } } diff --git a/cmd/iconbridge/chain/near/sender_test.go b/cmd/iconbridge/chain/near/sender_test.go index 19ad952c3..1ba0eecb5 100644 --- a/cmd/iconbridge/chain/near/sender_test.go +++ b/cmd/iconbridge/chain/near/sender_test.go @@ -206,18 +206,20 @@ func TestNearSender(t *testing.T) { sender, err := NewSender(SenderConfig{source: input.Source, destination: input.Destination, wallet: nearWallet, options: input.Options}, log.New(), client) require.NoError(f, err) - _, newMsg, err := sender.Segment(context.Background(), input.Message) + relayTx, newMsg, err := sender.Segment(context.Background(), input.Message) require.NoError(f, err) if testData.Expected.Success != nil { - expected, Ok := (testData.Expected.Success).(struct { - NewMessage *chain.Message - }) - require.True(f, Ok) + message := make([]byte, 0) - assert.Equal(f, expected.NewMessage, newMsg) + if relayTx != nil { + message = (relayTx).(*RelayTransaction).message + } + + testData.Expected.Success.(func(*testing.T, []byte, *chain.Message))(f, message, newMsg) + assert.Nil(f, err) } else { - assert.Error(f, err) + testData.Expected.Fail.(func(*testing.T, error))(f, err) } }) } diff --git a/cmd/iconbridge/chain/near/tests/sender_segment.go b/cmd/iconbridge/chain/near/tests/sender_segment.go index 3ca778e99..5d86fedf8 100644 --- a/cmd/iconbridge/chain/near/tests/sender_segment.go +++ b/cmd/iconbridge/chain/near/tests/sender_segment.go @@ -4,6 +4,8 @@ import ( "github.com/icon-project/icon-bridge/cmd/iconbridge/chain" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/tests/mock" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/types" + "github.com/stretchr/testify/assert" + "testing" ) type SenderSegmentTest struct { @@ -50,13 +52,8 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{}, - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -104,13 +101,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{}, - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f, len(relayMessage) > 3988) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -158,23 +151,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{{ - Index: uint64(0), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(0), - Message: make([]byte, 5000), - }, - }, - Height: uint64(1), - }}, - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f, len(relayMessage) > 5000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -227,17 +206,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 2000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -290,28 +261,10 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(0), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(0), - Message: make([]byte, 3988), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 3000) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 3988) }, }, }, @@ -380,16 +333,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 2000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -458,28 +404,10 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(1), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(4), - Message: make([]byte, 4000), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 1500) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 4000) }, }, }, @@ -548,33 +476,11 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(1), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(3), - Message: make([]byte, 4000), - }, - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(4), - Message: make([]byte, 4000), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 1000) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 4000) + assert.Equal(f, len(nextMessage.Receipts[0].Events[1].Message), 4000) }, }, }, @@ -625,13 +531,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{}, - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 3988) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -682,23 +584,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: []*chain.Receipt{{ - Index: uint64(0), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(0), - Message: make([]byte, 9000), - }, - }, - Height: uint64(1), - }}, - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 9000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -754,16 +642,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 7000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -819,28 +700,10 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(0), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(0), - Message: make([]byte, 3988), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 7000) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 3988) }, }, }, @@ -912,16 +775,9 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 6000) + assert.Equal(f, len(nextMessage.Receipts), 0) }, }, }, @@ -993,28 +849,10 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(1), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(4), - Message: make([]byte, 10000), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 4500) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 10000) }, }, }, @@ -1086,33 +924,11 @@ func init() { Success interface{} Fail interface{} }{ - Success: struct { - NewMessage *chain.Message - }{ - NewMessage: &chain.Message{ - From: chain.BTPAddress("btp://0x1.icon/0xc294b1A62E82d3f135A8F9b2f9cAEAA23fbD6Cf5"), - Receipts: func() []*chain.Receipt { - receipts := make([]*chain.Receipt, 0) - receipts = append(receipts, &chain.Receipt{ - Index: uint64(1), - Events: []*chain.Event{ - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(3), - Message: make([]byte, 10000), - }, - { - Next: chain.BTPAddress("btp://0x1.near/dev-20211206025826-24100687319598"), - Sequence: uint64(4), - Message: make([]byte, 10000), - }, - }, - Height: uint64(1), - }) - - return receipts - }(), - }, + Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { + assert.True(f,len(relayMessage) > 3000) + assert.Equal(f, len(nextMessage.Receipts), 1) + assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 10000) + assert.Equal(f, len(nextMessage.Receipts[0].Events[1].Message), 10000) }, }, }, diff --git a/cmd/iconbridge/chain/near/tests/tests.go b/cmd/iconbridge/chain/near/tests/tests.go index d69180e3c..a6fd93811 100644 --- a/cmd/iconbridge/chain/near/tests/tests.go +++ b/cmd/iconbridge/chain/near/tests/tests.go @@ -3,7 +3,7 @@ package tests import ( "fmt" "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/near/tests/mock" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "testing" ) @@ -35,6 +35,6 @@ func GetTest(module string, t *testing.T) (Test, error) { return test, nil } - assert.NoError(t, err) + require.NoError(t, err) return nil, err } From 099aff7aaf51b37549c664aea26e1182a7a8ea37 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan Date: Sat, 22 Oct 2022 09:21:23 +0530 Subject: [PATCH 10/11] fix missing param for e2e test --- cmd/e2etest/chain/near/requests.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/e2etest/chain/near/requests.go b/cmd/e2etest/chain/near/requests.go index 7207f5529..3fb621caa 100644 --- a/cmd/e2etest/chain/near/requests.go +++ b/cmd/e2etest/chain/near/requests.go @@ -347,7 +347,7 @@ func (r *requestAPI) transferNativeIntraChain(senderKey, recepientAddress string }, } - newRelay := near.NewRelayTransaction(context.Background(), senderWallet, recepientAddress, r.cl, actions) + newRelay := near.NewRelayTransaction(context.Background(), senderWallet, recepientAddress, r.cl, actions, []byte{}) newRelay.Send(context.Background()) txH := newRelay.ID().(types.CryptoHash) txHash = hexutil.Encode(txH[:]) @@ -442,7 +442,7 @@ func (r *requestAPI) transactWithContract(senderKey string, contractAddress stri }, } - newRelay := near.NewRelayTransaction(context.Background(), senderWallet, contractAddress, r.cl, actions) + newRelay := near.NewRelayTransaction(context.Background(), senderWallet, contractAddress, r.cl, actions, []byte{}) newRelay.Send(context.Background()) txH := newRelay.ID().(types.CryptoHash) txHash = txH.Base58Encode() From bf923b01cee04ad1bbf164c8abac045f25a62f17 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan Date: Sat, 22 Oct 2022 09:30:17 +0530 Subject: [PATCH 11/11] fix testcase assertions --- .../chain/near/tests/sender_segment.go | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/cmd/iconbridge/chain/near/tests/sender_segment.go b/cmd/iconbridge/chain/near/tests/sender_segment.go index 5d86fedf8..5456e5c9f 100644 --- a/cmd/iconbridge/chain/near/tests/sender_segment.go +++ b/cmd/iconbridge/chain/near/tests/sender_segment.go @@ -53,7 +53,7 @@ func init() { Fail interface{} }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -103,7 +103,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f, len(relayMessage) > 3988) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -153,7 +153,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f, len(relayMessage) > 5000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -208,7 +208,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 2000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -263,8 +263,8 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 3000) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 3988) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 3988, len(nextMessage.Receipts[0].Events[0].Message)) }, }, }, @@ -335,7 +335,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 2000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -406,8 +406,8 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 1500) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 4000) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 4000, len(nextMessage.Receipts[0].Events[0].Message)) }, }, }, @@ -478,9 +478,9 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 1000) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 4000) - assert.Equal(f, len(nextMessage.Receipts[0].Events[1].Message), 4000) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 4000, len(nextMessage.Receipts[0].Events[0].Message)) + assert.Equal(f, 4000, len(nextMessage.Receipts[0].Events[1].Message)) }, }, }, @@ -533,7 +533,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 3988) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -586,7 +586,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 9000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -644,7 +644,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 7000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -702,8 +702,8 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 7000) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 3988) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 3988, len(nextMessage.Receipts[0].Events[0].Message)) }, }, }, @@ -777,7 +777,7 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 6000) - assert.Equal(f, len(nextMessage.Receipts), 0) + assert.Equal(f, 0, len(nextMessage.Receipts)) }, }, }, @@ -851,8 +851,8 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 4500) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 10000) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 10000, len(nextMessage.Receipts[0].Events[0].Message)) }, }, }, @@ -926,9 +926,9 @@ func init() { }{ Success: func(f *testing.T, relayMessage []byte, nextMessage *chain.Message) { assert.True(f,len(relayMessage) > 3000) - assert.Equal(f, len(nextMessage.Receipts), 1) - assert.Equal(f, len(nextMessage.Receipts[0].Events[0].Message), 10000) - assert.Equal(f, len(nextMessage.Receipts[0].Events[1].Message), 10000) + assert.Equal(f, 1, len(nextMessage.Receipts)) + assert.Equal(f, 10000, len(nextMessage.Receipts[0].Events[0].Message)) + assert.Equal(f, 10000, len(nextMessage.Receipts[0].Events[1].Message)) }, }, },