backends

package
v1.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: GPL-3.0 Imports: 21 Imported by: 1

Documentation

Overview

Package backends implements simulated bind contract backend for testing contract bindings.

SimulatedBackend implements bind.ContractBackend, simulating a blockchain in the background. Its main purpose is to allow easily testing contract bindings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockChainForCaller added in v1.11.0

type BlockChainForCaller interface {
	// Required by NewEVMContext
	blockchain.ChainContext

	// Below is a subset of consensus.ChainReader
	// Only using the vocabulary of consensus.ChainReader for potential
	// usability within consensus package.
	Config() *params.ChainConfig
	GetHeaderByNumber(number uint64) *types.Header
	GetBlock(hash common.Hash, number uint64) *types.Block
	State() (*state.StateDB, error)
	StateAt(root common.Hash) (*state.StateDB, error)
	CurrentBlock() *types.Block
}

Maintain separate minimal interfaces of blockchain.BlockChain because ContractBackend are used in various situations. BlockChain instances are often passed down as different interfaces such as consensus.ChainReader, governance.blockChain, work.BlockChain.

type BlockchainContractBackend added in v1.12.0

type BlockchainContractBackend struct {
	// contains filtered or unexported fields
}

BlockchainContractBackend implements bind.Contract* and bind.DeployBackend, based on a user-supplied blockchain.BlockChain instance. Its intended purpose is reading system contracts during block processing.

Note that SimulatedBackend creates a new temporary BlockChain for testing, whereas BlockchainContractBackend uses an existing BlockChain with existing database.

func NewBlockchainContractBackend added in v1.12.0

func NewBlockchainContractBackend(bc BlockChainForCaller, tp TxPoolForCaller, es *filters.EventSystem) *BlockchainContractBackend

`txPool` is required for bind.ContractTransactor methods and `events` is required for bind.ContractFilterer methods. If `tp=nil`, bind.ContractTransactor methods could return errors. If `es=nil`, bind.ContractFilterer methods could return errors.

func (*BlockchainContractBackend) BalanceAt added in v1.12.0

func (b *BlockchainContractBackend) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)

func (*BlockchainContractBackend) CallContract added in v1.12.0

func (b *BlockchainContractBackend) CallContract(ctx context.Context, call klaytn.CallMsg, blockNumber *big.Int) ([]byte, error)

Executes a read-only function call with respect to the specified block's state, or latest state if not specified.

Returns call result in []byte. Returns error when: - cannot find the corresponding block or stateDB - VM revert error - VM other errors (e.g. NotProgramAccount, OutOfGas) - Error outside VM

func (*BlockchainContractBackend) ChainID added in v1.12.0

func (b *BlockchainContractBackend) ChainID(ctx context.Context) (*big.Int, error)

func (*BlockchainContractBackend) CodeAt added in v1.12.0

func (b *BlockchainContractBackend) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)

func (*BlockchainContractBackend) CurrentBlockNumber added in v1.12.0

func (b *BlockchainContractBackend) CurrentBlockNumber(ctx context.Context) (uint64, error)

func (*BlockchainContractBackend) EstimateGas added in v1.12.0

func (b *BlockchainContractBackend) EstimateGas(ctx context.Context, call klaytn.CallMsg) (uint64, error)

func (*BlockchainContractBackend) FilterLogs added in v1.12.0

func (b *BlockchainContractBackend) FilterLogs(ctx context.Context, query klaytn.FilterQuery) ([]types.Log, error)

func (*BlockchainContractBackend) PendingCodeAt added in v1.12.0

func (b *BlockchainContractBackend) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)

func (*BlockchainContractBackend) PendingNonceAt added in v1.12.0

func (b *BlockchainContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

func (*BlockchainContractBackend) SendTransaction added in v1.12.0

func (b *BlockchainContractBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error

func (*BlockchainContractBackend) SubscribeFilterLogs added in v1.12.0

func (b *BlockchainContractBackend) SubscribeFilterLogs(ctx context.Context, query klaytn.FilterQuery, ch chan<- types.Log) (klaytn.Subscription, error)

func (*BlockchainContractBackend) SuggestGasPrice added in v1.12.0

func (b *BlockchainContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error)

func (*BlockchainContractBackend) TransactionReceipt added in v1.12.0

func (b *BlockchainContractBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

type SimulatedBackend

type SimulatedBackend struct {
	// contains filtered or unexported fields
}

SimulatedBackend implements bind.ContractBackend, simulating a blockchain in the background. Its main purpose is to allow for easy testing of contract bindings. Simulated backend implements the following interfaces: ChainReader, ChainStateReader, ContractBackend, ContractCaller, ContractFilterer, ContractTransactor, DeployBackend, GasEstimator, GasPricer, LogFilterer, PendingContractCaller, TransactionReader, and TransactionSender

func NewSimulatedBackend

func NewSimulatedBackend(alloc blockchain.GenesisAlloc) *SimulatedBackend

NewSimulatedBackend creates a new binding backend using a simulated blockchain for testing purposes.

func NewSimulatedBackendWithDatabase added in v1.6.0

func NewSimulatedBackendWithDatabase(database database.DBManager, alloc blockchain.GenesisAlloc, cfg *params.ChainConfig) *SimulatedBackend

NewSimulatedBackendWithDatabase creates a new binding backend based on the given database and uses a simulated blockchain for testing purposes.

func NewSimulatedBackendWithGasPrice added in v1.3.0

func NewSimulatedBackendWithGasPrice(alloc blockchain.GenesisAlloc, unitPrice uint64) *SimulatedBackend

NewSimulatedBackendWithGasPrice creates a new binding backend using a simulated blockchain with a given unitPrice. for testing purposes.

func (*SimulatedBackend) AdjustTime

func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error

AdjustTime adds a time shift to the simulated clock. It can only be called on empty blocks.

func (*SimulatedBackend) BalanceAt

func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error)

BalanceAt returns the peb balance of a certain account in the blockchain.

func (*SimulatedBackend) BlockByHash added in v1.6.0

func (b *SimulatedBackend) BlockByHash(_ context.Context, hash common.Hash) (*types.Block, error)

BlockByHash retrieves a block based on the block hash.

func (*SimulatedBackend) BlockByNumber added in v1.6.0

func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)

BlockByNumber retrieves a block from the database by number, caching it (associated with its hash) if found.

func (*SimulatedBackend) BlockChain added in v1.2.0

func (b *SimulatedBackend) BlockChain() *blockchain.BlockChain

Blockchain returns the underlying blockchain.

func (*SimulatedBackend) CallContract

func (b *SimulatedBackend) CallContract(ctx context.Context, call klaytn.CallMsg, blockNumber *big.Int) ([]byte, error)

CallContract executes a contract call.

func (*SimulatedBackend) ChainID

func (b *SimulatedBackend) ChainID(ctx context.Context) (*big.Int, error)

ChainID can return the chain ID of the chain.

func (*SimulatedBackend) Close added in v1.6.0

func (b *SimulatedBackend) Close() error

Close terminates the underlying blockchain's update loop.

func (*SimulatedBackend) CodeAt

func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)

CodeAt returns the code associated with a certain account in the blockchain.

func (*SimulatedBackend) Commit

func (b *SimulatedBackend) Commit()

Commit imports all the pending transactions as a single block and starts a fresh new state.

func (*SimulatedBackend) CurrentBlockNumber added in v1.2.0

func (b *SimulatedBackend) CurrentBlockNumber(ctx context.Context) (uint64, error)

CurrentBlockNumber returns a current block number.

func (*SimulatedBackend) EstimateGas

func (b *SimulatedBackend) EstimateGas(ctx context.Context, call klaytn.CallMsg) (uint64, error)

EstimateGas executes the requested code against the latest block/state and returns the used amount of gas.

func (*SimulatedBackend) FilterLogs

func (b *SimulatedBackend) FilterLogs(ctx context.Context, query klaytn.FilterQuery) ([]types.Log, error)

FilterLogs executes a log filter operation, blocking during execution and returning all the results in one batch.

TODO(karalabe): Deprecate when the subscription one can return past data too.

func (*SimulatedBackend) HeaderByHash added in v1.6.0

func (b *SimulatedBackend) HeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error)

HeaderByHash returns a block header from the current canonical chain.

func (*SimulatedBackend) HeaderByNumber added in v1.6.0

func (b *SimulatedBackend) HeaderByNumber(_ context.Context, block *big.Int) (*types.Header, error)

HeaderByNumber returns a block header from the current canonical chain. If number is nil, the latest known header is returned.

func (*SimulatedBackend) NonceAt

func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (uint64, error)

NonceAt returns the nonce of a certain account in the blockchain.

func (*SimulatedBackend) PendingBlock added in v1.2.0

func (b *SimulatedBackend) PendingBlock() *types.Block

func (*SimulatedBackend) PendingCallContract

func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call klaytn.CallMsg) ([]byte, error)

PendingCallContract executes a contract call on the pending state.

func (*SimulatedBackend) PendingCodeAt

func (b *SimulatedBackend) PendingCodeAt(_ context.Context, contract common.Address) ([]byte, error)

PendingCodeAt returns the code associated with an account in the pending state.

func (*SimulatedBackend) PendingNonceAt

func (b *SimulatedBackend) PendingNonceAt(_ context.Context, account common.Address) (uint64, error)

PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving the nonce currently pending for the account.

func (*SimulatedBackend) Rollback

func (b *SimulatedBackend) Rollback()

Rollback aborts all pending transactions, reverting to the last committed state.

func (*SimulatedBackend) SendTransaction

func (b *SimulatedBackend) SendTransaction(_ context.Context, tx *types.Transaction) error

SendTransaction updates the pending block to include the given transaction. It panics if the transaction is invalid.

func (*SimulatedBackend) StorageAt

func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)

StorageAt returns the value of key in the storage of an account in the blockchain.

func (*SimulatedBackend) SubscribeFilterLogs

func (b *SimulatedBackend) SubscribeFilterLogs(_ context.Context, query klaytn.FilterQuery, ch chan<- types.Log) (klaytn.Subscription, error)

SubscribeFilterLogs creates a background log filtering operation, returning a subscription immediately, which can be used to stream the found events.

func (*SimulatedBackend) SubscribeNewHead added in v1.6.0

func (b *SimulatedBackend) SubscribeNewHead(_ context.Context, ch chan<- *types.Header) (klaytn.Subscription, error)

SubscribeNewHead returns an event subscription for a new header

func (*SimulatedBackend) SuggestGasPrice

func (b *SimulatedBackend) SuggestGasPrice(_ context.Context) (*big.Int, error)

SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated chain doesn't have miners, we just return a gas price of 1 for any call.

func (*SimulatedBackend) TransactionByHash added in v1.6.0

func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)

TransactionByHash checks the pool of pending transactions in addition to the blockchain. The isPending return value indicates whether the transaction has been mined yet. Note that the transaction may not be part of the canonical chain even if it's not pending.

func (*SimulatedBackend) TransactionCount added in v1.6.0

func (b *SimulatedBackend) TransactionCount(_ context.Context, blockHash common.Hash) (uint, error)

TransactionCount returns the number of transactions in a given block.

func (*SimulatedBackend) TransactionInBlock added in v1.6.0

func (b *SimulatedBackend) TransactionInBlock(_ context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)

TransactionInBlock returns the transaction for a specific block at a specific index.

func (*SimulatedBackend) TransactionReceipt

func (b *SimulatedBackend) TransactionReceipt(_ context.Context, txHash common.Hash) (*types.Receipt, error)

TransactionReceipt returns the receipt of a transaction.

type TxPoolForCaller added in v1.12.0

type TxPoolForCaller interface {
	// Below is a subset of work.TxPool
	GetPendingNonce(addr common.Address) uint64
	AddLocal(tx *types.Transaction) error
	GasPrice() *big.Int
}

Maintain separate minimal interfaces of blockchain.TxPool because ContractBackend are used in various situations. TxPool instances are often passed down as work.TxPool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL