Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bmr): send only one receipt per transaction #545

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/e2etest/chain/near/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
Expand Down Expand Up @@ -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()
Expand Down
36 changes: 6 additions & 30 deletions cmd/iconbridge/chain/bsc/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -161,38 +163,12 @@ func (s *sender) Segment(
return nil, msg, nil
}

rm := &chain.RelayMessage{
Receipts: make([][]byte, 0),
relayMsg, newMsg, err := chainutils.SegmentByTxDataSize(msg, s.opts.TxDataSizeLimit)
if err != nil {
return nil, nil, err
}

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)
}
message, err := codec.RLP.MarshalToBytes(rm)
message, err := codec.RLP.MarshalToBytes(relayMsg)
if err != nil {
return nil, nil, err
}
Expand Down
37 changes: 5 additions & 32 deletions cmd/iconbridge/chain/hmny/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
40 changes: 7 additions & 33 deletions cmd/iconbridge/chain/icon/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ 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/common/chainutils"

"github.com/icon-project/icon-bridge/cmd/iconbridge/chain"
"github.com/icon-project/icon-bridge/common"
"github.com/icon-project/icon-bridge/common/codec"
Expand Down Expand Up @@ -147,40 +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,
}

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
}
Expand Down
83 changes: 34 additions & 49 deletions cmd/iconbridge/chain/near/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ 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"
)

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
Expand All @@ -28,7 +31,7 @@ const (
type SenderConfig struct {
source chain.BTPAddress
destination chain.BTPAddress
options json.RawMessage
options types.SenderOptions
wallet wallet.Wallet
}

Expand All @@ -38,17 +41,21 @@ type Sender struct {
destination chain.BTPAddress
wallet Wallet
logger log.Logger
options struct {
BalanceThreshold types.BigInt `json:"balance_threshold"`
}
options types.SenderOptions
}

func senderFactory(source, destination chain.BTPAddress, urls []string, wallet wallet.Wallet, options json.RawMessage, logger log.Logger) (chain.Sender, error) {
func senderFactory(source, destination chain.BTPAddress, urls []string, wallet wallet.Wallet, opt json.RawMessage, logger log.Logger) (chain.Sender, error) {
var options types.SenderOptions
clients, err := newClients(urls, logger)
if err != nil {
return nil, err
}

if err := json.Unmarshal(opt, &options); err != nil {
logger.Panicf("fail to unmarshal options:%#v err:%+v", opt, err)
return nil, err
}

return NewSender(SenderConfig{source, destination, options, wallet}, logger, clients...)
}

Expand All @@ -63,11 +70,7 @@ func NewSender(config SenderConfig, logger log.Logger, clients ...IClient) (*Sen
logger: logger,
source: config.source,
destination: config.destination,
}

if err := json.Unmarshal(config.options, &s.options); err != nil && config.options != nil {
logger.Panicf("fail to unmarshal options:%#v err:%+v", config.options, err)
return nil, err
options: config.options,
}

return s, nil
Expand All @@ -77,48 +80,28 @@ 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 len(msg.Receipts) == 0 {
return nil, msg, nil
if s.options.TxDataSizeLimit == 0 {
limit := defaultTxSizeLimit
s.options.TxDataSizeLimit = uint64(limit)
}

rm := &chain.RelayMessage{
Receipts: make([][]byte, 0),
if len(msg.Receipts) == 0 {
return nil, msg, nil
}

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)
bbist marked this conversation as resolved.
Show resolved Hide resolved
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)
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
}
Expand Down Expand Up @@ -157,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")
Expand All @@ -168,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),
Expand All @@ -179,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,
}
}

Expand Down
Loading