2020-10-16 12:15:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2022-09-14 10:56:31 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/forkid"
|
2022-09-14 10:56:31 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2020-10-16 12:15:10 +00:00
|
|
|
)
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// Forks is a data type to record a list of forks passed by this node
|
2020-10-16 12:15:10 +00:00
|
|
|
type Forks struct {
|
|
|
|
GenesisHash common.Hash `json:"genesis"`
|
2020-10-25 21:34:00 +00:00
|
|
|
Forks []uint64 `json:"forks"`
|
2020-10-16 12:15:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
// 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) {
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err := api.db.BeginRo(ctx)
|
2020-10-16 12:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return Forks{}, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-01-02 19:28:22 +00:00
|
|
|
chainConfig, genesis, err := api.chainConfigWithGenesis(tx)
|
2020-10-24 06:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return Forks{}, err
|
|
|
|
}
|
2020-10-16 12:15:10 +00:00
|
|
|
forksBlocks := forkid.GatherForks(chainConfig)
|
|
|
|
|
2021-01-02 19:28:22 +00:00
|
|
|
return Forks{genesis.Hash(), forksBlocks}, nil
|
2020-10-16 12:15:10 +00:00
|
|
|
}
|
2022-09-14 10:56:31 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|