add BlockByNumber to simulated backend

This commit is contained in:
Ilan Gitter 2018-10-04 16:14:21 -04:00 committed by Josh Giles
parent e7dc0188ba
commit 47d1a8a321

View file

@ -46,6 +46,7 @@ import (
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
var errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block")
var errBlockDoesNotExist = errors.New("block does not exist in blockchain")
var errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
@ -170,6 +171,16 @@ func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.
return transaction, blockNumber != 0, nil
}
// BlockByNumber retrieves a block from the database by number, caching it
// (associated with its hash) if found.
func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
block := b.blockchain.GetBlockByNumber(number.Uint64())
if block == nil {
return nil, errBlockDoesNotExist
}
return block, nil
}
// HeaderByNumber returns a block header from the current canonical chain. If number is
// nil, the latest known header is returned.
func (b *SimulatedBackend) HeaderByNumber(ctx context.Context, block *big.Int) (*types.Header, error) {