Skip to content

Commit ae992a5

Browse files
chfastgballet
authored andcommitted
core/vm: Hide read only flag from Interpreter interface (#17461)
Makes Interface interface a bit more stateless and abstract. Obviously this change is dictated by EVMC design. The EVMC tries to keep the responsibility for EVM features totally inside the VMs, if feasible. This makes VM "stateless" because VM does not need to pass any information between executions, all information is included in parameters of the execute function.
1 parent 8b9b149 commit ae992a5

3 files changed

Lines changed: 17 additions & 31 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type (
4141
)
4242

4343
// run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
44-
func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
44+
func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, error) {
4545
if contract.CodeAddr != nil {
4646
precompiles := PrecompiledContractsHomestead
4747
if evm.ChainConfig().IsByzantium(evm.BlockNumber) {
@@ -61,7 +61,7 @@ func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
6161
}(evm.interpreter)
6262
evm.interpreter = interpreter
6363
}
64-
return interpreter.Run(contract, input)
64+
return interpreter.Run(contract, input, readOnly)
6565
}
6666
}
6767
return nil, ErrNoCompatibleInterpreter
@@ -210,7 +210,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
210210
evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
211211
}()
212212
}
213-
ret, err = run(evm, contract, input)
213+
ret, err = run(evm, contract, input, false)
214214

215215
// When an error was returned by the EVM or when setting the creation code
216216
// above we revert to the snapshot and consume any gas remaining. Additionally
@@ -255,7 +255,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
255255
contract := NewContract(caller, to, value, gas)
256256
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
257257

258-
ret, err = run(evm, contract, input)
258+
ret, err = run(evm, contract, input, false)
259259
if err != nil {
260260
evm.StateDB.RevertToSnapshot(snapshot)
261261
if err != errExecutionReverted {
@@ -288,7 +288,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
288288
contract := NewContract(caller, to, nil, gas).AsDelegate()
289289
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
290290

291-
ret, err = run(evm, contract, input)
291+
ret, err = run(evm, contract, input, false)
292292
if err != nil {
293293
evm.StateDB.RevertToSnapshot(snapshot)
294294
if err != errExecutionReverted {
@@ -310,13 +310,6 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
310310
if evm.depth > int(params.CallCreateDepth) {
311311
return nil, gas, ErrDepth
312312
}
313-
// Make sure the readonly is only set if we aren't in readonly yet
314-
// this makes also sure that the readonly flag isn't removed for
315-
// child calls.
316-
if !evm.interpreter.IsReadOnly() {
317-
evm.interpreter.SetReadOnly(true)
318-
defer func() { evm.interpreter.SetReadOnly(false) }()
319-
}
320313

321314
var (
322315
to = AccountRef(addr)
@@ -331,7 +324,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
331324
// When an error was returned by the EVM or when setting the creation code
332325
// above we revert to the snapshot and consume any gas remaining. Additionally
333326
// when we're in Homestead this also counts for code storage gas errors.
334-
ret, err = run(evm, contract, input)
327+
ret, err = run(evm, contract, input, true)
335328
if err != nil {
336329
evm.StateDB.RevertToSnapshot(snapshot)
337330
if err != errExecutionReverted {
@@ -382,7 +375,7 @@ func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.I
382375
}
383376
start := time.Now()
384377

385-
ret, err := run(evm, contract, nil)
378+
ret, err := run(evm, contract, nil, false)
386379

387380
// check whether the max code size has been exceeded
388381
maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockNumber) && len(ret) > params.MaxCodeSize

‎core/vm/interpreter.go‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Config struct {
4848
type Interpreter interface {
4949
// Run loops and evaluates the contract's code with the given input data and returns
5050
// the return byte-slice and an error if one occurred.
51-
Run(contract *Contract, input []byte) ([]byte, error)
51+
Run(contract *Contract, input []byte, static bool) ([]byte, error)
5252
// CanRun tells if the contract, passed as an argument, can be
5353
// run by the current interpreter. This is meant so that the
5454
// caller can do something like:
@@ -61,10 +61,6 @@ type Interpreter interface {
6161
// }
6262
// ```
6363
CanRun([]byte) bool
64-
// IsReadOnly reports if the interpreter is in read only mode.
65-
IsReadOnly() bool
66-
// SetReadOnly sets (or unsets) read only mode in the interpreter.
67-
SetReadOnly(bool)
6864
}
6965

7066
// EVMInterpreter represents an EVM interpreter
@@ -125,7 +121,7 @@ func (in *EVMInterpreter) enforceRestrictions(op OpCode, operation operation, st
125121
// It's important to note that any errors returned by the interpreter should be
126122
// considered a revert-and-consume-all-gas operation except for
127123
// errExecutionReverted which means revert-and-keep-gas-left.
128-
func (in *EVMInterpreter) Run(contract *Contract, input []byte) (ret []byte, err error) {
124+
func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) {
129125
if in.intPool == nil {
130126
in.intPool = poolOfIntPools.get()
131127
defer func() {
@@ -138,6 +134,13 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte) (ret []byte, err
138134
in.evm.depth++
139135
defer func() { in.evm.depth-- }()
140136

137+
// Make sure the readOnly is only set if we aren't in readOnly yet.
138+
// This makes also sure that the readOnly flag isn't removed for child calls.
139+
if readOnly && !in.readOnly {
140+
in.readOnly = true
141+
defer func() { in.readOnly = false }()
142+
}
143+
141144
// Reset the previous call's return data. It's unimportant to preserve the old buffer
142145
// as every returning call will return new data anyway.
143146
in.returnData = nil
@@ -263,13 +266,3 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte) (ret []byte, err
263266
func (in *EVMInterpreter) CanRun(code []byte) bool {
264267
return true
265268
}
266-
267-
// IsReadOnly reports if the interpreter is in read only mode.
268-
func (in *EVMInterpreter) IsReadOnly() bool {
269-
return in.readOnly
270-
}
271-
272-
// SetReadOnly sets (or unsets) read only mode in the interpreter.
273-
func (in *EVMInterpreter) SetReadOnly(ro bool) {
274-
in.readOnly = ro
275-
}

‎eth/tracers/tracer_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func runTrace(tracer *Tracer) (json.RawMessage, error) {
4949
contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
5050
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
5151

52-
_, err := env.Interpreter().Run(contract, []byte{})
52+
_, err := env.Interpreter().Run(contract, []byte{}, false)
5353
if err != nil {
5454
return nil, err
5555
}

0 commit comments

Comments
 (0)