Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: vm.Hooks.OverrideEVMResetArgs() receives params.Rules #38

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
// Reset resets the EVM with a new transaction context.Reset
// This is not threadsafe and should only be done very cautiously.
func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) {
evm.TxContext, evm.StateDB = overrideEVMResetArgs(txCtx, statedb)
evm.TxContext, evm.StateDB = evm.overrideEVMResetArgs(txCtx, statedb)
}

// Cancel cancels any running EVM operation. This may be called concurrently and
Expand Down
30 changes: 19 additions & 11 deletions core/vm/evm.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/params"
Expand All @@ -27,23 +28,25 @@ import (
type evmArgOverrider struct {
newEVMchainID int64

resetTxCtx TxContext
resetStateDB StateDB
gotResetChainID *big.Int
resetTxContextTo TxContext
resetStateDBTo StateDB
}

func (o evmArgOverrider) OverrideNewEVMArgs(args *NewEVMArgs) *NewEVMArgs {
func (o *evmArgOverrider) OverrideNewEVMArgs(args *NewEVMArgs) *NewEVMArgs {
args.ChainConfig = &params.ChainConfig{ChainID: big.NewInt(o.newEVMchainID)}
return args
}

func (o evmArgOverrider) OverrideEVMResetArgs(*EVMResetArgs) *EVMResetArgs {
func (o *evmArgOverrider) OverrideEVMResetArgs(r params.Rules, _ *EVMResetArgs) *EVMResetArgs {
o.gotResetChainID = r.ChainID
return &EVMResetArgs{
TxContext: o.resetTxCtx,
StateDB: o.resetStateDB,
TxContext: o.resetTxContextTo,
StateDB: o.resetStateDBTo,
}
}

func (o evmArgOverrider) register(t *testing.T) {
func (o *evmArgOverrider) register(t *testing.T) {
t.Helper()
libevmHooks = nil
RegisterHooks(o)
Expand Down Expand Up @@ -71,15 +74,20 @@ func TestOverrideEVMResetArgs(t *testing.T) {
// Equivalent to rationale for TestOverrideNewEVMArgs above.
var _ func(TxContext, StateDB) = (*EVM)(nil).Reset

const gasPrice = 1357924680
hooks := evmArgOverrider{
resetTxCtx: TxContext{
const (
chainID = 0xc0ffee
gasPrice = 1357924680
)
hooks := &evmArgOverrider{
newEVMchainID: chainID,
resetTxContextTo: TxContext{
GasPrice: big.NewInt(gasPrice),
},
}
hooks.register(t)

evm := NewEVM(BlockContext{}, TxContext{}, nil, nil, Config{})
evm.Reset(TxContext{}, nil)
require.Equalf(t, big.NewInt(gasPrice), evm.GasPrice, "%T.GasPrice set by Reset() hook", evm)
assert.Equalf(t, big.NewInt(chainID), hooks.gotResetChainID, "%T.ChainID passed to Reset() hook", params.Rules{})
assert.Equalf(t, big.NewInt(gasPrice), evm.GasPrice, "%T.GasPrice set by Reset() hook", evm)
}
6 changes: 3 additions & 3 deletions core/vm/hooks.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var libevmHooks Hooks
// See [RegisterHooks].
type Hooks interface {
OverrideNewEVMArgs(*NewEVMArgs) *NewEVMArgs
OverrideEVMResetArgs(*EVMResetArgs) *EVMResetArgs
OverrideEVMResetArgs(params.Rules, *EVMResetArgs) *EVMResetArgs
}

// NewEVMArgs are the arguments received by [NewEVM], available for override
Expand Down Expand Up @@ -67,10 +67,10 @@ func overrideNewEVMArgs(
return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config
}

func overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) {
func (evm *EVM) overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) {
if libevmHooks == nil {
return txCtx, statedb
}
args := libevmHooks.OverrideEVMResetArgs(&EVMResetArgs{txCtx, statedb})
args := libevmHooks.OverrideEVMResetArgs(evm.chainRules, &EVMResetArgs{txCtx, statedb})
return args.TxContext, args.StateDB
}