Skip to content

Commit

Permalink
Fix bridge e2e tests for transferring tokens (0xPolygon#1953)
Browse files Browse the repository at this point in the history
Fix e2e tests for transferring ERC1155 and ERC721.
  • Loading branch information
Nemanja0x authored Oct 3, 2023
1 parent 6544e22 commit 4d4d699
Showing 1 changed file with 74 additions and 39 deletions.
113 changes: 74 additions & 39 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func TestE2E_Bridge_Transfers(t *testing.T) {
amount = 100
numBlockConfirmations = 2
// make epoch size long enough, so that all exit events are processed within the same epoch
epochSize = 30
sprintSize = uint64(5)
numberOfAttempts = 5
epochSize = 30
sprintSize = uint64(5)
numberOfAttempts = 5
stateSyncedLogsCount = 1
)

receiversAddrs := make([]types.Address, transfersCount)
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestE2E_Bridge_Transfers(t *testing.T) {
require.NoError(t, err)

// assert that all deposits are executed successfully
checkStateSyncResultLogs(t, logs, 1)
checkStateSyncResultLogs(t, logs, stateSyncedLogsCount)

// check validator balance got increased by deposited amount
balance, err := childEthEndpoint.GetBalance(ethgo.Address(senderAccount.Address()), ethgo.Latest)
Expand Down Expand Up @@ -457,8 +458,10 @@ 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
stateSyncedLogsCount = 2
)

minter, err := ethgow.GenerateKey()
Expand Down Expand Up @@ -535,17 +538,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) == stateSyncedLogsCount || i == numberOfAttempts-1 {
// 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, stateSyncedLogsCount)

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 +619,11 @@ 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
stateSyncedLogsCount = 2
)

minter, err := ethgow.GenerateKey()
Expand Down Expand Up @@ -690,17 +703,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) == stateSyncedLogsCount || i == numberOfAttempts-1 {
// 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, stateSyncedLogsCount)

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 +818,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 +981,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("Attempt", it+1, "Balance before", balancesBefore[i], "Balance after", balance)
if balance.Cmp(balancesBefore[i].Add(balancesBefore[i], big.NewInt(amount))) != 0 {
allSuccessful = false

break
}
}
}

require.True(t, allSuccessful)
})

t.Run("bridge ERC 721 tokens", func(t *testing.T) {
Expand Down Expand Up @@ -1275,8 +1309,9 @@ func TestE2E_Bridge_Transfers_AccessLists(t *testing.T) {
depositAmount = ethgo.Ether(5)
withdrawAmount = ethgo.Ether(1)
// 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)
stateSyncedLogsCount = 1
)

receivers := make([]string, transfersCount)
Expand Down Expand Up @@ -1343,7 +1378,7 @@ func TestE2E_Bridge_Transfers_AccessLists(t *testing.T) {
require.NoError(t, err)

// assert that all deposits are executed successfully
checkStateSyncResultLogs(t, logs, 1)
checkStateSyncResultLogs(t, logs, stateSyncedLogsCount)

// check admin balance got increased by deposited amount
balance, err := childEthEndpoint.GetBalance(ethgo.Address(adminAddr), ethgo.Latest)
Expand Down Expand Up @@ -1488,12 +1523,12 @@ func TestE2E_Bridge_Transfers_AccessLists(t *testing.T) {

func TestE2E_Bridge_Transfers_WithBlockTrackerPollInterval(t *testing.T) {
var (
// amount = ethgo.Gwei(10)
numBlockConfirmations = uint64(2)
epochSize = 30
sprintSize = uint64(5)
rootPollInterval = 5 * time.Second
relayerPollInterval = 5 * time.Second
stateSyncedLogsCount = 1
)

cluster := framework.NewTestCluster(t, 5,
Expand Down Expand Up @@ -1542,7 +1577,7 @@ func TestE2E_Bridge_Transfers_WithBlockTrackerPollInterval(t *testing.T) {
require.NoError(t, err)

// assert that all deposits are executed successfully
checkStateSyncResultLogs(t, logs, 1)
checkStateSyncResultLogs(t, logs, stateSyncedLogsCount)

// check validator balance got increased by deposited amount
balance, err := childEthEndpoint.GetBalance(ethgo.Address(senderAccount.Address()), ethgo.Latest)
Expand Down

0 comments on commit 4d4d699

Please sign in to comment.