Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hookstest.Register() #12

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/state_transition.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCanExecuteTransaction(t *testing.T) {
return makeErr(from, to, s.GetState(account, slot))
},
}
hooks.RegisterForRules(t)
hooks.Register(t)

value := rng.Hash()

Expand Down
6 changes: 3 additions & 3 deletions core/vm/contracts.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestPrecompileOverride(t *testing.T) {
},
},
}
hooks.RegisterForRules(t)
hooks.Register(t)

t.Run(fmt.Sprintf("%T.Call([overridden precompile address = %v])", &vm.EVM{}, tt.addr), func(t *testing.T) {
_, evm := ethtest.NewZeroEVM(t)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
),
},
}
hooks.RegisterForRules(t)
hooks.Register(t)

caller := rng.Address()
input := rng.Bytes(8)
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestCanCreateContract(t *testing.T) {
return makeErr(cc, s.GetState(account, slot))
},
}
hooks.RegisterForRules(t)
hooks.Register(t)

origin := rng.Address()
caller := rng.Address()
Expand Down
25 changes: 16 additions & 9 deletions libevm/hookstest/stub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package hookstest provides test doubles for testing subsets of libevm hooks.
// Package hookstest provides test doubles and convenience wrappers for testing
// libevm hooks.
package hookstest

import (
Expand All @@ -10,6 +11,15 @@ import (
"github.com/ethereum/go-ethereum/params"
)

// Register clears any registered [params.Extras] and then registers `extras`
// for the liftime of the current test, clearing them via tb's
// [testing.TB.Cleanup].
func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, extras params.Extras[C, R]) {
params.TestOnlyClearRegisteredExtras()
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
params.RegisterExtras(extras)
}

// A Stub is a test double for [params.ChainConfigHooks] and
// [params.RulesHooks]. Each of the fields, if non-nil, back their respective
// hook methods, which otherwise fall back to the default behaviour.
Expand All @@ -19,17 +29,14 @@ type Stub struct {
CanCreateContractFn func(*libevm.AddressContext, libevm.StateReader) error
}

// RegisterForRules clears any registered [params.Extras] and then registers s
// as [params.RulesHooks], which are themselves cleared by the
// [testing.TB.Cleanup] routine.
func (s *Stub) RegisterForRules(tb testing.TB) {
params.TestOnlyClearRegisteredExtras()
params.RegisterExtras(params.Extras[params.NOOPHooks, Stub]{
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *params.NOOPHooks, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
// Register is a convenience wrapper for registering s as both the
// [params.ChainConfigHooks] and [params.RulesHooks] via [Register].
func (s *Stub) Register(tb testing.TB) {
Register(tb, params.Extras[Stub, Stub]{
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *Stub, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
return s
},
})
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
}

func (s Stub) PrecompileOverride(a common.Address) (libevm.PrecompiledContract, bool) {
Expand Down
Loading