From 9aca2ce7edd02013122c3fc5c14e0ae590424228 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Wed, 9 Oct 2024 16:52:40 +0300 Subject: [PATCH 1/9] feat: change endpoints to return finished blocks --- cmd/rpcdaemon/commands/debug_api.go | 2 +- cmd/rpcdaemon/commands/erigon_receipts.go | 6 +-- cmd/rpcdaemon/commands/erigon_system.go | 11 ++--- cmd/rpcdaemon/commands/eth_api.go | 3 ++ cmd/rpcdaemon/commands/eth_block.go | 8 ---- cmd/rpcdaemon/commands/eth_block_zkevm.go | 8 ++-- cmd/rpcdaemon/commands/eth_call.go | 2 +- cmd/rpcdaemon/commands/eth_receipts.go | 12 +++--- cmd/rpcdaemon/commands/eth_system.go | 6 ++- .../commands/otterscan_contract_creator.go | 2 +- .../commands/trace_filtering_zkevm.go | 10 ++--- cmd/rpcdaemon/commands/tracing.go | 2 +- cmd/rpcdaemon/commands/tracing_zkevm.go | 4 +- cmd/rpcdaemon/commands/zkevm_api.go | 17 +++----- eth/backend.go | 30 ++++++------- turbo/rpchelper/helper.go | 43 +++++++++++++------ turbo/rpchelper/helper_zkevm.go | 7 +-- turbo/rpchelper/rpc_block.go | 17 +++++--- 18 files changed, 103 insertions(+), 87 deletions(-) diff --git a/cmd/rpcdaemon/commands/debug_api.go b/cmd/rpcdaemon/commands/debug_api.go index 6b3b2e5e28e..fa63ba9ba56 100644 --- a/cmd/rpcdaemon/commands/debug_api.go +++ b/cmd/rpcdaemon/commands/debug_api.go @@ -114,7 +114,7 @@ func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash if number == rpc.LatestBlockNumber { var err error - blockNumber, err = stages.GetStageProgress(tx, stages.Execution) + blockNumber, err = stages.GetStageProgress(tx, stages.Finish) if err != nil { return state.IteratorDump{}, fmt.Errorf("last block has not found: %w", err) } diff --git a/cmd/rpcdaemon/commands/erigon_receipts.go b/cmd/rpcdaemon/commands/erigon_receipts.go index 168bfd8a272..ad395d08f0b 100644 --- a/cmd/rpcdaemon/commands/erigon_receipts.go +++ b/cmd/rpcdaemon/commands/erigon_receipts.go @@ -72,7 +72,7 @@ func (api *ErigonImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) end = *number } else { // Convert the RPC block numbers into internal representations - latest, err := rpchelper.GetLatestBlockNumber(tx) + latest, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err } @@ -227,9 +227,7 @@ func (api *ErigonImpl) GetLatestLogs(ctx context.Context, crit filters.FilterCri return nil, fmt.Errorf("negative value for ToBlock: %v", crit.ToBlock) } } else { - latest, err = rpchelper.GetLatestBlockNumber(tx) - //to fetch latest - latest += 1 + latest, err = rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/erigon_system.go b/cmd/rpcdaemon/commands/erigon_system.go index 172f43a2f12..47d22068fb7 100644 --- a/cmd/rpcdaemon/commands/erigon_system.go +++ b/cmd/rpcdaemon/commands/erigon_system.go @@ -2,6 +2,7 @@ package commands import ( "context" + "fmt" "github.com/gateway-fm/cdk-erigon-lib/common" @@ -46,18 +47,13 @@ func (api *ErigonImpl) BlockNumber(ctx context.Context, rpcBlockNumPtr *rpc.Bloc var rpcBlockNum rpc.BlockNumber if rpcBlockNumPtr == nil { - rpcBlockNum = rpc.LatestExecutedBlockNumber + rpcBlockNum = rpc.LatestBlockNumber } else { rpcBlockNum = *rpcBlockNumPtr } var blockNum uint64 switch rpcBlockNum { - case rpc.LatestBlockNumber: - blockNum, err = rpchelper.GetLatestBlockNumber(tx) - if err != nil { - return 0, err - } case rpc.EarliestBlockNumber: blockNum = 0 case rpc.SafeBlockNumber: @@ -71,7 +67,8 @@ func (api *ErigonImpl) BlockNumber(ctx context.Context, rpcBlockNumPtr *rpc.Bloc return 0, err } default: - blockNum, err = rpchelper.GetLatestExecutedBlockNumber(tx) + fmt.Println("== BlockNumber default ==") + blockNum, err = rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return 0, err } diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index ee7e00b3411..e686e91945f 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -178,6 +178,7 @@ func (api *BaseAPI) blockByNumberWithSenders(tx kv.Tx, number uint64) (*types.Bl if hashErr != nil { return nil, hashErr } + return api.blockWithSenders(tx, hash, number) } @@ -264,10 +265,12 @@ func (api *BaseAPI) pendingBlock() *types.Block { } func (api *BaseAPI) blockByRPCNumber(number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) { + fmt.Println("== blockByRPCNumber ==") n, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(number), tx, api.filters) if err != nil { return nil, err } + fmt.Println("== blockByRPCNumber n ==", n) block, err := api.blockByNumberWithSenders(tx, n) return block, err diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index c625431a7a9..c74329958de 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -342,14 +342,6 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN if err != nil { return nil, err } - latestBlockNumber, err := rpchelper.GetLatestBlockNumber(tx) - if err != nil { - return nil, err - } - if blockNum > latestBlockNumber { - // (Compatibility) Every other node just returns `null` for when the block does not exist. - return nil, nil - } _, txAmount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum) if err != nil { diff --git a/cmd/rpcdaemon/commands/eth_block_zkevm.go b/cmd/rpcdaemon/commands/eth_block_zkevm.go index f6537ae6ee3..727ce213a4c 100644 --- a/cmd/rpcdaemon/commands/eth_block_zkevm.go +++ b/cmd/rpcdaemon/commands/eth_block_zkevm.go @@ -214,15 +214,15 @@ func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber } defer tx.Rollback() - // get latest executed block - executedBlock, err := stages.GetStageProgress(tx, stages.Execution) + // get latest finished block + finishedBlock, err := stages.GetStageProgress(tx, stages.Finish) if err != nil { return nil, err } - // return null if requested block is higher than executed + // return null if requested block is higher than finished // made for consistency with zkevm - if number > 0 && executedBlock < uint64(number.Int64()) { + if number > 0 && finishedBlock < uint64(number.Int64()) { return nil, nil } diff --git a/cmd/rpcdaemon/commands/eth_call.go b/cmd/rpcdaemon/commands/eth_call.go index caa4a93f447..ccbdec92f30 100644 --- a/cmd/rpcdaemon/commands/eth_call.go +++ b/cmd/rpcdaemon/commands/eth_call.go @@ -320,7 +320,6 @@ var maxGetProofRewindBlockCount uint64 = 1_000 // GetProof is partially implemented; no Storage proofs, and proofs must be for // blocks within maxGetProofRewindBlockCount blocks of the head. func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, storageKeys []libcommon.Hash, blockNrOrHash rpc.BlockNumberOrHash) (*accounts.AccProofResult, error) { - tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err @@ -340,6 +339,7 @@ func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, sto return nil, err } + // TODO: Getting the latest finish block - using the finish stage latestBlock, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 238d35ab0e0..7e9b3442dae 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -105,7 +105,7 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t end = header.Number.Uint64() } else { // Convert the RPC block numbers into internal representations - latest, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(rpc.LatestExecutedBlockNumber), tx, nil) + latest, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber), tx, nil) if err != nil { return nil, err } @@ -131,7 +131,7 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t return nil, fmt.Errorf("end (%d) < begin (%d)", end, begin) } if end > roaring.MaxUint32 { - latest, err := rpchelper.GetLatestBlockNumber(tx) + latest, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err } @@ -267,6 +267,7 @@ func getTopicsBitmap(c kv.Tx, topics [][]common.Hash, from, to uint64) (*roaring } return result, nil } + func getAddrsBitmap(tx kv.Tx, addrs []common.Address, from, to uint64) (*roaring.Bitmap, error) { if len(addrs) == 0 { return nil, nil @@ -430,7 +431,7 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end exec.changeBlock(header) } - //fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, maxTxNumInBlock=%d,mixTxNumInBlock=%d\n", txNum, blockNum, txIndex, maxTxNumInBlock, minTxNumInBlock) + // fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, maxTxNumInBlock=%d,mixTxNumInBlock=%d\n", txNum, blockNum, txIndex, maxTxNumInBlock, minTxNumInBlock) txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, txIndex) if err != nil { return nil, err @@ -458,8 +459,8 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end logs = append(logs, filtered...) } - //stats := api._agg.GetAndResetStats() - //log.Info("Finished", "duration", time.Since(start), "history queries", stats.HistoryQueries, "ef search duration", stats.EfSearchTime) + // stats := api._agg.GetAndResetStats() + // log.Info("Finished", "duration", time.Since(start), "history queries", stats.HistoryQueries, "ef search duration", stats.EfSearchTime) return logs, nil } @@ -803,6 +804,7 @@ type MapTxNum2BlockNumIter struct { func MapTxNum2BlockNum(tx kv.Tx, it iter.U64) *MapTxNum2BlockNumIter { return &MapTxNum2BlockNumIter{tx: tx, it: it, orderAscend: true} } + func MapDescendTxNum2BlockNum(tx kv.Tx, it iter.U64) *MapTxNum2BlockNumIter { return &MapTxNum2BlockNumIter{tx: tx, it: it, orderAscend: false} } diff --git a/cmd/rpcdaemon/commands/eth_system.go b/cmd/rpcdaemon/commands/eth_system.go index b389123bb95..102cfaba3e4 100644 --- a/cmd/rpcdaemon/commands/eth_system.go +++ b/cmd/rpcdaemon/commands/eth_system.go @@ -27,7 +27,7 @@ func (api *APIImpl) BlockNumber(ctx context.Context) (hexutil.Uint64, error) { return 0, err } defer tx.Rollback() - blockNum, err := rpchelper.GetLatestBlockNumber(tx) + blockNum, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return 0, err } @@ -217,15 +217,19 @@ func (b *GasPriceOracleBackend) HeaderByNumber(ctx context.Context, number rpc.B } return header, nil } + func (b *GasPriceOracleBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { return b.baseApi.blockByRPCNumber(number, b.tx) } + func (b *GasPriceOracleBackend) ChainConfig() *chain.Config { return b.cc } + func (b *GasPriceOracleBackend) GetReceipts(ctx context.Context, hash libcommon.Hash) (types.Receipts, error) { return rawdb.ReadReceiptsByHash(b.tx, hash) } + func (b *GasPriceOracleBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { return nil, nil } diff --git a/cmd/rpcdaemon/commands/otterscan_contract_creator.go b/cmd/rpcdaemon/commands/otterscan_contract_creator.go index 16cecee26e7..332a25003b0 100644 --- a/cmd/rpcdaemon/commands/otterscan_contract_creator.go +++ b/cmd/rpcdaemon/commands/otterscan_contract_creator.go @@ -57,7 +57,7 @@ func (api *OtterscanAPIImpl) GetContractCreator(ctx context.Context, addr common var acc accounts.Account if api.historyV3(tx) { ttx := tx.(kv.TemporalTx) - headNumber, err := stages.GetStageProgress(tx, stages.Execution) + headNumber, err := stages.GetStageProgress(tx, stages.Finish) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/trace_filtering_zkevm.go b/cmd/rpcdaemon/commands/trace_filtering_zkevm.go index 3d9686188f0..edee84874d8 100644 --- a/cmd/rpcdaemon/commands/trace_filtering_zkevm.go +++ b/cmd/rpcdaemon/commands/trace_filtering_zkevm.go @@ -50,7 +50,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB } engine := api.engine() - var json = jsoniter.ConfigCompatibleWithStandardLibrary + json := jsoniter.ConfigCompatibleWithStandardLibrary stream.WriteArrayStart() first := true // Execute all transactions in picked blocks @@ -151,7 +151,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB if _, ok := toAddresses[lastHeader.Coinbase]; ok || includeAll { nSeen++ var tr ParityTrace - var rewardAction = &RewardTraceAction{} + rewardAction := &RewardTraceAction{} rewardAction.Author = lastHeader.Coinbase rewardAction.RewardType = "block" // nolint: goconst rewardAction.Value.ToInt().Set(minerReward.ToBig()) @@ -226,11 +226,11 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB } continue } - if txIndex == -1 { //is system tx + if txIndex == -1 { // is system tx continue } txIndexU64 := uint64(txIndex) - //fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d\n", txNum, blockNum, txIndex) + // fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d\n", txNum, blockNum, txIndex) txn, err := api._txnReader.TxnByIdxInBlock(ctx, dbtx, blockNum, txIndex) if err != nil { if first { @@ -244,7 +244,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB continue } if txn == nil { - continue //guess block doesn't have transactions + continue // guess block doesn't have transactions } txHash := txn.Hash() msg, err := txn.AsMessage(*lastSigner, lastHeader.BaseFee, lastRules) diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index b32cf4ae749..dd46959d6f9 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -29,6 +29,7 @@ import ( // TraceBlockByNumber implements debug_traceBlockByNumber. Returns Geth style block traces. func (api *PrivateDebugAPIImpl) TraceBlockByNumber(ctx context.Context, blockNum rpc.BlockNumber, config *tracers.TraceConfig_ZkEvm, stream *jsoniter.Stream) error { + fmt.Println("=== TraceBlockByNumber ===") return api.traceBlock(ctx, rpc.BlockNumberOrHashWithNumber(blockNum), config, stream) } @@ -501,7 +502,6 @@ func (api *PrivateDebugAPIImpl) TraceCallMany_deprecated(ctx context.Context, bu ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) - if err != nil { stream.WriteNil() return err diff --git a/cmd/rpcdaemon/commands/tracing_zkevm.go b/cmd/rpcdaemon/commands/tracing_zkevm.go index 21ce600c541..b04c6f310da 100644 --- a/cmd/rpcdaemon/commands/tracing_zkevm.go +++ b/cmd/rpcdaemon/commands/tracing_zkevm.go @@ -27,6 +27,7 @@ import ( ) func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig_ZkEvm, stream *jsoniter.Stream) error { + fmt.Println("=== traceBlock zkevm ===") tx, err := api.db.BeginRo(ctx) if err != nil { stream.WriteNil() @@ -296,7 +297,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun // and apply the message. gp := new(core.GasPool).AddGas(math.MaxUint64) for idx, txn := range replayTransactions { - //evm = vm.NewEVM(blockCtx, txCtx, evm.IntraBlockState(), chainConfig, vm.Config{Debug: false}) + // evm = vm.NewEVM(blockCtx, txCtx, evm.IntraBlockState(), chainConfig, vm.Config{Debug: false}) txHash := txn.Hash() evm, effectiveGasPricePercentage, err := core.PrepareForTxExecution(chainConfig, &vm.Config{}, &blockCtx, hermezReader, evm.IntraBlockState().(*state.IntraBlockState), block, &txHash, idx) if err != nil { @@ -337,7 +338,6 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) - if err != nil { stream.WriteNil() return err diff --git a/cmd/rpcdaemon/commands/zkevm_api.go b/cmd/rpcdaemon/commands/zkevm_api.go index b09fb402a7e..9953f3b5891 100644 --- a/cmd/rpcdaemon/commands/zkevm_api.go +++ b/cmd/rpcdaemon/commands/zkevm_api.go @@ -110,7 +110,6 @@ func NewZkEvmAPI( l1Syncer *syncer.L1Syncer, l2SequencerUrl string, ) *ZkEvmAPIImpl { - a := &ZkEvmAPIImpl{ ethApi: base, db: db, @@ -355,7 +354,7 @@ func (api *ZkEvmAPIImpl) getOrCalcBatchData(tx kv.Tx, dbReader state.ReadOnlyHer return nil, err } - //found in db, do not calculate + // found in db, do not calculate if len(batchData) != 0 { return batchData, nil } @@ -908,7 +907,6 @@ func (api *ZkEvmAPIImpl) GetBlockRangeWitness(ctx context.Context, startBlockNrO } func (api *ZkEvmAPIImpl) getBatchWitness(ctx context.Context, tx kv.Tx, batchNum uint64, debug bool, mode WitnessMode) (hexutility.Bytes, error) { - // limit in-flight requests by name semaphore := api.semaphores[getBatchWitness] if semaphore != nil { @@ -930,7 +928,6 @@ func (api *ZkEvmAPIImpl) getBatchWitness(ctx context.Context, tx kv.Tx, batchNum } return generator.GetWitnessByBatch(tx, ctx, batchNum, debug, fullWitness) - } func (api *ZkEvmAPIImpl) buildGenerator(tx kv.Tx, witnessMode WitnessMode) (*witness.Generator, bool, error) { @@ -976,7 +973,6 @@ func (api *ZkEvmAPIImpl) getBlockRangeWitness(ctx context.Context, db kv.RoDB, s } endBlockNr, _, _, err := rpchelper.GetCanonicalBlockNumber(endBlockNrOrHash, tx, api.ethApi.filters) // DoCall cannot be executed on non-canonical blocks - if err != nil { return nil, err } @@ -1030,9 +1026,8 @@ func (api *ZkEvmAPIImpl) GetBatchWitness(ctx context.Context, batchNumber uint64 } isWitnessModeNone := checkedMode == WitnessModeNone - rpcModeMatchesNodeMode := - checkedMode == WitnessModeFull && api.config.WitnessFull || - checkedMode == WitnessModeTrimmed && !api.config.WitnessFull + rpcModeMatchesNodeMode := checkedMode == WitnessModeFull && api.config.WitnessFull || + checkedMode == WitnessModeTrimmed && !api.config.WitnessFull // we only want to check the cache if no special run mode has been supplied. // or if requested mode matches the node mode // otherwise regenerate it @@ -1311,7 +1306,8 @@ func convertTransactionsReceipts( txs []eritypes.Transaction, receipts eritypes.Receipts, hermezReader hermez_db.HermezDbReader, - block eritypes.Block) ([]types.Transaction, error) { + block eritypes.Block, +) ([]types.Transaction, error) { if len(txs) != len(receipts) { return nil, errors.New("transactions and receipts length mismatch") } @@ -1569,12 +1565,13 @@ func (zkapi *ZkEvmAPIImpl) GetProof(ctx context.Context, address common.Address, return nil, fmt.Errorf("not supported by Erigon3") } + // TODO: Logic here for getting proof from "finish" stage blockNr, _, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters) if err != nil { return nil, err } - latestBlock, err := rpchelper.GetLatestBlockNumber(tx) + latestBlock, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err } diff --git a/eth/backend.go b/eth/backend.go index bb341749b39..f0cf9515d62 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -24,7 +24,9 @@ import ( "io/fs" "math/big" "net" + "net/url" "os" + "path" "path/filepath" "strconv" "strings" @@ -64,9 +66,6 @@ import ( "github.com/ledgerwatch/erigon/chain" - "net/url" - "path" - "github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer" log2 "github.com/0xPolygonHermez/zkevm-data-streamer/log" "github.com/ledgerwatch/erigon/cl/clparams" @@ -355,7 +354,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { sentries = append(sentries, sentryClient) } } else { - var readNodeInfo = func() *eth.NodeInfo { + readNodeInfo := func() *eth.NodeInfo { var res *eth.NodeInfo _ = backend.chainDB.View(context.Background(), func(tx kv.Tx) error { res = eth.ReadNodeInfo(tx, backend.chainConfig, backend.genesisHash, backend.networkID) @@ -439,7 +438,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } inMemoryExecution := func(batch kv.RwTx, header *types.Header, body *types.RawBody, unwindPoint uint64, headersChain []*types.Header, bodiesChain []*types.RawBody, - notifications *shards.Notifications) error { + notifications *shards.Notifications, + ) error { // Needs its own notifications to not update RPC daemon and txpool about pending blocks stateSync, err := stages2.NewInMemoryExecution(backend.sentryCtx, backend.chainDB, config, backend.sentriesClient, dirs, notifications, allSnapshots, backend.agg) if err != nil { @@ -502,11 +502,11 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.DeprecatedTxPool.Disable { backend.txPool2GrpcServer = &txpool2.GrpcDisabled{} } else { - //cacheConfig := kvcache.DefaultCoherentCacheConfig - //cacheConfig.MetricsLabel = "txpool" + // cacheConfig := kvcache.DefaultCoherentCacheConfig + // cacheConfig.MetricsLabel = "txpool" backend.newTxs2 = make(chan types2.Announcements, 1024) - //defer close(newTxs) + // defer close(newTxs) backend.txPool2DB, backend.txPool2, backend.txPool2Fetch, backend.txPool2Send, backend.txPool2GrpcServer, err = txpooluitl.AllComponents( ctx, config.TxPool, config, kvcache.NewDummy(), backend.newTxs2, backend.chainDB, backend.sentriesClient.Sentries(), stateDiffClient, ) @@ -611,7 +611,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } bs, err := clcore.RetrieveBeaconState(ctx, beaconCfg, genesisCfg, clparams.GetCheckpointSyncEndpoint(clparams.NetworkType(config.NetworkID))) - if err != nil { return nil, err } @@ -638,7 +637,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } backend.notifications.Accumulator.StartChange(currentBlock.NumberU64(), currentBlock.Hash(), nil, false) backend.notifications.Accumulator.SendAndReset(ctx, backend.notifications.StateChangesConsumer, baseFee, currentBlock.GasLimit()) - }() tx, err := backend.chainDB.BeginRw(ctx) @@ -684,9 +682,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } backend.sentriesClient.Bd.AddToPrefetch(b.Header(), b.RawBody()) - //p2p - //backend.sentriesClient.BroadcastNewBlock(context.Background(), b, b.Difficulty()) - //rpcdaemon + // p2p + // backend.sentriesClient.BroadcastNewBlock(context.Background(), b, b.Difficulty()) + // rpcdaemon if err := miningRPC.(*privateapi.MiningServer).BroadcastMinedBlock(b); err != nil { log.Error("txpool rpc mined block broadcast", "err", err) } @@ -1033,7 +1031,7 @@ func newEtherMan(cfg *ethconfig.Config, l2ChainName, url string) *etherman.Clien } em, err := etherman.NewClient(ethmanConf) - //panic on error + // panic on error if err != nil { panic(err) } @@ -1073,12 +1071,12 @@ func (backend *Ethereum) Init(stack *node.Node, config *ethconfig.Config) error } } - //eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} + // eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} gpoParams := config.GPO if gpoParams.Default == nil { gpoParams.Default = config.Miner.GasPrice } - //eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) + // eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) if config.Ethstats != "" { var headCh chan [][]byte headCh, backend.unsubscribeEthstat = backend.notifications.Events.AddHeaderSubscription() diff --git a/turbo/rpchelper/helper.go b/turbo/rpchelper/helper.go index bff67d270c6..ee769d9ef2c 100644 --- a/turbo/rpchelper/helper.go +++ b/turbo/rpchelper/helper.go @@ -24,6 +24,7 @@ func (e nonCanonocalHashError) Error() string { return fmt.Sprintf("hash %x is not currently canonical", e.hash) } +// TODO: GetBlockNumber func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (uint64, libcommon.Hash, bool, error) { return _GetBlockNumber(blockNrOrHash.RequireCanonical, blockNrOrHash, tx, filters) } @@ -33,53 +34,70 @@ func GetCanonicalBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filt } func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (blockNumber uint64, hash libcommon.Hash, latest bool, err error) { - // Due to changed semantics of `lastest` block in RPC request, it is now distinct - // from the block block number corresponding to the plain state - var plainStateBlockNumber uint64 - if plainStateBlockNumber, err = stages.GetStageProgress(tx, stages.Execution); err != nil { - return 0, libcommon.Hash{}, false, fmt.Errorf("getting plain state block number: %w", err) + fmt.Println("== _GetBlockNumber ==") + finishedBlockNumber, err := stages.GetStageProgress(tx, stages.Finish) + if err != nil { + return 0, libcommon.Hash{}, false, fmt.Errorf("getting finished block number: %w", err) + } + + // TODO: Should be moved to the if, keep it here for debug purposes + var latestExecutedBlockNumber uint64 + if latestExecutedBlockNumber, err = stages.GetStageProgress(tx, stages.Execution); err != nil { + return 0, libcommon.Hash{}, false, fmt.Errorf("getting latest executed block number: %w", err) } + fmt.Println("=== latestExecutedBlockNumber - Execution: ===", latestExecutedBlockNumber) + fmt.Println("=== finishedBlockNumber: ===", finishedBlockNumber) var ok bool hash, ok = blockNrOrHash.Hash() if !ok { + fmt.Println("== hash ==") number := *blockNrOrHash.BlockNumber switch number { case rpc.LatestBlockNumber: - if blockNumber, err = GetLatestBlockNumber(tx); err != nil { + fmt.Println("== GetLatestBlockNumber ==") + if blockNumber, err = GetLatestFinishedBlockNumber(tx); err != nil { return 0, libcommon.Hash{}, false, err } case rpc.EarliestBlockNumber: blockNumber = 0 case rpc.FinalizedBlockNumber: + fmt.Println("== GetFinalizedBlockNumber ==") blockNumber, err = GetFinalizedBlockNumber(tx) if err != nil { return 0, libcommon.Hash{}, false, err } case rpc.SafeBlockNumber: + fmt.Println("== GetSafeBlockNumber ==") // [zkevm] safe not available, returns finilized instead // blockNumber, err = GetSafeBlockNumber(tx) blockNumber, err = GetFinalizedBlockNumber(tx) - if err != nil { return 0, libcommon.Hash{}, false, err } case rpc.PendingBlockNumber: pendingBlock := filters.LastPendingBlock() if pendingBlock == nil { - blockNumber = plainStateBlockNumber + blockNumber = finishedBlockNumber } else { return pendingBlock.NumberU64(), pendingBlock.Hash(), false, nil } case rpc.LatestExecutedBlockNumber: - blockNumber = plainStateBlockNumber + fmt.Println("== LatestExecutedBlockNumber ==") + blockNumber = latestExecutedBlockNumber default: + fmt.Println("== int64 ==", number.Int64()) blockNumber = uint64(number.Int64()) + if blockNumber > finishedBlockNumber { + return 0, libcommon.Hash{}, false, fmt.Errorf("block with number %d not found", blockNumber) + } } hash, err = rawdb.ReadCanonicalHash(tx, blockNumber) if err != nil { return 0, libcommon.Hash{}, false, err } } else { + fmt.Println("== other ==") + number := rawdb.ReadHeaderNumber(tx, hash) if number == nil { return 0, libcommon.Hash{}, false, fmt.Errorf("block %x not found", hash) @@ -94,7 +112,8 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, return 0, libcommon.Hash{}, false, nonCanonocalHashError{hash} } } - return blockNumber, hash, blockNumber == plainStateBlockNumber, nil + fmt.Println("=== returning blockNumber: ===", blockNumber) + return blockNumber, hash, blockNumber == finishedBlockNumber, nil } func CreateStateReader(ctx context.Context, tx kv.Tx, blockNrOrHash rpc.BlockNumberOrHash, txnIndex int, filters *Filters, stateCache kvcache.Cache, historyV3 bool, chainName string) (state.StateReader, error) { @@ -119,12 +138,12 @@ func CreateStateReaderFromBlockNumber(ctx context.Context, tx kv.Tx, blockNumber func CreateHistoryStateReader(tx kv.Tx, blockNumber uint64, txnIndex int, historyV3 bool, chainName string) (state.StateReader, error) { if !historyV3 { r := state.NewPlainState(tx, blockNumber, systemcontracts.SystemContractCodeLookup[chainName]) - //r.SetTrace(true) + // r.SetTrace(true) return r, nil } r := state.NewHistoryReaderV3() r.SetTx(tx) - //r.SetTrace(true) + // r.SetTrace(true) minTxNum, err := rawdbv3.TxNums.Min(tx, blockNumber) if err != nil { return nil, err diff --git a/turbo/rpchelper/helper_zkevm.go b/turbo/rpchelper/helper_zkevm.go index 593549b224e..29bfd11a995 100644 --- a/turbo/rpchelper/helper_zkevm.go +++ b/turbo/rpchelper/helper_zkevm.go @@ -9,21 +9,22 @@ import ( "github.com/ledgerwatch/erigon/zk/hermez_db" ) +// TODO: View and possibly edit func GetBatchNumber(rpcBatchNumber rpc.BlockNumber, tx kv.Tx, filters *Filters) (batchNumber uint64, latest bool, err error) { // Due to changed semantics of `lastest` block in RPC request, it is now distinct // from the block block number corresponding to the plain state var ( - executedBlock uint64 + latestFinishedBlock uint64 plainStateBatchNumber uint64 ) hermezDb := hermez_db.NewHermezDbReader(tx) // get highest executed batch - if executedBlock, err = stages.GetStageProgress(tx, stages.Execution); err != nil { + if latestFinishedBlock, err = stages.GetStageProgress(tx, stages.Finish); err != nil { return 0, false, fmt.Errorf("getting plain state block number: %w", err) } - if plainStateBatchNumber, err = hermezDb.GetBatchNoByL2Block(executedBlock); err != nil { + if plainStateBatchNumber, err = hermezDb.GetBatchNoByL2Block(latestFinishedBlock); err != nil { return 0, false, fmt.Errorf("getting plain state batch number: %w", err) } diff --git a/turbo/rpchelper/rpc_block.go b/turbo/rpchelper/rpc_block.go index df8aef896e4..fd1ae3a4e4d 100644 --- a/turbo/rpchelper/rpc_block.go +++ b/turbo/rpchelper/rpc_block.go @@ -18,6 +18,7 @@ var UnknownBlockError = &rpc.CustomError{ } func GetLatestBlockNumber(tx kv.Tx) (uint64, error) { + fmt.Println("== GetLatestBlockNumber func ==") forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx) if forkchoiceHeadHash != (libcommon.Hash{}) { forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash) @@ -26,6 +27,7 @@ func GetLatestBlockNumber(tx kv.Tx) (uint64, error) { } } + // TODO: maybe change here - use finish stage blockNum, err := stages.GetStageProgress(tx, stages.Execution) if err != nil { return 0, fmt.Errorf("getting latest block number: %w", err) @@ -34,7 +36,9 @@ func GetLatestBlockNumber(tx kv.Tx) (uint64, error) { return blockNum, nil } +// TODO: Getting the latest finished block number func GetLatestFinishedBlockNumber(tx kv.Tx) (uint64, error) { + fmt.Println("== GetLatestFinishedBlockNumber ==") forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx) if forkchoiceHeadHash != (libcommon.Hash{}) { forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash) @@ -65,17 +69,18 @@ func GetFinalizedBlockNumber(tx kv.Tx) (uint64, error) { return 0, err } - execBlockNum, err := stages.GetStageProgress(tx, stages.Execution) + // TODO: maybe change here - use finish stage + finishedBlockNumber, err := stages.GetStageProgress(tx, stages.Finish) if err != nil { - return 0, fmt.Errorf("getting latest block number: %w", err) + return 0, fmt.Errorf("getting latest finished block number: %w", err) } - blockNum := highestVerifiedBlock - if execBlockNum < blockNum { - blockNum = execBlockNum + blockNumber := highestVerifiedBlock + if finishedBlockNumber < blockNumber { + blockNumber = finishedBlockNumber } - return blockNum, nil + return blockNumber, nil } func GetSafeBlockNumber(tx kv.Tx) (uint64, error) { From 0aa6cfbf6fb4d1e008666c977c65d87b14c7c581 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Thu, 10 Oct 2024 11:50:37 +0300 Subject: [PATCH 2/9] refactor: add finished block check in GetLatestFinishedBlockNumber, clean up code --- cmd/rpcdaemon/commands/erigon_system.go | 2 -- cmd/rpcdaemon/commands/eth_api.go | 3 -- cmd/rpcdaemon/commands/eth_block.go | 8 ++++++ cmd/rpcdaemon/commands/eth_call.go | 2 +- cmd/rpcdaemon/commands/eth_receipts.go | 7 ++--- cmd/rpcdaemon/commands/eth_system.go | 4 --- .../commands/trace_filtering_zkevm.go | 10 +++---- cmd/rpcdaemon/commands/tracing.go | 2 +- cmd/rpcdaemon/commands/tracing_zkevm.go | 4 +-- cmd/rpcdaemon/commands/zkevm_api.go | 15 ++++++---- eth/backend.go | 28 ++++++++++--------- turbo/rpchelper/helper.go | 27 ++++-------------- turbo/rpchelper/helper_zkevm.go | 10 +++---- turbo/rpchelper/rpc_block.go | 22 --------------- 14 files changed, 55 insertions(+), 89 deletions(-) diff --git a/cmd/rpcdaemon/commands/erigon_system.go b/cmd/rpcdaemon/commands/erigon_system.go index 47d22068fb7..c95b1834a48 100644 --- a/cmd/rpcdaemon/commands/erigon_system.go +++ b/cmd/rpcdaemon/commands/erigon_system.go @@ -2,7 +2,6 @@ package commands import ( "context" - "fmt" "github.com/gateway-fm/cdk-erigon-lib/common" @@ -67,7 +66,6 @@ func (api *ErigonImpl) BlockNumber(ctx context.Context, rpcBlockNumPtr *rpc.Bloc return 0, err } default: - fmt.Println("== BlockNumber default ==") blockNum, err = rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return 0, err diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index e686e91945f..ee7e00b3411 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -178,7 +178,6 @@ func (api *BaseAPI) blockByNumberWithSenders(tx kv.Tx, number uint64) (*types.Bl if hashErr != nil { return nil, hashErr } - return api.blockWithSenders(tx, hash, number) } @@ -265,12 +264,10 @@ func (api *BaseAPI) pendingBlock() *types.Block { } func (api *BaseAPI) blockByRPCNumber(number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) { - fmt.Println("== blockByRPCNumber ==") n, _, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(number), tx, api.filters) if err != nil { return nil, err } - fmt.Println("== blockByRPCNumber n ==", n) block, err := api.blockByNumberWithSenders(tx, n) return block, err diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index c74329958de..b159fbcda81 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -342,6 +342,14 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN if err != nil { return nil, err } + latestBlockNumber, err := rpchelper.GetLatestFinishedBlockNumber(tx) + if err != nil { + return nil, err + } + if blockNum > latestBlockNumber { + // (Compatibility) Every other node just returns `null` for when the block does not exist. + return nil, nil + } _, txAmount, err := api._blockReader.Body(ctx, tx, blockHash, blockNum) if err != nil { diff --git a/cmd/rpcdaemon/commands/eth_call.go b/cmd/rpcdaemon/commands/eth_call.go index ccbdec92f30..caa4a93f447 100644 --- a/cmd/rpcdaemon/commands/eth_call.go +++ b/cmd/rpcdaemon/commands/eth_call.go @@ -320,6 +320,7 @@ var maxGetProofRewindBlockCount uint64 = 1_000 // GetProof is partially implemented; no Storage proofs, and proofs must be for // blocks within maxGetProofRewindBlockCount blocks of the head. func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, storageKeys []libcommon.Hash, blockNrOrHash rpc.BlockNumberOrHash) (*accounts.AccProofResult, error) { + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err @@ -339,7 +340,6 @@ func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, sto return nil, err } - // TODO: Getting the latest finish block - using the finish stage latestBlock, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return nil, err diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 7e9b3442dae..86ed8b9e1e1 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -431,7 +431,7 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end exec.changeBlock(header) } - // fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, maxTxNumInBlock=%d,mixTxNumInBlock=%d\n", txNum, blockNum, txIndex, maxTxNumInBlock, minTxNumInBlock) + //fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, maxTxNumInBlock=%d,mixTxNumInBlock=%d\n", txNum, blockNum, txIndex, maxTxNumInBlock, minTxNumInBlock) txn, err := api._txnReader.TxnByIdxInBlock(ctx, tx, blockNum, txIndex) if err != nil { return nil, err @@ -459,8 +459,8 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.TemporalTx, begin, end logs = append(logs, filtered...) } - // stats := api._agg.GetAndResetStats() - // log.Info("Finished", "duration", time.Since(start), "history queries", stats.HistoryQueries, "ef search duration", stats.EfSearchTime) + //stats := api._agg.GetAndResetStats() + //log.Info("Finished", "duration", time.Since(start), "history queries", stats.HistoryQueries, "ef search duration", stats.EfSearchTime) return logs, nil } @@ -804,7 +804,6 @@ type MapTxNum2BlockNumIter struct { func MapTxNum2BlockNum(tx kv.Tx, it iter.U64) *MapTxNum2BlockNumIter { return &MapTxNum2BlockNumIter{tx: tx, it: it, orderAscend: true} } - func MapDescendTxNum2BlockNum(tx kv.Tx, it iter.U64) *MapTxNum2BlockNumIter { return &MapTxNum2BlockNumIter{tx: tx, it: it, orderAscend: false} } diff --git a/cmd/rpcdaemon/commands/eth_system.go b/cmd/rpcdaemon/commands/eth_system.go index 102cfaba3e4..6e4ab325226 100644 --- a/cmd/rpcdaemon/commands/eth_system.go +++ b/cmd/rpcdaemon/commands/eth_system.go @@ -217,19 +217,15 @@ func (b *GasPriceOracleBackend) HeaderByNumber(ctx context.Context, number rpc.B } return header, nil } - func (b *GasPriceOracleBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { return b.baseApi.blockByRPCNumber(number, b.tx) } - func (b *GasPriceOracleBackend) ChainConfig() *chain.Config { return b.cc } - func (b *GasPriceOracleBackend) GetReceipts(ctx context.Context, hash libcommon.Hash) (types.Receipts, error) { return rawdb.ReadReceiptsByHash(b.tx, hash) } - func (b *GasPriceOracleBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { return nil, nil } diff --git a/cmd/rpcdaemon/commands/trace_filtering_zkevm.go b/cmd/rpcdaemon/commands/trace_filtering_zkevm.go index edee84874d8..3d9686188f0 100644 --- a/cmd/rpcdaemon/commands/trace_filtering_zkevm.go +++ b/cmd/rpcdaemon/commands/trace_filtering_zkevm.go @@ -50,7 +50,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB } engine := api.engine() - json := jsoniter.ConfigCompatibleWithStandardLibrary + var json = jsoniter.ConfigCompatibleWithStandardLibrary stream.WriteArrayStart() first := true // Execute all transactions in picked blocks @@ -151,7 +151,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB if _, ok := toAddresses[lastHeader.Coinbase]; ok || includeAll { nSeen++ var tr ParityTrace - rewardAction := &RewardTraceAction{} + var rewardAction = &RewardTraceAction{} rewardAction.Author = lastHeader.Coinbase rewardAction.RewardType = "block" // nolint: goconst rewardAction.Value.ToInt().Set(minerReward.ToBig()) @@ -226,11 +226,11 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB } continue } - if txIndex == -1 { // is system tx + if txIndex == -1 { //is system tx continue } txIndexU64 := uint64(txIndex) - // fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d\n", txNum, blockNum, txIndex) + //fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d\n", txNum, blockNum, txIndex) txn, err := api._txnReader.TxnByIdxInBlock(ctx, dbtx, blockNum, txIndex) if err != nil { if first { @@ -244,7 +244,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.TemporalTx, fromB continue } if txn == nil { - continue // guess block doesn't have transactions + continue //guess block doesn't have transactions } txHash := txn.Hash() msg, err := txn.AsMessage(*lastSigner, lastHeader.BaseFee, lastRules) diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index dd46959d6f9..53c509c891e 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -29,7 +29,6 @@ import ( // TraceBlockByNumber implements debug_traceBlockByNumber. Returns Geth style block traces. func (api *PrivateDebugAPIImpl) TraceBlockByNumber(ctx context.Context, blockNum rpc.BlockNumber, config *tracers.TraceConfig_ZkEvm, stream *jsoniter.Stream) error { - fmt.Println("=== TraceBlockByNumber ===") return api.traceBlock(ctx, rpc.BlockNumberOrHashWithNumber(blockNum), config, stream) } @@ -502,6 +501,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany_deprecated(ctx context.Context, bu ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) + if err != nil { stream.WriteNil() return err diff --git a/cmd/rpcdaemon/commands/tracing_zkevm.go b/cmd/rpcdaemon/commands/tracing_zkevm.go index b04c6f310da..9512b985b4f 100644 --- a/cmd/rpcdaemon/commands/tracing_zkevm.go +++ b/cmd/rpcdaemon/commands/tracing_zkevm.go @@ -27,7 +27,6 @@ import ( ) func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig_ZkEvm, stream *jsoniter.Stream) error { - fmt.Println("=== traceBlock zkevm ===") tx, err := api.db.BeginRo(ctx) if err != nil { stream.WriteNil() @@ -297,7 +296,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun // and apply the message. gp := new(core.GasPool).AddGas(math.MaxUint64) for idx, txn := range replayTransactions { - // evm = vm.NewEVM(blockCtx, txCtx, evm.IntraBlockState(), chainConfig, vm.Config{Debug: false}) + //evm = vm.NewEVM(blockCtx, txCtx, evm.IntraBlockState(), chainConfig, vm.Config{Debug: false}) txHash := txn.Hash() evm, effectiveGasPricePercentage, err := core.PrepareForTxExecution(chainConfig, &vm.Config{}, &blockCtx, hermezReader, evm.IntraBlockState().(*state.IntraBlockState), block, &txHash, idx) if err != nil { @@ -338,6 +337,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) + if err != nil { stream.WriteNil() return err diff --git a/cmd/rpcdaemon/commands/zkevm_api.go b/cmd/rpcdaemon/commands/zkevm_api.go index 9953f3b5891..03fa1fdbe7e 100644 --- a/cmd/rpcdaemon/commands/zkevm_api.go +++ b/cmd/rpcdaemon/commands/zkevm_api.go @@ -110,6 +110,7 @@ func NewZkEvmAPI( l1Syncer *syncer.L1Syncer, l2SequencerUrl string, ) *ZkEvmAPIImpl { + a := &ZkEvmAPIImpl{ ethApi: base, db: db, @@ -354,7 +355,7 @@ func (api *ZkEvmAPIImpl) getOrCalcBatchData(tx kv.Tx, dbReader state.ReadOnlyHer return nil, err } - // found in db, do not calculate + //found in db, do not calculate if len(batchData) != 0 { return batchData, nil } @@ -907,6 +908,7 @@ func (api *ZkEvmAPIImpl) GetBlockRangeWitness(ctx context.Context, startBlockNrO } func (api *ZkEvmAPIImpl) getBatchWitness(ctx context.Context, tx kv.Tx, batchNum uint64, debug bool, mode WitnessMode) (hexutility.Bytes, error) { + // limit in-flight requests by name semaphore := api.semaphores[getBatchWitness] if semaphore != nil { @@ -928,6 +930,7 @@ func (api *ZkEvmAPIImpl) getBatchWitness(ctx context.Context, tx kv.Tx, batchNum } return generator.GetWitnessByBatch(tx, ctx, batchNum, debug, fullWitness) + } func (api *ZkEvmAPIImpl) buildGenerator(tx kv.Tx, witnessMode WitnessMode) (*witness.Generator, bool, error) { @@ -973,6 +976,7 @@ func (api *ZkEvmAPIImpl) getBlockRangeWitness(ctx context.Context, db kv.RoDB, s } endBlockNr, _, _, err := rpchelper.GetCanonicalBlockNumber(endBlockNrOrHash, tx, api.ethApi.filters) // DoCall cannot be executed on non-canonical blocks + if err != nil { return nil, err } @@ -1026,8 +1030,9 @@ func (api *ZkEvmAPIImpl) GetBatchWitness(ctx context.Context, batchNumber uint64 } isWitnessModeNone := checkedMode == WitnessModeNone - rpcModeMatchesNodeMode := checkedMode == WitnessModeFull && api.config.WitnessFull || - checkedMode == WitnessModeTrimmed && !api.config.WitnessFull + rpcModeMatchesNodeMode := + checkedMode == WitnessModeFull && api.config.WitnessFull || + checkedMode == WitnessModeTrimmed && !api.config.WitnessFull // we only want to check the cache if no special run mode has been supplied. // or if requested mode matches the node mode // otherwise regenerate it @@ -1306,8 +1311,7 @@ func convertTransactionsReceipts( txs []eritypes.Transaction, receipts eritypes.Receipts, hermezReader hermez_db.HermezDbReader, - block eritypes.Block, -) ([]types.Transaction, error) { + block eritypes.Block) ([]types.Transaction, error) { if len(txs) != len(receipts) { return nil, errors.New("transactions and receipts length mismatch") } @@ -1565,7 +1569,6 @@ func (zkapi *ZkEvmAPIImpl) GetProof(ctx context.Context, address common.Address, return nil, fmt.Errorf("not supported by Erigon3") } - // TODO: Logic here for getting proof from "finish" stage blockNr, _, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters) if err != nil { return nil, err diff --git a/eth/backend.go b/eth/backend.go index f0cf9515d62..200df979569 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -24,9 +24,7 @@ import ( "io/fs" "math/big" "net" - "net/url" "os" - "path" "path/filepath" "strconv" "strings" @@ -66,6 +64,9 @@ import ( "github.com/ledgerwatch/erigon/chain" + "net/url" + "path" + "github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer" log2 "github.com/0xPolygonHermez/zkevm-data-streamer/log" "github.com/ledgerwatch/erigon/cl/clparams" @@ -354,7 +355,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { sentries = append(sentries, sentryClient) } } else { - readNodeInfo := func() *eth.NodeInfo { + var readNodeInfo = func() *eth.NodeInfo { var res *eth.NodeInfo _ = backend.chainDB.View(context.Background(), func(tx kv.Tx) error { res = eth.ReadNodeInfo(tx, backend.chainConfig, backend.genesisHash, backend.networkID) @@ -438,8 +439,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } inMemoryExecution := func(batch kv.RwTx, header *types.Header, body *types.RawBody, unwindPoint uint64, headersChain []*types.Header, bodiesChain []*types.RawBody, - notifications *shards.Notifications, - ) error { + notifications *shards.Notifications) error { // Needs its own notifications to not update RPC daemon and txpool about pending blocks stateSync, err := stages2.NewInMemoryExecution(backend.sentryCtx, backend.chainDB, config, backend.sentriesClient, dirs, notifications, allSnapshots, backend.agg) if err != nil { @@ -502,11 +502,11 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.DeprecatedTxPool.Disable { backend.txPool2GrpcServer = &txpool2.GrpcDisabled{} } else { - // cacheConfig := kvcache.DefaultCoherentCacheConfig - // cacheConfig.MetricsLabel = "txpool" + //cacheConfig := kvcache.DefaultCoherentCacheConfig + //cacheConfig.MetricsLabel = "txpool" backend.newTxs2 = make(chan types2.Announcements, 1024) - // defer close(newTxs) + //defer close(newTxs) backend.txPool2DB, backend.txPool2, backend.txPool2Fetch, backend.txPool2Send, backend.txPool2GrpcServer, err = txpooluitl.AllComponents( ctx, config.TxPool, config, kvcache.NewDummy(), backend.newTxs2, backend.chainDB, backend.sentriesClient.Sentries(), stateDiffClient, ) @@ -611,6 +611,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } bs, err := clcore.RetrieveBeaconState(ctx, beaconCfg, genesisCfg, clparams.GetCheckpointSyncEndpoint(clparams.NetworkType(config.NetworkID))) + if err != nil { return nil, err } @@ -637,6 +638,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } backend.notifications.Accumulator.StartChange(currentBlock.NumberU64(), currentBlock.Hash(), nil, false) backend.notifications.Accumulator.SendAndReset(ctx, backend.notifications.StateChangesConsumer, baseFee, currentBlock.GasLimit()) + }() tx, err := backend.chainDB.BeginRw(ctx) @@ -682,9 +684,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } backend.sentriesClient.Bd.AddToPrefetch(b.Header(), b.RawBody()) - // p2p - // backend.sentriesClient.BroadcastNewBlock(context.Background(), b, b.Difficulty()) - // rpcdaemon + //p2p + //backend.sentriesClient.BroadcastNewBlock(context.Background(), b, b.Difficulty()) + //rpcdaemon if err := miningRPC.(*privateapi.MiningServer).BroadcastMinedBlock(b); err != nil { log.Error("txpool rpc mined block broadcast", "err", err) } @@ -1071,12 +1073,12 @@ func (backend *Ethereum) Init(stack *node.Node, config *ethconfig.Config) error } } - // eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} + //eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} gpoParams := config.GPO if gpoParams.Default == nil { gpoParams.Default = config.Miner.GasPrice } - // eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) + //eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) if config.Ethstats != "" { var headCh chan [][]byte headCh, backend.unsubscribeEthstat = backend.notifications.Events.AddHeaderSubscription() diff --git a/turbo/rpchelper/helper.go b/turbo/rpchelper/helper.go index ee769d9ef2c..24c26b6d0ab 100644 --- a/turbo/rpchelper/helper.go +++ b/turbo/rpchelper/helper.go @@ -24,7 +24,6 @@ func (e nonCanonocalHashError) Error() string { return fmt.Sprintf("hash %x is not currently canonical", e.hash) } -// TODO: GetBlockNumber func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (uint64, libcommon.Hash, bool, error) { return _GetBlockNumber(blockNrOrHash.RequireCanonical, blockNrOrHash, tx, filters) } @@ -34,40 +33,28 @@ func GetCanonicalBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filt } func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (blockNumber uint64, hash libcommon.Hash, latest bool, err error) { - fmt.Println("== _GetBlockNumber ==") finishedBlockNumber, err := stages.GetStageProgress(tx, stages.Finish) if err != nil { return 0, libcommon.Hash{}, false, fmt.Errorf("getting finished block number: %w", err) } - // TODO: Should be moved to the if, keep it here for debug purposes - var latestExecutedBlockNumber uint64 - if latestExecutedBlockNumber, err = stages.GetStageProgress(tx, stages.Execution); err != nil { - return 0, libcommon.Hash{}, false, fmt.Errorf("getting latest executed block number: %w", err) - } - fmt.Println("=== latestExecutedBlockNumber - Execution: ===", latestExecutedBlockNumber) - fmt.Println("=== finishedBlockNumber: ===", finishedBlockNumber) var ok bool hash, ok = blockNrOrHash.Hash() if !ok { - fmt.Println("== hash ==") number := *blockNrOrHash.BlockNumber switch number { case rpc.LatestBlockNumber: - fmt.Println("== GetLatestBlockNumber ==") if blockNumber, err = GetLatestFinishedBlockNumber(tx); err != nil { return 0, libcommon.Hash{}, false, err } case rpc.EarliestBlockNumber: blockNumber = 0 case rpc.FinalizedBlockNumber: - fmt.Println("== GetFinalizedBlockNumber ==") blockNumber, err = GetFinalizedBlockNumber(tx) if err != nil { return 0, libcommon.Hash{}, false, err } case rpc.SafeBlockNumber: - fmt.Println("== GetSafeBlockNumber ==") // [zkevm] safe not available, returns finilized instead // blockNumber, err = GetSafeBlockNumber(tx) blockNumber, err = GetFinalizedBlockNumber(tx) @@ -82,10 +69,11 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, return pendingBlock.NumberU64(), pendingBlock.Hash(), false, nil } case rpc.LatestExecutedBlockNumber: - fmt.Println("== LatestExecutedBlockNumber ==") - blockNumber = latestExecutedBlockNumber + blockNumber, err = stages.GetStageProgress(tx, stages.Execution) + if err != nil { + return 0, libcommon.Hash{}, false, fmt.Errorf("getting latest executed block number: %w", err) + } default: - fmt.Println("== int64 ==", number.Int64()) blockNumber = uint64(number.Int64()) if blockNumber > finishedBlockNumber { return 0, libcommon.Hash{}, false, fmt.Errorf("block with number %d not found", blockNumber) @@ -96,8 +84,6 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, return 0, libcommon.Hash{}, false, err } } else { - fmt.Println("== other ==") - number := rawdb.ReadHeaderNumber(tx, hash) if number == nil { return 0, libcommon.Hash{}, false, fmt.Errorf("block %x not found", hash) @@ -112,7 +98,6 @@ func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, return 0, libcommon.Hash{}, false, nonCanonocalHashError{hash} } } - fmt.Println("=== returning blockNumber: ===", blockNumber) return blockNumber, hash, blockNumber == finishedBlockNumber, nil } @@ -138,12 +123,12 @@ func CreateStateReaderFromBlockNumber(ctx context.Context, tx kv.Tx, blockNumber func CreateHistoryStateReader(tx kv.Tx, blockNumber uint64, txnIndex int, historyV3 bool, chainName string) (state.StateReader, error) { if !historyV3 { r := state.NewPlainState(tx, blockNumber, systemcontracts.SystemContractCodeLookup[chainName]) - // r.SetTrace(true) + //r.SetTrace(true) return r, nil } r := state.NewHistoryReaderV3() r.SetTx(tx) - // r.SetTrace(true) + //r.SetTrace(true) minTxNum, err := rawdbv3.TxNums.Min(tx, blockNumber) if err != nil { return nil, err diff --git a/turbo/rpchelper/helper_zkevm.go b/turbo/rpchelper/helper_zkevm.go index 29bfd11a995..e5451154df7 100644 --- a/turbo/rpchelper/helper_zkevm.go +++ b/turbo/rpchelper/helper_zkevm.go @@ -9,22 +9,21 @@ import ( "github.com/ledgerwatch/erigon/zk/hermez_db" ) -// TODO: View and possibly edit func GetBatchNumber(rpcBatchNumber rpc.BlockNumber, tx kv.Tx, filters *Filters) (batchNumber uint64, latest bool, err error) { // Due to changed semantics of `lastest` block in RPC request, it is now distinct // from the block block number corresponding to the plain state var ( - latestFinishedBlock uint64 + executedBlock uint64 plainStateBatchNumber uint64 ) hermezDb := hermez_db.NewHermezDbReader(tx) // get highest executed batch - if latestFinishedBlock, err = stages.GetStageProgress(tx, stages.Finish); err != nil { + if executedBlock, err = stages.GetStageProgress(tx, stages.Execution); err != nil { return 0, false, fmt.Errorf("getting plain state block number: %w", err) } - if plainStateBatchNumber, err = hermezDb.GetBatchNoByL2Block(latestFinishedBlock); err != nil { + if plainStateBatchNumber, err = hermezDb.GetBatchNoByL2Block(executedBlock); err != nil { return 0, false, fmt.Errorf("getting plain state batch number: %w", err) } @@ -45,6 +44,7 @@ func GetBatchNumber(rpcBatchNumber rpc.BlockNumber, tx kv.Tx, filters *Filters) default: batchNumber = uint64(rpcBatchNumber.Int64()) } - + fmt.Println("== ExecutedBlock == ", executedBlock) + fmt.Println("== GetBatchNumber == ", batchNumber) return batchNumber, batchNumber == plainStateBatchNumber, nil } diff --git a/turbo/rpchelper/rpc_block.go b/turbo/rpchelper/rpc_block.go index fd1ae3a4e4d..0d098257ff5 100644 --- a/turbo/rpchelper/rpc_block.go +++ b/turbo/rpchelper/rpc_block.go @@ -17,28 +17,7 @@ var UnknownBlockError = &rpc.CustomError{ Message: "Unknown block", } -func GetLatestBlockNumber(tx kv.Tx) (uint64, error) { - fmt.Println("== GetLatestBlockNumber func ==") - forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx) - if forkchoiceHeadHash != (libcommon.Hash{}) { - forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash) - if forkchoiceHeadNum != nil { - return *forkchoiceHeadNum, nil - } - } - - // TODO: maybe change here - use finish stage - blockNum, err := stages.GetStageProgress(tx, stages.Execution) - if err != nil { - return 0, fmt.Errorf("getting latest block number: %w", err) - } - - return blockNum, nil -} - -// TODO: Getting the latest finished block number func GetLatestFinishedBlockNumber(tx kv.Tx) (uint64, error) { - fmt.Println("== GetLatestFinishedBlockNumber ==") forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx) if forkchoiceHeadHash != (libcommon.Hash{}) { forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash) @@ -69,7 +48,6 @@ func GetFinalizedBlockNumber(tx kv.Tx) (uint64, error) { return 0, err } - // TODO: maybe change here - use finish stage finishedBlockNumber, err := stages.GetStageProgress(tx, stages.Finish) if err != nil { return 0, fmt.Errorf("getting latest finished block number: %w", err) From cb69fdae2397efd0acbfb3a7ff4d2b7a7a56fa53 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Thu, 10 Oct 2024 11:55:32 +0300 Subject: [PATCH 3/9] chore: clean up debug logs --- cmd/rpcdaemon/commands/eth_receipts.go | 1 - cmd/rpcdaemon/commands/tracing.go | 2 +- cmd/rpcdaemon/commands/tracing_zkevm.go | 2 +- eth/backend.go | 2 +- turbo/rpchelper/helper_zkevm.go | 3 +-- 5 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 86ed8b9e1e1..4d893398e80 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -267,7 +267,6 @@ func getTopicsBitmap(c kv.Tx, topics [][]common.Hash, from, to uint64) (*roaring } return result, nil } - func getAddrsBitmap(tx kv.Tx, addrs []common.Address, from, to uint64) (*roaring.Bitmap, error) { if len(addrs) == 0 { return nil, nil diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index 53c509c891e..b32cf4ae749 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -501,7 +501,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany_deprecated(ctx context.Context, bu ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) - + if err != nil { stream.WriteNil() return err diff --git a/cmd/rpcdaemon/commands/tracing_zkevm.go b/cmd/rpcdaemon/commands/tracing_zkevm.go index 9512b985b4f..21ce600c541 100644 --- a/cmd/rpcdaemon/commands/tracing_zkevm.go +++ b/cmd/rpcdaemon/commands/tracing_zkevm.go @@ -337,7 +337,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun ibs := evm.IntraBlockState().(*state.IntraBlockState) ibs.Prepare(common.Hash{}, parent.Hash(), txn_index) err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) - + if err != nil { stream.WriteNil() return err diff --git a/eth/backend.go b/eth/backend.go index 200df979569..bb341749b39 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -1033,7 +1033,7 @@ func newEtherMan(cfg *ethconfig.Config, l2ChainName, url string) *etherman.Clien } em, err := etherman.NewClient(ethmanConf) - // panic on error + //panic on error if err != nil { panic(err) } diff --git a/turbo/rpchelper/helper_zkevm.go b/turbo/rpchelper/helper_zkevm.go index e5451154df7..593549b224e 100644 --- a/turbo/rpchelper/helper_zkevm.go +++ b/turbo/rpchelper/helper_zkevm.go @@ -44,7 +44,6 @@ func GetBatchNumber(rpcBatchNumber rpc.BlockNumber, tx kv.Tx, filters *Filters) default: batchNumber = uint64(rpcBatchNumber.Int64()) } - fmt.Println("== ExecutedBlock == ", executedBlock) - fmt.Println("== GetBatchNumber == ", batchNumber) + return batchNumber, batchNumber == plainStateBatchNumber, nil } From e748751146292d5c356ef06eece65c7d70627266 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Thu, 10 Oct 2024 15:38:08 +0300 Subject: [PATCH 4/9] feat: use latest finished block in rpchelper.GetBatchNumber() --- turbo/rpchelper/helper_zkevm.go | 34 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/turbo/rpchelper/helper_zkevm.go b/turbo/rpchelper/helper_zkevm.go index 593549b224e..3226283702d 100644 --- a/turbo/rpchelper/helper_zkevm.go +++ b/turbo/rpchelper/helper_zkevm.go @@ -12,38 +12,32 @@ import ( func GetBatchNumber(rpcBatchNumber rpc.BlockNumber, tx kv.Tx, filters *Filters) (batchNumber uint64, latest bool, err error) { // Due to changed semantics of `lastest` block in RPC request, it is now distinct // from the block block number corresponding to the plain state - var ( - executedBlock uint64 - plainStateBatchNumber uint64 - ) hermezDb := hermez_db.NewHermezDbReader(tx) - // get highest executed batch - if executedBlock, err = stages.GetStageProgress(tx, stages.Execution); err != nil { - return 0, false, fmt.Errorf("getting plain state block number: %w", err) - } - - if plainStateBatchNumber, err = hermezDb.GetBatchNoByL2Block(executedBlock); err != nil { - return 0, false, fmt.Errorf("getting plain state batch number: %w", err) - } - switch rpcBatchNumber { case rpc.EarliestBlockNumber: batchNumber = 0 - case rpc.FinalizedBlockNumber: - case rpc.SafeBlockNumber: + case rpc.SafeBlockNumber, rpc.FinalizedBlockNumber: // [zkevm] safe not available, returns finilized instead // get highest verified batch if batchNumber, err = stages.GetStageProgress(tx, stages.L1VerificationsBatchNo); err != nil { return 0, false, fmt.Errorf("getting verified batch number: %w", err) } - case rpc.PendingBlockNumber: - case rpc.LatestBlockNumber: - case rpc.LatestExecutedBlockNumber: - batchNumber = plainStateBatchNumber + case rpc.LatestBlockNumber, rpc.LatestExecutedBlockNumber, rpc.PendingBlockNumber: + // get highest finished batch + latestFinishedBlock, err := stages.GetStageProgress(tx, stages.Finish) + if err != nil { + return 0, false, fmt.Errorf("getting latest finished state block number: %w", err) + } + + if batchNumber, err = hermezDb.GetBatchNoByL2Block(latestFinishedBlock); err != nil { + return 0, false, fmt.Errorf("getting latest finished state batch number: %w", err) + } + + latest = true default: batchNumber = uint64(rpcBatchNumber.Int64()) } - return batchNumber, batchNumber == plainStateBatchNumber, nil + return batchNumber, latest, nil } From d0cca9541d65fb1bf0410a56d666abc242a16893 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Thu, 10 Oct 2024 16:04:31 +0300 Subject: [PATCH 5/9] chore: Use GetLatestFinishedBlockNumber in overlay_api --- turbo/jsonrpc/overlay_api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/turbo/jsonrpc/overlay_api.go b/turbo/jsonrpc/overlay_api.go index ca551435bba..0b6949f5b87 100644 --- a/turbo/jsonrpc/overlay_api.go +++ b/turbo/jsonrpc/overlay_api.go @@ -554,7 +554,7 @@ func (api *OverlayAPIImpl) replayBlock(ctx context.Context, blockNum uint64, sta log.Debug("[replayBlock] res result for transaction", "transactionHash", txn.Hash(), "failed", res.Failed(), "revert", res.Revert(), "error", res.Err) log.Debug("[replayBlock] discarding txLogs because tx has status=failed", "transactionHash", txn.Hash()) } else { - //append logs only if tx has not reverted + // append logs only if tx has not reverted txLogs := statedb.GetLogs(txn.Hash()) log.Debug("[replayBlock]", "len(txLogs)", len(txLogs), "transactionHash", txn.Hash()) blockLogs = append(blockLogs, txLogs...) @@ -618,7 +618,7 @@ func getBeginEnd(ctx context.Context, tx kv.Tx, api *OverlayAPIImpl, crit filter return 0, 0, fmt.Errorf("end (%d) < begin (%d)", end, begin) } if end > roaring.MaxUint32 { - latest, err := rpchelper.GetLatestBlockNumber(tx) + latest, err := rpchelper.GetLatestFinishedBlockNumber(tx) if err != nil { return 0, 0, err } From e4ef4a15fb4b2ac73d1d6fd27aec2db1c1a36280 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Thu, 10 Oct 2024 16:43:12 +0300 Subject: [PATCH 6/9] fix: TestGetFullBlockByNumber add blocks in finished stage --- turbo/jsonrpc/zkevm_api_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/turbo/jsonrpc/zkevm_api_test.go b/turbo/jsonrpc/zkevm_api_test.go index d10dcea61a4..d1961084cc0 100644 --- a/turbo/jsonrpc/zkevm_api_test.go +++ b/turbo/jsonrpc/zkevm_api_test.go @@ -983,6 +983,11 @@ func TestGetFullBlockByNumber(t *testing.T) { err = erigonDB.WriteBody(big.NewInt(9), common.HexToHash("0x27ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a84b7ba81"), []types.Transaction{}) assert.NoError(err) + for i := 1; i <= 4; i++ { + err = stages.SaveStageProgress(tx, stages.Finish, uint64(i)) + assert.NoError(err) + } + tx.Commit() // Block 1 From 5c46bf903f7990fc2255038fbce424ce9d159ac8 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Wed, 16 Oct 2024 14:46:23 +0300 Subject: [PATCH 7/9] fix: use hexutil.Uint64 to marshal blockNrOrHashValue --- turbo/jsonrpc/eth_accounts_zk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/turbo/jsonrpc/eth_accounts_zk.go b/turbo/jsonrpc/eth_accounts_zk.go index 4e2e0b9ea39..55b11ab35d9 100644 --- a/turbo/jsonrpc/eth_accounts_zk.go +++ b/turbo/jsonrpc/eth_accounts_zk.go @@ -17,7 +17,7 @@ func (api *APIImpl) sendGetTransactionCountToSequencer(rpcUrl string, address li if blockNrOrHash != nil { if blockNrOrHash.BlockNumber != nil { bn := *blockNrOrHash.BlockNumber - blockNrOrHashValue = bn.MarshallJson() + blockNrOrHashValue = hexutil.Uint64(bn) } else if blockNrOrHash.BlockHash != nil { blockNrOrHashValue = "0x" + hex.EncodeToString(blockNrOrHash.BlockHash.Bytes()) } From 7d2fc918d87ef6bcf284579dd5548408fbacb260 Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Wed, 16 Oct 2024 17:26:27 +0300 Subject: [PATCH 8/9] fix: cast BlockNumber to int64 when converting to hex --- rpc/types.go | 11 +++++------ turbo/jsonrpc/eth_accounts_zk.go | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/rpc/types.go b/rpc/types.go index 7a6f5759caf..44562e18a0b 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -69,8 +69,10 @@ type jsonWriter interface { remoteAddr() string } -type BlockNumber int64 -type Timestamp uint64 +type ( + BlockNumber int64 + Timestamp uint64 +) const ( LatestExecutedBlockNumber = BlockNumber(-5) @@ -159,7 +161,7 @@ func (bn *BlockNumber) MarshallJson() string { case -5: return "latestExecuted" default: - return fmt.Sprintf("0x%x", *bn) + return fmt.Sprintf("0x%x", int64(*bn)) } } @@ -468,17 +470,14 @@ func (ts *Timestamp) UnmarshalJSON(data []byte) error { // parse string to uint64 timestamp, err := strconv.ParseUint(input, 10, 64) if err != nil { - // try hex number if timestamp, err = hexutil.DecodeUint64(input); err != nil { return err } - } *ts = Timestamp(timestamp) return nil - } type ForkInterval struct { diff --git a/turbo/jsonrpc/eth_accounts_zk.go b/turbo/jsonrpc/eth_accounts_zk.go index 55b11ab35d9..b3f8026ab8c 100644 --- a/turbo/jsonrpc/eth_accounts_zk.go +++ b/turbo/jsonrpc/eth_accounts_zk.go @@ -17,7 +17,7 @@ func (api *APIImpl) sendGetTransactionCountToSequencer(rpcUrl string, address li if blockNrOrHash != nil { if blockNrOrHash.BlockNumber != nil { bn := *blockNrOrHash.BlockNumber - blockNrOrHashValue = hexutil.Uint64(bn) + blockNrOrHashValue = bn.MarshallJson() } else if blockNrOrHash.BlockHash != nil { blockNrOrHashValue = "0x" + hex.EncodeToString(blockNrOrHash.BlockHash.Bytes()) } @@ -32,7 +32,7 @@ func (api *APIImpl) sendGetTransactionCountToSequencer(rpcUrl string, address li return nil, fmt.Errorf("RPC error response: %s", res.Error.Message) } - //hash comes in escaped quotes, so we trim them here + // hash comes in escaped quotes, so we trim them here // \"0x1234\" -> 0x1234 hashHex := strings.Trim(string(res.Result), "\"") From 367cbf65108650be048fb93a6806708befa50fce Mon Sep 17 00:00:00 2001 From: Moreti Georgiev Date: Mon, 21 Oct 2024 16:16:37 +0300 Subject: [PATCH 9/9] fix: use latest executed block for eth_getTransactionCount when running as sequencer --- rpc/types.go | 2 +- turbo/jsonrpc/eth_accounts.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/rpc/types.go b/rpc/types.go index 44562e18a0b..0c0969c4ef8 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -161,7 +161,7 @@ func (bn *BlockNumber) MarshallJson() string { case -5: return "latestExecuted" default: - return fmt.Sprintf("0x%x", int64(*bn)) + return fmt.Sprintf("0x%x", bn.Int64()) } } diff --git a/turbo/jsonrpc/eth_accounts.go b/turbo/jsonrpc/eth_accounts.go index 6f4ac5c1000..b80b6cc1542 100644 --- a/turbo/jsonrpc/eth_accounts.go +++ b/turbo/jsonrpc/eth_accounts.go @@ -79,6 +79,17 @@ func (api *APIImpl) GetTransactionCount(ctx context.Context, address libcommon.A return nil, fmt.Errorf("getTransactionCount cannot open tx: %w", err1) } defer tx.Rollback() + + latestExecutedBlockNumber, err := rpchelper.GetLatestExecutedBlockNumber(tx) + if err != nil { + return nil, fmt.Errorf("getTransactionCount cannot get latest executed block number: %w", err) + } + + if blockNrOrHash.BlockNumber != nil && *blockNrOrHash.BlockNumber == rpc.BlockNumber(latestExecutedBlockNumber) { + blockNumber := rpc.BlockNumber(rpc.LatestExecutedBlockNumber) + blockNrOrHash.BlockNumber = &blockNumber + } + reader, err := rpchelper.CreateStateReader(ctx, tx, *blockNrOrHash, 0, api.filters, api.stateCache, api.historyV3(tx), "") if err != nil { return nil, err