Skip to content

Commit

Permalink
try a factory approach
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush committed Aug 29, 2024
1 parent 7684553 commit 34baeec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
15 changes: 8 additions & 7 deletions core/evm_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
"github.com/ethereum/go-ethereum/log"
)

var (
newEVM = vm.NewEVM
)
var defaultEVMFactory vm.EvmFactory

func init() {
vm.NewEVM = NewEVM
defaultEVMFactory = vm.DefaultEVMFactory()
vm.SetDefaultEVMFactory(&evmFactory{})
}

// IsProhibited returns true if [addr] is in the prohibited list of addresses which should
Expand All @@ -34,13 +33,15 @@ func IsProhibited(addr common.Address) bool {
return modules.ReservedAddress(addr)
}

type evmFactory struct{}

type EVM struct {
*vm.EVM

chainConfig *params.ChainConfig
}

func NewEVM(
func (*evmFactory) NewEVM(
blockCtx vm.BlockContext, txCtx vm.TxContext, statedb vm.StateDB,
chainConfig vm.ChainConfig, config vm.Config,
) *vm.EVM {
Expand All @@ -49,7 +50,7 @@ func NewEVM(
// If the chainConfig is not a params.ChainConfig, then we can't use the custom
// EVM implementation, so we fall back to the default implementation.
log.Warn("ChainConfig is not a *params.ChainConfig, falling back to default EVM")
return newEVM(blockCtx, txCtx, statedb, chainConfig, config)
return defaultEVMFactory.NewEVM(blockCtx, txCtx, statedb, chainConfig, config)
}
evm := &EVM{
chainConfig: customChainConfig,
Expand All @@ -58,7 +59,7 @@ func NewEVM(
config.DeployerAllowed = evm.DeployerAllowed
config.CustomPrecompiles = evm.CustomPrecompiles

evm.EVM = newEVM(blockCtx, txCtx, statedb, chainConfig, config)
evm.EVM = defaultEVMFactory.NewEVM(blockCtx, txCtx, statedb, chainConfig, config)
return evm.EVM
}

Expand Down
12 changes: 1 addition & 11 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,9 @@ type EVM struct {
callGasTemp uint64
}

type ChainConfig interface {
IsEIP158(blockNum *big.Int) bool
IsSubnetEVM(timestamp uint64) bool
IsCancun(blockNum *big.Int, timestamp uint64) bool
IsPrecompileEnabled(addr common.Address, timestamp uint64) bool
Rules(blockNum *big.Int, timestamp uint64) params.Rules
}

var NewEVM = newEVM

// NewEVM returns a new EVM. The returned EVM is not thread safe and should
// only ever be used *once*.
func newEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig ChainConfig, config Config) *EVM {
func (*evmFactory) NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig ChainConfig, config Config) *EVM {
// If basefee tracking is disabled (eth_call, eth_estimateGas, etc), and no
// gas prices were specified, lower the basefee to 0 to avoid breaking EVM
// invariants (basefee < feecap)
Expand Down
36 changes: 36 additions & 0 deletions core/vm/evm_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package vm

import (
"math/big"

"github.com/ava-labs/subnet-evm/params"
"github.com/ethereum/go-ethereum/common"
)

var defaultEVMFactory EvmFactory = &evmFactory{}

type evmFactory struct{}

type ChainConfig interface {
IsEIP158(blockNum *big.Int) bool
IsSubnetEVM(timestamp uint64) bool
IsCancun(blockNum *big.Int, timestamp uint64) bool
IsPrecompileEnabled(addr common.Address, timestamp uint64) bool
Rules(blockNum *big.Int, timestamp uint64) params.Rules
}

type EvmFactory interface {
NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig ChainConfig, config Config) *EVM
}

func DefaultEVMFactory() EvmFactory {
return defaultEVMFactory
}

func SetDefaultEVMFactory(factory EvmFactory) {
defaultEVMFactory = factory
}

func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig ChainConfig, config Config) *EVM {
return DefaultEVMFactory().NewEVM(blockCtx, txCtx, statedb, chainConfig, config)
}

0 comments on commit 34baeec

Please sign in to comment.