fix api panic when block not exist

This commit is contained in:
mizuguchi_ef 2025-01-31 22:22:28 +09:00
parent 8daefeb890
commit 69614d2ac2
2 changed files with 13 additions and 2 deletions

View file

@ -19,6 +19,7 @@ package eth
import (
"context"
"errors"
"fmt"
"math/big"
"time"
@ -163,7 +164,12 @@ func (b *EthAPIBackend) GetBody(ctx context.Context, hash common.Hash, number rp
func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
if blockNr, ok := blockNrOrHash.Number(); ok {
return b.BlockByNumber(ctx, blockNr)
block, err := b.BlockByNumber(ctx, blockNr)
if block == nil && err == nil {
err = fmt.Errorf("block %v not found", blockNrOrHash)
return nil, err
}
return block, err
}
if hash, ok := blockNrOrHash.Hash(); ok {
header := b.eth.blockchain.GetHeaderByHash(hash)

View file

@ -526,7 +526,12 @@ func (b testBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.
}
func (b testBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
if blockNr, ok := blockNrOrHash.Number(); ok {
return b.BlockByNumber(ctx, blockNr)
block, err := b.BlockByNumber(ctx, blockNr)
if block == nil && err == nil {
err = fmt.Errorf("block %v not found", blockNrOrHash)
return nil, err
}
return block, err
}
if blockHash, ok := blockNrOrHash.Hash(); ok {
return b.BlockByHash(ctx, blockHash)