Documentation
¶
Overview ¶
Package testing provides test utilities and helpers for txhammer tests.
Index ¶
- Constants
- Variables
- func AddressFromKey(key *ecdsa.PrivateKey) common.Address
- func AssertEqual[T comparable](t *testing.T, got, want T)
- func AssertError(t *testing.T, err error)
- func AssertFalse(t *testing.T, condition bool, msg string)
- func AssertLen[T any](t *testing.T, slice []T, expectedLen int)
- func AssertNil(t *testing.T, value any)
- func AssertNoError(t *testing.T, err error)
- func AssertNotEqual[T comparable](t *testing.T, got, notWant T)
- func AssertNotNil(t *testing.T, value any)
- func AssertTrue(t *testing.T, condition bool, msg string)
- func BigInt(n int64) *big.Int
- func CreateFailedReceipt(txHash common.Hash, blockNumber, gasUsed uint64) *types.Receipt
- func CreateSuccessReceipt(txHash common.Hash, blockNumber, gasUsed uint64) *types.Receipt
- func Ether(n int64) *big.Int
- func GenerateTestKey(t *testing.T) *ecdsa.PrivateKey
- func GenerateTestKeys(t *testing.T, count int) []*ecdsa.PrivateKey
- func Gwei(n int64) *big.Int
- func HexToAddress(hex string) common.Address
- func HexToHash(hex string) common.Hash
- func InvalidConfigs(t *testing.T) map[string]*config.Config
- func MinimalConfig(t *testing.T) *config.Config
- func MustParseKey(t *testing.T, hexKey string) *ecdsa.PrivateKey
- func RandomAddress(t *testing.T) common.Address
- func RandomAddresses(t *testing.T, count int) []common.Address
- func TestConfig(t *testing.T) *config.Config
- func TestConfigContractCall(t *testing.T) *config.Config
- func TestConfigContractDeploy(t *testing.T) *config.Config
- func TestConfigERC20(t *testing.T) *config.Config
- func TestConfigFeeDelegation(t *testing.T) *config.Config
- func TestConfigWithMode(t *testing.T, mode string) *config.Config
- type MockClient
- func (m *MockClient) AddReceipt(txHash common.Hash, receipt *types.Receipt)
- func (m *MockClient) BalanceAt(_ context.Context, _ common.Address, _ *big.Int) (*big.Int, error)
- func (m *MockClient) BatchCall(_ []rpc.BatchElem) error
- func (m *MockClient) BatchSendRawTransactions(_ context.Context, rawTxs [][]byte) ([]common.Hash, error)
- func (m *MockClient) BlockByNumber(_ context.Context, number *big.Int) (*types.Block, error)
- func (m *MockClient) BlockNumber(_ context.Context) (uint64, error)
- func (m *MockClient) ChainID(_ context.Context) (*big.Int, error)
- func (m *MockClient) Close()
- func (m *MockClient) EstimateGas(_ context.Context, _ *ethereum.CallMsg) (uint64, error)
- func (m *MockClient) GetBlockGasLimit(_ context.Context, _ uint64) (uint64, error)
- func (m *MockClient) GetBlockGasUsed(_ context.Context, _ uint64) (uint64, error)
- func (m *MockClient) GetCallCount(method string) int
- func (m *MockClient) HeaderByNumber(_ context.Context, number *big.Int) (*types.Header, error)
- func (m *MockClient) PendingNonceAt(_ context.Context, _ common.Address) (uint64, error)
- func (m *MockClient) SendRawTransaction(_ context.Context, rawTx []byte) (common.Hash, error)
- func (m *MockClient) SendTransaction(_ context.Context, tx *types.Transaction) error
- func (m *MockClient) SuggestGasPrice(_ context.Context) (*big.Int, error)
- func (m *MockClient) SuggestGasTipCap(_ context.Context) (*big.Int, error)
- func (m *MockClient) TransactionReceipt(_ context.Context, txHash common.Hash) (*types.Receipt, error)
Constants ¶
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)
const TestPrivateKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
TestPrivateKey is a well-known test private key (DO NOT use in production)
Variables ¶
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 ¶
AssertError fails the test if err is nil
func AssertFalse ¶
AssertFalse fails the test if condition is true
func AssertNoError ¶
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 ¶
AssertNotNil fails the test if value is nil
func AssertTrue ¶
AssertTrue fails the test if condition is false
func CreateFailedReceipt ¶
CreateFailedReceipt creates a failed receipt for testing
func CreateSuccessReceipt ¶
CreateSuccessReceipt creates a successful receipt for testing
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 HexToAddress ¶
HexToAddress converts a hex string to an address
func InvalidConfigs ¶
InvalidConfigs returns a set of invalid configurations for testing validation
func MinimalConfig ¶
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 ¶
RandomAddress generates a random address for testing
func RandomAddresses ¶
RandomAddresses generates multiple random addresses for testing
func TestConfig ¶
TestConfig creates a valid test configuration
func TestConfigContractCall ¶
TestConfigContractCall creates a test configuration for contract call mode
func TestConfigContractDeploy ¶
TestConfigContractDeploy creates a test configuration for contract deployment mode
func TestConfigERC20 ¶
TestConfigERC20 creates a test configuration for ERC20 transfer mode
func TestConfigFeeDelegation ¶
TestConfigFeeDelegation creates a test configuration for fee delegation 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) 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 ¶
BlockByNumber returns a mock block
func (*MockClient) BlockNumber ¶
func (m *MockClient) BlockNumber(_ context.Context) (uint64, error)
BlockNumber returns the configured block number
func (*MockClient) EstimateGas ¶
EstimateGas returns the configured gas estimate
func (*MockClient) GetBlockGasLimit ¶
GetBlockGasLimit returns the configured block gas limit
func (*MockClient) GetBlockGasUsed ¶
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 ¶
HeaderByNumber returns a mock header
func (*MockClient) PendingNonceAt ¶
PendingNonceAt returns the configured nonce
func (*MockClient) SendRawTransaction ¶
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 ¶
SuggestGasPrice returns the configured gas price
func (*MockClient) SuggestGasTipCap ¶
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