Skip to content

Commit

Permalink
Test/broadcast (0xPolygonHermez#1850)
Browse files Browse the repository at this point in the history
Add test for trusted state consumed through permissionless node
  • Loading branch information
arnaubennassar authored Mar 21, 2023
1 parent a59f01f commit ce891c3
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
matrix:
go-version: [ 1.18.x ]
goarch: [ "amd64" ]
e2e-group: [ 1, 2, 3, 4, 5 ]
e2e-group: [ 1, 2, 3, 4, 5, 6 ]
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
matrix:
go-version: [ 1.18.x ]
goarch: [ "amd64" ]
e2e-group: [ 1, 2, 3, 4, 5 ]
e2e-group: [ 1, 2, 3, 4, 5, 6 ]
runs-on: ubuntu-latest
steps:
- name: Fork based /ok-to-test checkout
Expand Down
1 change: 1 addition & 0 deletions ci/e2e-group6/permissionlessrpc_test.go
19 changes: 14 additions & 5 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ test-e2e-group-5: stop ## Runs group 5 e2e tests checking race conditions
docker logs $(DOCKERCOMPOSEZKPROVER)
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group5/...

.PHONY: test-e2e-group-6
test-e2e-group-6: stop ## Runs group 6 e2e tests checking race conditions
$(RUNSTATEDB)
$(RUNPOOLDB)
sleep 5
$(RUNZKPROVER)
docker ps -a
docker logs $(DOCKERCOMPOSEZKPROVER)
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group6/...

.PHONY: benchmark-sequencer-eth-transfers
benchmark-sequencer-eth-transfers: stop
$(RUNL1NETWORK)
Expand Down Expand Up @@ -201,6 +211,7 @@ run-node: ## Runs the node
$(RUNL2GASPRICER)
$(RUNAGGREGATOR)
$(RUNJSONRPC)
$(RUNBROADCAST)

.PHONY: stop-node
stop-node: ## Stops the node
Expand All @@ -210,6 +221,7 @@ stop-node: ## Stops the node
$(STOPAGGREGATOR)
$(STOPSYNC)
$(STOPETHTXMANAGER)
$(STOPBROADCAST)

.PHONY: run-network
run-network: ## Runs the l1 network
Expand Down Expand Up @@ -340,18 +352,15 @@ stop-grafana: ## Stops the grafana service
$(STOPGRAFANA)

.PHONY: run-permissionless
run-permissionless: ## Runs the permissionless node
run-permissionless: run-node ## Runs the trusted and permissionless node
$(RUNPERMISSIONLESSDB)
$(RUNL1NETWORK)
$(RUNBROADCAST)
sleep 1
$(RUNPERMISSIONLESSZKPROVER)
$(RUNPERMISSIONLESSNODE)

.PHONY: stop-permissionless
stop-permissionless: ## Stops the permissionless node
stop-permissionless: stop-node## Stops the permissionless node
$(STOPPERMISSIONLESSNODE)
$(STOPBROADCAST)
$(STOPPERMISSIONLESSZKPROVER)
$(STOPPERMISSIONLESSDB)

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ethtransfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func TestEthTransfer(t *testing.T) {
txs = append(txs, tx)
}

_, err = operations.ApplyL2Txs(ctx, txs, auth, client)
_, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion test/e2e/forcedbatches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestForcedBatches(t *testing.T) {
var l2BlockNumbers []*big.Int
go func() {
defer wgNormalL2Transfers.Done()
l2BlockNumbers, err = operations.ApplyL2Txs(ctx, txs, auth, client)
l2BlockNumbers, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel)
require.NoError(t, err)
}()

Expand Down
127 changes: 127 additions & 0 deletions test/e2e/permissionlessrpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package e2e

import (
"context"
"math/big"
"testing"
"time"

"github.com/0xPolygonHermez/zkevm-node/db"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/0xPolygonHermez/zkevm-node/test/testutils"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

func TestPermissionlessJRPC(t *testing.T) {
// Initial setup:
// - permissionless RPC + Sync
// - trusted node with everything minus EthTxMan (to prevent the trusted state from being virtualized)
if testing.Short() {
t.Skip()
}
ctx := context.Background()
defer func() { require.NoError(t, operations.TeardownPermissionless()) }()
err := operations.Teardown()
require.NoError(t, err)
opsCfg := operations.GetDefaultOperationsConfig()
opsCfg.State.MaxCumulativeGasUsed = 80000000000
opsman, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
require.NoError(t, opsman.SetupWithPermissionless())
require.NoError(t, opsman.StopEthTxSender())
time.Sleep(5 * time.Second)

// Step 1:
// - actions: send nTxsStep1 transactions to the trusted sequencer through the permissionless sequencer
// first transaction gets the current nonce. The others are generated
// - assert: transactions are properly relayed, added in to the trusted state and broadcasted to the permissionless ndoe

nTxsStep1 := 10
// Load account with balance on local genesis
auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL2ChainID)
require.NoError(t, err)
// Load eth client (permissionless RPC)
client, err := ethclient.Dial(operations.PermissionlessL2NetworkURL)
require.NoError(t, err)
// Send txs
amount := big.NewInt(10000)
toAddress := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")
senderBalance, err := client.BalanceAt(ctx, auth.From, nil)
require.NoError(t, err)
nonceToBeUsedForNextTx, err := client.PendingNonceAt(ctx, auth.From)
require.NoError(t, err)

log.Infof("Receiver Addr: %v", toAddress.String())
log.Infof("Sender Addr: %v", auth.From.String())
log.Infof("Sender Balance: %v", senderBalance.String())
log.Infof("Sender Nonce: %v", nonceToBeUsedForNextTx)

gasLimit, err := client.EstimateGas(ctx, ethereum.CallMsg{From: auth.From, To: &toAddress, Value: amount})
require.NoError(t, err)

gasPrice, err := client.SuggestGasPrice(ctx)
require.NoError(t, err)

txsStep1 := make([]*types.Transaction, 0, nTxsStep1)
for i := 0; i < nTxsStep1; i++ {
tx := types.NewTransaction(nonceToBeUsedForNextTx, toAddress, amount, gasLimit, gasPrice, nil)
txsStep1 = append(txsStep1, tx)
nonceToBeUsedForNextTx += 1
}
log.Infof("sending %d txs and waiting until added in the permissionless RPC trusted state")
l2BlockNumbersStep1, err := operations.ApplyL2Txs(ctx, txsStep1, auth, client, operations.TrustedConfirmationLevel)
require.NoError(t, err)

// Step 2
// - actions: stop the sequencer and send nTxsStep2 transactions, then use the getPendingNonce, and send tx with the resulting nonce
// - assert: pendingNonce works as expected (force a scenario where the pool needs to be taken into consideration)
nTxsStep2 := 10
require.NoError(t, opsman.StopSequencer())
txsStep2 := make([]*types.Transaction, 0, nTxsStep2)
for i := 0; i < nTxsStep2; i++ {
tx := types.NewTransaction(nonceToBeUsedForNextTx, toAddress, amount, gasLimit, gasPrice, nil)
txsStep2 = append(txsStep2, tx)
nonceToBeUsedForNextTx += 1
}
log.Infof("sending %d txs and waiting until added into the trusted sequencer pool")
_, err = operations.ApplyL2Txs(ctx, txsStep2, auth, client, operations.PoolConfirmationLevel)
require.NoError(t, err)
actualNonce, err := client.PendingNonceAt(ctx, auth.From)
require.NoError(t, err)
require.Equal(t, nonceToBeUsedForNextTx, actualNonce)
// Step 3
// - actions: start Sequencer and EthTxSender
// - assert: all transactions get virtualized WITHOUT L2 reorgs
require.NoError(t, opsman.StartSequencer())
require.NoError(t, opsman.StartEthTxSender())
lastL2BlockNumberStep1 := l2BlockNumbersStep1[len(l2BlockNumbersStep1)-1]
lastL2BlockNumberStep2 := lastL2BlockNumberStep1.Add(
lastL2BlockNumberStep1,
big.NewInt(int64(nTxsStep2)),
)
err = operations.WaitL2BlockToBeVirtualizedCustomRPC(
lastL2BlockNumberStep2, 4*time.Minute, //nolint:gomnd
operations.PermissionlessL2NetworkURL,
)
require.NoError(t, err)
sqlDB, err := db.NewSQLDB(db.Config{
User: testutils.GetEnv("PERMISSIONLESSPGUSER", "test_user"),
Password: testutils.GetEnv("PERMISSIONLESSPGPASSWORD", "test_password"),
Name: testutils.GetEnv("PERMISSIONLESSPGDATABASE", "state_db"),
Host: testutils.GetEnv("PERMISSIONLESSPGHOST", "localhost"),
Port: testutils.GetEnv("PERMISSIONLESSPGPORT", "5434"),
EnableLog: true,
MaxConns: 4,
})
require.NoError(t, err)
const isThereL2ReorgQuery = "SELECT COUNT(*) > 0 FROM state.trusted_reorg;"
row := sqlDB.QueryRow(context.Background(), isThereL2ReorgQuery)
isThereL2Reorg := true
require.NoError(t, row.Scan(&isThereL2Reorg))
require.False(t, isThereL2Reorg)
}
2 changes: 1 addition & 1 deletion test/e2e/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestStateTransition(t *testing.T) {
}

// send transactions
_, err = operations.ApplyL2Txs(ctx, txs, nil, nil)
_, err = operations.ApplyL2Txs(ctx, txs, nil, nil, operations.VerifiedConfirmationLevel)
require.NoError(t, err)

st := opsman.State()
Expand Down
Loading

0 comments on commit ce891c3

Please sign in to comment.