github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/e2e/permissionlessrpc_test.go (about) 1 package e2e 2 3 import ( 4 "context" 5 "math/big" 6 "testing" 7 "time" 8 9 "github.com/0xPolygon/supernets2-node/db" 10 "github.com/0xPolygon/supernets2-node/log" 11 "github.com/0xPolygon/supernets2-node/test/operations" 12 "github.com/0xPolygon/supernets2-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 node 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 require.NoError(t, opsman.StopSequenceSender()) 86 txsStep2 := make([]*types.Transaction, 0, nTxsStep2) 87 for i := 0; i < nTxsStep2; i++ { 88 tx := types.NewTransaction(nonceToBeUsedForNextTx, toAddress, amount, gasLimit, gasPrice, nil) 89 txsStep2 = append(txsStep2, tx) 90 nonceToBeUsedForNextTx += 1 91 } 92 log.Infof("sending %d txs and waiting until added into the trusted sequencer pool") 93 _, err = operations.ApplyL2Txs(ctx, txsStep2, auth, client, operations.PoolConfirmationLevel) 94 require.NoError(t, err) 95 actualNonce, err := client.PendingNonceAt(ctx, auth.From) 96 require.NoError(t, err) 97 require.Equal(t, nonceToBeUsedForNextTx, actualNonce) 98 // Step 3 99 // - actions: start Sequencer and EthTxSender 100 // - assert: all transactions get virtualized WITHOUT L2 reorgs 101 require.NoError(t, opsman.StartSequencer()) 102 require.NoError(t, opsman.StartEthTxSender()) 103 require.NoError(t, opsman.StartSequenceSender()) 104 105 lastL2BlockNumberStep1 := l2BlockNumbersStep1[len(l2BlockNumbersStep1)-1] 106 lastL2BlockNumberStep2 := lastL2BlockNumberStep1.Add( 107 lastL2BlockNumberStep1, 108 big.NewInt(int64(nTxsStep2)), 109 ) 110 err = operations.WaitL2BlockToBeVirtualizedCustomRPC( 111 lastL2BlockNumberStep2, 4*time.Minute, //nolint:gomnd 112 operations.PermissionlessL2NetworkURL, 113 ) 114 require.NoError(t, err) 115 sqlDB, err := db.NewSQLDB(db.Config{ 116 User: testutils.GetEnv("PERMISSIONLESSPGUSER", "test_user"), 117 Password: testutils.GetEnv("PERMISSIONLESSPGPASSWORD", "test_password"), 118 Name: testutils.GetEnv("PERMISSIONLESSPGDATABASE", "state_db"), 119 Host: testutils.GetEnv("PERMISSIONLESSPGHOST", "localhost"), 120 Port: testutils.GetEnv("PERMISSIONLESSPGPORT", "5434"), 121 EnableLog: true, 122 MaxConns: 4, 123 }) 124 require.NoError(t, err) 125 const isThereL2ReorgQuery = "SELECT COUNT(*) > 0 FROM state.trusted_reorg;" 126 row := sqlDB.QueryRow(context.Background(), isThereL2ReorgQuery) 127 isThereL2Reorg := true 128 require.NoError(t, row.Scan(&isThereL2Reorg)) 129 require.False(t, isThereL2Reorg) 130 }