erigon-pulse/turbo/rpchelper/rpc_block.go
Enrique Jose Avila Asapche a5cb53d690
safe and finalized blocks from eth_getBlockByNumber (#4436)
* added getFinalzed and getSafe block num

* added rpc finalized and safe block num

* getting nums

* returning nil

* returning nil

* added to helper.go

* removed repeated code

* added functions into rpchelper

* returning err

* simplified

* using previous latest getter

* getting pending block with filter/

* Fix plain state block number

* Fix test

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2022-06-14 11:07:46 +03:00

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
}