From c87b856c1a7daff56b46be70cdb7092adc519b7c Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 2 Jun 2025 16:11:19 +0200 Subject: [PATCH] eth: return null for not-found in BlockByNumberOrHash (#31949) This changes the API backend to return null for not-found blocks. This behavior is required by the RPC When `BlockByNumberOrHash` always returned an error for this case ever since being added in https://github.com/ethereum/go-ethereum/pull/19491. The backend method has a couple of call sites, and all of them handle a `nil` block result because `BlockByNumber` returns `nil` for not-found. The only case where this makes a real difference is for `eth_getBlockReceipts`, which was changed in #31361 to actually forward the error from `BlockByNumberOrHash` to the caller. --- eth/api_backend.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 57f5a50837..8ec19308f9 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -196,7 +196,9 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r if hash, ok := blockNrOrHash.Hash(); ok { header := b.eth.blockchain.GetHeaderByHash(hash) if header == nil { - return nil, errors.New("header for hash not found") + // Return 'null' and no error if block is not found. + // This behavior is required by RPC spec. + return nil, nil } if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { return nil, errors.New("hash is not currently canonical")