erigon-pulse/cmd/rpcdaemon/commands/erigon_system.go
Enrique Jose Avila Asapche 0c64c3f2c7
added a way to get latest executed block post POS (#5343)
* added a way to get latest executed block post POS

* added erigon_ExecutedBlockNumber into readme

* optional rpc.BlockNumber

* better message

* updated readme
2022-09-14 13:56:31 +03:00

78 lines
2.0 KiB
Go

package commands
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
type Forks struct {
GenesisHash common.Hash `json:"genesis"`
Forks []uint64 `json:"forks"`
}
// Forks implements erigon_forks. Returns the genesis block hash and a sorted list of all forks block numbers
func (api *ErigonImpl) Forks(ctx context.Context) (Forks, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return Forks{}, err
}
defer tx.Rollback()
chainConfig, genesis, err := api.chainConfigWithGenesis(tx)
if err != nil {
return Forks{}, err
}
forksBlocks := forkid.GatherForks(chainConfig)
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
}