|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/0xPolygonHermez/zkevm-node/db" |
| 10 | + "github.com/0xPolygonHermez/zkevm-node/log" |
| 11 | + "github.com/0xPolygonHermez/zkevm-node/test/operations" |
| 12 | + "github.com/0xPolygonHermez/zkevm-node/test/testutils" |
| 13 | + "github.com/ethereum/go-ethereum" |
| 14 | + "github.com/ethereum/go-ethereum/common" |
| 15 | + "github.com/ethereum/go-ethereum/core/types" |
| 16 | + "github.com/ethereum/go-ethereum/ethclient" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestPermissionlessJRPC(t *testing.T) { |
| 21 | + // Initial setup: |
| 22 | + // - permissionless RPC + Sync |
| 23 | + // - trusted node with everything minus EthTxMan (to prevent the trusted state from being virtualized) |
| 24 | + if testing.Short() { |
| 25 | + t.Skip() |
| 26 | + } |
| 27 | + ctx := context.Background() |
| 28 | + defer func() { require.NoError(t, operations.TeardownPermissionless()) }() |
| 29 | + err := operations.Teardown() |
| 30 | + require.NoError(t, err) |
| 31 | + opsCfg := operations.GetDefaultOperationsConfig() |
| 32 | + opsCfg.State.MaxCumulativeGasUsed = 80000000000 |
| 33 | + opsman, err := operations.NewManager(ctx, opsCfg) |
| 34 | + require.NoError(t, err) |
| 35 | + require.NoError(t, opsman.SetupWithPermissionless()) |
| 36 | + require.NoError(t, opsman.StopEthTxSender()) |
| 37 | + time.Sleep(5 * time.Second) |
| 38 | + |
| 39 | + // Step 1: |
| 40 | + // - actions: send nTxsStep1 transactions to the trusted sequencer through the permissionless sequencer |
| 41 | + // first transaction gets the current nonce. The others are generated |
| 42 | + // - assert: transactions are properly relayed, added in to the trusted state and broadcasted to the permissionless ndoe |
| 43 | + |
| 44 | + nTxsStep1 := 10 |
| 45 | + // Load account with balance on local genesis |
| 46 | + auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL2ChainID) |
| 47 | + require.NoError(t, err) |
| 48 | + // Load eth client (permissionless RPC) |
| 49 | + client, err := ethclient.Dial(operations.PermissionlessL2NetworkURL) |
| 50 | + require.NoError(t, err) |
| 51 | + // Send txs |
| 52 | + amount := big.NewInt(10000) |
| 53 | + toAddress := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8") |
| 54 | + senderBalance, err := client.BalanceAt(ctx, auth.From, nil) |
| 55 | + require.NoError(t, err) |
| 56 | + nonceToBeUsedForNextTx, err := client.PendingNonceAt(ctx, auth.From) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + log.Infof("Receiver Addr: %v", toAddress.String()) |
| 60 | + log.Infof("Sender Addr: %v", auth.From.String()) |
| 61 | + log.Infof("Sender Balance: %v", senderBalance.String()) |
| 62 | + log.Infof("Sender Nonce: %v", nonceToBeUsedForNextTx) |
| 63 | + |
| 64 | + gasLimit, err := client.EstimateGas(ctx, ethereum.CallMsg{From: auth.From, To: &toAddress, Value: amount}) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + gasPrice, err := client.SuggestGasPrice(ctx) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + txsStep1 := make([]*types.Transaction, 0, nTxsStep1) |
| 71 | + for i := 0; i < nTxsStep1; i++ { |
| 72 | + tx := types.NewTransaction(nonceToBeUsedForNextTx, toAddress, amount, gasLimit, gasPrice, nil) |
| 73 | + txsStep1 = append(txsStep1, tx) |
| 74 | + nonceToBeUsedForNextTx += 1 |
| 75 | + } |
| 76 | + log.Infof("sending %d txs and waiting until added in the permissionless RPC trusted state") |
| 77 | + l2BlockNumbersStep1, err := operations.ApplyL2Txs(ctx, txsStep1, auth, client, operations.TrustedConfirmationLevel) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + // Step 2 |
| 81 | + // - actions: stop the sequencer and send nTxsStep2 transactions, then use the getPendingNonce, and send tx with the resulting nonce |
| 82 | + // - assert: pendingNonce works as expected (force a scenario where the pool needs to be taken into consideration) |
| 83 | + nTxsStep2 := 10 |
| 84 | + require.NoError(t, opsman.StopSequencer()) |
| 85 | + txsStep2 := make([]*types.Transaction, 0, nTxsStep2) |
| 86 | + for i := 0; i < nTxsStep2; i++ { |
| 87 | + tx := types.NewTransaction(nonceToBeUsedForNextTx, toAddress, amount, gasLimit, gasPrice, nil) |
| 88 | + txsStep2 = append(txsStep2, tx) |
| 89 | + nonceToBeUsedForNextTx += 1 |
| 90 | + } |
| 91 | + log.Infof("sending %d txs and waiting until added into the trusted sequencer pool") |
| 92 | + _, err = operations.ApplyL2Txs(ctx, txsStep2, auth, client, operations.PoolConfirmationLevel) |
| 93 | + require.NoError(t, err) |
| 94 | + actualNonce, err := client.PendingNonceAt(ctx, auth.From) |
| 95 | + require.NoError(t, err) |
| 96 | + require.Equal(t, nonceToBeUsedForNextTx, actualNonce) |
| 97 | + // Step 3 |
| 98 | + // - actions: start Sequencer and EthTxSender |
| 99 | + // - assert: all transactions get virtualized WITHOUT L2 reorgs |
| 100 | + require.NoError(t, opsman.StartSequencer()) |
| 101 | + require.NoError(t, opsman.StartEthTxSender()) |
| 102 | + lastL2BlockNumberStep1 := l2BlockNumbersStep1[len(l2BlockNumbersStep1)-1] |
| 103 | + lastL2BlockNumberStep2 := lastL2BlockNumberStep1.Add( |
| 104 | + lastL2BlockNumberStep1, |
| 105 | + big.NewInt(int64(nTxsStep2)), |
| 106 | + ) |
| 107 | + err = operations.WaitL2BlockToBeVirtualizedCustomRPC( |
| 108 | + lastL2BlockNumberStep2, 4*time.Minute, //nolint:gomnd |
| 109 | + operations.PermissionlessL2NetworkURL, |
| 110 | + ) |
| 111 | + require.NoError(t, err) |
| 112 | + sqlDB, err := db.NewSQLDB(db.Config{ |
| 113 | + User: testutils.GetEnv("PERMISSIONLESSPGUSER", "test_user"), |
| 114 | + Password: testutils.GetEnv("PERMISSIONLESSPGPASSWORD", "test_password"), |
| 115 | + Name: testutils.GetEnv("PERMISSIONLESSPGDATABASE", "state_db"), |
| 116 | + Host: testutils.GetEnv("PERMISSIONLESSPGHOST", "localhost"), |
| 117 | + Port: testutils.GetEnv("PERMISSIONLESSPGPORT", "5434"), |
| 118 | + EnableLog: true, |
| 119 | + MaxConns: 4, |
| 120 | + }) |
| 121 | + require.NoError(t, err) |
| 122 | + const isThereL2ReorgQuery = "SELECT COUNT(*) > 0 FROM state.trusted_reorg;" |
| 123 | + row := sqlDB.QueryRow(context.Background(), isThereL2ReorgQuery) |
| 124 | + isThereL2Reorg := true |
| 125 | + require.NoError(t, row.Scan(&isThereL2Reorg)) |
| 126 | + require.False(t, isThereL2Reorg) |
| 127 | +} |
0 commit comments