From 6f25e3427ecadaeba2c14d8568ccf0fad4cb21c4 Mon Sep 17 00:00:00 2001 From: inphi Date: Mon, 12 Feb 2024 11:15:34 -0500 Subject: [PATCH 1/2] core/vm: Allow precompiles to be overriden --- core/vm/evm.go | 7 +++++++ core/vm/interpreter.go | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index de94912f21..cae622cb7b 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -53,6 +53,13 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { precompiles = PrecompiledContractsHomestead } p, ok := precompiles[addr] + // Restrict overrides to known precompiles + if evm.chainConfig.IsOptimism() && evm.Config.OptimismPrecompileOverrides != nil { + override, ok := evm.Config.OptimismPrecompileOverrides(evm.chainRules, addr) + if ok { + return override, ok + } + } return p, ok } diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 28da2e80e6..002701d648 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -21,14 +21,19 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" ) +// PrecompileOverrides is a function that can be used to override the default precompiled contracts +type PrecompileOverrides func(params.Rules, common.Address) (PrecompiledContract, bool) + // Config are the configuration options for the Interpreter type Config struct { - Tracer EVMLogger // Opcode logger - NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls) - EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages - ExtraEips []int // Additional EIPS that are to be enabled + Tracer EVMLogger // Opcode logger + NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls) + EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages + ExtraEips []int // Additional EIPS that are to be enabled + OptimismPrecompileOverrides PrecompileOverrides // Precompile overrides for Optimism } // ScopeContext contains the things that are per-call, such as stack and memory, From e965f15396251313b166ebe504e4227ba8590d62 Mon Sep 17 00:00:00 2001 From: Inphi Date: Mon, 12 Feb 2024 18:17:19 -0500 Subject: [PATCH 2/2] Update core/vm/evm.go Co-authored-by: Adrian Sutton --- core/vm/evm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index cae622cb7b..b02988c122 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -54,7 +54,7 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { } p, ok := precompiles[addr] // Restrict overrides to known precompiles - if evm.chainConfig.IsOptimism() && evm.Config.OptimismPrecompileOverrides != nil { + if ok && evm.chainConfig.IsOptimism() && evm.Config.OptimismPrecompileOverrides != nil { override, ok := evm.Config.OptimismPrecompileOverrides(evm.chainRules, addr) if ok { return override, ok