erigon-pulse/turbo/rpchelper/rpc_block.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

65 lines
1.6 KiB
Go

package rpchelper
import (
"fmt"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rpc"
)
var UnknownBlockError = &rpc.CustomError{
Code: -39001,
Message: "Unknown block",
}
func GetLatestBlockNumber(tx kv.Tx) (uint64, error) {
forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx)
if forkchoiceHeadHash != (common.Hash{}) {
forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash)
if forkchoiceHeadNum != nil {
return *forkchoiceHeadNum, nil
}
}
blockNum, err := stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return 0, fmt.Errorf("getting latest block number: %w", err)
}
return blockNum, nil
}
func GetFinalizedBlockNumber(tx kv.Tx) (uint64, error) {
forkchoiceFinalizedHash := rawdb.ReadForkchoiceFinalized(tx)
if forkchoiceFinalizedHash != (common.Hash{}) {
forkchoiceFinalizedNum := rawdb.ReadHeaderNumber(tx, forkchoiceFinalizedHash)
if forkchoiceFinalizedNum != nil {
return *forkchoiceFinalizedNum, nil
}
}
return 0, UnknownBlockError
}
func GetSafeBlockNumber(tx kv.Tx) (uint64, error) {
forkchoiceSafeHash := rawdb.ReadForkchoiceSafe(tx)
if forkchoiceSafeHash != (common.Hash{}) {
forkchoiceSafeNum := rawdb.ReadHeaderNumber(tx, forkchoiceSafeHash)
if forkchoiceSafeNum != nil {
return *forkchoiceSafeNum, nil
}
}
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
}