mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-26 16:36:17 +00:00
* feat: override `vm.NewEVM()` args * test: `vm.Hooks.OverrideNewEVMArgs` * refactor: use `NewEVMArgs struct` in hook * chore: `gci` * fix: clear `vm.Hooks` at end of test
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package vm
|
|
|
|
import "github.com/ethereum/go-ethereum/params"
|
|
|
|
// RegisterHooks registers the Hooks. It is expected to be called in an `init()`
|
|
// function and MUST NOT be called more than once.
|
|
func RegisterHooks(h Hooks) {
|
|
if libevmHooks != nil {
|
|
panic("already registered")
|
|
}
|
|
libevmHooks = h
|
|
}
|
|
|
|
var libevmHooks Hooks
|
|
|
|
// Hooks are arbitrary configuration functions to modify default VM behaviour.
|
|
type Hooks interface {
|
|
OverrideNewEVMArgs(*NewEVMArgs) *NewEVMArgs
|
|
}
|
|
|
|
// NewEVMArgs are the arguments received by [NewEVM], available for override.
|
|
type NewEVMArgs struct {
|
|
BlockContext BlockContext
|
|
TxContext TxContext
|
|
StateDB StateDB
|
|
ChainConfig *params.ChainConfig
|
|
Config Config
|
|
}
|
|
|
|
func overrideNewEVMArgs(
|
|
blockCtx BlockContext,
|
|
txCtx TxContext,
|
|
statedb StateDB,
|
|
chainConfig *params.ChainConfig,
|
|
config Config,
|
|
) (BlockContext, TxContext, StateDB, *params.ChainConfig, Config) {
|
|
if libevmHooks == nil {
|
|
return blockCtx, txCtx, statedb, chainConfig, config
|
|
}
|
|
args := libevmHooks.OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config})
|
|
return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config
|
|
}
|