Skip to content

Commit

Permalink
refactor: abstract hookstest.Register() out of `hookstest.Stub.Regi…
Browse files Browse the repository at this point in the history
…ster()` (#12)
  • Loading branch information
ARR4N authored Sep 11, 2024
1 parent d9991bb commit 72744ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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

0 comments on commit 72744ce

Please sign in to comment.