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 bridge e2e tests for transferring tokens #1953

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Changes from 3 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
91 changes: 61 additions & 30 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ func TestE2E_Bridge_Transfers(t *testing.T) {

func TestE2E_Bridge_ERC721Transfer(t *testing.T) {
const (
transfersCount = 4
epochSize = 5
transfersCount = 4
epochSize = 5
numberOfAttempts = 4
)

minter, err := ethgow.GenerateKey()
Expand Down Expand Up @@ -535,17 +536,25 @@ func TestE2E_Bridge_ERC721Transfer(t *testing.T) {
// the transactions are processed and there should be a success events
var stateSyncedResult contractsapi.StateSyncResultEvent

logs, err := getFilteredLogs(stateSyncedResult.Sig(), 0, 50, childEthEndpoint)
require.NoError(t, err)
for i := 0; i < numberOfAttempts; i++ {
logs, err := getFilteredLogs(stateSyncedResult.Sig(), 0, uint64(50+i*epochSize), childEthEndpoint)
require.NoError(t, err)

if len(logs) == 2 || i == numberOfAttempts-1 {
Nemanja0x marked this conversation as resolved.
Show resolved Hide resolved
// assert that all deposits are executed successfully.
// All deposits are sent using a single transaction, so arbitrary message bridge emits two state sync events:
// MAP_TOKEN_SIG and DEPOSIT_BATCH_SIG state sync events
checkStateSyncResultLogs(t, logs, 2)

break
}

require.NoError(t, cluster.WaitForBlock(uint64(50+(i+1)*epochSize), 1*time.Minute))
}

txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(validatorSrv.JSONRPC()))
require.NoError(t, err)

// assert that all deposits are executed successfully.
// All deposits are sent using a single transaction, so arbitrary message bridge emits two state sync events:
// MAP_TOKEN_SIG and DEPOSIT_BATCH_SIG state sync events
checkStateSyncResultLogs(t, logs, 2)

// retrieve child token address (from both chains, and assert they are the same)
l1ChildTokenAddr := getChildToken(t, contractsapi.RootERC721Predicate.Abi, polybftCfg.Bridge.RootERC721PredicateAddr,
types.Address(rootERC721Addr), rootchainTxRelayer)
Expand Down Expand Up @@ -608,9 +617,10 @@ func TestE2E_Bridge_ERC721Transfer(t *testing.T) {

func TestE2E_Bridge_ERC1155Transfer(t *testing.T) {
const (
transfersCount = 5
amount = 100
epochSize = 5
transfersCount = 5
amount = 100
epochSize = 5
numberOfAttempts = 4
)

minter, err := ethgow.GenerateKey()
Expand Down Expand Up @@ -690,17 +700,25 @@ func TestE2E_Bridge_ERC1155Transfer(t *testing.T) {
// the transactions are processed and there should be a success events
var stateSyncedResult contractsapi.StateSyncResultEvent

logs, err := getFilteredLogs(stateSyncedResult.Sig(), 0, 50, childEthEndpoint)
require.NoError(t, err)
for i := 0; i < numberOfAttempts; i++ {
logs, err := getFilteredLogs(stateSyncedResult.Sig(), 0, uint64(50+i*epochSize), childEthEndpoint)
require.NoError(t, err)

if len(logs) == 2 || i == numberOfAttempts-1 {
Nemanja0x marked this conversation as resolved.
Show resolved Hide resolved
// assert that all deposits are executed successfully.
// All deposits are sent using a single transaction, so arbitrary message bridge emits two state sync events:
// MAP_TOKEN_SIG and DEPOSIT_BATCH_SIG state sync events
checkStateSyncResultLogs(t, logs, 2)

break
}

require.NoError(t, cluster.WaitForBlock(uint64(50+(i+1)*epochSize), 1*time.Minute))
}

txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(validatorSrv.JSONRPC()))
require.NoError(t, err)

// assert that all deposits are executed successfully.
// All deposits are sent using a single transaction, so arbitrary message bridge emits two state sync events:
// MAP_TOKEN_SIG and DEPOSIT_BATCH_SIG state sync events
checkStateSyncResultLogs(t, logs, 2)

// retrieve child token address
l1ChildTokenAddr := getChildToken(t, contractsapi.RootERC1155Predicate.Abi, polybftCfg.Bridge.RootERC1155PredicateAddr,
types.Address(rootERC1155Addr), rootchainTxRelayer)
Expand Down Expand Up @@ -797,8 +815,9 @@ func TestE2E_Bridge_ChildChainMintableTokensTransfer(t *testing.T) {
transfersCount = uint64(4)
amount = 100
// make epoch size long enough, so that all exit events are processed within the same epoch
epochSize = 30
sprintSize = uint64(5)
epochSize = 30
sprintSize = uint64(5)
numberOfAttempts = 4
)

// init private keys and amounts
Expand Down Expand Up @@ -959,18 +978,30 @@ func TestE2E_Bridge_ChildChainMintableTokensTransfer(t *testing.T) {
require.NoError(t, err)
}

blockNum, err := childEthEndpoint.BlockNumber()
require.NoError(t, err)
allSuccessful := false

for it := 0; it < numberOfAttempts && !allSuccessful; it++ {
blockNum, err := childEthEndpoint.BlockNumber()
require.NoError(t, err)

// wait a couple of sprints to finalize state sync events
require.NoError(t, cluster.WaitForBlock(blockNum+3*sprintSize, 2*time.Minute))
// wait a couple of sprints to finalize state sync events
require.NoError(t, cluster.WaitForBlock(blockNum+3*sprintSize, 2*time.Minute))

// check that balances on the child chain are correct
for i, receiver := range depositors {
balance := erc20BalanceOf(t, receiver, contracts.NativeERC20TokenContract, childchainTxRelayer)
t.Log("Balance before", balancesBefore[i], "Balance after", balance)
require.Equal(t, balance, balancesBefore[i].Add(balancesBefore[i], big.NewInt(amount)))
allSuccessful = true

// check that balances on the child chain are correct
for i, receiver := range depositors {
balance := erc20BalanceOf(t, receiver, contracts.NativeERC20TokenContract, childchainTxRelayer)
t.Log("Balance before", balancesBefore[i], "Balance after", balance)
if balance.Cmp(balancesBefore[i].Add(balancesBefore[i], big.NewInt(amount))) != 0 {
Nemanja0x marked this conversation as resolved.
Show resolved Hide resolved
allSuccessful = false

break
}
}
}

require.True(t, allSuccessful)
})

t.Run("bridge ERC 721 tokens", func(t *testing.T) {
Expand Down
Loading