Documentation
¶
Overview ¶
Package ethtest serves a deterministic Ethereum JSON-RPC endpoint so tests can drive the production contract bindings, chain handle constructors and admission predicates without a live chain behind them.
Every read is routed by contract address, matched against the generated contract ABI by method selector, decoded and recorded. A test can therefore assert what was actually asked of the chain - which contract, which method, with which arguments and how many times - rather than what a stand-in was told to answer. Anything the fixture cannot attribute to a registered contract method is recorded as unexpected and answered with an error.
Index ¶
- Constants
- Variables
- func Address(t *testing.T, index int64) common.Address
- func Key(t *testing.T, index int64) *ecdsa.PrivateKey
- func TTokens(amount int64) *big.Int
- type AdmissionCase
- type Backend
- func (b *Backend) AssertNoUnexpectedCalls(t *testing.T)
- func (b *Backend) AssertTrace(t *testing.T, description string, expected ...ExpectedCall)
- func (b *Backend) CallCount(contractName, method string) int
- func (b *Backend) Calls() []Call
- func (b *Backend) CallsTo(contractName, method string) []Call
- func (b *Backend) ChainConfig(t *testing.T) chainethereum.Config
- func (b *Backend) Fail(contractName, method, message string)
- func (b *Backend) Recover(contractName, method string)
- func (b *Backend) ResetCalls()
- func (b *Backend) SetEligibleStake(stakingProvider common.Address, amount *big.Int)
- func (b *Backend) URL() string
- func (b *Backend) UnexpectedCalls() []string
- type Call
- type ExpectedCall
- type Roles
- type State
Constants ¶
const ( BridgeContract = "Bridge" MaintainerProxyContract = "MaintainerProxy" WalletProposalValidatorContract = "WalletProposalValidator" WalletRegistryContract = "WalletRegistry" RandomBeaconContract = "RandomBeacon" TokenStakingContract = "TokenStaking" AllowlistContract = "Allowlist" )
Contract names the fixture serves reads for. They double as the keys the recorded calls are attributed to. The names are spelled out here rather than taken from the chain handle package because that package's own tests use this fixture, and importing it back would close an import cycle.
Variables ¶
var ( BridgeAddress = common.HexToAddress("0x0000000000000000000000000000000000000101") MaintainerProxyAddress = common.HexToAddress("0x0000000000000000000000000000000000000102") WalletProposalValidatorAddress = common.HexToAddress("0x0000000000000000000000000000000000000103") WalletRegistryAddress = common.HexToAddress("0x0000000000000000000000000000000000000104") EcdsaSortitionPoolAddress = common.HexToAddress("0x0000000000000000000000000000000000000105") RandomBeaconAddress = common.HexToAddress("0x0000000000000000000000000000000000000106") BeaconSortitionPoolAddress = common.HexToAddress("0x0000000000000000000000000000000000000107") TokenStakingAddress = common.HexToAddress("0x0000000000000000000000000000000000000108") AllowlistAddress = common.HexToAddress("0x0000000000000000000000000000000000000109") BankAddress = common.HexToAddress("0x000000000000000000000000000000000000010a") RelayAddress = common.HexToAddress("0x000000000000000000000000000000000000010b") ReimbursementPoolAddress = common.HexToAddress("0x000000000000000000000000000000000000010c") )
Addresses the fixture deploys its contracts at. They are all distinct so that every read can be attributed to exactly one contract. TokenStaking and Allowlist are deliberately separate: rolesOf has to be read from token staking, and a read landing on the allowlist instead must be observable rather than silently answered.
Functions ¶
func Key ¶
func Key(t *testing.T, index int64) *ecdsa.PrivateKey
Key returns the chain private key whose scalar is the given index. These are the canonical low-scalar keys: public knowledge rather than secrets, with addresses that exist on mainnet and carry live nonces. That is harmless here because the only endpoint these keys are ever offered to is the local one this package serves - the configured chain ID is mainnet's, so it is the endpoint and nothing else that keeps them off a live chain. All the fixture asks of them is that they be distinct and reproducible across runs.
Types ¶
type AdmissionCase ¶
type AdmissionCase struct {
Name string
// OperatorKey and StakingProviderKey index the synthetic keys the
// identity is built from.
OperatorKey int64
StakingProviderKey int64
// The chain state the identity stands for.
RegisteredWithBeacon bool
RegisteredWithTbtc bool
HasLegacyDelegation bool
EligibleStake *big.Int
PendingDecrease *big.Int
// The pinned outcome.
TbtcRecognizes bool
Admitted bool
}
AdmissionCase is one synthetic operator identity together with the results the admission predicate and policy are pinned to produce for it. The result fields are stated here rather than computed from the state below, so a test asserting them cannot derive its expectation from the behaviour under test.
func AdmissionCaseNamed ¶
func AdmissionCaseNamed(t *testing.T, name string) AdmissionCase
AdmissionCase returns the table entry with the given name.
func AdmissionCases ¶
func AdmissionCases() []AdmissionCase
AdmissionCases returns the fixed admission table. Admission follows tBTC registration and positive eligible stake; beacon registration and legacy token staking roles are present only to catch accidental fallback to them.
func (AdmissionCase) AdmissionReads ¶
func (c AdmissionCase) AdmissionReads(t *testing.T) []ExpectedCall
AdmissionReads is what validating this identity through the production policy must cost.
func (AdmissionCase) Operator ¶
func (c AdmissionCase) Operator(t *testing.T) common.Address
Operator is the identity's operator address.
func (AdmissionCase) StakingProvider ¶
func (c AdmissionCase) StakingProvider(t *testing.T) common.Address
StakingProvider is the single staking-provider address this identity uses on whichever registries it is registered with.
func (AdmissionCase) TbtcReads ¶
func (c AdmissionCase) TbtcReads(t *testing.T) []ExpectedCall
TbtcReads is the trace for the tBTC predicate: the registry mapping, and eligible stake only when the registry holds a mapping. A pending authorization decrease is seeded for some identities and is not an admission credential, so it never appears in a trace.
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend is a deterministic Ethereum JSON-RPC endpoint. Create one with New.
func (*Backend) AssertNoUnexpectedCalls ¶
AssertNoUnexpectedCalls fails the test if the fixture was asked for anything it could not attribute to a registered contract method - an unknown target address, an unknown selector, an unsupported JSON-RPC method, or a read routed to the allowlist instead of token staking.
func (*Backend) AssertTrace ¶
func (b *Backend) AssertTrace( t *testing.T, description string, expected ...ExpectedCall, )
AssertTrace fails unless the reads served since the fixture last forgot them are exactly the expected ones, in order. Comparing whole traces rather than counting matches means a repeated read, a read of another modelled method or a read in the wrong order is a failure rather than something an assertion over one method happens not to look at.
Each entry is rendered from the target address the contract is registered at, the selector the caller actually encoded and the decoded arguments, so the comparison covers what went over the wire and not only what the fixture resolved it to.
func (*Backend) ChainConfig ¶
func (b *Backend) ChainConfig(t *testing.T) chainethereum.Config
ChainConfig returns the configuration pointing the production chain handle constructors at this fixture. The EcdsaDkgValidator address is deliberately left unconfigured so construction takes its optional-contract path.
func (*Backend) Fail ¶
Fail makes every subsequent read of the given contract method return a chain error carrying the given message, until Recover clears it.
func (*Backend) ResetCalls ¶
func (b *Backend) ResetCalls()
ResetCalls forgets the reads served so far, so that a following assertion counts only the reads a specific action performs.
func (*Backend) SetEligibleStake ¶
SetEligibleStake seeds the eligible stake the wallet registry reports for the given staking provider.
func (*Backend) UnexpectedCalls ¶
UnexpectedCalls returns the requests the fixture refused, in order.
type Call ¶
type Call struct {
Contract string
To common.Address
Method string
// Selector is the four leading bytes of the call data, as the caller
// encoded them. It is kept apart from Method so a trace can assert what
// went over the wire rather than what the fixture resolved it to.
Selector []byte
Args []interface{}
}
Call is one read the fixture served, decoded with the contract ABI the target address is registered under.
type ExpectedCall ¶
ExpectedCall is one entry of a contract-read trace: the contract the read must land on, the method it must select and the arguments it must carry.
func Read ¶
func Read(contract, method string, args ...interface{}) ExpectedCall
Read builds a trace entry.
type State ¶
type State struct {
// ChainID is reported by eth_chainId and must match the configured
// network for the chain handle constructors to accept the endpoint.
ChainID *big.Int
// BlockNumber is the height every served block reports.
BlockNumber uint64
// BlockHash is the hash the served block reports as its own. It is a
// standalone value rather than a digest of the served fields, so a reader
// that recomputes the hash instead of reading it back cannot agree with
// it.
BlockHash common.Hash
// BlockFields overrides or adds raw JSON members of the served block, so a
// test can serve fields the pinned header type does not model, or replace
// one the fixture would otherwise report.
BlockFields map[string]json.RawMessage
// BlockMissing makes the endpoint answer a null block, the way a node
// answers for a height it does not have.
BlockMissing bool
// WalletRegistry state.
TbtcOperators map[common.Address]common.Address
EligibleStakes map[common.Address]*big.Int
PendingDecreases map[common.Address]*big.Int
MinimumAuthorization *big.Int
// RandomBeacon state.
BeaconOperators map[common.Address]common.Address
// TokenStaking state.
Roles map[common.Address]Roles
}
State is the chain state the fixture answers reads from. Absent map entries read as the zero value the corresponding contract would return, so a test only has to seed what it wants to differ from an unknown address.
func AdmissionState ¶
AdmissionState builds the chain state the admission table describes.