test

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package test contains utilities for testing the Ethereum channel backend, such as a simulated blockchain backend and a custom Adjudicator.

Index

Constants

View Source
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

SimSigner is the latest types.Signer that can be used with the simulated backend.

Functions

func GenericSignerTest added in v0.5.2

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 added in v0.2.2

func NewRandomAsset(rng *rand.Rand) *ethchannel.Asset

NewRandomAsset returns a new random ethereum Asset.

func NonceDiff added in v0.2.1

func NonceDiff(address perun.Address, ct bind.ContractTransactor, f func() error) (int, error)

NonceDiff returns the difference between the nonce of `address` before and after calling `f` iff no other error was encountered.

Types

type Reorder added in v0.8.0

type Reorder func([]types.Transactions) []types.Transactions

Reorder can be used to insert, reorder and exclude transactions in combination with `Reorg`.

type Setup added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.6.0

Subscribe returns a RegisteredEvent subscription on the simulated blockchain backend.

type SimBackendOpt added in v0.8.0

type SimBackendOpt func(*SimulatedBackend)

SimBackendOpt represents an optional argument for the sim backend.

func WithCommitTx added in v0.8.0

func WithCommitTx(b bool) SimBackendOpt

WithCommitTx controls whether the simulated backend should automatically mine a block after a transaction was sent.

type SimRegisteredSub added in v0.2.0

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 added in v0.2.0

Next calls Next on the underlying subscription, converting the TimeTimeout to a SimTimeout.

type SimSetup added in v0.2.0

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.

func NewSimSetup added in v0.2.0

func NewSimSetup(t *testing.T, rng *rand.Rand, txFinalityDepth uint64, blockInterval time.Duration, opts ...SimBackendOpt) *SimSetup

NewSimSetup return a simulated backend test setup. The rng is used to generate the random account for sending of transaction.

type SimTimeout added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

func (t *SimTimeout) String() string

String returns the timeout in absolute seconds as a string.

func (*SimTimeout) Wait added in v0.2.0

func (t *SimTimeout) Wait(ctx context.Context) error

Wait advances the clock of the simulated blockchain past the timeout. Access to the blockchain by different SimTimeouts is guarded by a shared mutex.

type SimulatedBackend

type SimulatedBackend struct {
	backends.SimulatedBackend
	// 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) 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) Reorg added in v0.8.0

func (s *SimulatedBackend) Reorg(ctx context.Context, depth uint64, reorder Reorder) error

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.8.0

type TokenSetup struct {
	SB         *SimulatedBackend
	CB         ethchannel.ContractBackend
	Token      *peruntoken.ERC20
	Contract   *bind.BoundContract
	R          *require.Assertions
	T          *testing.T
	Acc1, Acc2 *accounts.Account

	SinkApproval chan *peruntoken.ERC20Approval
	SinkTransfer chan *peruntoken.ERC20Transfer
	// contains filtered or unexported fields
}

TokenSetup is used to create specific Events and TX easily.

func NewTokenSetup added in v0.8.0

func NewTokenSetup(ctx context.Context, t *testing.T, rng *rand.Rand, txFinalityDepth uint64) *TokenSetup

NewTokenSetup creates a new TokenSetup.

func (*TokenSetup) AllowanceEvent added in v0.8.0

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 added in v0.8.0

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 added in v0.8.0

func (s *TokenSetup) IncAllowance(ctx context.Context) *types.Transaction

IncAllowance sends an IncreaseAllowance TX.

func (*TokenSetup) NoMoreEvents added in v0.8.0

func (s *TokenSetup) NoMoreEvents()

NoMoreEvents asserts that no more events should be generated.

func (*TokenSetup) StartSubs added in v0.8.0

func (s *TokenSetup) StartSubs()

StartSubs starts the Approval and Transfer subscriptions.

func (*TokenSetup) StopSubs added in v0.8.0

func (s *TokenSetup) StopSubs()

StopSubs stops the event subs. Should be called for cleanup iff StartSubs was called.

func (*TokenSetup) Transfer added in v0.8.0

func (s *TokenSetup) Transfer(ctx context.Context) *types.Transaction

Transfer sends a Transfer TX.

func (*TokenSetup) TransferEvent added in v0.8.0

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 added in v0.5.0

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.

type TxType added in v0.9.0

type TxType int

TxType is a transaction type, specifying how it is hashed for signing and how the v value of the signature is coded.

const (
	// LegacyTx - legacy transaction with v = {0,1} + 27.
	LegacyTx TxType = iota
	// EIP155Tx - EIP155 transaction with v = {0,1} + CHAIN_ID * 2 + 35.
	EIP155Tx
	// EIP1559Tx - EIP1559 transaction with v = {0,1}.
	EIP1559Tx
)

Jump to

Keyboard shortcuts

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