Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
blockCtx.BlobBaseFee = new(big.Int)
}
}
blockCtx, txCtx, statedb, chainConfig, config = overrideNewEVMArgs(blockCtx, txCtx, statedb, chainConfig, config)
evm := &EVM{
Context: blockCtx,
TxContext: txCtx,
Expand Down
32 changes: 32 additions & 0 deletions core/vm/evm.libevm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package vm

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
)

type chainIDOverrider struct {
chainID int64
}

func (o chainIDOverrider) OverrideNewEVMArgs(BlockContext, TxContext, StateDB, *params.ChainConfig, Config) (BlockContext, TxContext, StateDB, *params.ChainConfig, Config) {
return BlockContext{}, TxContext{}, nil, &params.ChainConfig{ChainID: big.NewInt(o.chainID)}, Config{}
}

func TestOverrideNewEVMArgs(t *testing.T) {
// The OverrideNewEVMArgs hook accepts and returns all arguments to
// NewEVM(), in order. Here we lock in our assumption of that order. If this
// breaks then the Hooks.OverrideNewEVMArgs() signature MUST be changed to
// match.
var _ func(BlockContext, TxContext, StateDB, *params.ChainConfig, Config) *EVM = NewEVM

const chainID = 13579
libevmHooks = nil
RegisterHooks(chainIDOverrider{chainID: chainID})

got := NewEVM(BlockContext{}, TxContext{}, nil, nil, Config{}).ChainConfig().ChainID
require.Equal(t, big.NewInt(chainID), got)
}
32 changes: 32 additions & 0 deletions core/vm/hooks.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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(BlockContext, TxContext, StateDB, *params.ChainConfig, Config) (BlockContext, TxContext, StateDB, *params.ChainConfig, 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
}
return libevmHooks.OverrideNewEVMArgs(blockCtx, txCtx, statedb, chainConfig, config)
}
Loading