mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
fix nil pointer when block does not exist
This commit is contained in:
parent
8daefeb890
commit
306eb6a72a
3 changed files with 9 additions and 7 deletions
|
|
@ -19,6 +19,7 @@ package eth
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -143,7 +144,11 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
|
||||||
}
|
}
|
||||||
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
|
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
|
||||||
}
|
}
|
||||||
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
|
block := b.eth.blockchain.GetBlockByNumber(uint64(number))
|
||||||
|
if block == nil {
|
||||||
|
return nil, fmt.Errorf("block #%d not found", number)
|
||||||
|
}
|
||||||
|
return block, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
||||||
|
|
|
||||||
|
|
@ -114,9 +114,6 @@ func (api *API) blockByNumber(ctx context.Context, number rpc.BlockNumber) (*typ
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if block == nil {
|
|
||||||
return nil, fmt.Errorf("block #%d not found", number)
|
|
||||||
}
|
|
||||||
return block, nil
|
return block, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1746,9 +1746,9 @@ func (api *DebugAPI) GetRawTransaction(ctx context.Context, hash common.Hash) (h
|
||||||
|
|
||||||
// PrintBlock retrieves a block and returns its pretty printed form.
|
// PrintBlock retrieves a block and returns its pretty printed form.
|
||||||
func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, error) {
|
func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, error) {
|
||||||
block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
|
block, err := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
|
||||||
if block == nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("block #%d not found", number)
|
return "", err
|
||||||
}
|
}
|
||||||
return spew.Sdump(block), nil
|
return spew.Sdump(block), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue