eth, internal, les: don't allow nils as non-error values

This commit is contained in:
Péter Szilágyi 2016-12-22 14:31:22 +02:00 committed by Felix Lange
parent 794741b8b2
commit 31ac90ca0b
3 changed files with 26 additions and 11 deletions

View file

@ -18,6 +18,7 @@ package eth
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts"
@ -61,11 +62,14 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum
block := b.eth.miner.PendingBlock()
return block.Header(), nil
}
// Otherwise resolve and return the block
// Otherwise resolve and return the header
if blockNr == rpc.LatestBlockNumber {
return b.eth.blockchain.CurrentBlock().Header(), nil
}
return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
if header := b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)); header != nil {
return header, nil
}
return nil, errors.New("non-existent block") // Although only header was requested, the cause is the missing block
}
func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
@ -78,7 +82,10 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb
if blockNr == rpc.LatestBlockNumber {
return b.eth.blockchain.CurrentBlock(), nil
}
return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
if block := b.eth.blockchain.GetBlockByNumber(uint64(blockNr)); block != nil {
return block, nil
}
return nil, errors.New("non-existent block")
}
func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
@ -89,7 +96,7 @@ func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.
}
// Otherwise resolve the block number and return its state
header, err := b.HeaderByNumber(ctx, blockNr)
if header == nil || err != nil {
if err != nil {
return nil, nil, err
}
stateDb, err := b.eth.BlockChain().StateAt(header.Root)

View file

@ -464,7 +464,7 @@ func (s *PublicBlockChainAPI) BlockNumber() *big.Int {
// block numbers are also allowed.
func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
if err != nil {
return nil, err
}
b := state.GetBalance(address)
@ -551,7 +551,11 @@ func (s *PublicBlockChainAPI) GetUncleCountByBlockHash(ctx context.Context, bloc
// GetCode returns the code stored at the given address in the state for the given block number.
func (s *PublicBlockChainAPI) GetCode(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (hexutil.Bytes, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
if err != nil {
return nil, err
}
res, err := state.GetCode(ctx, address)
if len(res) == 0 || err != nil { // backwards compatibility
return nil, err
}
code := state.GetCode(address)
@ -563,7 +567,11 @@ func (s *PublicBlockChainAPI) GetCode(ctx context.Context, address common.Addres
// numbers are also allowed.
func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.Address, key string, blockNr rpc.BlockNumber) (hexutil.Bytes, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
if err != nil {
return nil, err
}
res, err := state.GetState(ctx, address, common.HexToHash(key))
if err != nil {
return nil, err
}
res := state.GetState(address, common.HexToHash(key))
@ -584,9 +592,10 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
state, header, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
if err != nil {
return nil, common.Big0, err
}
// Set sender address or use a default if none specified
addr := args.From
if addr == (common.Address{}) {
@ -940,7 +949,7 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx cont
// GetTransactionCount returns the number of transactions the given address has sent for the given block number
func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*hexutil.Uint64, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil {
if err != nil {
return nil, err
}
nonce := state.GetNonce(address)

View file

@ -59,7 +59,6 @@ func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum
if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
return b.eth.blockchain.CurrentHeader(), nil
}
return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
}
@ -73,7 +72,7 @@ func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb
func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
header, err := b.HeaderByNumber(ctx, blockNr)
if header == nil || err != nil {
if err != nil {
return nil, nil, err
}
return light.NewState(ctx, header, b.eth.odr), header, nil