Skip to content

Commit 72744ce

Browse files
authored
refactor: abstract hookstest.Register() out of hookstest.Stub.Register() (#12)
1 parent d9991bb commit 72744ce

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

core/state_transition.libevm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestCanExecuteTransaction(t *testing.T) {
2525
return makeErr(from, to, s.GetState(account, slot))
2626
},
2727
}
28-
hooks.RegisterForRules(t)
28+
hooks.Register(t)
2929

3030
value := rng.Hash()
3131

core/vm/contracts.libevm_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestPrecompileOverride(t *testing.T) {
6464
},
6565
},
6666
}
67-
hooks.RegisterForRules(t)
67+
hooks.Register(t)
6868

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

108108
caller := rng.Address()
109109
input := rng.Bytes(8)
@@ -133,7 +133,7 @@ func TestCanCreateContract(t *testing.T) {
133133
return makeErr(cc, s.GetState(account, slot))
134134
},
135135
}
136-
hooks.RegisterForRules(t)
136+
hooks.Register(t)
137137

138138
origin := rng.Address()
139139
caller := rng.Address()

libevm/hookstest/stub.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Package hookstest provides test doubles for testing subsets of libevm hooks.
1+
// Package hookstest provides test doubles and convenience wrappers for testing
2+
// libevm hooks.
23
package hookstest
34

45
import (
@@ -10,6 +11,15 @@ import (
1011
"github.com/ethereum/go-ethereum/params"
1112
)
1213

14+
// Register clears any registered [params.Extras] and then registers `extras`
15+
// for the liftime of the current test, clearing them via tb's
16+
// [testing.TB.Cleanup].
17+
func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, extras params.Extras[C, R]) {
18+
params.TestOnlyClearRegisteredExtras()
19+
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
20+
params.RegisterExtras(extras)
21+
}
22+
1323
// A Stub is a test double for [params.ChainConfigHooks] and
1424
// [params.RulesHooks]. Each of the fields, if non-nil, back their respective
1525
// hook methods, which otherwise fall back to the default behaviour.
@@ -19,17 +29,14 @@ type Stub struct {
1929
CanCreateContractFn func(*libevm.AddressContext, libevm.StateReader) error
2030
}
2131

22-
// RegisterForRules clears any registered [params.Extras] and then registers s
23-
// as [params.RulesHooks], which are themselves cleared by the
24-
// [testing.TB.Cleanup] routine.
25-
func (s *Stub) RegisterForRules(tb testing.TB) {
26-
params.TestOnlyClearRegisteredExtras()
27-
params.RegisterExtras(params.Extras[params.NOOPHooks, Stub]{
28-
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *params.NOOPHooks, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
32+
// Register is a convenience wrapper for registering s as both the
33+
// [params.ChainConfigHooks] and [params.RulesHooks] via [Register].
34+
func (s *Stub) Register(tb testing.TB) {
35+
Register(tb, params.Extras[Stub, Stub]{
36+
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *Stub, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
2937
return s
3038
},
3139
})
32-
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
3340
}
3441

3542
func (s Stub) PrecompileOverride(a common.Address) (libevm.PrecompiledContract, bool) {

0 commit comments

Comments
 (0)