From 4c3fa352bc35fa1f63ec8f2d91d7f0dfc8a509c7 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 18:38:35 +0100 Subject: [PATCH 1/9] feat: Setting up a single function for initing simulated backend with all required contracts --- aggoracle/e2e_test.go | 4 +- bridgesync/e2e_test.go | 33 +--- claimsponsor/e2e_test.go | 3 +- .../datacommittee/datacommittee_test.go | 80 ++------ l1infotreesync/e2e_test.go | 2 + lastgersync/e2e_test.go | 4 +- reorgdetector/reorgdetector_test.go | 35 +--- .../aggoracle_e2e.go | 179 ++++-------------- test/helpers/simulated.go | 50 +++++ 9 files changed, 134 insertions(+), 256 deletions(-) rename test/{helpers => aggoraclehelpers}/aggoracle_e2e.go (66%) diff --git a/aggoracle/e2e_test.go b/aggoracle/e2e_test.go index 25a8a96d..b1506032 100644 --- a/aggoracle/e2e_test.go +++ b/aggoracle/e2e_test.go @@ -8,7 +8,7 @@ import ( gerContractL1 "github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/globalexitrootnopush0" "github.com/0xPolygon/cdk/aggoracle" - "github.com/0xPolygon/cdk/test/helpers" + "github.com/0xPolygon/cdk/test/aggoraclehelpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient/simulated" @@ -16,7 +16,7 @@ import ( ) func TestEVM(t *testing.T) { - env := helpers.SetupAggoracleWithEVMChain(t) + env := aggoraclehelpers.SetupAggoracleWithEVMChain(t) runTest(t, env.GERL1Contract, env.AggOracleSender, env.L1Client, env.AuthL1) } diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index a19afb8d..2e2ee322 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -16,45 +16,32 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" ) -func newSimulatedClient(t *testing.T, auth *bind.TransactOpts) ( - client *simulated.Backend, - bridgeAddr common.Address, - bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2, +func newSimulatedClient(t *testing.T) ( + *simulated.Backend, + *bind.TransactOpts, + common.Address, + *polygonzkevmbridgev2.Polygonzkevmbridgev2, ) { t.Helper() - var err error - balance, _ := big.NewInt(0).SetString("10000000000000000000000000", 10) - address := auth.From - genesisAlloc := map[common.Address]types.Account{ - address: { - Balance: balance, - }, - } - blockGasLimit := uint64(999999999999999999) - client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) - - bridgeAddr, _, bridgeContract, err = polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(auth, client.Client()) + client, userAuth, deployerAuth := helpers.SimulatedBackend(t, nil) + bridgeAddr, _, bridgeContract, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(deployerAuth, client.Client()) require.NoError(t, err) client.Commit() - return + return client, userAuth, bridgeAddr, bridgeContract } func TestBridgeEventE2E(t *testing.T) { ctx := context.Background() dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared") dbPathReorg := t.TempDir() - privateKey, err := crypto.GenerateKey() - require.NoError(t, err) - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) - require.NoError(t, err) - client, bridgeAddr, bridgeSc := newSimulatedClient(t, auth) + + client, auth, bridgeAddr, bridgeSc := newSimulatedClient(t) rd, err := reorgdetector.New(client.Client(), reorgdetector.Config{DBPath: dbPathReorg}) require.NoError(t, err) diff --git a/claimsponsor/e2e_test.go b/claimsponsor/e2e_test.go index 8a037a58..b4fce499 100644 --- a/claimsponsor/e2e_test.go +++ b/claimsponsor/e2e_test.go @@ -13,6 +13,7 @@ import ( "github.com/0xPolygon/cdk/claimsponsor" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/test/aggoraclehelpers" "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -22,7 +23,7 @@ import ( func TestE2EL1toEVML2(t *testing.T) { // start other needed components ctx := context.Background() - env := helpers.SetupAggoracleWithEVMChain(t) + env := aggoraclehelpers.SetupAggoracleWithEVMChain(t) dbPathBridgeSyncL1 := path.Join(t.TempDir(), "file::memory:?cache=shared") testClient := helpers.TestClient{ClientRenamed: env.L1Client.Client()} bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetector, testClient, 0, time.Millisecond*10, 0, 0) diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index fcacef3c..c095540c 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -5,13 +5,14 @@ import ( "math/big" "testing" + "github.com/0xPolygon/cdk/test/helpers" + smcparis "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana-paris/polygondatacommittee" "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygondatacommittee" "github.com/0xPolygon/cdk/log" erc1967proxy "github.com/0xPolygon/cdk/test/contracts/erc1967proxy" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/assert" @@ -20,7 +21,7 @@ import ( func TestUpdateDataCommitteeEvent(t *testing.T) { // Set up testing environment - dac, ethBackend, auth, da := newTestingEnv(t) + dac, ethBackend, da, auth := newSimulatedDacman(t) // Update the committee requiredAmountOfSignatures := big.NewInt(2) @@ -63,82 +64,39 @@ func init() { }) } -// This function prepare the blockchain, the wallet with funds and deploy the smc -func newTestingEnv(t *testing.T) ( - dac *Backend, - ethBackend *simulated.Backend, - auth *bind.TransactOpts, - da *polygondatacommittee.Polygondatacommittee, -) { - t.Helper() - privateKey, err := crypto.GenerateKey() - if err != nil { - log.Fatal(err) - } - auth, err = bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) - if err != nil { - log.Fatal(err) - } - dac, ethBackend, da, err = newSimulatedDacman(t, auth) - if err != nil { - log.Fatal(err) - } - - return dac, ethBackend, auth, da -} - // NewSimulatedEtherman creates an etherman that uses a simulated blockchain. It's important to notice that the ChainID of the auth // must be 1337. The address that holds the auth will have an initial balance of 10 ETH -func newSimulatedDacman(t *testing.T, auth *bind.TransactOpts) ( +func newSimulatedDacman(t *testing.T) ( dacman *Backend, ethBackend *simulated.Backend, da *polygondatacommittee.Polygondatacommittee, - err error, + auth *bind.TransactOpts, ) { t.Helper() - if auth == nil { - // read only client - return &Backend{}, nil, nil, nil - } - // 10000000 ETH in wei - balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) - address := auth.From - genesisAlloc := map[common.Address]types.Account{ - address: { - Balance: balance, - }, - } - blockGasLimit := uint64(999999999999999999) - client := simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) + + ethBackend, auth, _ = helpers.SimulatedBackend(t, nil) // DAC Setup - addr, _, _, err := smcparis.DeployPolygondatacommittee(auth, client.Client()) - if err != nil { - return &Backend{}, nil, nil, err - } - client.Commit() - proxyAddr, err := deployDACProxy(auth, client.Client(), addr) - if err != nil { - return &Backend{}, nil, nil, err - } + addr, _, _, err := smcparis.DeployPolygondatacommittee(auth, ethBackend.Client()) + require.NoError(t, err) + ethBackend.Commit() - client.Commit() - da, err = polygondatacommittee.NewPolygondatacommittee(proxyAddr, client.Client()) - if err != nil { - return &Backend{}, nil, nil, err - } + proxyAddr, err := deployDACProxy(auth, ethBackend.Client(), addr) + require.NoError(t, err) + ethBackend.Commit() + + da, err = polygondatacommittee.NewPolygondatacommittee(proxyAddr, ethBackend.Client()) + require.NoError(t, err) _, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{}) - if err != nil { - return &Backend{}, nil, nil, err - } - client.Commit() + require.NoError(t, err) + ethBackend.Commit() c := &Backend{ dataCommitteeContract: da, } - return c, client, da, nil + return c, ethBackend, da, auth } func deployDACProxy(auth *bind.TransactOpts, client bind.ContractBackend, dacImpl common.Address) (common.Address, error) { diff --git a/l1infotreesync/e2e_test.go b/l1infotreesync/e2e_test.go index c522c73a..cd4169d0 100644 --- a/l1infotreesync/e2e_test.go +++ b/l1infotreesync/e2e_test.go @@ -273,6 +273,8 @@ func TestWithReorgs(t *testing.T) { } func TestStressAndReorgs(t *testing.T) { + t.Skip() + const ( totalIterations = 3 blocksInIteration = 140 diff --git a/lastgersync/e2e_test.go b/lastgersync/e2e_test.go index 979d55a2..e4d5e407 100644 --- a/lastgersync/e2e_test.go +++ b/lastgersync/e2e_test.go @@ -9,7 +9,7 @@ import ( "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/lastgersync" - "github.com/0xPolygon/cdk/test/helpers" + "github.com/0xPolygon/cdk/test/aggoraclehelpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" @@ -17,7 +17,7 @@ import ( func TestE2E(t *testing.T) { ctx := context.Background() - env := helpers.SetupAggoracleWithEVMChain(t) + env := aggoraclehelpers.SetupAggoracleWithEVMChain(t) dbPathSyncer := t.TempDir() syncer, err := lastgersync.New( ctx, diff --git a/reorgdetector/reorgdetector_test.go b/reorgdetector/reorgdetector_test.go index 7efe0892..45c15e70 100644 --- a/reorgdetector/reorgdetector_test.go +++ b/reorgdetector/reorgdetector_test.go @@ -2,47 +2,22 @@ package reorgdetector import ( "context" - big "math/big" "testing" "time" + "github.com/0xPolygon/cdk/test/helpers" + cdktypes "github.com/0xPolygon/cdk/config/types" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" ) -func newSimulatedL1(t *testing.T, auth *bind.TransactOpts) *simulated.Backend { - t.Helper() - - balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) - - blockGasLimit := uint64(999999999999999999) - client := simulated.NewBackend(map[common.Address]types.Account{ - auth.From: { - Balance: balance, - }, - }, simulated.WithBlockGasLimit(blockGasLimit)) - client.Commit() - - return client -} - func Test_ReorgDetector(t *testing.T) { const subID = "test" ctx := context.Background() // Simulated L1 - privateKeyL1, err := crypto.GenerateKey() - require.NoError(t, err) - authL1, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(1337)) - require.NoError(t, err) - clientL1 := newSimulatedL1(t, authL1) - require.NoError(t, err) + clientL1, _, _ := helpers.SimulatedBackend(t, nil) // Create test DB dir testDir := t.TempDir() @@ -92,6 +67,6 @@ func Test_ReorgDetector(t *testing.T) { headersList, ok := reorgDetector.trackedBlocks[subID] reorgDetector.trackedBlocksLock.Unlock() require.True(t, ok) - require.Equal(t, 1, headersList.len()) // Only block 2 left - require.Equal(t, remainingHeader.Hash(), headersList.get(2).Hash) + require.Equal(t, 1, headersList.len()) // Only block 1 left + require.Equal(t, remainingHeader.Hash(), headersList.get(1).Hash) } diff --git a/test/helpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go similarity index 66% rename from test/helpers/aggoracle_e2e.go rename to test/aggoraclehelpers/aggoracle_e2e.go index a19cfd42..f9c57f0a 100644 --- a/test/helpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -1,9 +1,7 @@ -package helpers +package aggoraclehelpers import ( "context" - "errors" - "fmt" "math/big" "path" "testing" @@ -19,6 +17,7 @@ import ( "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/reorgdetector" "github.com/0xPolygon/cdk/test/contracts/transparentupgradableproxy" + "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -55,7 +54,7 @@ type AggoracleWithEVMChainEnv struct { BridgeL2Contract *polygonzkevmbridgev2.Polygonzkevmbridgev2 BridgeL2Addr common.Address NetworkIDL2 uint32 - EthTxManMockL2 *EthTxManagerMock + EthTxManMockL2 *helpers.EthTxManagerMock } func SetupAggoracleWithEVMChain(t *testing.T) *AggoracleWithEVMChainEnv { @@ -112,8 +111,7 @@ func CommonSetup(t *testing.T) ( require.NoError(t, err) authL1, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) require.NoError(t, err) - l1Client, gerL1Addr, gerL1Contract, bridgeL1Addr, bridgeL1Contract, err := newSimulatedL1(authL1) - require.NoError(t, err) + l1Client, gerL1Addr, gerL1Contract, bridgeL1Addr, bridgeL1Contract := newSimulatedL1(t, authL1) // Reorg detector dbPathReorgDetector := t.TempDir() reorg, err := reorgdetector.New(l1Client.Client(), reorgdetector.Config{DBPath: dbPathReorgDetector}) @@ -139,7 +137,7 @@ func EVMSetup(t *testing.T) ( *polygonzkevmbridgev2.Polygonzkevmbridgev2, common.Address, *bind.TransactOpts, - *EthTxManagerMock, + *helpers.EthTxManagerMock, ) { t.Helper() @@ -147,9 +145,8 @@ func EVMSetup(t *testing.T) ( require.NoError(t, err) authL2, err := bind.NewKeyedTransactorWithChainID(privateKeyL2, big.NewInt(chainID)) require.NoError(t, err) - l2Client, gerL2Addr, gerL2Sc, bridgeL2Addr, bridgeL2Sc, err := newSimulatedEVMAggSovereignChain(authL2) - require.NoError(t, err) - ethTxManMock := NewEthTxManMock(t, l2Client, authL2) + l2Client, gerL2Addr, gerL2Sc, bridgeL2Addr, bridgeL2Sc := newSimulatedEVMAggSovereignChain(t, authL2) + ethTxManMock := helpers.NewEthTxManMock(t, l2Client, authL2) sender, err := chaingersender.NewEVMChainGERSender(log.GetDefaultLogger(), gerL2Addr, authL2.From, l2Client.Client(), ethTxManMock, 0, time.Millisecond*50) //nolint:mnd require.NoError(t, err) @@ -157,25 +154,22 @@ func EVMSetup(t *testing.T) ( return sender, l2Client, gerL2Sc, gerL2Addr, bridgeL2Sc, bridgeL2Addr, authL2, ethTxManMock } -func newSimulatedL1(auth *bind.TransactOpts) ( +func newSimulatedL1(t *testing.T, auth *bind.TransactOpts) ( client *simulated.Backend, gerAddr common.Address, gerContract *gerContractL1.Globalexitrootnopush0, bridgeAddr common.Address, bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2, - err error, ) { + t.Helper() + ctx := context.Background() privateKeyL1, err := crypto.GenerateKey() - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to generate private key: %w", err) - } + require.NoError(t, err) authDeployer, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to create transactor: %w", err) - } + require.NoError(t, err) balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd address := auth.From @@ -191,26 +185,15 @@ func newSimulatedL1(auth *bind.TransactOpts) ( client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to deploy bridge implementation: %w", err) - } + require.NoError(t, err) client.Commit() nonce, err := client.Client().PendingNonceAt(ctx, authDeployer.From) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get pending nonce: %w", err) - } + require.NoError(t, err) precalculatedAddr := crypto.CreateAddress(authDeployer.From, nonce+1) bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get bridge ABI: %w", err) - } - if bridgeABI == nil { - err = errors.New("GetABI returned nil") - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get bridge ABI: %w", err) - } + require.NoError(t, err) + require.NotNil(t, bridgeABI) dataCallProxy, err := bridgeABI.Pack("initialize", uint32(0), // networkIDMainnet @@ -220,11 +203,7 @@ func newSimulatedL1(auth *bind.TransactOpts) ( common.Address{}, []byte{}, // gasTokenMetadata ) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to pack data for proxy initialization: %w", err) - } + require.NoError(t, err) bridgeAddr, _, _, err = transparentupgradableproxy.DeployTransparentupgradableproxy( authDeployer, @@ -233,69 +212,40 @@ func newSimulatedL1(auth *bind.TransactOpts) ( authDeployer.From, dataCallProxy, ) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to deploy transparent upgradable proxy: %w", err) - } + require.NoError(t, err) client.Commit() bridgeContract, err = polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to create bridge contract instance: %w", err) - } + require.NoError(t, err) checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{Pending: false}) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to get Global Exit Root Manager: %w", err) - } - if precalculatedAddr != checkGERAddr { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf( - "error deploying bridge, unexpected GER addr. Expected %s. Actual %s", - precalculatedAddr.Hex(), checkGERAddr.Hex(), - ) - } + require.NoError(t, err) + require.Equal(t, precalculatedAddr, checkGERAddr) gerAddr, _, gerContract, err = gerContractL1.DeployGlobalexitrootnopush0(authDeployer, client.Client(), auth.From, bridgeAddr) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to deploy GER contract: %w", err) - } + require.NoError(t, err) client.Commit() - if precalculatedAddr != gerAddr { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf( - "error calculating GER address. Expected %s. Actual %s", - precalculatedAddr.Hex(), gerAddr.Hex(), - ) - } + require.Equal(t, precalculatedAddr, gerAddr) - return client, gerAddr, gerContract, bridgeAddr, bridgeContract, nil + return client, gerAddr, gerContract, bridgeAddr, bridgeContract } -func newSimulatedEVMAggSovereignChain(auth *bind.TransactOpts) ( +func newSimulatedEVMAggSovereignChain(t *testing.T, auth *bind.TransactOpts) ( client *simulated.Backend, gerAddr common.Address, gerContract *gerContractEVMChain.Pessimisticglobalexitrootnopush0, bridgeAddr common.Address, bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2, - err error, ) { ctx := context.Background() privateKeyL1, err := crypto.GenerateKey() - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to generate private key: %w", err) - } + require.NoError(t, err) authDeployer, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to create transactor: %w", err) - } + require.NoError(t, err) balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd address := auth.From @@ -317,27 +267,16 @@ func newSimulatedEVMAggSovereignChain(auth *bind.TransactOpts) ( client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to deploy bridge implementation: %w", err) - } + require.NoError(t, err) client.Commit() nonce, err := client.Client().PendingNonceAt(ctx, authDeployer.From) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get pending nonce: %w", err) - } + require.NoError(t, err) precalculatedAddr := crypto.CreateAddress(authDeployer.From, nonce+1) bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get bridge ABI: %w", err) - } - if bridgeABI == nil { - err = errors.New("GetABI returned nil") - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to get bridge ABI: %w", err) - } + require.NoError(t, err) + require.NotNil(t, bridgeABI) dataCallProxy, err := bridgeABI.Pack("initialize", NetworkIDL2, @@ -347,11 +286,7 @@ func newSimulatedEVMAggSovereignChain(auth *bind.TransactOpts) ( common.Address{}, []byte{}, // gasTokenMetadata ) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to pack data for proxy initialization: %w", err) - } + require.NoError(t, err) bridgeAddr, _, _, err = transparentupgradableproxy.DeployTransparentupgradableproxy( authDeployer, @@ -360,60 +295,30 @@ func newSimulatedEVMAggSovereignChain(auth *bind.TransactOpts) ( authDeployer.From, dataCallProxy, ) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to deploy transparent upgradable proxy: %w", err) - } - if bridgeAddr != precalculatedBridgeAddr { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf( - "error calculating bridge addr. Expected: %s. Actual: %s", - precalculatedBridgeAddr, bridgeAddr, - ) - } + require.NoError(t, err) + require.Equal(t, precalculatedBridgeAddr, bridgeAddr) client.Commit() bridgeContract, err = polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to create bridge contract instance: %w", err) - } + require.NoError(t, err) checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{}) - if err != nil { - return nil, common.Address{}, nil, - common.Address{}, nil, - fmt.Errorf("failed to get Global Exit Root Manager: %w", err) - } - if precalculatedAddr != checkGERAddr { - return nil, common.Address{}, nil, common.Address{}, nil, errors.New( - "error deploying bridge, unexpected GER Manager address", - ) - } + require.NoError(t, err) + require.Equal(t, precalculatedAddr, checkGERAddr) gerAddr, _, gerContract, err = gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( authDeployer, client.Client(), auth.From) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to deploy GER contract: %w", err) - } + require.NoError(t, err) client.Commit() globalExitRootSetterRole := common.HexToHash("0x7b95520991dfda409891be0afa2635b63540f92ee996fda0bf695a166e5c5176") _, err = gerContract.GrantRole(authDeployer, globalExitRootSetterRole, auth.From) - if err != nil { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to grant role to GER contract: %w", err) - } + require.NoError(t, err) client.Commit() hasRole, _ := gerContract.HasRole(&bind.CallOpts{Pending: false}, globalExitRootSetterRole, auth.From) - if !hasRole { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("failed to set role for GER contract") - } - - if precalculatedAddr != gerAddr { - return nil, common.Address{}, nil, common.Address{}, nil, fmt.Errorf("error calculating GER address") - } + require.True(t, hasRole) + require.Equal(t, precalculatedAddr, gerAddr) - return client, gerAddr, gerContract, bridgeAddr, bridgeContract, nil + return client, gerAddr, gerContract, bridgeAddr, bridgeContract } diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index eb4cab20..8f48503c 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -1,10 +1,25 @@ package helpers import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/require" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/ethereum/go-ethereum/rpc" ) +const ( + defaultBlockGasLimit = uint64(999999999999999999) + defaultBalance = "10000000000000000000000000" + chainID = 1337 +) + type ClientRenamed simulated.Client type TestClient struct { @@ -14,3 +29,38 @@ type TestClient struct { func (tc TestClient) Client() *rpc.Client { return nil } + +// SimulatedBackend creates a simulated backend with two accounts: user and deployer. +func SimulatedBackend( + t *testing.T, + balances map[common.Address]types.Account, +) (*simulated.Backend, *bind.TransactOpts, *bind.TransactOpts) { + t.Helper() + + // Define default balance + balance, ok := new(big.Int).SetString(defaultBalance, 10) + require.Truef(t, ok, "failed to set balance") + + // Create user + userPK, err := crypto.GenerateKey() + require.NoError(t, err) + userAuth, err := bind.NewKeyedTransactorWithChainID(userPK, big.NewInt(chainID)) + require.NoError(t, err) + + // Create deployer + deployerPK, err := crypto.GenerateKey() + require.NoError(t, err) + deployerAuth, err := bind.NewKeyedTransactorWithChainID(deployerPK, big.NewInt(chainID)) + require.NoError(t, err) + + // Define balances map + if balances == nil { + balances = make(map[common.Address]types.Account) + } + balances[userAuth.From] = types.Account{Balance: balance} + balances[deployerAuth.From] = types.Account{Balance: balance} + + client := simulated.NewBackend(balances, simulated.WithBlockGasLimit(defaultBlockGasLimit)) + + return client, userAuth, deployerAuth +} From dbf94270964798d8a0f7d1d76406000160a17081 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 18:53:37 +0100 Subject: [PATCH 2/9] feat: Setting up a single function for initing simulated backend with all required contracts --- l1infotreesync/e2e_test.go | 81 +++++++------------ test/aggoraclehelpers/aggoracle_e2e.go | 108 +++++++++---------------- 2 files changed, 67 insertions(+), 122 deletions(-) diff --git a/l1infotreesync/e2e_test.go b/l1infotreesync/e2e_test.go index cd4169d0..4526f7b2 100644 --- a/l1infotreesync/e2e_test.go +++ b/l1infotreesync/e2e_test.go @@ -2,7 +2,6 @@ package l1infotreesync_test import ( "context" - "errors" "fmt" "math/big" "path" @@ -16,6 +15,7 @@ import ( "github.com/0xPolygon/cdk/l1infotreesync" "github.com/0xPolygon/cdk/reorgdetector" "github.com/0xPolygon/cdk/test/contracts/verifybatchesmock" + "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -25,61 +25,44 @@ import ( "github.com/stretchr/testify/require" ) -func newSimulatedClient(auth *bind.TransactOpts) ( - client *simulated.Backend, - gerAddr common.Address, - verifyAddr common.Address, - gerContract *polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2, - verifyContract *verifybatchesmock.Verifybatchesmock, - err error, +func newSimulatedClient(t *testing.T) ( + *simulated.Backend, + *bind.TransactOpts, + common.Address, + common.Address, + *polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2, + *verifybatchesmock.Verifybatchesmock, ) { + t.Helper() + ctx := context.Background() - balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) - address := auth.From - genesisAlloc := map[common.Address]types.Account{ - address: { - Balance: balance, - }, - } - blockGasLimit := uint64(999999999999999999) - client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) + client, auth, _ := helpers.SimulatedBackend(t, nil) nonce, err := client.Client().PendingNonceAt(ctx, auth.From) - if err != nil { - return - } + require.NoError(t, err) + precalculatedAddr := crypto.CreateAddress(auth.From, nonce+1) - verifyAddr, _, verifyContract, err = verifybatchesmock.DeployVerifybatchesmock(auth, client.Client(), precalculatedAddr) - if err != nil { - return - } + verifyAddr, _, verifyContract, err := verifybatchesmock.DeployVerifybatchesmock(auth, client.Client(), precalculatedAddr) + require.NoError(t, err) client.Commit() - gerAddr, _, gerContract, err = polygonzkevmglobalexitrootv2.DeployPolygonzkevmglobalexitrootv2(auth, client.Client(), verifyAddr, auth.From) - if err != nil { - return - } + gerAddr, _, gerContract, err := polygonzkevmglobalexitrootv2.DeployPolygonzkevmglobalexitrootv2(auth, client.Client(), verifyAddr, auth.From) + require.NoError(t, err) client.Commit() - if precalculatedAddr != gerAddr { - err = errors.New("error calculating addr") - } + require.Equal(t, precalculatedAddr, gerAddr) - return + return client, auth, gerAddr, verifyAddr, gerContract, verifyContract } func TestE2E(t *testing.T) { ctx := context.Background() dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared") - privateKey, err := crypto.GenerateKey() - require.NoError(t, err) - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) - require.NoError(t, err) + rdm := l1infotreesync.NewReorgDetectorMock(t) rdm.On("Subscribe", mock.Anything).Return(&reorgdetector.Subscription{}, nil) rdm.On("AddBlockToTrack", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) - client, gerAddr, verifyAddr, gerSc, verifySC, err := newSimulatedClient(auth) - require.NoError(t, err) + client, auth, gerAddr, verifyAddr, gerSc, verifySC := newSimulatedClient(t) syncer, err := l1infotreesync.New(ctx, dbPath, gerAddr, verifyAddr, 10, etherman.LatestBlock, rdm, client.Client(), time.Millisecond, 0, 100*time.Millisecond, 3, l1infotreesync.FlagAllowWrongContractsAddrs) require.NoError(t, err) @@ -165,15 +148,13 @@ func TestWithReorgs(t *testing.T) { ctx := context.Background() dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared") dbPathReorg := t.TempDir() - privateKey, err := crypto.GenerateKey() - require.NoError(t, err) - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) - require.NoError(t, err) - client, gerAddr, verifyAddr, gerSc, verifySC, err := newSimulatedClient(auth) - require.NoError(t, err) + + client, auth, gerAddr, verifyAddr, gerSc, verifySC := newSimulatedClient(t) + rd, err := reorgdetector.New(client.Client(), reorgdetector.Config{DBPath: dbPathReorg, CheckReorgsInterval: cdktypes.NewDuration(time.Millisecond * 30)}) require.NoError(t, err) require.NoError(t, rd.Start(ctx)) + syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerAddr, verifyAddr, 10, etherman.LatestBlock, rd, client.Client(), time.Millisecond, 0, time.Second, 25, l1infotreesync.FlagAllowWrongContractsAddrs) require.NoError(t, err) @@ -273,8 +254,6 @@ func TestWithReorgs(t *testing.T) { } func TestStressAndReorgs(t *testing.T) { - t.Skip() - const ( totalIterations = 3 blocksInIteration = 140 @@ -287,15 +266,13 @@ func TestStressAndReorgs(t *testing.T) { ctx := context.Background() dbPathSyncer := path.Join(t.TempDir(), "file:TestStressAndReorgs:memory:?cache=shared") dbPathReorg := t.TempDir() - privateKey, err := crypto.GenerateKey() - require.NoError(t, err) - auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337)) - require.NoError(t, err) - client, gerAddr, verifyAddr, gerSc, verifySC, err := newSimulatedClient(auth) - require.NoError(t, err) + + client, auth, gerAddr, verifyAddr, gerSc, verifySC := newSimulatedClient(t) + rd, err := reorgdetector.New(client.Client(), reorgdetector.Config{DBPath: dbPathReorg, CheckReorgsInterval: cdktypes.NewDuration(time.Millisecond * 100)}) require.NoError(t, err) require.NoError(t, rd.Start(ctx)) + syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerAddr, verifyAddr, 10, etherman.LatestBlock, rd, client.Client(), time.Millisecond, 0, time.Second, 100, l1infotreesync.FlagAllowWrongContractsAddrs) require.NoError(t, err) diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index f9c57f0a..bdcff02a 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/core/types" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" gerContractL1 "github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/globalexitrootnopush0" gerContractEVMChain "github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/pessimisticglobalexitrootnopush0" @@ -20,7 +22,6 @@ import ( "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" @@ -106,16 +107,15 @@ func CommonSetup(t *testing.T) ( // Config and spin up ctx := context.Background() + // Simulated L1 - privateKeyL1, err := crypto.GenerateKey() - require.NoError(t, err) - authL1, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) - require.NoError(t, err) - l1Client, gerL1Addr, gerL1Contract, bridgeL1Addr, bridgeL1Contract := newSimulatedL1(t, authL1) + l1Client, authL1, gerL1Addr, gerL1Contract, bridgeL1Addr, bridgeL1Contract := newSimulatedL1(t) + // Reorg detector dbPathReorgDetector := t.TempDir() reorg, err := reorgdetector.New(l1Client.Client(), reorgdetector.Config{DBPath: dbPathReorgDetector}) require.NoError(t, err) + // Syncer dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared") syncer, err := l1infotreesync.New(ctx, dbPathSyncer, @@ -141,11 +141,7 @@ func EVMSetup(t *testing.T) ( ) { t.Helper() - privateKeyL2, err := crypto.GenerateKey() - require.NoError(t, err) - authL2, err := bind.NewKeyedTransactorWithChainID(privateKeyL2, big.NewInt(chainID)) - require.NoError(t, err) - l2Client, gerL2Addr, gerL2Sc, bridgeL2Addr, bridgeL2Sc := newSimulatedEVMAggSovereignChain(t, authL2) + l2Client, authL2, gerL2Addr, gerL2Sc, bridgeL2Addr, bridgeL2Sc := newSimulatedEVMAggSovereignChain(t) ethTxManMock := helpers.NewEthTxManMock(t, l2Client, authL2) sender, err := chaingersender.NewEVMChainGERSender(log.GetDefaultLogger(), gerL2Addr, authL2.From, l2Client.Client(), ethTxManMock, 0, time.Millisecond*50) //nolint:mnd @@ -154,35 +150,19 @@ func EVMSetup(t *testing.T) ( return sender, l2Client, gerL2Sc, gerL2Addr, bridgeL2Sc, bridgeL2Addr, authL2, ethTxManMock } -func newSimulatedL1(t *testing.T, auth *bind.TransactOpts) ( - client *simulated.Backend, - gerAddr common.Address, - gerContract *gerContractL1.Globalexitrootnopush0, - bridgeAddr common.Address, - bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2, +func newSimulatedL1(t *testing.T) ( + *simulated.Backend, + *bind.TransactOpts, + common.Address, + *gerContractL1.Globalexitrootnopush0, + common.Address, + *polygonzkevmbridgev2.Polygonzkevmbridgev2, ) { t.Helper() ctx := context.Background() - privateKeyL1, err := crypto.GenerateKey() - require.NoError(t, err) - - authDeployer, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) - require.NoError(t, err) - - balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd - address := auth.From - genesisAlloc := map[common.Address]types.Account{ - address: { - Balance: balance, - }, - authDeployer.From: { - Balance: balance, - }, - } - - client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) + client, auth, authDeployer := helpers.SimulatedBackend(t, nil) bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) require.NoError(t, err) @@ -205,7 +185,7 @@ func newSimulatedL1(t *testing.T, auth *bind.TransactOpts) ( ) require.NoError(t, err) - bridgeAddr, _, _, err = transparentupgradableproxy.DeployTransparentupgradableproxy( + bridgeAddr, _, _, err := transparentupgradableproxy.DeployTransparentupgradableproxy( authDeployer, client.Client(), bridgeImplementationAddr, @@ -215,56 +195,44 @@ func newSimulatedL1(t *testing.T, auth *bind.TransactOpts) ( require.NoError(t, err) client.Commit() - bridgeContract, err = polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) + bridgeContract, err := polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) require.NoError(t, err) checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{Pending: false}) require.NoError(t, err) require.Equal(t, precalculatedAddr, checkGERAddr) - gerAddr, _, gerContract, err = gerContractL1.DeployGlobalexitrootnopush0(authDeployer, client.Client(), + gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(authDeployer, client.Client(), auth.From, bridgeAddr) require.NoError(t, err) client.Commit() require.Equal(t, precalculatedAddr, gerAddr) - return client, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, auth, gerAddr, gerContract, bridgeAddr, bridgeContract } -func newSimulatedEVMAggSovereignChain(t *testing.T, auth *bind.TransactOpts) ( - client *simulated.Backend, - gerAddr common.Address, - gerContract *gerContractEVMChain.Pessimisticglobalexitrootnopush0, - bridgeAddr common.Address, - bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2, +func newSimulatedEVMAggSovereignChain(t *testing.T) ( + *simulated.Backend, + *bind.TransactOpts, + common.Address, + *gerContractEVMChain.Pessimisticglobalexitrootnopush0, + common.Address, + *polygonzkevmbridgev2.Polygonzkevmbridgev2, ) { ctx := context.Background() - privateKeyL1, err := crypto.GenerateKey() + // Create deployer + deployerPK, err := crypto.GenerateKey() require.NoError(t, err) - - authDeployer, err := bind.NewKeyedTransactorWithChainID(privateKeyL1, big.NewInt(chainID)) + authDeployer, err := bind.NewKeyedTransactorWithChainID(deployerPK, big.NewInt(chainID)) require.NoError(t, err) - - balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd - address := auth.From + balance, _ := new(big.Int).SetString(initialBalance, 10) precalculatedBridgeAddr := crypto.CreateAddress(authDeployer.From, 1) - - genesisAlloc := map[common.Address]types.Account{ - address: { - Balance: balance, - }, - authDeployer.From: { - Balance: balance, - }, - precalculatedBridgeAddr: { - Balance: balance, - }, - } - - const blockGasLimit = uint64(999999999999999999) - client = simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit)) + client, auth, _ := helpers.SimulatedBackend(t, map[common.Address]types.Account{ + authDeployer.From: {Balance: balance}, + precalculatedBridgeAddr: {Balance: balance}, + }) bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) require.NoError(t, err) @@ -288,7 +256,7 @@ func newSimulatedEVMAggSovereignChain(t *testing.T, auth *bind.TransactOpts) ( ) require.NoError(t, err) - bridgeAddr, _, _, err = transparentupgradableproxy.DeployTransparentupgradableproxy( + bridgeAddr, _, _, err := transparentupgradableproxy.DeployTransparentupgradableproxy( authDeployer, client.Client(), bridgeImplementationAddr, @@ -299,14 +267,14 @@ func newSimulatedEVMAggSovereignChain(t *testing.T, auth *bind.TransactOpts) ( require.Equal(t, precalculatedBridgeAddr, bridgeAddr) client.Commit() - bridgeContract, err = polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) + bridgeContract, err := polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) require.NoError(t, err) checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{}) require.NoError(t, err) require.Equal(t, precalculatedAddr, checkGERAddr) - gerAddr, _, gerContract, err = gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( + gerAddr, _, gerContract, err := gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( authDeployer, client.Client(), auth.From) require.NoError(t, err) client.Commit() @@ -320,5 +288,5 @@ func newSimulatedEVMAggSovereignChain(t *testing.T, auth *bind.TransactOpts) ( require.True(t, hasRole) require.Equal(t, precalculatedAddr, gerAddr) - return client, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, auth, gerAddr, gerContract, bridgeAddr, bridgeContract } From 346d4bd1ee08483dd71f68def8d4ff5c62b19547 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 18:54:56 +0100 Subject: [PATCH 3/9] feat: Setting up a single function for initing simulated backend with all required contracts --- dataavailability/datacommittee/datacommittee_test.go | 3 +-- reorgdetector/reorgdetector_test.go | 3 +-- test/aggoraclehelpers/aggoracle_e2e.go | 7 ++++--- test/helpers/simulated.go | 7 +++---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index c095540c..431a4565 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -5,12 +5,11 @@ import ( "math/big" "testing" - "github.com/0xPolygon/cdk/test/helpers" - smcparis "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana-paris/polygondatacommittee" "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygondatacommittee" "github.com/0xPolygon/cdk/log" erc1967proxy "github.com/0xPolygon/cdk/test/contracts/erc1967proxy" + "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" diff --git a/reorgdetector/reorgdetector_test.go b/reorgdetector/reorgdetector_test.go index 45c15e70..ed059f69 100644 --- a/reorgdetector/reorgdetector_test.go +++ b/reorgdetector/reorgdetector_test.go @@ -5,9 +5,8 @@ import ( "testing" "time" - "github.com/0xPolygon/cdk/test/helpers" - cdktypes "github.com/0xPolygon/cdk/config/types" + "github.com/0xPolygon/cdk/test/helpers" "github.com/stretchr/testify/require" ) diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index bdcff02a..6c76f14e 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -7,8 +7,6 @@ import ( "testing" "time" - "github.com/ethereum/go-ethereum/core/types" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" gerContractL1 "github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/globalexitrootnopush0" gerContractEVMChain "github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/pessimisticglobalexitrootnopush0" @@ -22,6 +20,7 @@ import ( "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" @@ -220,6 +219,8 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( common.Address, *polygonzkevmbridgev2.Polygonzkevmbridgev2, ) { + t.Helper() + ctx := context.Background() // Create deployer @@ -227,7 +228,7 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( require.NoError(t, err) authDeployer, err := bind.NewKeyedTransactorWithChainID(deployerPK, big.NewInt(chainID)) require.NoError(t, err) - balance, _ := new(big.Int).SetString(initialBalance, 10) + balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd precalculatedBridgeAddr := crypto.CreateAddress(authDeployer.From, 1) client, auth, _ := helpers.SimulatedBackend(t, map[common.Address]types.Account{ authDeployer.From: {Balance: balance}, diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index 8f48503c..586a03a8 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -4,14 +4,13 @@ import ( "math/big" "testing" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/stretchr/testify/require" - - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/ethereum/go-ethereum/rpc" + "github.com/stretchr/testify/require" ) const ( @@ -38,7 +37,7 @@ func SimulatedBackend( t.Helper() // Define default balance - balance, ok := new(big.Int).SetString(defaultBalance, 10) + balance, ok := new(big.Int).SetString(defaultBalance, 10) //nolint:mnd require.Truef(t, ok, "failed to set balance") // Create user From 731a704c29599d7b56a27f9e212edd8f48b0a3fd Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 19:07:07 +0100 Subject: [PATCH 4/9] feat: Setting up a single function for initing simulated backend with all required contracts --- bridgesync/e2e_test.go | 27 +++--------------- .../datacommittee/datacommittee_test.go | 20 ++++++------- l1infotreesync/e2e_test.go | 12 ++++---- reorgdetector/reorgdetector_test.go | 6 ++-- test/aggoraclehelpers/aggoracle_e2e.go | 28 +++++++++---------- test/helpers/simulated.go | 26 +++++++++++++++-- 6 files changed, 61 insertions(+), 58 deletions(-) diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index 2e2ee322..1b1a80b1 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -8,47 +8,28 @@ import ( "testing" "time" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" "github.com/0xPolygon/cdk/bridgesync" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/reorgdetector" "github.com/0xPolygon/cdk/test/helpers" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" ) -func newSimulatedClient(t *testing.T) ( - *simulated.Backend, - *bind.TransactOpts, - common.Address, - *polygonzkevmbridgev2.Polygonzkevmbridgev2, -) { - t.Helper() - - client, userAuth, deployerAuth := helpers.SimulatedBackend(t, nil) - bridgeAddr, _, bridgeContract, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(deployerAuth, client.Client()) - require.NoError(t, err) - client.Commit() - - return client, userAuth, bridgeAddr, bridgeContract -} - func TestBridgeEventE2E(t *testing.T) { ctx := context.Background() dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared") dbPathReorg := t.TempDir() - client, auth, bridgeAddr, bridgeSc := newSimulatedClient(t) + client, setup := helpers.SimulatedBackend(t, nil) rd, err := reorgdetector.New(client.Client(), reorgdetector.Config{DBPath: dbPathReorg}) require.NoError(t, err) go rd.Start(ctx) //nolint:errcheck testClient := helpers.TestClient{ClientRenamed: client.Client()} - syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, bridgeAddr, 10, etherman.LatestBlock, rd, testClient, 0, time.Millisecond*10, 0, 0) + syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, setup.EBZkevmBridgeAddr, 10, etherman.LatestBlock, rd, testClient, 0, time.Millisecond*10, 0, 0) require.NoError(t, err) go syncer.Start(ctx) @@ -65,8 +46,8 @@ func TestBridgeEventE2E(t *testing.T) { DestinationAddress: common.HexToAddress("f00"), Metadata: []byte{}, } - tx, err := bridgeSc.BridgeAsset( - auth, + tx, err := setup.EBZkevmBridgeContract.BridgeAsset( + setup.UserAuth, bridge.DestinationNetwork, bridge.DestinationAddress, bridge.Amount, diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index 431a4565..9e1779f7 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -66,28 +66,28 @@ func init() { // NewSimulatedEtherman creates an etherman that uses a simulated blockchain. It's important to notice that the ChainID of the auth // must be 1337. The address that holds the auth will have an initial balance of 10 ETH func newSimulatedDacman(t *testing.T) ( - dacman *Backend, - ethBackend *simulated.Backend, - da *polygondatacommittee.Polygondatacommittee, - auth *bind.TransactOpts, + *Backend, + *simulated.Backend, + *polygondatacommittee.Polygondatacommittee, + *bind.TransactOpts, ) { t.Helper() - ethBackend, auth, _ = helpers.SimulatedBackend(t, nil) + ethBackend, setup := helpers.SimulatedBackend(t, nil) // DAC Setup - addr, _, _, err := smcparis.DeployPolygondatacommittee(auth, ethBackend.Client()) + addr, _, _, err := smcparis.DeployPolygondatacommittee(setup.UserAuth, ethBackend.Client()) require.NoError(t, err) ethBackend.Commit() - proxyAddr, err := deployDACProxy(auth, ethBackend.Client(), addr) + proxyAddr, err := deployDACProxy(setup.UserAuth, ethBackend.Client(), addr) require.NoError(t, err) ethBackend.Commit() - da, err = polygondatacommittee.NewPolygondatacommittee(proxyAddr, ethBackend.Client()) + da, err := polygondatacommittee.NewPolygondatacommittee(proxyAddr, ethBackend.Client()) require.NoError(t, err) - _, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{}) + _, err = da.SetupCommittee(setup.UserAuth, big.NewInt(0), []string{}, []byte{}) require.NoError(t, err) ethBackend.Commit() @@ -95,7 +95,7 @@ func newSimulatedDacman(t *testing.T) ( dataCommitteeContract: da, } - return c, ethBackend, da, auth + return c, ethBackend, da, setup.UserAuth } func deployDACProxy(auth *bind.TransactOpts, client bind.ContractBackend, dacImpl common.Address) (common.Address, error) { diff --git a/l1infotreesync/e2e_test.go b/l1infotreesync/e2e_test.go index 4526f7b2..c2c015c5 100644 --- a/l1infotreesync/e2e_test.go +++ b/l1infotreesync/e2e_test.go @@ -36,23 +36,23 @@ func newSimulatedClient(t *testing.T) ( t.Helper() ctx := context.Background() - client, auth, _ := helpers.SimulatedBackend(t, nil) + client, setup := helpers.SimulatedBackend(t, nil) - nonce, err := client.Client().PendingNonceAt(ctx, auth.From) + nonce, err := client.Client().PendingNonceAt(ctx, setup.UserAuth.From) require.NoError(t, err) - precalculatedAddr := crypto.CreateAddress(auth.From, nonce+1) - verifyAddr, _, verifyContract, err := verifybatchesmock.DeployVerifybatchesmock(auth, client.Client(), precalculatedAddr) + precalculatedAddr := crypto.CreateAddress(setup.UserAuth.From, nonce+1) + verifyAddr, _, verifyContract, err := verifybatchesmock.DeployVerifybatchesmock(setup.UserAuth, client.Client(), precalculatedAddr) require.NoError(t, err) client.Commit() - gerAddr, _, gerContract, err := polygonzkevmglobalexitrootv2.DeployPolygonzkevmglobalexitrootv2(auth, client.Client(), verifyAddr, auth.From) + gerAddr, _, gerContract, err := polygonzkevmglobalexitrootv2.DeployPolygonzkevmglobalexitrootv2(setup.UserAuth, client.Client(), verifyAddr, setup.UserAuth.From) require.NoError(t, err) client.Commit() require.Equal(t, precalculatedAddr, gerAddr) - return client, auth, gerAddr, verifyAddr, gerContract, verifyContract + return client, setup.UserAuth, gerAddr, verifyAddr, gerContract, verifyContract } func TestE2E(t *testing.T) { diff --git a/reorgdetector/reorgdetector_test.go b/reorgdetector/reorgdetector_test.go index ed059f69..b94e300f 100644 --- a/reorgdetector/reorgdetector_test.go +++ b/reorgdetector/reorgdetector_test.go @@ -16,7 +16,7 @@ func Test_ReorgDetector(t *testing.T) { ctx := context.Background() // Simulated L1 - clientL1, _, _ := helpers.SimulatedBackend(t, nil) + clientL1, _ := helpers.SimulatedBackend(t, nil) // Create test DB dir testDir := t.TempDir() @@ -66,6 +66,6 @@ func Test_ReorgDetector(t *testing.T) { headersList, ok := reorgDetector.trackedBlocks[subID] reorgDetector.trackedBlocksLock.Unlock() require.True(t, ok) - require.Equal(t, 1, headersList.len()) // Only block 1 left - require.Equal(t, remainingHeader.Hash(), headersList.get(1).Hash) + require.Equal(t, 1, headersList.len()) // Only block 3 left + require.Equal(t, remainingHeader.Hash(), headersList.get(3).Hash) } diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index 6c76f14e..14738d73 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -161,15 +161,15 @@ func newSimulatedL1(t *testing.T) ( ctx := context.Background() - client, auth, authDeployer := helpers.SimulatedBackend(t, nil) + client, setup := helpers.SimulatedBackend(t, nil) - bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) + bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(setup.DeployerAuth, client.Client()) require.NoError(t, err) client.Commit() - nonce, err := client.Client().PendingNonceAt(ctx, authDeployer.From) + nonce, err := client.Client().PendingNonceAt(ctx, setup.DeployerAuth.From) require.NoError(t, err) - precalculatedAddr := crypto.CreateAddress(authDeployer.From, nonce+1) + precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, nonce+1) bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() require.NoError(t, err) require.NotNil(t, bridgeABI) @@ -185,10 +185,10 @@ func newSimulatedL1(t *testing.T) ( require.NoError(t, err) bridgeAddr, _, _, err := transparentupgradableproxy.DeployTransparentupgradableproxy( - authDeployer, + setup.DeployerAuth, client.Client(), bridgeImplementationAddr, - authDeployer.From, + setup.DeployerAuth.From, dataCallProxy, ) require.NoError(t, err) @@ -201,14 +201,14 @@ func newSimulatedL1(t *testing.T) ( require.NoError(t, err) require.Equal(t, precalculatedAddr, checkGERAddr) - gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(authDeployer, client.Client(), - auth.From, bridgeAddr) + gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(setup.DeployerAuth, client.Client(), + setup.UserAuth.From, bridgeAddr) require.NoError(t, err) client.Commit() require.Equal(t, precalculatedAddr, gerAddr) - return client, auth, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, setup.UserAuth, gerAddr, gerContract, bridgeAddr, bridgeContract } func newSimulatedEVMAggSovereignChain(t *testing.T) ( @@ -230,7 +230,7 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( require.NoError(t, err) balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd precalculatedBridgeAddr := crypto.CreateAddress(authDeployer.From, 1) - client, auth, _ := helpers.SimulatedBackend(t, map[common.Address]types.Account{ + client, setup := helpers.SimulatedBackend(t, map[common.Address]types.Account{ authDeployer.From: {Balance: balance}, precalculatedBridgeAddr: {Balance: balance}, }) @@ -276,18 +276,18 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( require.Equal(t, precalculatedAddr, checkGERAddr) gerAddr, _, gerContract, err := gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( - authDeployer, client.Client(), auth.From) + authDeployer, client.Client(), setup.UserAuth.From) require.NoError(t, err) client.Commit() globalExitRootSetterRole := common.HexToHash("0x7b95520991dfda409891be0afa2635b63540f92ee996fda0bf695a166e5c5176") - _, err = gerContract.GrantRole(authDeployer, globalExitRootSetterRole, auth.From) + _, err = gerContract.GrantRole(authDeployer, globalExitRootSetterRole, setup.UserAuth.From) require.NoError(t, err) client.Commit() - hasRole, _ := gerContract.HasRole(&bind.CallOpts{Pending: false}, globalExitRootSetterRole, auth.From) + hasRole, _ := gerContract.HasRole(&bind.CallOpts{Pending: false}, globalExitRootSetterRole, setup.UserAuth.From) require.True(t, hasRole) require.Equal(t, precalculatedAddr, gerAddr) - return client, auth, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, setup.UserAuth, gerAddr, gerContract, bridgeAddr, bridgeContract } diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index 586a03a8..6a1ab2e9 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -4,6 +4,7 @@ import ( "math/big" "testing" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -29,11 +30,19 @@ func (tc TestClient) Client() *rpc.Client { return nil } +// SimulatedBackendSetup defines the setup for a simulated backend. +type SimulatedBackendSetup struct { + UserAuth *bind.TransactOpts + DeployerAuth *bind.TransactOpts + EBZkevmBridgeAddr common.Address + EBZkevmBridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 +} + // SimulatedBackend creates a simulated backend with two accounts: user and deployer. func SimulatedBackend( t *testing.T, balances map[common.Address]types.Account, -) (*simulated.Backend, *bind.TransactOpts, *bind.TransactOpts) { +) (*simulated.Backend, *SimulatedBackendSetup) { t.Helper() // Define default balance @@ -61,5 +70,18 @@ func SimulatedBackend( client := simulated.NewBackend(balances, simulated.WithBlockGasLimit(defaultBlockGasLimit)) - return client, userAuth, deployerAuth + // Mine the first block + client.Commit() + + // Deploy zkevm bridge contract + ebZkevmBridgeAddr, _, ebZkevmBridgeContract, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(deployerAuth, client.Client()) + require.NoError(t, err) + client.Commit() + + return client, &SimulatedBackendSetup{ + UserAuth: userAuth, + DeployerAuth: deployerAuth, + EBZkevmBridgeAddr: ebZkevmBridgeAddr, + EBZkevmBridgeContract: ebZkevmBridgeContract, + } } From f851b53635bd2867e32c369c33d2943f54498160 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 19:31:27 +0100 Subject: [PATCH 5/9] feat: Setting up a single function for initing simulated backend with all required contracts --- bridgesync/e2e_test.go | 2 +- .../datacommittee/datacommittee_test.go | 2 +- l1infotreesync/e2e_test.go | 2 +- reorgdetector/reorgdetector_test.go | 4 +- test/aggoraclehelpers/aggoracle_e2e.go | 111 ++---------------- test/helpers/simulated.go | 65 ++++++++-- 6 files changed, 71 insertions(+), 115 deletions(-) diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index 1b1a80b1..b5be892f 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -22,7 +22,7 @@ func TestBridgeEventE2E(t *testing.T) { dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared") dbPathReorg := t.TempDir() - client, setup := helpers.SimulatedBackend(t, nil) + client, setup := helpers.SimulatedBackend(t, nil, 0) rd, err := reorgdetector.New(client.Client(), reorgdetector.Config{DBPath: dbPathReorg}) require.NoError(t, err) diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index 9e1779f7..7e2a8d3e 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -73,7 +73,7 @@ func newSimulatedDacman(t *testing.T) ( ) { t.Helper() - ethBackend, setup := helpers.SimulatedBackend(t, nil) + ethBackend, setup := helpers.SimulatedBackend(t, nil, 0) // DAC Setup addr, _, _, err := smcparis.DeployPolygondatacommittee(setup.UserAuth, ethBackend.Client()) diff --git a/l1infotreesync/e2e_test.go b/l1infotreesync/e2e_test.go index c2c015c5..94596f23 100644 --- a/l1infotreesync/e2e_test.go +++ b/l1infotreesync/e2e_test.go @@ -36,7 +36,7 @@ func newSimulatedClient(t *testing.T) ( t.Helper() ctx := context.Background() - client, setup := helpers.SimulatedBackend(t, nil) + client, setup := helpers.SimulatedBackend(t, nil, 0) nonce, err := client.Client().PendingNonceAt(ctx, setup.UserAuth.From) require.NoError(t, err) diff --git a/reorgdetector/reorgdetector_test.go b/reorgdetector/reorgdetector_test.go index b94e300f..c99bb484 100644 --- a/reorgdetector/reorgdetector_test.go +++ b/reorgdetector/reorgdetector_test.go @@ -16,7 +16,7 @@ func Test_ReorgDetector(t *testing.T) { ctx := context.Background() // Simulated L1 - clientL1, _ := helpers.SimulatedBackend(t, nil) + clientL1, _ := helpers.SimulatedBackend(t, nil, 0) // Create test DB dir testDir := t.TempDir() @@ -67,5 +67,5 @@ func Test_ReorgDetector(t *testing.T) { reorgDetector.trackedBlocksLock.Unlock() require.True(t, ok) require.Equal(t, 1, headersList.len()) // Only block 3 left - require.Equal(t, remainingHeader.Hash(), headersList.get(3).Hash) + require.Equal(t, remainingHeader.Hash(), headersList.get(4).Hash) } diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index 14738d73..93dcebfd 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -2,7 +2,6 @@ package aggoraclehelpers import ( "context" - "math/big" "path" "testing" "time" @@ -16,11 +15,9 @@ import ( "github.com/0xPolygon/cdk/l1infotreesync" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/reorgdetector" - "github.com/0xPolygon/cdk/test/contracts/transparentupgradableproxy" "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient/simulated" "github.com/stretchr/testify/require" @@ -159,56 +156,16 @@ func newSimulatedL1(t *testing.T) ( ) { t.Helper() - ctx := context.Background() - - client, setup := helpers.SimulatedBackend(t, nil) - - bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(setup.DeployerAuth, client.Client()) - require.NoError(t, err) - client.Commit() - - nonce, err := client.Client().PendingNonceAt(ctx, setup.DeployerAuth.From) - require.NoError(t, err) - precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, nonce+1) - bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() - require.NoError(t, err) - require.NotNil(t, bridgeABI) - - dataCallProxy, err := bridgeABI.Pack("initialize", - uint32(0), // networkIDMainnet - common.Address{}, // gasTokenAddressMainnet" - uint32(0), // gasTokenNetworkMainnet - precalculatedAddr, - common.Address{}, - []byte{}, // gasTokenMetadata - ) - require.NoError(t, err) - - bridgeAddr, _, _, err := transparentupgradableproxy.DeployTransparentupgradableproxy( - setup.DeployerAuth, - client.Client(), - bridgeImplementationAddr, - setup.DeployerAuth.From, - dataCallProxy, - ) - require.NoError(t, err) - client.Commit() - - bridgeContract, err := polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) - require.NoError(t, err) - - checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{Pending: false}) - require.NoError(t, err) - require.Equal(t, precalculatedAddr, checkGERAddr) + client, setup := helpers.SimulatedBackend(t, nil, 0) gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(setup.DeployerAuth, client.Client(), - setup.UserAuth.From, bridgeAddr) + setup.UserAuth.From, setup.EBZkevmBridgeProxyAddr) require.NoError(t, err) client.Commit() - require.Equal(t, precalculatedAddr, gerAddr) + // require.Equal(t, precalculatedAddr, gerAddr) - return client, setup.UserAuth, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, setup.UserAuth, gerAddr, gerContract, setup.EBZkevmBridgeProxyAddr, setup.EBZkevmBridgeProxyContract } func newSimulatedEVMAggSovereignChain(t *testing.T) ( @@ -221,67 +178,17 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( ) { t.Helper() - ctx := context.Background() + client, setup := helpers.SimulatedBackend(t, nil, NetworkIDL2) - // Create deployer - deployerPK, err := crypto.GenerateKey() - require.NoError(t, err) - authDeployer, err := bind.NewKeyedTransactorWithChainID(deployerPK, big.NewInt(chainID)) - require.NoError(t, err) - balance, _ := new(big.Int).SetString(initialBalance, 10) //nolint:mnd - precalculatedBridgeAddr := crypto.CreateAddress(authDeployer.From, 1) - client, setup := helpers.SimulatedBackend(t, map[common.Address]types.Account{ - authDeployer.From: {Balance: balance}, - precalculatedBridgeAddr: {Balance: balance}, - }) - - bridgeImplementationAddr, _, _, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(authDeployer, client.Client()) - require.NoError(t, err) - client.Commit() - - nonce, err := client.Client().PendingNonceAt(ctx, authDeployer.From) - require.NoError(t, err) - precalculatedAddr := crypto.CreateAddress(authDeployer.From, nonce+1) - - bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() - require.NoError(t, err) - require.NotNil(t, bridgeABI) - - dataCallProxy, err := bridgeABI.Pack("initialize", - NetworkIDL2, - common.Address{}, // gasTokenAddressMainnet" - uint32(0), // gasTokenNetworkMainnet - precalculatedAddr, - common.Address{}, - []byte{}, // gasTokenMetadata - ) - require.NoError(t, err) - - bridgeAddr, _, _, err := transparentupgradableproxy.DeployTransparentupgradableproxy( - authDeployer, - client.Client(), - bridgeImplementationAddr, - authDeployer.From, - dataCallProxy, - ) - require.NoError(t, err) - require.Equal(t, precalculatedBridgeAddr, bridgeAddr) - client.Commit() - - bridgeContract, err := polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridgeAddr, client.Client()) - require.NoError(t, err) - - checkGERAddr, err := bridgeContract.GlobalExitRootManager(&bind.CallOpts{}) - require.NoError(t, err) - require.Equal(t, precalculatedAddr, checkGERAddr) + precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) gerAddr, _, gerContract, err := gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( - authDeployer, client.Client(), setup.UserAuth.From) + setup.DeployerAuth, client.Client(), setup.UserAuth.From) require.NoError(t, err) client.Commit() globalExitRootSetterRole := common.HexToHash("0x7b95520991dfda409891be0afa2635b63540f92ee996fda0bf695a166e5c5176") - _, err = gerContract.GrantRole(authDeployer, globalExitRootSetterRole, setup.UserAuth.From) + _, err = gerContract.GrantRole(setup.DeployerAuth, globalExitRootSetterRole, setup.UserAuth.From) require.NoError(t, err) client.Commit() @@ -289,5 +196,5 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( require.True(t, hasRole) require.Equal(t, precalculatedAddr, gerAddr) - return client, setup.UserAuth, gerAddr, gerContract, bridgeAddr, bridgeContract + return client, setup.UserAuth, gerAddr, gerContract, setup.EBZkevmBridgeProxyAddr, setup.EBZkevmBridgeProxyContract } diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index 6a1ab2e9..b4c158bf 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -4,6 +4,8 @@ import ( "math/big" "testing" + "github.com/0xPolygon/cdk/test/contracts/transparentupgradableproxy" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -32,16 +34,19 @@ func (tc TestClient) Client() *rpc.Client { // SimulatedBackendSetup defines the setup for a simulated backend. type SimulatedBackendSetup struct { - UserAuth *bind.TransactOpts - DeployerAuth *bind.TransactOpts - EBZkevmBridgeAddr common.Address - EBZkevmBridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 + UserAuth *bind.TransactOpts + DeployerAuth *bind.TransactOpts + EBZkevmBridgeAddr common.Address + EBZkevmBridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 + EBZkevmBridgeProxyAddr common.Address + EBZkevmBridgeProxyContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 } // SimulatedBackend creates a simulated backend with two accounts: user and deployer. func SimulatedBackend( t *testing.T, balances map[common.Address]types.Account, + ebZkevmBridgeNetwork uint32, ) (*simulated.Backend, *SimulatedBackendSetup) { t.Helper() @@ -60,6 +65,7 @@ func SimulatedBackend( require.NoError(t, err) deployerAuth, err := bind.NewKeyedTransactorWithChainID(deployerPK, big.NewInt(chainID)) require.NoError(t, err) + precalculatedBridgeAddr := crypto.CreateAddress(deployerAuth.From, 1) // Define balances map if balances == nil { @@ -67,21 +73,64 @@ func SimulatedBackend( } balances[userAuth.From] = types.Account{Balance: balance} balances[deployerAuth.From] = types.Account{Balance: balance} + balances[precalculatedBridgeAddr] = types.Account{Balance: balance} client := simulated.NewBackend(balances, simulated.WithBlockGasLimit(defaultBlockGasLimit)) // Mine the first block client.Commit() + // MUST BE DEPLOYED FIRST // Deploy zkevm bridge contract ebZkevmBridgeAddr, _, ebZkevmBridgeContract, err := polygonzkevmbridgev2.DeployPolygonzkevmbridgev2(deployerAuth, client.Client()) require.NoError(t, err) client.Commit() + // Create proxy contract for the bridge + var ebZkevmBridgeProxyAddr common.Address + var ebZkevmBridgeProxyContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 + { + precalculatedAddr := crypto.CreateAddress(deployerAuth.From, 2) + + bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() + require.NoError(t, err) + require.NotNil(t, bridgeABI) + + dataCallProxy, err := bridgeABI.Pack("initialize", + ebZkevmBridgeNetwork, + common.Address{}, // gasTokenAddressMainnet + uint32(0), // gasTokenNetworkMainnet + precalculatedAddr, + common.Address{}, + []byte{}, // gasTokenMetadata + ) + require.NoError(t, err) + + ebZkevmBridgeProxyAddr, _, _, err = transparentupgradableproxy.DeployTransparentupgradableproxy( + deployerAuth, + client.Client(), + ebZkevmBridgeAddr, + deployerAuth.From, + dataCallProxy, + ) + require.NoError(t, err) + require.Equal(t, precalculatedBridgeAddr, ebZkevmBridgeProxyAddr) + client.Commit() + + ebZkevmBridgeProxyContract, err = polygonzkevmbridgev2.NewPolygonzkevmbridgev2(ebZkevmBridgeProxyAddr, client.Client()) + require.NoError(t, err) + + checkGERAddr, err := ebZkevmBridgeProxyContract.GlobalExitRootManager(&bind.CallOpts{}) + require.NoError(t, err) + require.Equal(t, precalculatedAddr, checkGERAddr) + } + return client, &SimulatedBackendSetup{ - UserAuth: userAuth, - DeployerAuth: deployerAuth, - EBZkevmBridgeAddr: ebZkevmBridgeAddr, - EBZkevmBridgeContract: ebZkevmBridgeContract, + UserAuth: userAuth, + DeployerAuth: deployerAuth, + EBZkevmBridgeAddr: ebZkevmBridgeAddr, + EBZkevmBridgeContract: ebZkevmBridgeContract, + EBZkevmBridgeProxyAddr: ebZkevmBridgeProxyAddr, + EBZkevmBridgeProxyContract: ebZkevmBridgeProxyContract, } } From 8f63e4ebdb43e7faf618e488d58341c6a7c24334 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 19:33:49 +0100 Subject: [PATCH 6/9] feat: Setting up a single function for initing simulated backend with all required contracts --- test/aggoraclehelpers/aggoracle_e2e.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index 93dcebfd..e48490df 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -158,12 +158,14 @@ func newSimulatedL1(t *testing.T) ( client, setup := helpers.SimulatedBackend(t, nil, 0) + precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) + gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(setup.DeployerAuth, client.Client(), setup.UserAuth.From, setup.EBZkevmBridgeProxyAddr) require.NoError(t, err) client.Commit() - // require.Equal(t, precalculatedAddr, gerAddr) + require.Equal(t, precalculatedAddr, gerAddr) return client, setup.UserAuth, gerAddr, gerContract, setup.EBZkevmBridgeProxyAddr, setup.EBZkevmBridgeProxyContract } From 5127b85fed095735568e75ffcc1d421f74c37315 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 19:34:56 +0100 Subject: [PATCH 7/9] feat: Setting up a single function for initing simulated backend with all required contracts --- test/aggoraclehelpers/aggoracle_e2e.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index e48490df..05044edf 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -25,9 +25,6 @@ import ( const ( NetworkIDL2 = uint32(1) - chainID = 1337 - initialBalance = "10000000000000000000000000" - blockGasLimit = uint64(999999999999999999) syncBlockChunkSize = 10 retries = 3 periodRetry = time.Millisecond * 100 From 96289f6125e5c2d07570626a42538ff171fed62e Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 19:38:34 +0100 Subject: [PATCH 8/9] feat: Setting up a single function for initing simulated backend with all required contracts --- test/aggoraclehelpers/aggoracle_e2e.go | 4 ++-- test/helpers/simulated.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/aggoraclehelpers/aggoracle_e2e.go b/test/aggoraclehelpers/aggoracle_e2e.go index 05044edf..be362ccc 100644 --- a/test/aggoraclehelpers/aggoracle_e2e.go +++ b/test/aggoraclehelpers/aggoracle_e2e.go @@ -155,7 +155,7 @@ func newSimulatedL1(t *testing.T) ( client, setup := helpers.SimulatedBackend(t, nil, 0) - precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) + precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) //nolint:mnd gerAddr, _, gerContract, err := gerContractL1.DeployGlobalexitrootnopush0(setup.DeployerAuth, client.Client(), setup.UserAuth.From, setup.EBZkevmBridgeProxyAddr) @@ -179,7 +179,7 @@ func newSimulatedEVMAggSovereignChain(t *testing.T) ( client, setup := helpers.SimulatedBackend(t, nil, NetworkIDL2) - precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) + precalculatedAddr := crypto.CreateAddress(setup.DeployerAuth.From, 2) //nolint:mnd gerAddr, _, gerContract, err := gerContractEVMChain.DeployPessimisticglobalexitrootnopush0( setup.DeployerAuth, client.Client(), setup.UserAuth.From) diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index b4c158bf..d85baf92 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -4,9 +4,8 @@ import ( "math/big" "testing" - "github.com/0xPolygon/cdk/test/contracts/transparentupgradableproxy" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry-paris/polygonzkevmbridgev2" + "github.com/0xPolygon/cdk/test/contracts/transparentupgradableproxy" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -90,7 +89,7 @@ func SimulatedBackend( var ebZkevmBridgeProxyAddr common.Address var ebZkevmBridgeProxyContract *polygonzkevmbridgev2.Polygonzkevmbridgev2 { - precalculatedAddr := crypto.CreateAddress(deployerAuth.From, 2) + precalculatedAddr := crypto.CreateAddress(deployerAuth.From, 2) //nolint:mnd bridgeABI, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi() require.NoError(t, err) From ee7b6e7bfa4e3816eff7729731ae7e9bbc941753 Mon Sep 17 00:00:00 2001 From: begmaroman Date: Tue, 8 Oct 2024 20:07:21 +0100 Subject: [PATCH 9/9] feat: Setting up a single function for initing simulated backend with all required contracts --- bridgesync/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index b5be892f..c0a22484 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -39,7 +39,7 @@ func TestBridgeEventE2E(t *testing.T) { for i := 0; i < 100; i++ { bridge := bridgesync.Bridge{ - BlockNum: uint64(2 + i), + BlockNum: uint64(4 + i), Amount: big.NewInt(0), DepositCount: uint32(i), DestinationNetwork: 3,