Skip to content

Commit

Permalink
E2E / ETH TX test improvements (0xPolygonHermez#1001)
Browse files Browse the repository at this point in the history
* WIP -- ETHTX test improvements

* Merge from develop

* Merge from develop + changes

* Send 1000 TX instead of just one

* Teardown when ending

* Add test for checking each TX's Nonce against its own L2 Block Num

* Satisfy linter

* change naming

* Correct naming, s/batch/block/g
  • Loading branch information
KonradIT authored Aug 8, 2022
1 parent 44c9dec commit 6fa77e2
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 114 deletions.
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DOCKERCOMPOSEEXPLORERRPC := zkevm-explorer-json-rpc
DOCKERCOMPOSEZKPROVER := zkevm-prover
DOCKERCOMPOSEPERMISSIONLESSDB := zkevm-permissionless-db
DOCKERCOMPOSEPERMISSIONLESSNODE := zkevm-permissionless-node
DOCKERCOMPOSENODEAPPROVE := zkevm-approve

RUNDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEDB)
RUNSEQUENCER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEAPPSEQ)
Expand All @@ -29,6 +30,8 @@ RUNZKPROVER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEZKPROVER)
RUNPERMISSIONLESSDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSDB)
RUNPERMISSIONLESSNODE := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSNODE)

RUNAPPROVE := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSENODEAPPROVE)

RUN := $(DOCKERCOMPOSE) up -d

STOPDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEDB)
Expand All @@ -47,6 +50,8 @@ STOPZKPROVER := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEZKPROVER) && $(DOCKERCOMPOS
STOPPERMISSIONLESSDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSDB)
STOPPERMISSIONLESSNODE := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSNODE) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSNODE)

STOPAPPROVE := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSENODEAPPROVE) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSENODEAPPROVE)

STOP := $(DOCKERCOMPOSE) down --remove-orphans

VERSION := $(shell git describe --tags --always)
Expand Down Expand Up @@ -148,10 +153,10 @@ stop-db: ## Stops the node database

.PHONY: run-node
run-node: ## Runs the node
$(RUNSYNC)
$(RUNSEQUENCER)
$(RUNAGGREGATOR)
$(RUNJSONRPC)
$(RUNSYNC)

.PHONY: stop-node
stop-node: ## Stops the node
Expand Down Expand Up @@ -226,6 +231,14 @@ stop-permissionless: ## Stops the permissionless node
$(STOPPERMISSIONLESSNODE)
$(STOPPERMISSIONLESSDB)

.PHONY: run-approve-matic
run-approve-matic: ## Runs approve in node container
$(RUNAPPROVE)

.PHONY: stop-approve-matic
stop-approve-matic: ## Stops approve in node container
$(STOPAPPROVE)

.PHONY: init-network
init-network: ## Initializes the network
go run ./scripts/init_network/main.go .
Expand Down
4 changes: 2 additions & 2 deletions docs/ci/opsman.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ containers, the package exposes the function `StartComponent` which takes a
container name (as defined in the [docker compose file]) and a variadic parameter
with a set of condition functions to check when the container can be considered
as ready. So we can call it without any condition like:
```
```go
operations.StartComponent("my-container")
```
or adding readiness conditions as:
```
```go
operations.StartComponent("my-container", func() (done bool, err error){
// run some checks
return true, nil
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type stateInterface interface {
GetL2BlockHashesSince(ctx context.Context, since time.Time, dbTx pgx.Tx) ([]common.Hash, error)
DebugTransaction(ctx context.Context, transactionHash common.Hash, tracer string) (*runtime.ExecutionResult, error)
ProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, blockNumber uint64, dbTx pgx.Tx) *runtime.ExecutionResult
IsBatchConsolidated(ctx context.Context, batchNumber int, dbTx pgx.Tx) (bool, error)
IsBatchVirtualized(ctx context.Context, batchNumber int, dbTx pgx.Tx) (bool, error)
IsL2BlockConsolidated(ctx context.Context, blockNumber int, dbTx pgx.Tx) (bool, error)
IsL2BlockVirtualized(ctx context.Context, blockNumber int, dbTx pgx.Tx) (bool, error)
}

type storageInterface interface {
Expand Down
22 changes: 11 additions & 11 deletions jsonrpc/mock_state_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions jsonrpc/zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ func (h *ZKEVM) ConsolidatedBlockNumber() (interface{}, rpcError) {
})
}

// IsBatchConsolidated returns the consolidation status of a provided batch ID
func (h *ZKEVM) IsBatchConsolidated(batchNumber int) (interface{}, rpcError) {
// IsL2BlockConsolidated returns the consolidation status of a provided block number
func (h *ZKEVM) IsL2BlockConsolidated(blockNumber int) (interface{}, rpcError) {
return h.txMan.NewDbTxScope(h.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
isBatchConsolidated, err := h.state.IsBatchConsolidated(ctx, batchNumber, dbTx)
IsL2BlockConsolidated, err := h.state.IsL2BlockConsolidated(ctx, blockNumber, dbTx)
if err != nil {
const errorMessage = "failed to check if the batch is consolidated"
const errorMessage = "failed to check if the block is consolidated"
log.Errorf("%v:%v", errorMessage, err)
return nil, newRPCError(defaultErrorCode, errorMessage)
}

return isBatchConsolidated, nil
return IsL2BlockConsolidated, nil
})
}

// IsBatchVirtualized returns the virtualisation status of a provided batch ID
func (h *ZKEVM) IsBatchVirtualized(batchNumber int) (interface{}, rpcError) {
// IsL2BlockVirtualized returns the virtualisation status of a provided block number
func (h *ZKEVM) IsL2BlockVirtualized(blockNumber int) (interface{}, rpcError) {
return h.txMan.NewDbTxScope(h.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
isBatchVirtualized, err := h.state.IsBatchVirtualized(ctx, batchNumber, dbTx)
IsL2BlockVirtualized, err := h.state.IsL2BlockVirtualized(ctx, blockNumber, dbTx)
if err != nil {
const errorMessage = "failed to check if the batch is virtualized"
const errorMessage = "failed to check if the block is virtualized"
log.Errorf("%v:%v", errorMessage, err)
return nil, newRPCError(defaultErrorCode, errorMessage)
}

return isBatchVirtualized, nil
return IsL2BlockVirtualized, nil
})
}

Expand Down
24 changes: 12 additions & 12 deletions jsonrpc/zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestConsolidatedBlockNumber(t *testing.T) {
}
}

func TestIsBatchConsolidated(t *testing.T) {
func TestIsL2BlockConsolidated(t *testing.T) {
s, m, _ := newSequencerMockedServer(t)
defer s.Stop()

Expand All @@ -115,15 +115,15 @@ func TestIsBatchConsolidated(t *testing.T) {
Once()

m.State.
On("IsBatchConsolidated", context.Background(), 1, m.DbTx).
On("IsL2BlockConsolidated", context.Background(), 1, m.DbTx).
Return(true, nil).
Once()
},
},
{
Name: "Failed to query the consolidation status",
ExpectedResult: false,
ExpectedError: newRPCError(defaultErrorCode, "failed to check if the batch is consolidated"),
ExpectedError: newRPCError(defaultErrorCode, "failed to check if the block is consolidated"),
SetupMocks: func(m *mocks) {
m.DbTx.
On("Rollback", context.Background()).
Expand All @@ -136,8 +136,8 @@ func TestIsBatchConsolidated(t *testing.T) {
Once()

m.State.
On("IsBatchConsolidated", context.Background(), 1, m.DbTx).
Return(true, errors.New("failed to check if the batch is consolidated")).
On("IsL2BlockConsolidated", context.Background(), 1, m.DbTx).
Return(true, errors.New("failed to check if the block is consolidated")).
Once()
},
},
Expand All @@ -148,7 +148,7 @@ func TestIsBatchConsolidated(t *testing.T) {
tc := testCase
tc.SetupMocks(m)

res, err := s.JSONRPCCall("zkevm_isBatchConsolidated", 1)
res, err := s.JSONRPCCall("zkevm_isL2BlockConsolidated", 1)
require.NoError(t, err)

if res.Result != nil {
Expand All @@ -166,7 +166,7 @@ func TestIsBatchConsolidated(t *testing.T) {
}
}

func TestIsBatchVirtualized(t *testing.T) {
func TestIsL2BlockVirtualized(t *testing.T) {
s, m, _ := newSequencerMockedServer(t)
defer s.Stop()

Expand All @@ -193,15 +193,15 @@ func TestIsBatchVirtualized(t *testing.T) {
Once()

m.State.
On("IsBatchVirtualized", context.Background(), 1, m.DbTx).
On("IsL2BlockVirtualized", context.Background(), 1, m.DbTx).
Return(true, nil).
Once()
},
},
{
Name: "Failed to query the virtualization status",
ExpectedResult: false,
ExpectedError: newRPCError(defaultErrorCode, "failed to check if the batch is virtualized"),
ExpectedError: newRPCError(defaultErrorCode, "failed to check if the block is virtualized"),
SetupMocks: func(m *mocks) {
m.DbTx.
On("Rollback", context.Background()).
Expand All @@ -214,8 +214,8 @@ func TestIsBatchVirtualized(t *testing.T) {
Once()

m.State.
On("IsBatchVirtualized", context.Background(), 1, m.DbTx).
Return(true, errors.New("failed to check if the batch is virtualized")).
On("IsL2BlockVirtualized", context.Background(), 1, m.DbTx).
Return(true, errors.New("failed to check if the block is virtualized")).
Once()
},
},
Expand All @@ -226,7 +226,7 @@ func TestIsBatchVirtualized(t *testing.T) {
tc := testCase
tc.SetupMocks(m)

res, err := s.JSONRPCCall("zkevm_isBatchVirtualized", 1)
res, err := s.JSONRPCCall("zkevm_isL2BlockVirtualized", 1)
require.NoError(t, err)

if res.Result != nil {
Expand Down
46 changes: 27 additions & 19 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ const (
addLogSQL = "INSERT INTO state.log (transaction_hash, log_index, transaction_index, address, data, topic0, topic1, topic2, topic3) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"
getBatchNumByBlockNum = "SELECT batch_num FROM state.virtual_batch WHERE block_num = $1 ORDER BY batch_num ASC LIMIT 1"
getTxsHashesBeforeBatchNum = "SELECT hash FROM state.transaction JOIN state.l2block ON state.transaction.l2_block_num = state.l2block.block_num AND state.l2block.batch_num <= $1"
isBatchVirtualized = "SELECT l2b.block_num FROM state.l2block l2b INNER JOIN state.virtual_batch vb ON vb.batch_num = l2b.batch_num WHERE l2b.block_num = $1"
isBatchConsolidated = "SELECT l2b.block_num FROM state.l2block l2b INNER JOIN state.verified_batch vb ON vb.batch_num = l2b.batch_num WHERE l2b.block_num = $1"
isL2BlockVirtualized = "SELECT l2b.block_num FROM state.l2block l2b INNER JOIN state.virtual_batch vb ON vb.batch_num = l2b.batch_num WHERE l2b.block_num = $1"
isL2BlockConsolidated = "SELECT l2b.block_num FROM state.l2block l2b INNER JOIN state.verified_batch vb ON vb.batch_num = l2b.batch_num WHERE l2b.block_num = $1"
)

// PostgresStorage implements the Storage interface
Expand Down Expand Up @@ -1068,7 +1068,7 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
func (p *PostgresStorage) GetLastConsolidatedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error) {
var lastConsolidatedBlockNumber uint64
q := p.getExecQuerier(dbTx)
err := q.QueryRow(ctx, getLastConsolidatedBlockNumberSQL, common.Hash{}).Scan(&lastConsolidatedBlockNumber)
err := q.QueryRow(ctx, getLastConsolidatedBlockNumberSQL).Scan(&lastConsolidatedBlockNumber)

if errors.Is(err, pgx.ErrNoRows) {
return 0, ErrNotFound
Expand Down Expand Up @@ -1331,30 +1331,38 @@ func (p *PostgresStorage) GetL2BlockHashesSince(ctx context.Context, since time.
return blockHashes, nil
}

// IsBatchConsolidated checks if the batch ID is consolidated
func (p *PostgresStorage) IsBatchConsolidated(ctx context.Context, batchNumber int, dbTx pgx.Tx) (bool, error) {
// IsL2BlockConsolidated checks if the block ID is consolidated
func (p *PostgresStorage) IsL2BlockConsolidated(ctx context.Context, blockNumber int, dbTx pgx.Tx) (bool, error) {
q := p.getExecQuerier(dbTx)
_, err := q.Query(ctx, isBatchConsolidated, batchNumber)

if errors.Is(err, pgx.ErrNoRows) {
return false, ErrNotFound
} else if err != nil {
rows, err := q.Query(ctx, isL2BlockConsolidated, blockNumber)
if err != nil {
return false, err
}
return true, nil
defer rows.Close()
isConsolidated := rows.Next()

if rows.Err() != nil {
return false, rows.Err()
}

return isConsolidated, nil
}

// IsBatchVirtualized checks if the batch ID is virtualized
func (p *PostgresStorage) IsBatchVirtualized(ctx context.Context, batchNumber int, dbTx pgx.Tx) (bool, error) {
// IsL2BlockVirtualized checks if the block ID is virtualized
func (p *PostgresStorage) IsL2BlockVirtualized(ctx context.Context, blockNumber int, dbTx pgx.Tx) (bool, error) {
q := p.getExecQuerier(dbTx)
_, err := q.Query(ctx, isBatchVirtualized, batchNumber)

if errors.Is(err, pgx.ErrNoRows) {
return false, ErrNotFound
} else if err != nil {
rows, err := q.Query(ctx, isL2BlockVirtualized, blockNumber)
if err != nil {
return false, err
}
return true, nil
defer rows.Close()
isVirtualized := rows.Next()

if rows.Err() != nil {
return false, rows.Err()
}

return isVirtualized, nil
}

// GetLogs returns the logs that match the filter
Expand Down
Loading

0 comments on commit 6fa77e2

Please sign in to comment.