Skip to content

Commit

Permalink
update dependencies and update stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Dec 6, 2023
1 parent 3f245a3 commit db6acdc
Show file tree
Hide file tree
Showing 19 changed files with 2,446 additions and 379 deletions.
35 changes: 3 additions & 32 deletions bridges/ethMultiversX/bridgeExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ethmultiversx
import (
"context"
"fmt"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"math/big"
"time"

Expand All @@ -19,14 +20,6 @@ const splits = 10

const minRetries = 1

type ArgListsBatch struct {
Tokens []common.Address
Recipients []common.Address
ConvertedTokenBytes [][]byte
Amounts []*big.Int
Nonces []*big.Int
}

// ArgsBridgeExecutor is the arguments DTO struct used in both bridges
type ArgsBridgeExecutor struct {
Log logger.Logger
Expand Down Expand Up @@ -477,7 +470,7 @@ func (executor *bridgeExecutor) SignTransferOnEthereum() error {
return ErrNilBatch
}

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

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

argLists, err := executor.extractList(executor.batch)
argLists, err := batchProcessor.ExtractList(executor.batch)
if err != nil {
return err
}
Expand Down Expand Up @@ -602,28 +595,6 @@ func (executor *bridgeExecutor) checkAvailableTokens(ctx context.Context, tokens
return executor.checkCumulatedTransfers(ctx, transfers)
}

func (executor *bridgeExecutor) extractList(batch *clients.TransferBatch) (*ArgListsBatch, error) {
arg := ArgListsBatch{}

for _, dt := range batch.Deposits {
recipient := common.BytesToAddress(dt.ToBytes)
arg.Recipients = append(arg.Recipients, recipient)

token := common.BytesToAddress(dt.ConvertedTokenBytes)
arg.Tokens = append(arg.Tokens, token)

amount := big.NewInt(0).Set(dt.Amount)
arg.Amounts = append(arg.Amounts, amount)

nonce := big.NewInt(0).SetUint64(dt.Nonce)
arg.Nonces = append(arg.Nonces, nonce)

arg.ConvertedTokenBytes = append(arg.ConvertedTokenBytes, dt.ConvertedTokenBytes)
}

return &arg, nil
}

func (executor *bridgeExecutor) getCumulatedTransfers(tokens []common.Address, amounts []*big.Int) map[common.Address]*big.Int {
transfers := make(map[common.Address]*big.Int)
for i, token := range tokens {
Expand Down
34 changes: 29 additions & 5 deletions bridges/ethMultiversX/bridgeExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"math/big"
"strings"
"testing"
Expand Down Expand Up @@ -1083,7 +1084,7 @@ func TestMultiversXToEthBridgeExecutor_SignTransferOnEthereum(t *testing.T) {

args := createMockExecutorArgs()
args.EthereumClient = &bridgeTests.EthereumClientStub{
GenerateMessageHashCalled: func(batch *clients.TransferBatch) (common.Hash, error) {
GenerateMessageHashCalled: func(batch *batchProcessor.ArgListsBatch, batchID uint64) (common.Hash, error) {
return common.Hash{}, expectedErr
},
}
Expand All @@ -1100,7 +1101,7 @@ func TestMultiversXToEthBridgeExecutor_SignTransferOnEthereum(t *testing.T) {
wasCalledBroadcastSignatureForMessageHashCalled := false
args := createMockExecutorArgs()
args.EthereumClient = &bridgeTests.EthereumClientStub{
GenerateMessageHashCalled: func(batch *clients.TransferBatch) (common.Hash, error) {
GenerateMessageHashCalled: func(batch *batchProcessor.ArgListsBatch, batchID uint64) (common.Hash, error) {
wasCalledGenerateMessageHashCalled = true
return common.Hash{}, nil
},
Expand Down Expand Up @@ -1153,7 +1154,7 @@ func TestMultiversXToEthBridgeExecutor_PerformTransferOnEthereum(t *testing.T) {
GetQuorumSizeCalled: func(ctx context.Context) (*big.Int, error) {
return big.NewInt(0), nil
},
ExecuteTransferCalled: func(ctx context.Context, msgHash common.Hash, batch *clients.TransferBatch, quorum int) (string, error) {
ExecuteTransferCalled: func(ctx context.Context, msgHash common.Hash, batch *batchProcessor.ArgListsBatch, batchId uint64, quorum int) (string, error) {
return "", expectedErr
},
}
Expand All @@ -1176,9 +1177,16 @@ func TestMultiversXToEthBridgeExecutor_PerformTransferOnEthereum(t *testing.T) {
wasCalledGetQuorumSizeCalled = true
return big.NewInt(int64(providedQuorum)), nil
},
ExecuteTransferCalled: func(ctx context.Context, msgHash common.Hash, batch *clients.TransferBatch, quorum int) (string, error) {
ExecuteTransferCalled: func(ctx context.Context, msgHash common.Hash, batch *batchProcessor.ArgListsBatch, batchId uint64, quorum int) (string, error) {
assert.True(t, providedHash == msgHash)
assert.True(t, providedBatch == batch)
assert.True(t, providedBatch.ID == batchId)
for i := 0; i < len(providedBatch.Deposits); i++ {
assert.True(t, providedBatch.Deposits[i].Amount == batch.Amounts[i])
assert.True(t, providedBatch.Deposits[i].Nonce == batch.Nonces[i].Uint64())
assert.True(t, CompareBytes(providedBatch.Deposits[i].ToBytes, batch.Recipients[i].Bytes()))
assert.True(t, CompareBytes(providedBatch.Deposits[i].TokenBytes, batch.Tokens[i].Bytes()))
assert.True(t, CompareBytes(providedBatch.Deposits[i].ConvertedTokenBytes, batch.ConvertedTokenBytes[i]))
}
assert.True(t, providedQuorum == quorum)

wasCalledExecuteTransferCalled = true
Expand All @@ -1196,6 +1204,9 @@ func TestMultiversXToEthBridgeExecutor_PerformTransferOnEthereum(t *testing.T) {
})
}

func TestMultiversXToEthBridgeExecutor_checkAvailableTokens(t *testing.T) {

}
func TestMultiversXToEthBridgeExecutor_IsQuorumReachedOnEthereum(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1655,3 +1666,16 @@ func TestBridgeExecutor_ValidateBatch(t *testing.T) {
assert.True(t, result)
assert.True(t, validateBatchCalled)
}

// CompareBytes compares two byte slices and returns true if they are equal.
func CompareBytes(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
5 changes: 3 additions & 2 deletions bridges/ethMultiversX/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ethmultiversx

import (
"context"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -39,10 +40,10 @@ type MultiversXClient interface {
type EthereumClient interface {
GetBatch(ctx context.Context, nonce uint64) (*clients.TransferBatch, error)
WasExecuted(ctx context.Context, batchID uint64) (bool, error)
GenerateMessageHash(batch *ArgListsBatch, batchId uint64) (common.Hash, error)
GenerateMessageHash(batch *batchProcessor.ArgListsBatch, batchId uint64) (common.Hash, error)

BroadcastSignatureForMessageHash(msgHash common.Hash)
ExecuteTransfer(ctx context.Context, msgHash common.Hash, batch *ArgListsBatch, batchId uint64, quorum int) (string, error)
ExecuteTransfer(ctx context.Context, msgHash common.Hash, batch *batchProcessor.ArgListsBatch, batchId uint64, quorum int) (string, error)
GetTransactionsStatuses(ctx context.Context, batchId uint64) ([]byte, error)
GetQuorumSize(ctx context.Context) (*big.Int, error)
IsQuorumReached(ctx context.Context, msgHash common.Hash) (bool, error)
Expand Down
5 changes: 3 additions & 2 deletions clients/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"math/big"
"sync"

Expand Down Expand Up @@ -214,7 +215,7 @@ func (c *client) BroadcastSignatureForMessageHash(msgHash common.Hash) {
}

// GenerateMessageHash will generate the message hash based on the provided batch
func (c *client) GenerateMessageHash(batch *ethmultiversx.ArgListsBatch, batchId uint64) (common.Hash, error) {
func (c *client) GenerateMessageHash(batch *batchProcessor.ArgListsBatch, batchId uint64) (common.Hash, error) {
if batch == nil {
return common.Hash{}, clients.ErrNilBatch
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func generateTransferArgs() (abi.Arguments, error) {
func (c *client) ExecuteTransfer(
ctx context.Context,
msgHash common.Hash,
argLists *ethmultiversx.ArgListsBatch,
argLists *batchProcessor.ArgListsBatch,
batchId uint64,
quorum int,
) (string, error) {
Expand Down
Loading

0 comments on commit db6acdc

Please sign in to comment.