Skip to content

Commit

Permalink
Merge pull request #281 from multiversx/batch-refactor
Browse files Browse the repository at this point in the history
Renamed several fields in the deposit batch
  • Loading branch information
dragos-rebegea authored Dec 27, 2023
2 parents eb5baab + b2bcf5b commit 8accbcf
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 180 deletions.
18 changes: 9 additions & 9 deletions bridges/ethMultiversX/bridgeExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (executor *bridgeExecutor) SignTransferOnEthereum() error {
return ErrNilBatch
}

argLists, err := batchProcessor.ExtractList(executor.batch)
argLists, err := batchProcessor.ExtractListMvxToEth(executor.batch)
if err != nil {
return err
}
Expand Down Expand Up @@ -558,12 +558,12 @@ func (executor *bridgeExecutor) PerformTransferOnEthereum(ctx context.Context) e

executor.log.Debug("fetched quorum size", "quorum", quorumSize.Int64())

argLists, err := batchProcessor.ExtractList(executor.batch)
argLists, err := batchProcessor.ExtractListMvxToEth(executor.batch)
if err != nil {
return err
}

err = executor.checkAvailableTokensOnEthereum(ctx, argLists.Tokens, argLists.ConvertedTokenBytes, argLists.Amounts)
err = executor.checkAvailableTokensOnEthereum(ctx, argLists.EthTokens, argLists.MvxTokenBytes, argLists.Amounts)
if err != nil {
return err
}
Expand Down Expand Up @@ -647,18 +647,18 @@ func (executor *bridgeExecutor) isMintBurnOnMultiversX(ctx context.Context, toke
return isMintBurn
}

func (executor *bridgeExecutor) checkAvailableTokensOnEthereum(ctx context.Context, tokens []common.Address, convertedTokens [][]byte, amounts []*big.Int) error {
tokens, convertedTokens, amounts = executor.getCumulatedTransfers(tokens, convertedTokens, amounts)
func (executor *bridgeExecutor) checkAvailableTokensOnEthereum(ctx context.Context, ethTokens []common.Address, mvxTokens [][]byte, amounts []*big.Int) error {
ethTokens, mvxTokens, amounts = executor.getCumulatedTransfers(ethTokens, mvxTokens, amounts)

return executor.checkCumulatedTransfers(ctx, tokens, convertedTokens, amounts)
return executor.checkCumulatedTransfers(ctx, ethTokens, mvxTokens, amounts)
}

func (executor *bridgeExecutor) getCumulatedTransfers(tokens []common.Address, convertedTokens [][]byte, amounts []*big.Int) ([]common.Address, [][]byte, []*big.Int) {
func (executor *bridgeExecutor) getCumulatedTransfers(ethTokens []common.Address, mvxTokens [][]byte, amounts []*big.Int) ([]common.Address, [][]byte, []*big.Int) {
cumulatedAmounts := make(map[common.Address]*big.Int)
uniqueTokens := make([]common.Address, 0)
uniqueConvertedTokens := make([][]byte, 0)

for i, token := range tokens {
for i, token := range ethTokens {
existingValue, exists := cumulatedAmounts[token]
if exists {
existingValue.Add(existingValue, amounts[i])
Expand All @@ -667,7 +667,7 @@ func (executor *bridgeExecutor) getCumulatedTransfers(tokens []common.Address, c

cumulatedAmounts[token] = amounts[i]
uniqueTokens = append(uniqueTokens, token)
uniqueConvertedTokens = append(uniqueConvertedTokens, convertedTokens[i])
uniqueConvertedTokens = append(uniqueConvertedTokens, mvxTokens[i])
}

finalAmounts := make([]*big.Int, len(uniqueTokens))
Expand Down
4 changes: 2 additions & 2 deletions bridges/ethMultiversX/bridgeExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,8 +1255,8 @@ func TestMultiversXToEthBridgeExecutor_PerformTransferOnEthereum(t *testing.T) {
assert.Equal(t, providedBatch.Deposits[i].Amount, batch.Amounts[i])
assert.Equal(t, providedBatch.Deposits[i].Nonce, batch.Nonces[i].Uint64())
assert.Equal(t, providedBatch.Deposits[i].ToBytes, batch.Recipients[i].Bytes())
assert.Equal(t, providedBatch.Deposits[i].TokenBytes, batch.Tokens[i].Bytes())
assert.Equal(t, providedBatch.Deposits[i].ConvertedTokenBytes, batch.ConvertedTokenBytes[i])
assert.Equal(t, providedBatch.Deposits[i].SourceTokenBytes, batch.EthTokens[i].Bytes())
assert.Equal(t, providedBatch.Deposits[i].DestinationTokenBytes, batch.MvxTokenBytes[i])
}
assert.True(t, providedQuorum == quorum)

Expand Down
52 changes: 26 additions & 26 deletions clients/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ func (tb *TransferBatch) ResolveNewDeposits(newNumDeposits int) {

// DepositTransfer is the deposit transfer structure agnostic of any chain implementation
type DepositTransfer struct {
Nonce uint64 `json:"nonce"`
ToBytes []byte `json:"-"`
DisplayableTo string `json:"to"`
FromBytes []byte `json:"-"`
DisplayableFrom string `json:"from"`
TokenBytes []byte `json:"-"`
ConvertedTokenBytes []byte `json:"-"`
DisplayableToken string `json:"token"`
Amount *big.Int `json:"amount"`
ExtraGasLimit uint64 `json:"gasLimit"`
Data []byte `json:"-"`
DisplayableData string `json:"data"`
Nonce uint64 `json:"nonce"`
ToBytes []byte `json:"-"`
DisplayableTo string `json:"to"`
FromBytes []byte `json:"-"`
DisplayableFrom string `json:"from"`
SourceTokenBytes []byte `json:"-"`
DestinationTokenBytes []byte `json:"-"`
DisplayableToken string `json:"token"`
Amount *big.Int `json:"amount"`
ExtraGasLimit uint64 `json:"gasLimit"`
Data []byte `json:"-"`
DisplayableData string `json:"data"`
}

// String will convert the deposit transfer to a string
Expand All @@ -85,26 +85,26 @@ func (dt *DepositTransfer) String() string {
dt.DisplayableTo, dt.DisplayableFrom, dt.DisplayableToken, dt.Amount, dt.Nonce, dt.ExtraGasLimit, dt.DisplayableData)
}

// Clone will deep clone the current DepositTransfer instance
// Clone will deeply clone the current DepositTransfer instance
func (dt *DepositTransfer) Clone() *DepositTransfer {
cloned := &DepositTransfer{
Nonce: dt.Nonce,
ToBytes: make([]byte, len(dt.ToBytes)),
DisplayableTo: dt.DisplayableTo,
FromBytes: make([]byte, len(dt.FromBytes)),
DisplayableFrom: dt.DisplayableFrom,
TokenBytes: make([]byte, len(dt.TokenBytes)),
ConvertedTokenBytes: make([]byte, len(dt.ConvertedTokenBytes)),
DisplayableToken: dt.DisplayableToken,
Amount: big.NewInt(0),
Data: make([]byte, len(dt.Data)),
DisplayableData: dt.DisplayableData,
Nonce: dt.Nonce,
ToBytes: make([]byte, len(dt.ToBytes)),
DisplayableTo: dt.DisplayableTo,
FromBytes: make([]byte, len(dt.FromBytes)),
DisplayableFrom: dt.DisplayableFrom,
SourceTokenBytes: make([]byte, len(dt.SourceTokenBytes)),
DestinationTokenBytes: make([]byte, len(dt.DestinationTokenBytes)),
DisplayableToken: dt.DisplayableToken,
Amount: big.NewInt(0),
Data: make([]byte, len(dt.Data)),
DisplayableData: dt.DisplayableData,
}

copy(cloned.ToBytes, dt.ToBytes)
copy(cloned.FromBytes, dt.FromBytes)
copy(cloned.TokenBytes, dt.TokenBytes)
copy(cloned.ConvertedTokenBytes, dt.ConvertedTokenBytes)
copy(cloned.SourceTokenBytes, dt.SourceTokenBytes)
copy(cloned.DestinationTokenBytes, dt.DestinationTokenBytes)
copy(cloned.Data, dt.Data)
if dt.Amount != nil {
cloned.Amount.Set(dt.Amount)
Expand Down
66 changes: 33 additions & 33 deletions clients/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ func TestDepositTransfer_Clone(t *testing.T) {
t.Parallel()

dt := &DepositTransfer{
Nonce: 112334,
ToBytes: []byte("to"),
DisplayableTo: "to",
FromBytes: []byte("from"),
DisplayableFrom: "from",
TokenBytes: []byte("token"),
DisplayableToken: "token",
Amount: big.NewInt(7463),
ConvertedTokenBytes: []byte("converted token"),
Data: []byte("tx data"),
Nonce: 112334,
ToBytes: []byte("to"),
DisplayableTo: "to",
FromBytes: []byte("from"),
DisplayableFrom: "from",
SourceTokenBytes: []byte("source token"),
DisplayableToken: "token",
Amount: big.NewInt(7463),
DestinationTokenBytes: []byte("destination token"),
Data: []byte("tx data"),
}

cloned := dt.Clone()
Expand All @@ -38,7 +38,7 @@ func TestDepositTransfer_String(t *testing.T) {
DisplayableTo: "to",
FromBytes: []byte("from"),
DisplayableFrom: "from",
TokenBytes: []byte("token"),
SourceTokenBytes: []byte("source token"),
DisplayableToken: "token",
Amount: big.NewInt(7463),
}
Expand All @@ -54,28 +54,28 @@ func TestTransferBatch_Clone(t *testing.T) {
ID: 2243,
Deposits: []*DepositTransfer{
{
Nonce: 1,
ToBytes: []byte("to1"),
DisplayableTo: "to1",
FromBytes: []byte("from1"),
DisplayableFrom: "from1",
TokenBytes: []byte("token1"),
DisplayableToken: "token1",
Amount: big.NewInt(3344),
ConvertedTokenBytes: []byte("converted token1"),
Data: []byte("tx data"),
Nonce: 1,
ToBytes: []byte("to1"),
DisplayableTo: "to1",
FromBytes: []byte("from1"),
DisplayableFrom: "from1",
SourceTokenBytes: []byte("source token1"),
DisplayableToken: "token1",
Amount: big.NewInt(3344),
DestinationTokenBytes: []byte("destination token1"),
Data: []byte("tx data"),
},
{
Nonce: 2,
ToBytes: []byte("to2"),
DisplayableTo: "to2",
FromBytes: []byte("from2"),
DisplayableFrom: "from2",
TokenBytes: []byte("token2"),
DisplayableToken: "token2",
Amount: big.NewInt(5566),
ConvertedTokenBytes: []byte("converted token2"),
Data: []byte("tx data"),
Nonce: 2,
ToBytes: []byte("to2"),
DisplayableTo: "to2",
FromBytes: []byte("from2"),
DisplayableFrom: "from2",
SourceTokenBytes: []byte("source token2"),
DisplayableToken: "token2",
Amount: big.NewInt(5566),
DestinationTokenBytes: []byte("destination token2"),
Data: []byte("tx data"),
},
},
Statuses: []byte{Executed, Rejected},
Expand All @@ -99,7 +99,7 @@ func TestTransferBatch_String(t *testing.T) {
DisplayableTo: "to1",
FromBytes: []byte("from1"),
DisplayableFrom: "from1",
TokenBytes: []byte("token1"),
SourceTokenBytes: []byte("source token1"),
DisplayableToken: "token1",
Amount: big.NewInt(3344),
},
Expand All @@ -109,7 +109,7 @@ func TestTransferBatch_String(t *testing.T) {
DisplayableTo: "to2",
FromBytes: []byte("from2"),
DisplayableFrom: "from2",
TokenBytes: []byte("token2"),
SourceTokenBytes: []byte("source token2"),
DisplayableToken: "token2",
Amount: big.NewInt(5566),
},
Expand Down
14 changes: 7 additions & 7 deletions clients/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ func (c *client) GetBatch(ctx context.Context, nonce uint64) (*clients.TransferB
DisplayableTo: c.addressConverter.ToBech32StringSilent(toBytes),
FromBytes: fromBytes,
DisplayableFrom: c.addressConverter.ToHexString(fromBytes),
TokenBytes: tokenBytes,
SourceTokenBytes: tokenBytes,
DisplayableToken: c.addressConverter.ToHexString(tokenBytes),
Amount: big.NewInt(0).Set(deposit.Amount),
}
storedConvertedTokenBytes, exists := cachedTokens[depositTransfer.DisplayableToken]
if !exists {
depositTransfer.ConvertedTokenBytes, err = c.tokensMapper.ConvertToken(ctx, depositTransfer.TokenBytes)
depositTransfer.DestinationTokenBytes, err = c.tokensMapper.ConvertToken(ctx, depositTransfer.SourceTokenBytes)
if err != nil {
return nil, err
}
cachedTokens[depositTransfer.DisplayableToken] = depositTransfer.ConvertedTokenBytes
cachedTokens[depositTransfer.DisplayableToken] = depositTransfer.DestinationTokenBytes
} else {
depositTransfer.ConvertedTokenBytes = storedConvertedTokenBytes
depositTransfer.DestinationTokenBytes = storedConvertedTokenBytes
}

transferBatch.Deposits = append(transferBatch.Deposits, depositTransfer)
Expand Down Expand Up @@ -267,7 +267,7 @@ func (c *client) GenerateMessageHash(batch *batchProcessor.ArgListsBatch, batchI
return common.Hash{}, err
}

pack, err := args.Pack(batch.Recipients, batch.Tokens, batch.Amounts, batch.Nonces, big.NewInt(0).SetUint64(batchId), "ExecuteBatchedTransfer")
pack, err := args.Pack(batch.Recipients, batch.EthTokens, batch.Amounts, batch.Nonces, big.NewInt(0).SetUint64(batchId), "ExecuteBatchedTransfer")
if err != nil {
return common.Hash{}, err
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func (c *client) ExecuteTransfer(

auth.Nonce = big.NewInt(nonce)
auth.Value = big.NewInt(0)
auth.GasLimit = c.transferGasLimitBase + uint64(len(argLists.Tokens))*c.transferGasLimitForEach
auth.GasLimit = c.transferGasLimitBase + uint64(len(argLists.EthTokens))*c.transferGasLimitForEach
auth.Context = ctx
auth.GasPrice = gasPrice

Expand All @@ -382,7 +382,7 @@ func (c *client) ExecuteTransfer(
}

batchID := big.NewInt(0).SetUint64(batchId)
tx, err := c.clientWrapper.ExecuteTransfer(auth, argLists.Tokens, argLists.Recipients, argLists.Amounts, argLists.Nonces, batchID, signatures)
tx, err := c.clientWrapper.ExecuteTransfer(auth, argLists.EthTokens, argLists.Recipients, argLists.Amounts, argLists.Nonces, batchID, signatures)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 8accbcf

Please sign in to comment.