mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
fix api panic when block not exist
This commit is contained in:
parent
8daefeb890
commit
69614d2ac2
2 changed files with 13 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue