Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 committed Aug 27, 2024
1 parent 37eab4d commit cd174bb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 63 deletions.
2 changes: 1 addition & 1 deletion db/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestL1GlobalExitRoot(t *testing.T) {
require.Equal(t, ger.BlockID, l1GER.BlockID)
require.Equal(t, ger.GlobalExitRoot, l1GER.GlobalExitRoot)

latestGER, err := pg.GetLatestExitRoot(ctx, 1, tx)
latestGER, err := pg.GetLatestExitRoot(ctx, 0, tx)
require.NoError(t, err)
require.Equal(t, latestGER.GlobalExitRoot, l1GER.GlobalExitRoot)
require.Equal(t, latestGER.BlockNumber, l1GER.BlockNumber)
Expand Down
93 changes: 39 additions & 54 deletions test/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ import (

"github.com/0xPolygonHermez/zkevm-bridge-service/bridgectrl"
"github.com/0xPolygonHermez/zkevm-bridge-service/db"
"github.com/0xPolygonHermez/zkevm-bridge-service/log"
"github.com/0xPolygonHermez/zkevm-bridge-service/server"
"github.com/0xPolygonHermez/zkevm-bridge-service/test/operations"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

var (
l1BridgeAddr = common.HexToAddress("0xFe12ABaa190Ef0c8638Ee0ba9F828BF41368Ca0E")
l2BridgeAddr = common.HexToAddress("0xFe12ABaa190Ef0c8638Ee0ba9F828BF41368Ca0E")
)

// TestE2E tests the flow of deposit and withdraw funds using the vector
func TestE2E(t *testing.T) {
if testing.Short() {
Expand Down Expand Up @@ -78,17 +74,16 @@ func TestE2E(t *testing.T) {
tokenAddr := common.Address{} // This means is eth
destAddr := common.HexToAddress("0xc949254d682d8c9ad5682521675b8f43b102aec4")

l1Balance, err := opsman.CheckAccountBalance(ctx, operations.L1, &l1BridgeAddr)
// Check L2 funds
balance, err := opsman.CheckAccountBalance(ctx, operations.L2, &destAddr)
require.NoError(t, err)
t.Logf("L1 Bridge Balance: %v", l1Balance)
initL2Balance := big.NewInt(0)
require.Equal(t, 0, balance.Cmp(initL2Balance))
err = opsman.SendL1Deposit(ctx, tokenAddr, amount, destNetwork, &destAddr)
require.NoError(t, err)
l1Balance, err = opsman.CheckAccountBalance(ctx, operations.L1, &l1BridgeAddr)
require.NoError(t, err)
t.Logf("L1 Bridge Balance: %v", l1Balance)

// Check globalExitRoot
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx, uint(destNetwork))
require.NoError(t, err)
t.Logf("Before deposit global exit root: %v", globalExitRootSMC)
t.Logf("After deposit global exit root: %v", globalExitRoot2)
Expand All @@ -97,11 +92,6 @@ func TestE2E(t *testing.T) {
// Get Bridge Info By DestAddr
deposits, err := opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
require.NoError(t, err)
// Check L2 funds
balance, err := opsman.CheckAccountBalance(ctx, operations.L2, &destAddr)
require.NoError(t, err)
initL2Balance := big.NewInt(0)
require.Equal(t, 0, balance.Cmp(initL2Balance))
t.Log("Deposit: ", deposits[0])
// Check the claim tx
err = opsman.CheckClaim(ctx, deposits[0])
Expand All @@ -118,14 +108,8 @@ func TestE2E(t *testing.T) {
// Send L2 Deposit to withdraw the some funds
destNetwork = 0
amount = new(big.Int).SetUint64(1000000000000000000)
l2Balance, err := opsman.CheckAccountBalance(ctx, operations.L2, &l2BridgeAddr)
require.NoError(t, err)
t.Logf("L2 Bridge Balance: %v", l2Balance)
err = opsman.SendL2Deposit(ctx, tokenAddr, amount, destNetwork, &destAddr, operations.L2)
require.NoError(t, err)
l2Balance, err = opsman.CheckAccountBalance(ctx, operations.L2, &l2BridgeAddr)
require.NoError(t, err)
t.Logf("L2 Bridge Balance: %v", l2Balance)

// Get Bridge Info By DestAddr
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
Expand All @@ -142,6 +126,7 @@ func TestE2E(t *testing.T) {
balance, err = opsman.CheckAccountBalance(ctx, operations.L1, &destAddr)
require.NoError(t, err)
require.Equal(t, 0, big.NewInt(0).Cmp(balance))
time.Sleep(6 * time.Second)
// Get the claim data
smtProof, smtRollupProof, globaExitRoot, err := opsman.GetClaimData(ctx, uint(deposits[0].NetworkId), uint(deposits[0].DepositCnt))
require.NoError(t, err)
Expand All @@ -157,6 +142,7 @@ func TestE2E(t *testing.T) {
balance, err = opsman.CheckAccountBalance(ctx, operations.L2, &destAddr)
require.NoError(t, err)
require.True(t, big.NewInt(9000000000000000000).Cmp(balance) > 0)
log.Debug("L1-L2 eth bridge end")
})

t.Run("L1-L2 token bridge", func(t *testing.T) {
Expand Down Expand Up @@ -196,15 +182,12 @@ func TestE2E(t *testing.T) {
time.Sleep(3 * time.Second) // wait for sync token_wrapped event
tokenWrapped, err := opsman.GetTokenWrapped(ctx, 0, tokenAddr, false)
require.NoError(t, err)
balance2, err := opsman.CheckAccountTokenBalance(ctx, "l2", tokenWrapped.WrappedTokenAddress, &destAddr)
require.NoError(t, err)
t.Log("Init account balance l2: ", balance2)

// Second deposit
err = opsman.SendL1Deposit(ctx, tokenAddr, amount1, destNetwork, &destAddr)
require.NoError(t, err)
// Check globalExitRoot
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx, uint(destNetwork))
require.NoError(t, err)
t.Logf("Before deposits global exit root: %v", globalExitRootSMC)
t.Logf("After deposits global exit root: %v", globalExitRoot2)
Expand All @@ -216,6 +199,7 @@ func TestE2E(t *testing.T) {
// L2 deposit
err = opsman.SendL2Deposit(ctx, tokenWrapped.WrappedTokenAddress, amount4, destNetwork, &origAddr, operations.L2)
require.NoError(t, err)
time.Sleep(6 * time.Second)
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &origAddr)
require.NoError(t, err)
t.Log("deposit: ", deposits[0])
Expand All @@ -240,6 +224,7 @@ func TestE2E(t *testing.T) {
require.NoError(t, err)
t.Log("Balance tokenWrapped: ", balance)
require.Equal(t, new(big.Int).SetUint64(1500000000000000000), balance)
log.Debug("L1-L2 token bridge end")
})

t.Run("Reversal ERC20", func(t *testing.T) {
Expand Down Expand Up @@ -280,6 +265,7 @@ func TestE2E(t *testing.T) {
require.Equal(t, 0, balance.Cmp(big.NewInt(9000000000000000000)))
t.Log("Deposit: ", deposits[0])
t.Log("Before getClaimData: ", deposits[0].NetworkId, deposits[0].DepositCnt)
time.Sleep(6 * time.Second)
// Get the claim data
smtProof, smtRollupProof, globaExitRoot, err := opsman.GetClaimData(ctx, uint(deposits[0].NetworkId), uint(deposits[0].DepositCnt))
require.NoError(t, err)
Expand All @@ -300,32 +286,28 @@ func TestE2E(t *testing.T) {
require.Equal(t, amount, balance2)

// Check globalExitRoot
globalExitRoot3, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot3, err := opsman.GetLatestGlobalExitRootFromL1(ctx) // TODO UY aqui no me cuadra que consulte al de trusted en vez de al normal. Seria GetLatestGlobalExitRootFromL1
require.NoError(t, err)
// Send L2 Deposit to withdraw the some funds
// Check L2 funds
balance, err = opsman.CheckAccountTokenBalance(ctx, operations.L2, tokenAddr, &destAddr)
require.NoError(t, err)
t.Log("balance: ", balance)
require.Equal(t, 0, big.NewInt(0).Cmp(balance))
// Send L1 Deposit to withdraw the some funds
destNetwork = 1
amount = new(big.Int).SetUint64(600000000000000000)
err = opsman.SendL1Deposit(ctx, tokenWrapped.WrappedTokenAddress, amount, destNetwork, &destAddr)
require.NoError(t, err)
// Get Bridge Info By DestAddr
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
// Check the claim tx
err = opsman.CheckClaim(ctx, deposits[0])
require.NoError(t, err)
t.Log("Deposit 2: ", deposits[0])
time.Sleep(6 * time.Second)
// Check globalExitRoot
globalExitRoot4, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot4, err := opsman.GetTrustedGlobalExitRootSynced(ctx, uint(destNetwork))
require.NoError(t, err)
t.Logf("Global3 %+v: ", globalExitRoot3)
t.Logf("Global4 %+v: ", globalExitRoot4)
require.NotEqual(t, globalExitRoot3.ExitRoots[0], globalExitRoot4.ExitRoots[0])
// Check L2 funds
balance, err = opsman.CheckAccountTokenBalance(ctx, operations.L2, tokenAddr, &destAddr)
require.NoError(t, err)
t.Log("balance: ", balance)
require.Equal(t, 0, big.NewInt(0).Cmp(balance))
t.Log("deposits[0]: ", deposits[0])
// Check the claim tx
err = opsman.CheckClaim(ctx, deposits[0])
require.NoError(t, err)
// Check L2 funds to see if the amount has been increased
balance, err = opsman.CheckAccountTokenBalance(ctx, operations.L2, tokenAddr, &destAddr)
require.NoError(t, err)
Expand All @@ -334,6 +316,7 @@ func TestE2E(t *testing.T) {
balance, err = opsman.CheckAccountTokenBalance(ctx, operations.L1, tokenWrapped.WrappedTokenAddress, &destAddr)
require.NoError(t, err)
require.Equal(t, 0, big.NewInt(400000000000000000).Cmp(balance))
log.Debug("Reversal ERC20 end")
})

t.Run("ERC20", func(t *testing.T) {
Expand All @@ -356,7 +339,7 @@ func TestE2E(t *testing.T) {
err = opsman.SendL1Deposit(ctx, tokenAddr, amount, destNetwork, &destAddr)
require.NoError(t, err)
// Check globalExitRoot
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx, uint(destNetwork))
require.NoError(t, err)
t.Logf("Before deposit global exit root: %v", globalExitRootSMC)
t.Logf("After deposit global exit root: %v", globalExitRoot2)
Expand Down Expand Up @@ -388,6 +371,7 @@ func TestE2E(t *testing.T) {
amount = new(big.Int).SetUint64(8000000000000000000)
err = opsman.SendL2Deposit(ctx, tokenWrapped.WrappedTokenAddress, amount, destNetwork, &destAddr, operations.L2)
require.NoError(t, err)
time.Sleep(6 * time.Second)
// Get Bridge Info By DestAddr
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
require.NoError(t, err)
Expand Down Expand Up @@ -417,13 +401,14 @@ func TestE2E(t *testing.T) {
balance, err = opsman.CheckAccountTokenBalance(ctx, operations.L2, tokenWrapped.WrappedTokenAddress, &destAddr)
require.NoError(t, err)
require.Equal(t, big.NewInt(2000000000000000000), balance)
log.Debug("ERC20 end")
})

t.Run("Multi deposits", func(t *testing.T) {
/*
1. Do 3 deposits/bridges
2. Do 2 more deposits
3. Claim the first 3 deposits in L2
3. Check the first 3 deposits in L2
*/

// Check initial globalExitRoot.
Expand Down Expand Up @@ -455,7 +440,7 @@ func TestE2E(t *testing.T) {
err = opsman.SendL1Deposit(ctx, tokenAddr, amount3, destNetwork, &destAddr)
require.NoError(t, err)
// Check globalExitRoot
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx)
globalExitRoot2, err := opsman.GetTrustedGlobalExitRootSynced(ctx, uint(destNetwork))
require.NoError(t, err)
t.Logf("Before deposits global exit root: %v", globalExitRootSMC)
t.Logf("After deposits global exit root: %v", globalExitRoot2)
Expand All @@ -465,7 +450,6 @@ func TestE2E(t *testing.T) {
deposits, err := opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
require.NoError(t, err)
t.Log("Deposits: ", deposits[:2])
t.Log("Before getClaimData: ", deposits[2].NetworkId, deposits[2].DepositCnt)
// Fourth deposit
err = opsman.SendL1Deposit(ctx, tokenAddr, amount1, destNetwork, &origAddr)
require.NoError(t, err)
Expand All @@ -475,6 +459,10 @@ func TestE2E(t *testing.T) {
// Check the claim tx
err = opsman.CheckClaim(ctx, deposits[0])
require.NoError(t, err)
err = opsman.CheckClaim(ctx, deposits[1])
require.NoError(t, err)
err = opsman.CheckClaim(ctx, deposits[2])
require.NoError(t, err)
time.Sleep(3 * time.Second) // wait for sync token_wrapped event
tokenWrapped, err := opsman.GetTokenWrapped(ctx, 0, tokenAddr, false)
require.NoError(t, err)
Expand All @@ -483,15 +471,8 @@ func TestE2E(t *testing.T) {
balance, err = opsman.CheckAccountTokenBalance(ctx, "l2", tokenWrapped.WrappedTokenAddress, &destAddr)
require.NoError(t, err)
t.Log("Balance tokenWrapped: ", balance)

// Check the claim tx
err = opsman.CheckClaim(ctx, deposits[0])
require.NoError(t, err)
// Check L2 funds to see if the amount has been increased
balance, err = opsman.CheckAccountTokenBalance(ctx, "l2", tokenWrapped.WrappedTokenAddress, &destAddr)
require.NoError(t, err)
t.Log("Balance tokenWrapped: ", balance)
require.Equal(t, new(big.Int).SetUint64(6000000000000000000), balance)
log.Debug("Multi deposits end")
})

t.Run("Bridge Message Test", func(t *testing.T) {
Expand All @@ -505,6 +486,7 @@ func TestE2E(t *testing.T) {

err = opsman.SendL1BridgeMessage(ctx, destAddr, destNetwork, amount, []byte("metadata 1"), nil)
require.NoError(t, err)
time.Sleep(6 * time.Second)

// Get Bridge Info By DestAddr
deposits, err := opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
Expand All @@ -525,6 +507,7 @@ func TestE2E(t *testing.T) {

err = opsman.SendL2BridgeMessage(ctx, destAddr, destNetwork, amount, []byte("metadata 2"))
require.NoError(t, err)
time.Sleep(6 * time.Second)

// Get Bridge Info By DestAddr
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
Expand All @@ -536,6 +519,7 @@ func TestE2E(t *testing.T) {
t.Logf("globalExitRoot: %+v", globaExitRoot)
err = opsman.SendL1Claim(ctx, deposits[0], smtProof, smtRollupProof, globaExitRoot)
require.NoError(t, err)
log.Debug("Bridge Message Test end")
})
t.Run("Bridge Message Authorized Account Test", func(t *testing.T) {
// Test L1 Bridge Message
Expand Down Expand Up @@ -568,7 +552,7 @@ func TestE2E(t *testing.T) {

err = opsman.SendL2BridgeMessage(ctx, destAddr, destNetwork, amount, []byte("metadata 4"))
require.NoError(t, err)

time.Sleep(6 * time.Second)
// Get Bridge Info By DestAddr
deposits, err = opsman.GetBridgeInfoByDestAddr(ctx, &destAddr)
require.NoError(t, err)
Expand All @@ -579,5 +563,6 @@ func TestE2E(t *testing.T) {
t.Logf("globalExitRoot: %+v", globaExitRoot)
err = opsman.SendL1Claim(ctx, deposits[0], smtProof, smtRollupProof, globaExitRoot)
require.NoError(t, err)
log.Debug("Bridge Message Authorized Account Test end")
})
}
14 changes: 6 additions & 8 deletions test/operations/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ func (m *Manager) GetNumberClaims(ctx context.Context, destAddr string) (int, er
}

// SendL1Deposit sends a deposit from l1 to l2.
func (m *Manager) SendL1Deposit(ctx context.Context, tokenAddr common.Address, amount *big.Int,
destNetwork uint32, destAddr *common.Address,
) error {
func (m *Manager) SendL1Deposit(ctx context.Context, tokenAddr common.Address, amount *big.Int, destNetwork uint32, destAddr *common.Address) error {
client := m.clients[L1]
auth, err := client.GetSigner(ctx, accHexPrivateKeys[L1])
if err != nil {
Expand All @@ -238,7 +236,7 @@ func (m *Manager) SendL1Deposit(ctx context.Context, tokenAddr common.Address, a
}

// sync for new exit root
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, 0)
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(destNetwork))
}

// SendMultipleL1Deposit sends a deposit from l1 to l2.
Expand Down Expand Up @@ -288,7 +286,7 @@ func (m *Manager) SendL2Deposit(ctx context.Context, tokenAddr common.Address, a
}

// sync for new exit root
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(networkID))
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(destNetwork))
}

// SendL1BridgeMessage bridges a message from l1 to l2.
Expand Down Expand Up @@ -317,7 +315,7 @@ func (m *Manager) SendL1BridgeMessage(ctx context.Context, destAddr common.Addre
}

// sync for new exit root
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, 0)
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(destNetwork))
}

// SendL2BridgeMessage bridges a message from l2 to l1.
Expand Down Expand Up @@ -346,7 +344,7 @@ func (m *Manager) SendL2BridgeMessage(ctx context.Context, destAddr common.Addre
}

// sync for new exit root
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(networkID))
return m.WaitExitRootToBeSynced(ctx, orgExitRoot, uint(destNetwork))
}

// Setup creates all the required components and initializes them according to
Expand Down Expand Up @@ -805,7 +803,7 @@ func (m *Manager) WaitExitRootToBeSynced(ctx context.Context, orgExitRoot *ether
return false, err
}
tID := 0
if networkID != 0 {
if networkID == 0 {
tID = 1
}
return exitRoot.ExitRoots[tID] != orgExitRoot.ExitRoots[tID], nil
Expand Down

0 comments on commit cd174bb

Please sign in to comment.