Skip to content

Commit

Permalink
added a way to get latest executed block post POS (erigontech#5343)
Browse files Browse the repository at this point in the history
* added a way to get latest executed block post POS

* added erigon_ExecutedBlockNumber into readme

* optional rpc.BlockNumber

* better message

* updated readme
  • Loading branch information
enriavil1 authored Sep 14, 2022
1 parent 566cd65 commit 0c64c3f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/rpcdaemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ The following table shows the current implementation status of Erigon's RPC daem
| erigon_forks | Yes | Erigon only |
| erigon_issuance | Yes | Erigon only |
| erigon_GetBlockByTimestamp | Yes | Erigon only |
| erigon_BlockNumber | Yes | Erigon only |
| | | |
| bor_getSnapshot | Yes | Bor only |
| bor_getAuthor | Yes | Bor only |
Expand Down
46 changes: 46 additions & 0 deletions cmd/rpcdaemon/commands/erigon_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
)

// Forks is a data type to record a list of forks passed by this node
Expand All @@ -29,3 +32,46 @@ func (api *ErigonImpl) Forks(ctx context.Context) (Forks, error) {

return Forks{genesis.Hash(), forksBlocks}, nil
}

// Post the merge eth_blockNumber will return latest forkChoiceHead block number
// erigon_blockNumber will return latest executed block number or any block number requested
func (api *ErigonImpl) BlockNumber(ctx context.Context, rpcBlockNumPtr *rpc.BlockNumber) (hexutil.Uint64, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return 0, err
}
defer tx.Rollback()

var rpcBlockNum rpc.BlockNumber
if rpcBlockNumPtr == nil {
rpcBlockNum = rpc.LatestExecutedBlockNumber
}

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:
blockNum, err = rpchelper.GetSafeBlockNumber(tx)
if err != nil {
return 0, err
}
case rpc.FinalizedBlockNumber:
blockNum, err = rpchelper.GetFinalizedBlockNumber(tx)
if err != nil {
return 0, err
}
default:
blockNum, err = rpchelper.GetLatestExecutedBlockNumber(tx)
if err != nil {
return 0, err
}
}

return hexutil.Uint64(blockNum), nil
}
8 changes: 8 additions & 0 deletions turbo/rpchelper/rpc_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ func GetSafeBlockNumber(tx kv.Tx) (uint64, error) {
}
return 0, UnknownBlockError
}

func GetLatestExecutedBlockNumber(tx kv.Tx) (uint64, error) {
blockNum, err := stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return 0, err
}
return blockNum, err
}

0 comments on commit 0c64c3f

Please sign in to comment.