Skip to content

Commit

Permalink
feat: change endpoints to return finished blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MorettiGeorgiev committed Oct 9, 2024
1 parent 9ded83e commit 9aca2ce
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 87 deletions.
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/rpcdaemon/commands/erigon_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/rpcdaemon/commands/erigon_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"fmt"

"github.com/gateway-fm/cdk-erigon-lib/common"

Expand Down Expand Up @@ -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:
Expand All @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/commands/eth_block_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/rpcdaemon/commands/eth_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/otterscan_contract_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/rpcdaemon/commands/trace_filtering_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/tracing_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func NewZkEvmAPI(
l1Syncer *syncer.L1Syncer,
l2SequencerUrl string,
) *ZkEvmAPIImpl {

a := &ZkEvmAPIImpl{
ethApi: base,
db: db,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 9aca2ce

Please sign in to comment.