17
17
package vm
18
18
19
19
import (
20
- "errors"
21
20
"math/big"
22
21
"sync/atomic"
23
22
"time"
@@ -58,24 +57,6 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
58
57
return p , ok
59
58
}
60
59
61
- // run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
62
- func run (evm * EVM , contract * Contract , input []byte , readOnly bool ) ([]byte , error ) {
63
- for _ , interpreter := range evm .interpreters {
64
- if interpreter .CanRun (contract .Code ) {
65
- if evm .interpreter != interpreter {
66
- // Ensure that the interpreter pointer is set back
67
- // to its current value upon return.
68
- defer func (i Interpreter ) {
69
- evm .interpreter = i
70
- }(evm .interpreter )
71
- evm .interpreter = interpreter
72
- }
73
- return interpreter .Run (contract , input , readOnly )
74
- }
75
- }
76
- return nil , errors .New ("no compatible interpreter" )
77
- }
78
-
79
60
// BlockContext provides the EVM with auxiliary information. Once provided
80
61
// it shouldn't be modified.
81
62
type BlockContext struct {
@@ -131,8 +112,7 @@ type EVM struct {
131
112
Config Config
132
113
// global (to this context) ethereum virtual machine
133
114
// used throughout the execution of the tx.
134
- interpreters []Interpreter
135
- interpreter Interpreter
115
+ interpreter * EVMInterpreter
136
116
// abort is used to abort the EVM calling operations
137
117
// NOTE: must be set atomically
138
118
abort int32
@@ -146,36 +126,14 @@ type EVM struct {
146
126
// only ever be used *once*.
147
127
func NewEVM (blockCtx BlockContext , txCtx TxContext , statedb StateDB , chainConfig * params.ChainConfig , config Config ) * EVM {
148
128
evm := & EVM {
149
- Context : blockCtx ,
150
- TxContext : txCtx ,
151
- StateDB : statedb ,
152
- Config : config ,
153
- chainConfig : chainConfig ,
154
- chainRules : chainConfig .Rules (blockCtx .BlockNumber ),
155
- interpreters : make ([]Interpreter , 0 , 1 ),
156
- }
157
-
158
- if chainConfig .IsEWASM (blockCtx .BlockNumber ) {
159
- // to be implemented by EVM-C and Wagon PRs.
160
- // if vmConfig.EWASMInterpreter != "" {
161
- // extIntOpts := strings.Split(vmConfig.EWASMInterpreter, ":")
162
- // path := extIntOpts[0]
163
- // options := []string{}
164
- // if len(extIntOpts) > 1 {
165
- // options = extIntOpts[1..]
166
- // }
167
- // evm.interpreters = append(evm.interpreters, NewEVMVCInterpreter(evm, vmConfig, options))
168
- // } else {
169
- // evm.interpreters = append(evm.interpreters, NewEWASMInterpreter(evm, vmConfig))
170
- // }
171
- panic ("No supported ewasm interpreter yet." )
172
- }
173
-
174
- // vmConfig.EVMInterpreter will be used by EVM-C, it won't be checked here
175
- // as we always want to have the built-in EVM as the failover option.
176
- evm .interpreters = append (evm .interpreters , NewEVMInterpreter (evm , config ))
177
- evm .interpreter = evm .interpreters [0 ]
178
-
129
+ Context : blockCtx ,
130
+ TxContext : txCtx ,
131
+ StateDB : statedb ,
132
+ Config : config ,
133
+ chainConfig : chainConfig ,
134
+ chainRules : chainConfig .Rules (blockCtx .BlockNumber ),
135
+ }
136
+ evm .interpreter = NewEVMInterpreter (evm , config )
179
137
return evm
180
138
}
181
139
@@ -198,7 +156,7 @@ func (evm *EVM) Cancelled() bool {
198
156
}
199
157
200
158
// Interpreter returns the current interpreter
201
- func (evm * EVM ) Interpreter () Interpreter {
159
+ func (evm * EVM ) Interpreter () * EVMInterpreter {
202
160
return evm .interpreter
203
161
}
204
162
@@ -256,7 +214,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
256
214
// The depth-check is already done, and precompiles handled above
257
215
contract := NewContract (caller , AccountRef (addrCopy ), value , gas )
258
216
contract .SetCallCode (& addrCopy , evm .StateDB .GetCodeHash (addrCopy ), code )
259
- ret , err = run ( evm , contract , input , false )
217
+ ret , err = evm . interpreter . Run ( contract , input , false )
260
218
gas = contract .Gas
261
219
}
262
220
}
@@ -308,7 +266,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
308
266
// The contract is a scoped environment for this execution context only.
309
267
contract := NewContract (caller , AccountRef (caller .Address ()), value , gas )
310
268
contract .SetCallCode (& addrCopy , evm .StateDB .GetCodeHash (addrCopy ), evm .StateDB .GetCode (addrCopy ))
311
- ret , err = run ( evm , contract , input , false )
269
+ ret , err = evm . interpreter . Run ( contract , input , false )
312
270
gas = contract .Gas
313
271
}
314
272
if err != nil {
@@ -343,7 +301,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
343
301
// Initialise a new contract and make initialise the delegate values
344
302
contract := NewContract (caller , AccountRef (caller .Address ()), nil , gas ).AsDelegate ()
345
303
contract .SetCallCode (& addrCopy , evm .StateDB .GetCodeHash (addrCopy ), evm .StateDB .GetCode (addrCopy ))
346
- ret , err = run ( evm , contract , input , false )
304
+ ret , err = evm . interpreter . Run ( contract , input , false )
347
305
gas = contract .Gas
348
306
}
349
307
if err != nil {
@@ -394,7 +352,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
394
352
// When an error was returned by the EVM or when setting the creation code
395
353
// above we revert to the snapshot and consume any gas remaining. Additionally
396
354
// when we're in Homestead this also counts for code storage gas errors.
397
- ret , err = run ( evm , contract , input , true )
355
+ ret , err = evm . interpreter . Run ( contract , input , true )
398
356
gas = contract .Gas
399
357
}
400
358
if err != nil {
@@ -462,7 +420,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
462
420
}
463
421
start := time .Now ()
464
422
465
- ret , err := run ( evm , contract , nil , false )
423
+ ret , err := evm . interpreter . Run ( contract , nil , false )
466
424
467
425
// Check whether the max code size has been exceeded, assign err if the case.
468
426
if err == nil && evm .chainRules .IsEIP158 && len (ret ) > params .MaxCodeSize {
0 commit comments