Skip to content

Commit

Permalink
Fix GetL2Hash function. Use tx L2 hash returned by the executor when …
Browse files Browse the repository at this point in the history
…storing L2Block. Logs changes (0xPolygonHermez#3134)

* fix l2 hash when to is nill

* get l2 hash from executor

* fix use tx L2 hash returned by the executor when storing L2Block. Logs changes

* fix linter

* add L2block tracking number to process tx logs

* fix ProcessBatchV2 log format

* fix UTs. set useMainExecGenerated to true

* set prover image to v4.0.0-RC30

* test setting useMainExecGenerated = false

* fix independent_test

* fix non-e2e tests

---------

Co-authored-by: Toni Ramírez <[email protected]>
  • Loading branch information
agnusmor and ToniRamirezM authored Jan 24, 2024
1 parent c3dddf7 commit 8e1e5e9
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 95 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:v4.0.0-RC29
image: hermeznetwork/zkevm-prover:v4.0.0-RC30
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
11 changes: 5 additions & 6 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
metrics.ProcessingTime(time.Since(start))
}()

log.Infof("processing tx %s, batchNumber: %d, l2Block: [%d], oldStateRoot: %s, L1InfoRootIndex: %d",
tx.HashStr, f.wipBatch.batchNumber, f.wipL2Block.trackingNum, f.wipBatch.imStateRoot, f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex)

batchRequest := state.ProcessRequest{
BatchNumber: f.wipBatch.batchNumber,
OldStateRoot: f.wipBatch.imStateRoot,
Expand Down Expand Up @@ -425,9 +428,6 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first

batchRequest.Transactions = append(batchRequest.Transactions, effectivePercentageAsDecodedHex...)

log.Infof("processing tx %s, wipBatch.BatchNumber: %d, batchNumber: %d, oldStateRoot: %s, L1InfoRootIndex: %d",
tx.HashStr, f.wipBatch.batchNumber, batchRequest.BatchNumber, batchRequest.OldStateRoot, f.wipL2Block.l1InfoTreeExitRoot.L1InfoTreeIndex)

batchResponse, err := f.stateIntf.ProcessBatchV2(ctx, batchRequest, false)

if err != nil && (errors.Is(err, runtime.ErrExecutorDBError) || errors.Is(err, runtime.ErrInvalidTxChangeL2BlockMinTimestamp)) {
Expand Down Expand Up @@ -463,9 +463,8 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
// Update imStateRoot
f.wipBatch.imStateRoot = batchResponse.NewStateRoot

log.Infof("processed tx %s. Batch.batchNumber: %d, batchNumber: %d, newStateRoot: %s, oldStateRoot: %s, used counters: %s",
tx.HashStr, f.wipBatch.batchNumber, batchRequest.BatchNumber, batchResponse.NewStateRoot.String(),
batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))
log.Infof("processed tx %s, batchNumber: %d, l2Block: [%d], newStateRoot: %s, oldStateRoot: %s, used counters: %s",
tx.HashStr, batchRequest.BatchNumber, f.wipL2Block.trackingNum, batchResponse.NewStateRoot.String(), batchRequest.OldStateRoot.String(), f.logZKCounters(batchResponse.UsedZkCounters))

return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type stateInterface interface {
FlushMerkleTree(ctx context.Context, newStateRoot common.Hash) error
GetStoredFlushID(ctx context.Context) (uint64, string, error)
GetForkIDByBatchNumber(batchNumber uint64) uint64
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error
GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*state.DSL2Block, error)
GetDSBatches(ctx context.Context, firstBatchNumber, lastBatchNumber uint64, readWIPBatch bool, dbTx pgx.Tx) ([]*state.DSBatch, error)
GetDSL2Blocks(ctx context.Context, firstBatchNumber, lastBatchNumber uint64, dbTx pgx.Tx) ([]*state.DSL2Block, error)
Expand Down
15 changes: 8 additions & 7 deletions sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (f *finalizer) storePendingL2Blocks(ctx context.Context) {

if err != nil {
// Dump L2Block info
f.logL2Block(l2Block)
f.dumpL2Block(l2Block)
f.Halt(ctx, fmt.Errorf("error storing L2 block %d [%d], error: %v", l2Block.batchResponse.BlockResponses[0].BlockNumber, l2Block.trackingNum, err))
}

Expand Down Expand Up @@ -556,23 +556,24 @@ func (f *finalizer) executeNewWIPL2Block(ctx context.Context) (*state.ProcessBat
return batchResponse, nil
}

func (f *finalizer) logL2Block(l2Block *L2Block) {
func (f *finalizer) dumpL2Block(l2Block *L2Block) {
var blockResp *state.ProcessBlockResponse
if l2Block.batchResponse != nil {
if len(l2Block.batchResponse.BlockResponses) > 0 {
blockResp = l2Block.batchResponse.BlockResponses[0]
}
}

txsLog := ""
if blockResp != nil {
log.Infof("DUMP L2 block %d [%d], Timestamp: %d, ParentHash: %s, Coinbase: %s, GER: %s, BlockHashL1: %s, GasUsed: %d, BlockInfoRoot: %s, BlockHash: %s",
blockResp.BlockNumber, l2Block.trackingNum, blockResp.Timestamp, blockResp.ParentHash, blockResp.Coinbase, blockResp.GlobalExitRoot, blockResp.BlockHashL1,
blockResp.GasUsed, blockResp.BlockInfoRoot, blockResp.BlockHash)

for i, txResp := range blockResp.TransactionResponses {
log.Infof(" tx[%d] Hash: %s, HashL2: %s, StateRoot: %s, Type: %d, GasLeft: %d, GasUsed: %d, GasRefund: %d, CreateAddress: %s, ChangesStateRoot: %v, EGP: %s, EGPPct: %d, HasGaspriceOpcode: %v, HasBalanceOpcode: %v",
txsLog += fmt.Sprintf(" tx[%d] Hash: %s, HashL2: %s, StateRoot: %s, Type: %d, GasLeft: %d, GasUsed: %d, GasRefund: %d, CreateAddress: %s, ChangesStateRoot: %v, EGP: %s, EGPPct: %d, HasGaspriceOpcode: %v, HasBalanceOpcode: %v\n",
i, txResp.TxHash, txResp.TxHashL2_V2, txResp.StateRoot, txResp.Type, txResp.GasLeft, txResp.GasUsed, txResp.GasRefunded, txResp.CreateAddress, txResp.ChangesStateRoot, txResp.EffectiveGasPrice,
txResp.EffectivePercentage, txResp.HasGaspriceOpcode, txResp.HasBalanceOpcode)
}

log.Infof("DUMP L2 block %d [%d], Timestamp: %d, ParentHash: %s, Coinbase: %s, GER: %s, BlockHashL1: %s, GasUsed: %d, BlockInfoRoot: %s, BlockHash: %s\n%s",
blockResp.BlockNumber, l2Block.trackingNum, blockResp.Timestamp, blockResp.ParentHash, blockResp.Coinbase, blockResp.GlobalExitRoot, blockResp.BlockHashL1,
blockResp.GasUsed, blockResp.BlockInfoRoot, blockResp.BlockHash, txsLog)
}
}
10 changes: 5 additions & 5 deletions sequencer/mock_state.go

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

2 changes: 1 addition & 1 deletion sequencer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (w *Worker) GetBestFittingTx(resources state.BatchResources) (*TxTracker, e
wg.Wait()

if foundAt != -1 {
log.Debugf("best fitting tx found: tx %s at index %d with gasPrice %d", tx.HashStr, foundAt, tx.GasPrice)
log.Debugf("best fitting tx %s found at index %d with gasPrice %d", tx.HashStr, foundAt, tx.GasPrice)
return tx, nil
} else {
return nil, ErrNoFittingTransaction
Expand Down
4 changes: 2 additions & 2 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (s *State) sendBatchRequestToExecutorV2(ctx context.Context, batchRequest *
batchRequest.SkipWriteBlockInfoRoot, batchRequest.ChainId, batchRequest.ForkId, batchRequest.ContextId, batchRequest.SkipVerifyL1InfoRoot, hex.EncodeToHex(batchRequest.ForcedBlockhashL1),
l1DataStr, hex.EncodeToHex(batchRequest.BatchL2Data))

log.Debugf("executor batchRequest: %s", batchRequestLog)
log.Debugf("executor batchRequest, %s", batchRequestLog)

now := time.Now()
batchResponse, err := s.executorClient.ProcessBatchV2(ctx, batchRequest)
Expand Down Expand Up @@ -316,7 +316,7 @@ func (s *State) sendBatchRequestToExecutorV2(ctx context.Context, batchRequest *
}

func processBatchResponseToString(batchResponse *executor.ProcessBatchResponseV2) string {
batchResponseLog := "executor batchResponse: NewStateRoot: %v, NewAccInputHash: %v, NewLocalExitRoot: %v, NewBatchNumber: %v, GasUsed: %v, FlushId: %v, StoredFlushId: %v, ProverId:%v, ForkId:%v, Error: %v\n"
batchResponseLog := "executor batchResponse, NewStateRoot: %v, NewAccInputHash: %v, NewLocalExitRoot: %v, NewBatchNumber: %v, GasUsed: %v, FlushId: %v, StoredFlushId: %v, ProverId:%v, ForkId:%v, Error: %v\n"
batchResponseLog = fmt.Sprintf(batchResponseLog, hex.EncodeToHex(batchResponse.NewStateRoot), hex.EncodeToHex(batchResponse.NewAccInputHash), hex.EncodeToHex(batchResponse.NewLocalExitRoot),
batchResponse.NewBatchNum, batchResponse.GasUsed, batchResponse.FlushId, batchResponse.StoredFlushId, batchResponse.ProverId, batchResponse.ForkId, batchResponse.Error)

Expand Down
1 change: 1 addition & 0 deletions state/convertersV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s *State) convertToProcessTransactionResponseV2(responses []*executor.Proc
isRomOOCError := false

results := make([]*ProcessTransactionResponse, 0, len(responses))

for _, response := range responses {
if response.Error != executor.RomError_ROM_ERROR_NO_ERROR {
isRomLevelError = true
Expand Down
16 changes: 12 additions & 4 deletions state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,20 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, m
l2Block := NewL2Block(header, []*types.Transaction{}, []*L2Header{}, receipts, &trie.StackTrie{})
l2Block.ReceivedAt = block.ReceivedAt

storeTxsEGPData := []StoreTxEGPData{}
for range l2Block.Transactions() {
storeTxsEGPData = append(storeTxsEGPData, StoreTxEGPData{EGPLog: nil, EffectivePercentage: MaxEffectivePercentage})
numTxs := len(l2Block.Transactions())
storeTxsEGPData := make([]StoreTxEGPData, numTxs)
txsL2Hash := make([]common.Hash, numTxs)
for i, txTmp := range l2Block.Transactions() {
storeTxsEGPData[i] = StoreTxEGPData{EGPLog: nil, EffectivePercentage: MaxEffectivePercentage}
aux := *txTmp
l2TxHash, err := GetL2Hash(aux)
if err != nil {
return common.Hash{}, err
}
txsL2Hash[i] = l2TxHash
}

err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, storeTxsEGPData, dbTx)
err = s.AddL2Block(ctx, batch.BatchNumber, l2Block, receipts, txsL2Hash, storeTxsEGPData, dbTx)
if err != nil {
return common.Hash{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type storage interface {
GetL2BlockTransactionCountByHash(ctx context.Context, blockHash common.Hash, dbTx pgx.Tx) (uint64, error)
GetL2BlockTransactionCountByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetTransactionEGPLogByHash(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*EffectiveGasPriceLog, error)
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsEGPData []StoreTxEGPData, dbTx pgx.Tx) error
AddL2Block(ctx context.Context, batchNumber uint64, l2Block *L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []StoreTxEGPData, dbTx pgx.Tx) error
GetLastVirtualizedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastConsolidatedL2BlockNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastVerifiedL2BlockNumberUntilL1Block(ctx context.Context, l1FinalizedBlockNumber uint64, dbTx pgx.Tx) (uint64, error)
Expand Down
21 changes: 11 additions & 10 deletions state/mocks/mock_storage.go

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

32 changes: 18 additions & 14 deletions state/pgstatestorage/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"github.com/0xPolygonHermez/zkevm-node/hex"
Expand Down Expand Up @@ -146,9 +147,9 @@ func (p *PostgresStorage) GetL2BlockTransactionCountByNumber(ctx context.Context
}

// AddL2Block adds a new L2 block to the State Store
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error {
func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2Block *state.L2Block, receipts []*types.Receipt, txsL2Hash []common.Hash, txsEGPData []state.StoreTxEGPData, dbTx pgx.Tx) error {
//TODO: Optmize this function using only one SQL (with several values) to insert all the txs, receips and logs
log.Debugf("[AddL2Block] adding l2 block: %v", l2Block.NumberU64())
log.Debugf("[AddL2Block] adding L2 block %d", l2Block.NumberU64())
start := time.Now()

e := p.getExecQuerier(dbTx)
Expand Down Expand Up @@ -182,6 +183,9 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
return err
}

logTxsL2Hash := ""
forkId := p.GetForkIDByBatchNumber(batchNumber)

if len(l2Block.Transactions()) > 0 {
txRows := [][]interface{}{}

Expand All @@ -206,21 +210,21 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
return err
}

aux := *tx
l2TxHash, err := state.GetL2Hash(aux)
if err != nil {
return err
}

log.Debugf("[AddL2Block] L2Block num: %d, tx[%d] = txHash: %s, txHashL2: %s", l2Block.Number().Uint64(), idx, tx.Hash().String(), l2TxHash.String())
logTxsL2Hash += fmt.Sprintf("tx[%d] txHash: %s, txHashL2: %s\n", idx, tx.Hash().String(), txsL2Hash[idx].String())

txRow := []interface{}{tx.Hash().String(), encoded, decoded, l2Block.Number().Uint64(), txsEGPData[idx].EffectivePercentage, egpLogBytes, l2TxHash.String()}
txRow := []interface{}{tx.Hash().String(), encoded, decoded, l2Block.Number().Uint64(), txsEGPData[idx].EffectivePercentage, egpLogBytes}
if forkId >= state.FORKID_ETROG {
txRow = append(txRow, txsL2Hash[idx].String())
}
txRows = append(txRows, txRow)
}

_, err := dbTx.CopyFrom(ctx, pgx.Identifier{"state", "transaction"},
[]string{"hash", "encoded", "decoded", "l2_block_num", "effective_percentage", "egp_log", "l2_hash"},
pgx.CopyFromRows(txRows))
txFields := []string{"hash", "encoded", "decoded", "l2_block_num", "effective_percentage", "egp_log"}
if forkId >= state.FORKID_ETROG {
txFields = append(txFields, "l2_hash")
}

_, err := dbTx.CopyFrom(ctx, pgx.Identifier{"state", "transaction"}, txFields, pgx.CopyFromRows(txRows))

if err != nil {
return err
Expand All @@ -237,7 +241,7 @@ func (p *PostgresStorage) AddL2Block(ctx context.Context, batchNumber uint64, l2
p.AddLogs(ctx, logs, dbTx)
}

log.Debugf("[AddL2Block] l2 block %v took %v to be added", l2Block.NumberU64(), time.Since(start))
log.Debugf("[AddL2Block] added L2 block %d, time: %v\n%s", l2Block.NumberU64(), time.Since(start), logTxsL2Hash)
return nil
}

Expand Down
Loading

0 comments on commit 8e1e5e9

Please sign in to comment.