From 83152bb60d2eebd8a02630a68a7aeaf900a052c5 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 30 Oct 2023 16:39:40 +0800 Subject: [PATCH] accounts/abi: simulator implements PendingStateReader Signed-off-by: jsvisa --- accounts/abi/bind/backends/simulated.go | 47 ++++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index a26ee12e0a..4fe53b27fe 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -413,14 +413,6 @@ func (b *SimulatedBackend) TransactionInBlock(ctx context.Context, blockHash com return transactions[index], nil } -// PendingCodeAt returns the code associated with an account in the pending state. -func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) { - b.mu.Lock() - defer b.mu.Unlock() - - return b.pendingState.GetCode(contract), nil -} - func newRevertError(result *core.ExecutionResult) *revertError { reason, errUnpack := abi.UnpackRevert(result.Revert()) err := errors.New("execution reverted") @@ -507,8 +499,35 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu return res.Return(), res.Err } +// PendingBalanceAt implements PendingStateReader.PendingBalanceAt, retrieving +// the balance of the account in the pending state. +func (b *SimulatedBackend) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) { + b.mu.Lock() + defer b.mu.Unlock() + + return b.pendingState.GetOrNewStateObject(account).Balance(), nil +} + +// PendingStorageAt implements PendingStateReader.PendingStorageAt, retrieving +// the account's storage value of key in the pending state. +func (b *SimulatedBackend) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) { + b.mu.Lock() + defer b.mu.Unlock() + + return b.pendingState.GetOrNewStateObject(account).Code(), nil +} + +// PendingCodeAt implements PendingStateReader.PendingCodeAt, retrieving +// the code associated with an account in the pending state. +func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) { + b.mu.Lock() + defer b.mu.Unlock() + + return b.pendingState.GetCode(contract), nil +} + // PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving -// the nonce currently pending for the account. +// the account's nonce in the pending state. func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { b.mu.Lock() defer b.mu.Unlock() @@ -516,6 +535,16 @@ func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Ad return b.pendingState.GetOrNewStateObject(account).Nonce(), nil } +// PendingBalanceAt implements PendingStateReader.PendingTransactionCount, retrieving +// the currently pending transaction count +func (b *SimulatedBackend) PendingTransactionCount(ctx context.Context) (uint, error) { + b.mu.Lock() + defer b.mu.Unlock() + + txs := b.pendingBlock.Transactions().Len() + return uint(txs), nil +} + // SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated // chain doesn't have miners, we just return a gas price of 1 for any call. func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {