eth/api_backend: return error code for PoS tag requests before merge

This commit is contained in:
lightclient 2023-10-10 08:19:39 -06:00
parent 4e1e37323d
commit 54f840a40a
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -42,7 +42,31 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
// EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes // rpcError returns error messages with an error code that can be encoded in the
// RPC response.
type rpcError struct {
code int
msg string
}
func (e *rpcError) Error() string {
return e.msg
}
func (e *rpcError) ErrorCode() int {
return e.code
}
func (e *rpcError) With(msg string) *rpcError {
e.msg = msg
return e
}
// posTagBeforeMerge is returned if a block is requested using either "safe" or
// "finalized" before the chain has fully transitioned to PoS.
var posTagBeforeMerge = &rpcError{code: -39001}
// EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes.
type EthAPIBackend struct { type EthAPIBackend struct {
extRPCEnabled bool extRPCEnabled bool
allowUnprotectedTxs bool allowUnprotectedTxs bool
@ -80,14 +104,14 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
if number == rpc.FinalizedBlockNumber { if number == rpc.FinalizedBlockNumber {
block := b.eth.blockchain.CurrentFinalBlock() block := b.eth.blockchain.CurrentFinalBlock()
if block == nil { if block == nil {
return nil, errors.New("finalized block not found") return nil, posTagBeforeMerge.With("finalized block not found")
} }
return block, nil return block, nil
} }
if number == rpc.SafeBlockNumber { if number == rpc.SafeBlockNumber {
block := b.eth.blockchain.CurrentSafeBlock() block := b.eth.blockchain.CurrentSafeBlock()
if block == nil { if block == nil {
return nil, errors.New("safe block not found") return nil, posTagBeforeMerge.With("safe block not found")
} }
return block, nil return block, nil
} }
@ -132,14 +156,14 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
if number == rpc.FinalizedBlockNumber { if number == rpc.FinalizedBlockNumber {
header := b.eth.blockchain.CurrentFinalBlock() header := b.eth.blockchain.CurrentFinalBlock()
if header == nil { if header == nil {
return nil, errors.New("finalized block not found") return nil, posTagBeforeMerge.With("finalized block not found")
} }
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
} }
if number == rpc.SafeBlockNumber { if number == rpc.SafeBlockNumber {
header := b.eth.blockchain.CurrentSafeBlock() header := b.eth.blockchain.CurrentSafeBlock()
if header == nil { if header == nil {
return nil, errors.New("safe block not found") return nil, posTagBeforeMerge.With("safe block not found1")
} }
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
} }