Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle finalized and safe tags for block nubmer in RPC requests #373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions gossip/ethapi_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b *EthAPIBackend) CurrentBlock() *evmcore.EvmBlock {

func (b *EthAPIBackend) ResolveRpcBlockNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (idx.Block, error) {
latest := b.svc.store.GetLatestBlockIndex()
if number, ok := blockNrOrHash.Number(); ok && (number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber) {
if number, ok := blockNrOrHash.Number(); ok && isLatestBlockNumber(number) {
return latest, nil
} else if number, ok := blockNrOrHash.Number(); ok {
if idx.Block(number) > latest {
Expand Down Expand Up @@ -99,12 +99,9 @@ func (b *EthAPIBackend) HeaderByHash(ctx context.Context, h common.Hash) (*evmco

// BlockByNumber returns evm block by its number, or nil if not exists.
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*evmcore.EvmBlock, error) {
if number == rpc.PendingBlockNumber {
number = rpc.LatestBlockNumber
}
// Otherwise, resolve and return the block
var blk *evmcore.EvmBlock
if number == rpc.LatestBlockNumber {
if isLatestBlockNumber(number) {
blk = b.state.CurrentBlock()
} else {
n := uint64(number.Int64())
Expand All @@ -114,10 +111,18 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
return blk, nil
}

// isLatestBlockNumber returns true if the block number is latest, pending, finalized or safe
func isLatestBlockNumber(number rpc.BlockNumber) bool {
return number == rpc.LatestBlockNumber ||
number == rpc.PendingBlockNumber ||
number == rpc.FinalizedBlockNumber ||
number == rpc.SafeBlockNumber
}

// StateAndHeaderByNumberOrHash returns evm state and block header by block number or block hash, err if not exists.
func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (state.StateDB, *evmcore.EvmHeader, error) {
var header *evmcore.EvmHeader
if number, ok := blockNrOrHash.Number(); ok && (number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber) {
if number, ok := blockNrOrHash.Number(); ok && isLatestBlockNumber(number) {
var err error
header, err = b.state.LastHeaderWithArchiveState()
if err != nil {
Expand Down Expand Up @@ -288,10 +293,7 @@ func (b *EthAPIBackend) GetReceiptsByNumber(ctx context.Context, number rpc.Bloc
return nil, errors.New("transactions index is disabled (enable TxIndex and re-process the DAGs)")
}

if number == rpc.PendingBlockNumber {
number = rpc.LatestBlockNumber
}
if number == rpc.LatestBlockNumber {
if isLatestBlockNumber(number) {
header := b.state.CurrentHeader()
number = rpc.BlockNumber(header.Number.Uint64())
}
Expand Down
Loading