Skip to content

Commit f829a92

Browse files
committed
feat(core/vm): allow clearing of registered hooks in tests
1 parent 86a2fa8 commit f829a92

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

core/vm/evm.libevm_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 the libevm authors.
1+
// Copyright 2024-2025 the libevm authors.
22
//
33
// The libevm additions to go-ethereum are free software: you can redistribute
44
// them and/or modify them under the terms of the GNU Lesser General Public License
@@ -48,11 +48,9 @@ func (o *evmArgOverrider) OverrideEVMResetArgs(r params.Rules, _ *EVMResetArgs)
4848

4949
func (o *evmArgOverrider) register(t *testing.T) {
5050
t.Helper()
51-
libevmHooks = nil
51+
TestOnlyClearRegisteredHooks()
5252
RegisterHooks(o)
53-
t.Cleanup(func() {
54-
libevmHooks = nil
55-
})
53+
t.Cleanup(TestOnlyClearRegisteredHooks)
5654
}
5755

5856
func TestOverrideNewEVMArgs(t *testing.T) {

core/vm/hooks.libevm.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 the libevm authors.
1+
// Copyright 2024-2025 the libevm authors.
22
//
33
// The libevm additions to go-ethereum are free software: you can redistribute
44
// them and/or modify them under the terms of the GNU Lesser General Public License
@@ -16,18 +16,24 @@
1616

1717
package vm
1818

19-
import "github.com/ava-labs/libevm/params"
19+
import (
20+
"github.com/ava-labs/libevm/libevm/register"
21+
"github.com/ava-labs/libevm/params"
22+
)
2023

2124
// RegisterHooks registers the Hooks. It is expected to be called in an `init()`
2225
// function and MUST NOT be called more than once.
2326
func RegisterHooks(h Hooks) {
24-
if libevmHooks != nil {
25-
panic("already registered")
26-
}
27-
libevmHooks = h
27+
libevmHooks.MustRegister(h)
28+
}
29+
30+
// TestOnlyClearRegisteredHooks clears the [Hooks] previously passed to
31+
// [RegisterHooks]. It panics if called from a non-testing call stack.
32+
func TestOnlyClearRegisteredHooks() {
33+
libevmHooks.TestOnlyClear()
2834
}
2935

30-
var libevmHooks Hooks
36+
var libevmHooks register.AtMostOnce[Hooks]
3137

3238
// Hooks are arbitrary configuration functions to modify default VM behaviour.
3339
// See [RegisterHooks].
@@ -60,17 +66,17 @@ func overrideNewEVMArgs(
6066
chainConfig *params.ChainConfig,
6167
config Config,
6268
) (BlockContext, TxContext, StateDB, *params.ChainConfig, Config) {
63-
if libevmHooks == nil {
69+
if !libevmHooks.Registered() {
6470
return blockCtx, txCtx, statedb, chainConfig, config
6571
}
66-
args := libevmHooks.OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config})
72+
args := libevmHooks.Get().OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config})
6773
return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config
6874
}
6975

7076
func (evm *EVM) overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) {
71-
if libevmHooks == nil {
77+
if !libevmHooks.Registered() {
7278
return txCtx, statedb
7379
}
74-
args := libevmHooks.OverrideEVMResetArgs(evm.chainRules, &EVMResetArgs{txCtx, statedb})
80+
args := libevmHooks.Get().OverrideEVMResetArgs(evm.chainRules, &EVMResetArgs{txCtx, statedb})
7581
return args.TxContext, args.StateDB
7682
}

0 commit comments

Comments
 (0)