Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for all the contracts on test/helpers so it's easy to build E2E tests #115

Merged
merged 9 commits into from
Oct 10, 2024
Merged
4 changes: 2 additions & 2 deletions aggoracle/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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"
"github.com/stretchr/testify/require"
)

func TestEVM(t *testing.T) {
env := helpers.SetupAggoracleWithEVMChain(t)
env := aggoraclehelpers.SetupAggoracleWithEVMChain(t)
runTest(t, env.GERL1Contract, env.AggOracleSender, env.L1Client, env.AuthL1)
}

Expand Down
44 changes: 6 additions & 38 deletions bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +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/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,
) {
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())
require.NoError(t, err)
client.Commit()

return
}

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, setup := helpers.SimulatedBackend(t, nil, 0)
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)
Expand All @@ -71,15 +39,15 @@ 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,
DestinationAddress: common.HexToAddress("f00"),
Metadata: []byte{},
}
tx, err := bridgeSc.BridgeAsset(
auth,
tx, err := setup.EBZkevmBridgeContract.BridgeAsset(
setup.UserAuth,
bridge.DestinationNetwork,
bridge.DestinationAddress,
bridge.Amount,
Expand Down
3 changes: 2 additions & 1 deletion claimsponsor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
87 changes: 22 additions & 65 deletions dataavailability/datacommittee/datacommittee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"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/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/stretchr/testify/assert"
Expand All @@ -20,7 +20,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)
Expand Down Expand Up @@ -63,82 +63,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) (
dacman *Backend,
ethBackend *simulated.Backend,
da *polygondatacommittee.Polygondatacommittee,
err error,
func newSimulatedDacman(t *testing.T) (
*Backend,
*simulated.Backend,
*polygondatacommittee.Polygondatacommittee,
*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, setup := helpers.SimulatedBackend(t, nil, 0)

// 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(setup.UserAuth, 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(setup.UserAuth, ethBackend.Client(), addr)
require.NoError(t, err)
ethBackend.Commit()

_, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{})
if err != nil {
return &Backend{}, nil, nil, err
}
client.Commit()
da, err := polygondatacommittee.NewPolygondatacommittee(proxyAddr, ethBackend.Client())
require.NoError(t, err)

_, err = da.SetupCommittee(setup.UserAuth, big.NewInt(0), []string{}, []byte{})
require.NoError(t, err)
ethBackend.Commit()

c := &Backend{
dataCommitteeContract: da,
}

return c, client, da, nil
return c, ethBackend, da, setup.UserAuth
}

func deployDACProxy(auth *bind.TransactOpts, client bind.ContractBackend, dacImpl common.Address) (common.Address, error) {
Expand Down
83 changes: 31 additions & 52 deletions l1infotreesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package l1infotreesync_test

import (
"context"
"errors"
"fmt"
"math/big"
"path"
Expand All @@ -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"
Expand All @@ -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, setup := helpers.SimulatedBackend(t, nil, 0)

nonce, err := client.Client().PendingNonceAt(ctx, auth.From)
if err != nil {
return
}
precalculatedAddr := crypto.CreateAddress(auth.From, nonce+1)
verifyAddr, _, verifyContract, err = verifybatchesmock.DeployVerifybatchesmock(auth, client.Client(), precalculatedAddr)
if err != nil {
return
}
nonce, err := client.Client().PendingNonceAt(ctx, setup.UserAuth.From)
require.NoError(t, err)

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)
if err != nil {
return
}
gerAddr, _, gerContract, err := polygonzkevmglobalexitrootv2.DeployPolygonzkevmglobalexitrootv2(setup.UserAuth, client.Client(), verifyAddr, setup.UserAuth.From)
require.NoError(t, err)
client.Commit()

if precalculatedAddr != gerAddr {
err = errors.New("error calculating addr")
}
require.Equal(t, precalculatedAddr, gerAddr)

return
return client, setup.UserAuth, 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -285,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)
Expand Down
4 changes: 2 additions & 2 deletions lastgersync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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"
)

func TestE2E(t *testing.T) {
ctx := context.Background()
env := helpers.SetupAggoracleWithEVMChain(t)
env := aggoraclehelpers.SetupAggoracleWithEVMChain(t)
dbPathSyncer := t.TempDir()
syncer, err := lastgersync.New(
ctx,
Expand Down
Loading
Loading