Skip to content

Commit 7abedf9

Browse files
authored
core/vm: support for multiple interpreters (#17093)
- Define an Interpreter interface - One contract can call contracts from other interpreter types. - Pass the interpreter to the operands instead of the evm. This is meant to prevent type assertions in operands.
1 parent 27a278e commit 7abedf9

6 files changed

Lines changed: 315 additions & 240 deletions

File tree

‎core/vm/errors.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ var (
2626
ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
2727
ErrInsufficientBalance = errors.New("insufficient balance for transfer")
2828
ErrContractAddressCollision = errors.New("contract address collision")
29+
ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
2930
)

‎core/vm/evm.go‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
5151
return RunPrecompiledContract(p, input, contract)
5252
}
5353
}
54-
return evm.interpreter.Run(contract, input)
54+
for _, interpreter := range evm.interpreters {
55+
if interpreter.CanRun(contract.Code) {
56+
if evm.interpreter != interpreter {
57+
// Ensure that the interpreter pointer is set back
58+
// to its current value upon return.
59+
defer func(i Interpreter) {
60+
evm.interpreter = i
61+
}(evm.interpreter)
62+
evm.interpreter = interpreter
63+
}
64+
return interpreter.Run(contract, input)
65+
}
66+
}
67+
return nil, ErrNoCompatibleInterpreter
5568
}
5669

5770
// Context provides the EVM with auxiliary information. Once provided
@@ -103,7 +116,8 @@ type EVM struct {
103116
vmConfig Config
104117
// global (to this context) ethereum virtual machine
105118
// used throughout the execution of the tx.
106-
interpreter *Interpreter
119+
interpreters []Interpreter
120+
interpreter Interpreter
107121
// abort is used to abort the EVM calling operations
108122
// NOTE: must be set atomically
109123
abort int32
@@ -117,14 +131,17 @@ type EVM struct {
117131
// only ever be used *once*.
118132
func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *EVM {
119133
evm := &EVM{
120-
Context: ctx,
121-
StateDB: statedb,
122-
vmConfig: vmConfig,
123-
chainConfig: chainConfig,
124-
chainRules: chainConfig.Rules(ctx.BlockNumber),
134+
Context: ctx,
135+
StateDB: statedb,
136+
vmConfig: vmConfig,
137+
chainConfig: chainConfig,
138+
chainRules: chainConfig.Rules(ctx.BlockNumber),
139+
interpreters: make([]Interpreter, 1),
125140
}
126141

127-
evm.interpreter = NewInterpreter(evm, vmConfig)
142+
evm.interpreters[0] = NewEVMInterpreter(evm, vmConfig)
143+
evm.interpreter = evm.interpreters[0]
144+
128145
return evm
129146
}
130147

@@ -134,6 +151,11 @@ func (evm *EVM) Cancel() {
134151
atomic.StoreInt32(&evm.abort, 1)
135152
}
136153

154+
// Interpreter returns the current interpreter
155+
func (evm *EVM) Interpreter() Interpreter {
156+
return evm.interpreter
157+
}
158+
137159
// Call executes the contract associated with the addr with the given input as
138160
// parameters. It also handles any necessary value transfer required and takes
139161
// the necessary steps to create accounts and reverses the state in case of an
@@ -291,9 +313,9 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
291313
// Make sure the readonly is only set if we aren't in readonly yet
292314
// this makes also sure that the readonly flag isn't removed for
293315
// child calls.
294-
if !evm.interpreter.readOnly {
295-
evm.interpreter.readOnly = true
296-
defer func() { evm.interpreter.readOnly = false }()
316+
if !evm.interpreter.IsReadOnly() {
317+
evm.interpreter.SetReadOnly(true)
318+
defer func() { evm.interpreter.SetReadOnly(false) }()
297319
}
298320

299321
var (
@@ -414,6 +436,3 @@ func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *
414436

415437
// ChainConfig returns the environment's chain configuration
416438
func (evm *EVM) ChainConfig() *params.ChainConfig { return evm.chainConfig }
417-
418-
// Interpreter returns the EVM interpreter
419-
func (evm *EVM) Interpreter() *Interpreter { return evm.interpreter }

0 commit comments

Comments
 (0)