mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-08 03:51:20 +00:00
57 lines
1.5 KiB
Go
57 lines
1.5 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
|
||
|
}
|