Skip to content

Commit

Permalink
Initial implementation of Executor mock (0xPolygonHermez#1010)
Browse files Browse the repository at this point in the history
* Initial implementation of executor mock tool

* configure executor port

* extend mock test and configure state to connect to executor mock

* add missing counters

* renamed FindValue -> FindSMTValue; take into account executor-related fields in container creation test; add FindProcessBatchResponse test

* renamed FindValue -> FindSMTValue; add FindProcessBatchResponse method

* renamed FindValue -> FindSMTValue

* implement ProcessBatch method in executor service mock

* changes to process batch from testvector data

* removed uneeded elements in test vector

* Do not check ProcessAndStoreClosedBatch and link issue

* only left logs emited by the current tx

* update TODOs
  • Loading branch information
fgimenez authored Aug 10, 2022
1 parent d0c9f09 commit 0269e36
Show file tree
Hide file tree
Showing 8 changed files with 107,887 additions and 21 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,11 @@ services:
image: hermeznetwork/zkprover-mock:latest
ports:
- 43061:43061 # MT
- 43071:43071 # Executor
volumes:
- ./test/vectors/src:/app/testvectors
command: >
/app/zkprover-mock server --statedb-port 43061 --test-vector-path /app/testvectors
/app/zkprover-mock server --statedb-port 43061 --executor-port 43071 --test-vector-path /app/testvectors
zkevm-approve:
container_name: zkevm-approve
Expand Down
14 changes: 7 additions & 7 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (p *PostgresStorage) GetLastNBatches(ctx context.Context, numBatches uint,
// GetLastNBatchesByL2BlockNumber returns the last numBatches batches along with the l2 block state root by l2BlockNumber
func (p *PostgresStorage) GetLastNBatchesByL2BlockNumber(ctx context.Context, l2BlockNumber uint64, numBatches uint, dbTx pgx.Tx) ([]*Batch, common.Hash, error) {
const getLastNBatchesByBlockNumberSQL = `
SELECT b.batch_num, b.global_exit_root, b.local_exit_root, b.state_root, b.timestamp, b.coinbase, b.raw_txs_data, l2.header->>'stateRoot' as l2_block_state_root
SELECT b.batch_num, b.global_exit_root, b.local_exit_root, b.state_root, b.timestamp, b.coinbase, b.raw_txs_data, l2.header->>'stateRoot' as l2_block_state_root
FROM state.batch b, state.l2block l2
WHERE l2.block_num = $1 AND b.batch_num <= l2.batch_num
ORDER BY b.batch_num DESC LIMIT $2`
Expand Down Expand Up @@ -516,17 +516,17 @@ func (p *PostgresStorage) GetBatchByNumber(ctx context.Context, batchNumber uint
// GetVirtualBatchByNumber gets batch from batch table that exists on virtual batch
func (p *PostgresStorage) GetVirtualBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*Batch, error) {
const query = `
SELECT
SELECT
batch_num,
global_exit_root,
local_exit_root,
state_root,
timestamp,
timestamp,
coinbase,
raw_txs_data
FROM
state.batch
WHERE
FROM
state.batch
WHERE
batch_num = $1 AND
EXISTS (SELECT batch_num FROM state.virtual_batch WHERE batch_num = $1)
`
Expand Down Expand Up @@ -1371,7 +1371,7 @@ func (p *PostgresStorage) IsL2BlockVirtualized(ctx context.Context, blockNumber
// GetLogs returns the logs that match the filter
func (p *PostgresStorage) GetLogs(ctx context.Context, fromBlock uint64, toBlock uint64, addresses []common.Address, topics [][]common.Hash, blockHash *common.Hash, since *time.Time, dbTx pgx.Tx) ([]*types.Log, error) {
const getLogsByBlockHashSQL = `
SELECT t.l2_block_num, b.block_hash, l.tx_hash, l.log_index, l.address, l.data, l.topic0, l.topic1, l.topic2, l.topic3
SELECT t.l2_block_num, b.block_hash, l.tx_hash, l.log_index, l.address, l.data, l.topic0, l.topic1, l.topic2, l.topic3
FROM state.log l
INNER JOIN state.transaction t ON t.hash = l.tx_hash
INNER JOIN state.l2block b ON b.block_num = t.l2_block_num
Expand Down
45 changes: 41 additions & 4 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,14 +1395,28 @@ func TestGenesisNewLeafType(t *testing.T) {
require.Equal(t, "49461512068930131501252998918674096186707801477301326632372959001738876161218", new(big.Int).SetBytes(stateRoot).String())
}

func TestGenesisFromMock(t *testing.T) {
mtDBServiceClientBack := mtDBServiceClient
func TestFromMock(t *testing.T) {
executorClientBack := executorClient

executorServerConfig := executor.Config{URI: "127.0.0.1:43071"}
var executorCancel context.CancelFunc
executorClient, executorClientConn, executorCancel = executor.NewExecutorClient(ctx, executorServerConfig)
log.Infof("executorClientConn state: %s", executorClientConn.GetState().String())

testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree)

defer func() {
executorCancel()
executorClientConn.Close()
executorClient = executorClientBack
testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree)
}()

mtDBServiceClientBack := mtDBServiceClient
mtDBServerConfig := merkletree.Config{URI: "127.0.0.1:43061"}
var mtDBCancel context.CancelFunc
mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig)
s := mtDBClientConn.GetState()
log.Infof("stateDbClientConn state: %s", s.String())
log.Infof("stateDbClientConn state: %s", mtDBClientConn.GetState().String())

stateTree = merkletree.NewStateTree(mtDBServiceClient)
testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree)
Expand Down Expand Up @@ -1513,6 +1527,29 @@ func TestGenesisFromMock(t *testing.T) {
require.Equal(t, expectedValue, actualValue)
}
}

processCtx := state.ProcessingContext{
BatchNumber: tv.Traces.NumBatch,
Coinbase: common.HexToAddress(tv.Traces.SequencerAddr),
Timestamp: time.Unix(int64(tv.Traces.Timestamp), 0),
GlobalExitRoot: common.HexToHash(tv.GlobalExitRoot),
}

if strings.HasPrefix(tv.BatchL2Data, "0x") { // nolint
tv.BatchL2Data = tv.BatchL2Data[2:]
}
dbTx, err = testState.BeginStateTransaction(ctx)
require.NoError(t, err)

err = testState.ProcessAndStoreClosedBatch(ctx, processCtx, common.Hex2Bytes(tv.BatchL2Data), dbTx) // nolint:ineffassign,staticcheck
// TODO: actually check for nil err in ProcessAndStoreClosedBatch return value,
// currently blocked by the issue about the mismatched tx hashes described here
// https://github.com/0xPolygonHermez/zkevm-node/issues/1033
// require.NoError(t, err)

// TODO: currently the db tx is marked as invalid after the first error, once
// testState.ProcessAndStoreClosedBatch works properly we should make assertions
// about the database contents: batches, blocksL2, logs, receipts, ....
}

func TestExecutorUnsignedTransactions(t *testing.T) {
Expand Down
Loading

0 comments on commit 0269e36

Please sign in to comment.