Skip to content

Commit f9fb70d

Browse files
committed
core/vm: rework reversion to work on a higher level
1 parent b70a73c commit f9fb70d

6 files changed

Lines changed: 53 additions & 45 deletions

File tree

‎core/vm/evm.go‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
168168
// above we revert to the snapshot and consume any gas remaining. Additionally
169169
// when we're in homestead this also counts for code storage gas errors.
170170
if err != nil {
171-
contract.UseGas(contract.Gas)
172171
evm.StateDB.RevertToSnapshot(snapshot)
172+
if err != errExecutionReverted {
173+
contract.UseGas(contract.Gas)
174+
}
173175
}
174176
return ret, contract.Gas, err
175177
}
@@ -207,10 +209,11 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
207209

208210
ret, err = run(evm, snapshot, contract, input)
209211
if err != nil {
210-
contract.UseGas(contract.Gas)
211212
evm.StateDB.RevertToSnapshot(snapshot)
213+
if err != errExecutionReverted {
214+
contract.UseGas(contract.Gas)
215+
}
212216
}
213-
214217
return ret, contract.Gas, err
215218
}
216219

@@ -239,10 +242,11 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
239242

240243
ret, err = run(evm, snapshot, contract, input)
241244
if err != nil {
242-
contract.UseGas(contract.Gas)
243245
evm.StateDB.RevertToSnapshot(snapshot)
246+
if err != errExecutionReverted {
247+
contract.UseGas(contract.Gas)
248+
}
244249
}
245-
246250
return ret, contract.Gas, err
247251
}
248252

@@ -281,8 +285,10 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
281285
// when we're in Homestead this also counts for code storage gas errors.
282286
ret, err = run(evm, snapshot, contract, input)
283287
if err != nil {
284-
contract.UseGas(contract.Gas)
285288
evm.StateDB.RevertToSnapshot(snapshot)
289+
if err != errExecutionReverted {
290+
contract.UseGas(contract.Gas)
291+
}
286292
}
287293
return ret, contract.Gas, err
288294
}
@@ -339,18 +345,12 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
339345
// When an error was returned by the EVM or when setting the creation code
340346
// above we revert to the snapshot and consume any gas remaining. Additionally
341347
// when we're in homestead this also counts for code storage gas errors.
342-
if maxCodeSizeExceeded ||
343-
(err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) {
344-
contract.UseGas(contract.Gas)
348+
if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) {
345349
evm.StateDB.RevertToSnapshot(snapshot)
350+
if err != errExecutionReverted {
351+
contract.UseGas(contract.Gas)
352+
}
346353
}
347-
// If the vm returned with an error the return value should be set to nil.
348-
// This isn't consensus critical but merely to for behaviour reasons such as
349-
// tests, RPC calls, etc.
350-
if err != nil {
351-
ret = nil
352-
}
353-
354354
return ret, contractAddr, contract.Gas, err
355355
}
356356

‎core/vm/gas_table.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
396396
return memoryGasCost(mem, memorySize)
397397
}
398398

399+
func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
400+
return memoryGasCost(mem, memorySize)
401+
}
402+
399403
func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
400404
var gas uint64
401405
// EIP150 homestead gas reprice fork:

‎core/vm/instructions.go‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
bigZero = new(big.Int)
3333
errWriteProtection = errors.New("evm: write protection")
3434
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
35+
errExecutionReverted = errors.New("evm: execution reverted")
3536
)
3637

3738
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@@ -579,7 +580,7 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
579580
}
580581

581582
contract.UseGas(gas)
582-
_, addr, returnGas, suberr := evm.Create(contract, input, gas, value)
583+
res, addr, returnGas, suberr := evm.Create(contract, input, gas, value)
583584
// Push item on the stack based on the returned error. If the ruleset is
584585
// homestead we must check for CodeStoreOutOfGasError (homestead only
585586
// rule) and treat as an error, if the ruleset is frontier we must
@@ -592,9 +593,11 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
592593
stack.push(addr.Big())
593594
}
594595
contract.Gas += returnGas
595-
596596
evm.interpreter.intPool.put(value, offset, size)
597597

598+
if suberr == errExecutionReverted {
599+
return res, nil
600+
}
598601
return nil, nil
599602
}
600603

@@ -622,7 +625,8 @@ func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
622625
stack.push(new(big.Int))
623626
} else {
624627
stack.push(big.NewInt(1))
625-
628+
}
629+
if err == nil || err == errExecutionReverted {
626630
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
627631
}
628632
contract.Gas += returnGas
@@ -653,10 +657,10 @@ func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
653657
ret, returnGas, err := evm.CallCode(contract, address, args, gas, value)
654658
if err != nil {
655659
stack.push(new(big.Int))
656-
657660
} else {
658661
stack.push(big.NewInt(1))
659-
662+
}
663+
if err == nil || err == errExecutionReverted {
660664
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
661665
}
662666
contract.Gas += returnGas
@@ -676,6 +680,8 @@ func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st
676680
stack.push(new(big.Int))
677681
} else {
678682
stack.push(big.NewInt(1))
683+
}
684+
if err == nil || err == errExecutionReverted {
679685
memory.Set(outOffset.Uint64(), outSize.Uint64(), ret)
680686
}
681687
contract.Gas += returnGas
@@ -704,7 +710,8 @@ func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac
704710
stack.push(new(big.Int))
705711
} else {
706712
stack.push(big.NewInt(1))
707-
713+
}
714+
if err == nil || err == errExecutionReverted {
708715
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
709716
}
710717
contract.Gas += returnGas

‎core/vm/interpreter.go‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,22 @@ func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret
209209
if verifyPool {
210210
verifyIntegerPool(in.intPool)
211211
}
212-
// checks whether the operation should revert state.
213-
if operation.reverts {
214-
in.evm.StateDB.RevertToSnapshot(snapshot)
212+
// if the operation clears the return data (e.g. it has returning data)
213+
// set the last return to the result of the operation.
214+
if operation.returns {
215+
in.returnData = res
215216
}
216217

217218
switch {
218219
case err != nil:
219220
return nil, err
221+
case operation.reverts:
222+
return res, errExecutionReverted
220223
case operation.halts:
221224
return res, nil
222225
case !operation.jumps:
223226
pc++
224227
}
225-
// if the operation clears the return data (e.g. it has returning data)
226-
// set the last return to the result of the operation.
227-
if operation.returns {
228-
in.returnData = res
229-
}
230228
}
231229
return nil, nil
232230
}

‎core/vm/jump_table.go‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,13 @@ type operation struct {
4141
validateStack stackValidationFunc
4242
// memorySize returns the memory size required for the operation
4343
memorySize memorySizeFunc
44-
// halts indicates whether the operation shoult halt further execution
45-
// and return
46-
halts bool
47-
// jumps indicates whether operation made a jump. This prevents the program
48-
// counter from further incrementing.
49-
jumps bool
50-
// writes determines whether this a state modifying operation
51-
writes bool
52-
// valid is used to check whether the retrieved operation is valid and known
53-
valid bool
54-
// reverts determined whether the operation reverts state
55-
reverts bool
56-
// returns determines whether the opertions sets the return data
57-
returns bool
44+
45+
halts bool // indicates whether the operation shoult halt further execution
46+
jumps bool // indicates whether the program counter should not increment
47+
writes bool // determines whether this a state modifying operation
48+
valid bool // indication whether the retrieved operation is valid and known
49+
reverts bool // determines whether the operation reverts state (implicitly halts)
50+
returns bool // determines whether the opertions sets the return data content
5851
}
5952

6053
var (
@@ -91,10 +84,12 @@ func NewMetropolisInstructionSet() [256]operation {
9184
}
9285
instructionSet[REVERT] = operation{
9386
execute: opRevert,
94-
gasCost: constGasFunc(GasFastestStep),
87+
gasCost: gasRevert,
9588
validateStack: makeStackFunc(2, 0),
89+
memorySize: memoryRevert,
9690
valid: true,
9791
reverts: true,
92+
returns: true,
9893
}
9994
return instructionSet
10095
}

‎core/vm/memory_table.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func memoryReturn(stack *Stack) *big.Int {
8989
return calcMemSize(stack.Back(0), stack.Back(1))
9090
}
9191

92+
func memoryRevert(stack *Stack) *big.Int {
93+
return calcMemSize(stack.Back(0), stack.Back(1))
94+
}
95+
9296
func memoryLog(stack *Stack) *big.Int {
9397
mSize, mStart := stack.Back(1), stack.Back(0)
9498
return calcMemSize(mStart, mSize)

0 commit comments

Comments
 (0)