From 47d1a8a32148fd558e57e16fb03cbf7cfdcbc3f7 Mon Sep 17 00:00:00 2001 From: Ilan Gitter Date: Thu, 4 Oct 2018 16:14:21 -0400 Subject: [PATCH] add BlockByNumber to simulated backend --- accounts/abi/bind/backends/simulated.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 1138d2ac92..7d7f132154 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -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) {