refactor(core/vm): expose clearing of registered hooks in tests (#181)

## Why this should be merged

Importing some `avalanchego` test fixtures automatically imports
`coreth` packages that register extras, which affects testing of other
VMs that depend on `libevm`. All other registered extras can be cleared
with the pattern in this PR.

## How this works

Uses the `register.AtMostOnce` type already used in `params` and
`types`. I forgot to update the `vm` package when implementing the type.

## How this was tested

Existing unit tests.
This commit is contained in:
Arran Schlosberg 2025-04-16 14:05:45 +01:00 committed by GitHub
parent 86a2fa8836
commit c2e6df1b99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 16 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2024 the libevm authors. // Copyright 2024-2025 the libevm authors.
// //
// The libevm additions to go-ethereum are free software: you can redistribute // The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License // 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)
func (o *evmArgOverrider) register(t *testing.T) { func (o *evmArgOverrider) register(t *testing.T) {
t.Helper() t.Helper()
libevmHooks = nil TestOnlyClearRegisteredHooks()
RegisterHooks(o) RegisterHooks(o)
t.Cleanup(func() { t.Cleanup(TestOnlyClearRegisteredHooks)
libevmHooks = nil
})
} }
func TestOverrideNewEVMArgs(t *testing.T) { func TestOverrideNewEVMArgs(t *testing.T) {

View file

@ -1,4 +1,4 @@
// Copyright 2024 the libevm authors. // Copyright 2024-2025 the libevm authors.
// //
// The libevm additions to go-ethereum are free software: you can redistribute // The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License // them and/or modify them under the terms of the GNU Lesser General Public License
@ -16,18 +16,24 @@
package vm package vm
import "github.com/ava-labs/libevm/params" import (
"github.com/ava-labs/libevm/libevm/register"
"github.com/ava-labs/libevm/params"
)
// RegisterHooks registers the Hooks. It is expected to be called in an `init()` // RegisterHooks registers the Hooks. It is expected to be called in an `init()`
// function and MUST NOT be called more than once. // function and MUST NOT be called more than once.
func RegisterHooks(h Hooks) { func RegisterHooks(h Hooks) {
if libevmHooks != nil { libevmHooks.MustRegister(h)
panic("already registered")
}
libevmHooks = h
} }
var libevmHooks Hooks // TestOnlyClearRegisteredHooks clears the [Hooks] previously passed to
// [RegisterHooks]. It panics if called from a non-testing call stack.
func TestOnlyClearRegisteredHooks() {
libevmHooks.TestOnlyClear()
}
var libevmHooks register.AtMostOnce[Hooks]
// Hooks are arbitrary configuration functions to modify default VM behaviour. // Hooks are arbitrary configuration functions to modify default VM behaviour.
// See [RegisterHooks]. // See [RegisterHooks].
@ -60,17 +66,17 @@ func overrideNewEVMArgs(
chainConfig *params.ChainConfig, chainConfig *params.ChainConfig,
config Config, config Config,
) (BlockContext, TxContext, StateDB, *params.ChainConfig, Config) { ) (BlockContext, TxContext, StateDB, *params.ChainConfig, Config) {
if libevmHooks == nil { if !libevmHooks.Registered() {
return blockCtx, txCtx, statedb, chainConfig, config return blockCtx, txCtx, statedb, chainConfig, config
} }
args := libevmHooks.OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config}) args := libevmHooks.Get().OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config})
return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config
} }
func (evm *EVM) overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) { func (evm *EVM) overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) {
if libevmHooks == nil { if !libevmHooks.Registered() {
return txCtx, statedb return txCtx, statedb
} }
args := libevmHooks.OverrideEVMResetArgs(evm.chainRules, &EVMResetArgs{txCtx, statedb}) args := libevmHooks.Get().OverrideEVMResetArgs(evm.chainRules, &EVMResetArgs{txCtx, statedb})
return args.TxContext, args.StateDB return args.TxContext, args.StateDB
} }