testing

package
v0.0.0-...-b770427 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testing provides test utilities and helpers for txhammer tests.

Index

Constants

View Source
const TestMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"

TestMnemonic is a well-known test mnemonic (DO NOT use in production)

View Source
const TestPrivateKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"

TestPrivateKey is a well-known test private key (DO NOT use in production)

Variables

View Source
var TestChainID = big.NewInt(1337)

TestChainID is the default chain ID for tests

Functions

func AddressFromKey

func AddressFromKey(key *ecdsa.PrivateKey) common.Address

AddressFromKey returns the address for a private key

func AssertEqual

func AssertEqual[T comparable](t *testing.T, got, want T)

AssertEqual fails the test if got != want

func AssertError

func AssertError(t *testing.T, err error)

AssertError fails the test if err is nil

func AssertFalse

func AssertFalse(t *testing.T, condition bool, msg string)

AssertFalse fails the test if condition is true

func AssertLen

func AssertLen[T any](t *testing.T, slice []T, expectedLen int)

AssertLen fails the test if the slice length doesn't match

func AssertNil

func AssertNil(t *testing.T, value any)

AssertNil fails the test if value is not nil

func AssertNoError

func AssertNoError(t *testing.T, err error)

AssertNoError fails the test if err is not nil

func AssertNotEqual

func AssertNotEqual[T comparable](t *testing.T, got, notWant T)

AssertNotEqual fails the test if got == want

func AssertNotNil

func AssertNotNil(t *testing.T, value any)

AssertNotNil fails the test if value is nil

func AssertTrue

func AssertTrue(t *testing.T, condition bool, msg string)

AssertTrue fails the test if condition is false

func BigInt

func BigInt(n int64) *big.Int

BigInt creates a big.Int from an int64

func CreateFailedReceipt

func CreateFailedReceipt(txHash common.Hash, blockNumber, gasUsed uint64) *types.Receipt

CreateFailedReceipt creates a failed receipt for testing

func CreateSuccessReceipt

func CreateSuccessReceipt(txHash common.Hash, blockNumber, gasUsed uint64) *types.Receipt

CreateSuccessReceipt creates a successful receipt for testing

func Ether

func Ether(n int64) *big.Int

Ether converts ether to wei

func GenerateTestKey

func GenerateTestKey(t *testing.T) *ecdsa.PrivateKey

GenerateTestKey generates a random private key for testing

func GenerateTestKeys

func GenerateTestKeys(t *testing.T, count int) []*ecdsa.PrivateKey

GenerateTestKeys generates multiple random private keys for testing

func Gwei

func Gwei(n int64) *big.Int

Gwei converts gwei to wei

func HexToAddress

func HexToAddress(hex string) common.Address

HexToAddress converts a hex string to an address

func HexToHash

func HexToHash(hex string) common.Hash

HexToHash converts a hex string to a hash

func InvalidConfigs

func InvalidConfigs(t *testing.T) map[string]*config.Config

InvalidConfigs returns a set of invalid configurations for testing validation

func MinimalConfig

func MinimalConfig(t *testing.T) *config.Config

MinimalConfig creates a minimal valid configuration for quick tests

func MustParseKey

func MustParseKey(t *testing.T, hexKey string) *ecdsa.PrivateKey

MustParseKey parses a hex private key or fails the test

func RandomAddress

func RandomAddress(t *testing.T) common.Address

RandomAddress generates a random address for testing

func RandomAddresses

func RandomAddresses(t *testing.T, count int) []common.Address

RandomAddresses generates multiple random addresses for testing

func TestConfig

func TestConfig(t *testing.T) *config.Config

TestConfig creates a valid test configuration

func TestConfigContractCall

func TestConfigContractCall(t *testing.T) *config.Config

TestConfigContractCall creates a test configuration for contract call mode

func TestConfigContractDeploy

func TestConfigContractDeploy(t *testing.T) *config.Config

TestConfigContractDeploy creates a test configuration for contract deployment mode

func TestConfigERC20

func TestConfigERC20(t *testing.T) *config.Config

TestConfigERC20 creates a test configuration for ERC20 transfer mode

func TestConfigFeeDelegation

func TestConfigFeeDelegation(t *testing.T) *config.Config

TestConfigFeeDelegation creates a test configuration for fee delegation mode

func TestConfigWithMode

func TestConfigWithMode(t *testing.T, mode string) *config.Config

TestConfigWithMode creates a test configuration with a specific mode

Types

type MockClient

type MockClient struct {

	// Configurable return values
	ChainIDValue       *big.Int
	BlockNumberValue   uint64
	BalanceValue       *big.Int
	NonceValue         uint64
	GasPriceValue      *big.Int
	GasTipCapValue     *big.Int
	EstimateGasValue   uint64
	BlockGasLimitValue uint64

	// Error responses
	ChainIDError         error
	BlockNumberError     error
	BalanceError         error
	NonceError           error
	GasPriceError        error
	GasTipCapError       error
	EstimateGasError     error
	SendTransactionError error
	ReceiptError         error

	// Receipts storage
	Receipts map[common.Hash]*types.Receipt

	// Sent transactions tracking
	SentTransactions []*types.Transaction
	SentRawTxs       [][]byte

	// Call counters
	CallCounts map[string]int
	// contains filtered or unexported fields
}

MockClient is a mock implementation of the RPC client for testing

func NewMockClient

func NewMockClient() *MockClient

NewMockClient creates a new mock client with default values

func (*MockClient) AddReceipt

func (m *MockClient) AddReceipt(txHash common.Hash, receipt *types.Receipt)

AddReceipt adds a receipt to the mock storage

func (*MockClient) BalanceAt

func (m *MockClient) BalanceAt(_ context.Context, _ common.Address, _ *big.Int) (*big.Int, error)

BalanceAt returns the configured balance

func (*MockClient) BatchCall

func (m *MockClient) BatchCall(_ []rpc.BatchElem) error

BatchCall is a mock implementation of batch RPC calls

func (*MockClient) BatchSendRawTransactions

func (m *MockClient) BatchSendRawTransactions(_ context.Context, rawTxs [][]byte) ([]common.Hash, error)

BatchSendRawTransactions sends multiple raw transactions

func (*MockClient) BlockByNumber

func (m *MockClient) BlockByNumber(_ context.Context, number *big.Int) (*types.Block, error)

BlockByNumber returns a mock block

func (*MockClient) BlockNumber

func (m *MockClient) BlockNumber(_ context.Context) (uint64, error)

BlockNumber returns the configured block number

func (*MockClient) ChainID

func (m *MockClient) ChainID(_ context.Context) (*big.Int, error)

ChainID returns the configured chain ID

func (*MockClient) Close

func (m *MockClient) Close()

Close closes the mock client

func (*MockClient) EstimateGas

func (m *MockClient) EstimateGas(_ context.Context, _ *ethereum.CallMsg) (uint64, error)

EstimateGas returns the configured gas estimate

func (*MockClient) GetBlockGasLimit

func (m *MockClient) GetBlockGasLimit(_ context.Context, _ uint64) (uint64, error)

GetBlockGasLimit returns the configured block gas limit

func (*MockClient) GetBlockGasUsed

func (m *MockClient) GetBlockGasUsed(_ context.Context, _ uint64) (uint64, error)

GetBlockGasUsed returns mock gas used

func (*MockClient) GetCallCount

func (m *MockClient) GetCallCount(method string) int

GetCallCount returns the number of times a method was called

func (*MockClient) HeaderByNumber

func (m *MockClient) HeaderByNumber(_ context.Context, number *big.Int) (*types.Header, error)

HeaderByNumber returns a mock header

func (*MockClient) PendingNonceAt

func (m *MockClient) PendingNonceAt(_ context.Context, _ common.Address) (uint64, error)

PendingNonceAt returns the configured nonce

func (*MockClient) SendRawTransaction

func (m *MockClient) SendRawTransaction(_ context.Context, rawTx []byte) (common.Hash, error)

SendRawTransaction stores the raw transaction

func (*MockClient) SendTransaction

func (m *MockClient) SendTransaction(_ context.Context, tx *types.Transaction) error

SendTransaction stores the transaction and returns configured error

func (*MockClient) SuggestGasPrice

func (m *MockClient) SuggestGasPrice(_ context.Context) (*big.Int, error)

SuggestGasPrice returns the configured gas price

func (*MockClient) SuggestGasTipCap

func (m *MockClient) SuggestGasTipCap(_ context.Context) (*big.Int, error)

SuggestGasTipCap returns the configured gas tip cap

func (*MockClient) TransactionReceipt

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

TransactionReceipt returns the receipt for a transaction

Jump to

Keyboard shortcuts

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