Skip to content

Commit ce891c3

Browse files
Test/broadcast (0xPolygon#1850)
Add test for trusted state consumed through permissionless node
1 parent a59f01f commit ce891c3

10 files changed

Lines changed: 265 additions & 20 deletions

File tree

.github/workflows/test-e2e.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
matrix:
2121
go-version: [ 1.18.x ]
2222
goarch: [ "amd64" ]
23-
e2e-group: [ 1, 2, 3, 4, 5 ]
23+
e2e-group: [ 1, 2, 3, 4, 5, 6 ]
2424
runs-on: ubuntu-latest
2525
steps:
2626
- name: Checkout code
@@ -59,7 +59,7 @@ jobs:
5959
matrix:
6060
go-version: [ 1.18.x ]
6161
goarch: [ "amd64" ]
62-
e2e-group: [ 1, 2, 3, 4, 5 ]
62+
e2e-group: [ 1, 2, 3, 4, 5, 6 ]
6363
runs-on: ubuntu-latest
6464
steps:
6565
- name: Fork based /ok-to-test checkout
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../test/e2e/permissionlessrpc_test.go

test/Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ test-e2e-group-5: stop ## Runs group 5 e2e tests checking race conditions
146146
docker logs $(DOCKERCOMPOSEZKPROVER)
147147
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group5/...
148148

149+
.PHONY: test-e2e-group-6
150+
test-e2e-group-6: stop ## Runs group 6 e2e tests checking race conditions
151+
$(RUNSTATEDB)
152+
$(RUNPOOLDB)
153+
sleep 5
154+
$(RUNZKPROVER)
155+
docker ps -a
156+
docker logs $(DOCKERCOMPOSEZKPROVER)
157+
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group6/...
158+
149159
.PHONY: benchmark-sequencer-eth-transfers
150160
benchmark-sequencer-eth-transfers: stop
151161
$(RUNL1NETWORK)
@@ -201,6 +211,7 @@ run-node: ## Runs the node
201211
$(RUNL2GASPRICER)
202212
$(RUNAGGREGATOR)
203213
$(RUNJSONRPC)
214+
$(RUNBROADCAST)
204215

205216
.PHONY: stop-node
206217
stop-node: ## Stops the node
@@ -210,6 +221,7 @@ stop-node: ## Stops the node
210221
$(STOPAGGREGATOR)
211222
$(STOPSYNC)
212223
$(STOPETHTXMANAGER)
224+
$(STOPBROADCAST)
213225

214226
.PHONY: run-network
215227
run-network: ## Runs the l1 network
@@ -340,18 +352,15 @@ stop-grafana: ## Stops the grafana service
340352
$(STOPGRAFANA)
341353

342354
.PHONY: run-permissionless
343-
run-permissionless: ## Runs the permissionless node
355+
run-permissionless: run-node ## Runs the trusted and permissionless node
344356
$(RUNPERMISSIONLESSDB)
345-
$(RUNL1NETWORK)
346-
$(RUNBROADCAST)
347357
sleep 1
348358
$(RUNPERMISSIONLESSZKPROVER)
349359
$(RUNPERMISSIONLESSNODE)
350360

351361
.PHONY: stop-permissionless
352-
stop-permissionless: ## Stops the permissionless node
362+
stop-permissionless: stop-node## Stops the permissionless node
353363
$(STOPPERMISSIONLESSNODE)
354-
$(STOPBROADCAST)
355364
$(STOPPERMISSIONLESSZKPROVER)
356365
$(STOPPERMISSIONLESSDB)
357366

test/e2e/ethtransfer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ func TestEthTransfer(t *testing.T) {
6767
txs = append(txs, tx)
6868
}
6969

70-
_, err = operations.ApplyL2Txs(ctx, txs, auth, client)
70+
_, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel)
7171
require.NoError(t, err)
7272
}

test/e2e/forcedbatches_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestForcedBatches(t *testing.T) {
8080
var l2BlockNumbers []*big.Int
8181
go func() {
8282
defer wgNormalL2Transfers.Done()
83-
l2BlockNumbers, err = operations.ApplyL2Txs(ctx, txs, auth, client)
83+
l2BlockNumbers, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel)
8484
require.NoError(t, err)
8585
}()
8686

test/e2e/permissionlessrpc_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}

test/e2e/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestStateTransition(t *testing.T) {
6969
}
7070

7171
// send transactions
72-
_, err = operations.ApplyL2Txs(ctx, txs, nil, nil)
72+
_, err = operations.ApplyL2Txs(ctx, txs, nil, nil, operations.VerifiedConfirmationLevel)
7373
require.NoError(t, err)
7474

7575
st := opsman.State()

0 commit comments

Comments
 (0)