Documentation ¶
Overview ¶
Package test contains utilities for testing the Ethereum channel backend, such as a simulated blockchain backend and a custom Adjudicator.
Index ¶
- Constants
- func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup)
- func NewRandomAsset(rng *rand.Rand) *ethchannel.Asset
- func NewRandomChainID(rng *rand.Rand) ethchannel.ChainID
- func NonceDiff(address perun.Address, ct bind.ContractTransactor, f func() error) (int, error)
- type BalanceReader
- type Reorder
- type Setup
- type SimAdjudicator
- type SimBackendOpt
- type SimRegisteredSub
- type SimSetup
- type SimTimeout
- type SimulatedBackend
- func (s *SimulatedBackend) Balance(addr wallet.Address, _ perunchannel.Asset) perunchannel.Bal
- func (s *SimulatedBackend) ChainID() *big.Int
- func (s *SimulatedBackend) FundAddress(ctx context.Context, addr common.Address)
- func (s *SimulatedBackend) NewBalanceReader(acc wallet.Address) *BalanceReader
- func (s *SimulatedBackend) Reorg(ctx context.Context, depth uint64, reorder Reorder) error
- func (s *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error
- func (s *SimulatedBackend) StartMining(interval time.Duration)
- func (s *SimulatedBackend) StopMining()
- type TokenSetup
- func (s *TokenSetup) AllowanceEvent(v uint64, included bool)
- func (s *TokenSetup) ConfirmTx(tx *types.Transaction, confirm bool)
- func (s *TokenSetup) IncAllowance(ctx context.Context) *types.Transaction
- func (s *TokenSetup) NoMoreEvents()
- func (s *TokenSetup) StartSubs()
- func (s *TokenSetup) StopSubs()
- func (s *TokenSetup) Transfer(ctx context.Context) *types.Transaction
- func (s *TokenSetup) TransferEvent(included bool)
- type TransactorSetup
- type TxType
Constants ¶
const ( // InitialGasBaseFee is the simulated backend's initial base fee. // It should only decrease from the first block onwards, as no gas auctions // are taking place. // // When constructing a transaction manually, GasFeeCap can be set to this // value to avoid the error 'max fee per gas less than block base fee'. InitialGasBaseFee = 875_000_000 )
Variables ¶
This section is empty.
Functions ¶
func GenericSignerTest ¶
func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup)
GenericSignerTest tests that a transactor produces the correct signatures for the passed signer.
func NewRandomAsset ¶
func NewRandomAsset(rng *rand.Rand) *ethchannel.Asset
NewRandomAsset returns a new random ethereum Asset.
func NewRandomChainID ¶
func NewRandomChainID(rng *rand.Rand) ethchannel.ChainID
NewRandomChainID returns a new random ChainID.
Types ¶
type BalanceReader ¶
type BalanceReader struct {
// contains filtered or unexported fields
}
BalanceReader is a balance reader used for testing. It is associated with a given account.
func (*BalanceReader) Balance ¶
func (br *BalanceReader) Balance(asset perunchannel.Asset) perunchannel.Bal
Balance returns the asset balance of the associated account.
type Reorder ¶
type Reorder func([]types.Transactions) []types.Transactions
Reorder can be used to insert, reorder and exclude transactions in combination with `Reorg`.
type Setup ¶
type Setup struct { SimSetup Accs []*keystore.Account // on-chain funders and channel participant accounts Parts []wallet.Address // channel participants Recvs []*ethwallet.Address // on-chain receivers of withdrawn funds Funders []*ethchannel.Funder // funders, bound to respective account Adjs []*SimAdjudicator // adjudicator, withdrawal bound to respecive receivers Asset *ethchannel.Asset // the asset }
Setup holds a complete test setup for channel backend testing.
func NewSetup ¶
func NewSetup(t *testing.T, rng *rand.Rand, n int, blockInterval time.Duration, txFinalityDepth uint64) *Setup
NewSetup returns a channel backend testing setup. When the adjudicator and asset holder contract are deployed and an error occurs, Fatal is called on the passed *testing.T. Parameter n determines how many accounts, receivers adjudicators and funders are created. The Parts are the Addresses of the Accs. `blockInterval` enables the auto-mining feature if set to a value != 0.
type SimAdjudicator ¶
type SimAdjudicator struct { ethchannel.Adjudicator // contains filtered or unexported fields }
A SimAdjudicator is an Adjudicator for simulated backends. Its Register method and subscription return a *channel.RegisteredEvent whose Timeout is a SimTimeout. SimTimeouts advance the clock of the simulated backend when Wait is called.
func NewSimAdjudicator ¶
func NewSimAdjudicator(backend ethchannel.ContractBackend, contract common.Address, receiver common.Address, acc accounts.Account) *SimAdjudicator
NewSimAdjudicator returns a new SimAdjudicator for the given backend. The backend must be a SimulatedBackend or it panics.
func (*SimAdjudicator) Subscribe ¶
func (a *SimAdjudicator) Subscribe(ctx context.Context, chID channel.ID) (channel.AdjudicatorSubscription, error)
Subscribe returns a RegisteredEvent subscription on the simulated blockchain backend.
type SimBackendOpt ¶
type SimBackendOpt func(*SimulatedBackend)
SimBackendOpt represents an optional argument for the sim backend.
func WithCommitTx ¶
func WithCommitTx(b bool) SimBackendOpt
WithCommitTx controls whether the simulated backend should automatically mine a block after a transaction was sent.
type SimRegisteredSub ¶
type SimRegisteredSub struct { *ethchannel.RegisteredSub // contains filtered or unexported fields }
A SimRegisteredSub embeds an ethereum/channel.RegisteredSub, converting normal TimeTimeouts to SimTimeouts.
func (*SimRegisteredSub) Next ¶
func (r *SimRegisteredSub) Next() channel.AdjudicatorEvent
Next calls Next on the underlying subscription, converting the TimeTimeout to a SimTimeout.
type SimSetup ¶
type SimSetup struct { SimBackend *SimulatedBackend // A simulated blockchain backend TxSender *keystore.Account // funded account for sending transactions CB *ethchannel.ContractBackend // contract backend bound to the TxSender }
SimSetup holds the test setup for a simulated backend.
type SimTimeout ¶
type SimTimeout struct { Time uint64 // contains filtered or unexported fields }
A SimTimeout is a timeout on a simulated blockchain. The first call to Wait advances the clock of the simulated blockchain past the timeout. Access to the blockchain by different SimTimeouts is guarded by a shared mutex.
func (*SimTimeout) IsElapsed ¶
func (t *SimTimeout) IsElapsed(ctx context.Context) bool
IsElapsed returns whether the timeout is higher than the current block's timestamp. Access to the blockchain by different SimTimeouts is guarded by a shared mutex.
func (*SimTimeout) String ¶
func (t *SimTimeout) String() string
String returns the timeout in absolute seconds as a string.
type SimulatedBackend ¶
type SimulatedBackend struct { backends.SimulatedBackend Signer types.Signer // contains filtered or unexported fields }
SimulatedBackend provides a simulated ethereum blockchain for tests.
func NewSimulatedBackend ¶
func NewSimulatedBackend(opts ...SimBackendOpt) *SimulatedBackend
NewSimulatedBackend creates a new Simulated Backend.
func (*SimulatedBackend) Balance ¶
func (s *SimulatedBackend) Balance(addr wallet.Address, _ perunchannel.Asset) perunchannel.Bal
Balance returns the balance of the given address on the simulated backend.
func (*SimulatedBackend) ChainID ¶
func (s *SimulatedBackend) ChainID() *big.Int
ChainID returns the chainID of the underlying blockchain.
func (*SimulatedBackend) FundAddress ¶
func (s *SimulatedBackend) FundAddress(ctx context.Context, addr common.Address)
FundAddress funds a given address with `test.MaxBalance` eth from a faucet.
func (*SimulatedBackend) NewBalanceReader ¶
func (s *SimulatedBackend) NewBalanceReader(acc wallet.Address) *BalanceReader
NewBalanceReader creates a new balance reader for the given account.
func (*SimulatedBackend) Reorg ¶
Reorg applies a chain reorg. `depth` is the number of blocks to be removed. `reorder` is a function that gets as input the removed blocks and outputs a list of blocks that are to be added after the removal. It is required that the number of added blocks is greater than `depth` for a reorg to be accepted. The nonce prevents transactions of the same account from being re-ordered. Trying to do this will panic.
func (*SimulatedBackend) SendTransaction ¶
func (s *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error
SendTransaction executes a transaction.
func (*SimulatedBackend) StartMining ¶
func (s *SimulatedBackend) StartMining(interval time.Duration)
StartMining makes the simulated blockchain auto-mine blocks with the given interval. Must be stopped with `StopMining`. The block time of generated blocks will always increase by 10 seconds.
func (*SimulatedBackend) StopMining ¶
func (s *SimulatedBackend) StopMining()
StopMining stops the auto-mining of the simulated blockchain. Must be called exactly once to free resources iff `StartMining` was called. Waits until the auto-mining routine terminates.
type TokenSetup ¶
type TokenSetup struct { SB *SimulatedBackend CB ethchannel.ContractBackend Token *peruntoken.Peruntoken Contract *bind.BoundContract R *require.Assertions T *testing.T Acc1, Acc2 *accounts.Account SinkApproval chan *peruntoken.PeruntokenApproval SinkTransfer chan *peruntoken.PeruntokenTransfer // contains filtered or unexported fields }
TokenSetup is used to create specific Events and TX easily.
func NewTokenSetup ¶
func NewTokenSetup(ctx context.Context, t *testing.T, rng *rand.Rand, txFinalityDepth uint64) *TokenSetup
NewTokenSetup creates a new TokenSetup.
func (*TokenSetup) AllowanceEvent ¶
func (s *TokenSetup) AllowanceEvent(v uint64, included bool)
AllowanceEvent waits for an allowance event with value `v`. `included` decided whether or not its `Removed` values should not be set.
func (*TokenSetup) ConfirmTx ¶
func (s *TokenSetup) ConfirmTx(tx *types.Transaction, confirm bool)
ConfirmTx confirms that a TX is included in the chain at least once.
func (*TokenSetup) IncAllowance ¶
func (s *TokenSetup) IncAllowance(ctx context.Context) *types.Transaction
IncAllowance sends an IncreaseAllowance TX.
func (*TokenSetup) NoMoreEvents ¶
func (s *TokenSetup) NoMoreEvents()
NoMoreEvents asserts that no more events should be generated.
func (*TokenSetup) StartSubs ¶
func (s *TokenSetup) StartSubs()
StartSubs starts the Approval and Transfer subscriptions.
func (*TokenSetup) StopSubs ¶
func (s *TokenSetup) StopSubs()
StopSubs stops the event subs. Should be called for cleanup iff StartSubs was called.
func (*TokenSetup) Transfer ¶
func (s *TokenSetup) Transfer(ctx context.Context) *types.Transaction
Transfer sends a Transfer TX.
func (*TokenSetup) TransferEvent ¶
func (s *TokenSetup) TransferEvent(included bool)
TransferEvent waits for a transfer event. `included` decided whether or not its `Removed` values should not be set.
type TransactorSetup ¶
type TransactorSetup struct { Signer types.Signer ChainID int64 TxType TxType // Transaction type to generate and check against this signer Tr channel.Transactor ValidAcc accounts.Account // wallet should contain key corresponding to this account. MissingAcc accounts.Account // wallet should not contain key corresponding to this account. }
TransactorSetup holds the setup for running generic tests on a transactor implementation.