Skip to content

Commit b70a73c

Browse files
obscurenkaralabe
authored andcommitted
core/vm: implement REVERT metropolis opcode
1 parent 0b978f9 commit b70a73c

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎core/vm/instructions.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,14 @@ func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
718718
ret := memory.GetPtr(offset.Int64(), size.Int64())
719719

720720
evm.interpreter.intPool.put(offset, size)
721+
return ret, nil
722+
}
721723

724+
func opRevert(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
725+
offset, size := stack.pop(), stack.pop()
726+
ret := memory.GetPtr(offset.Int64(), size.Int64())
727+
728+
evm.interpreter.intPool.put(offset, size)
722729
return ret, nil
723730
}
724731

@@ -731,7 +738,6 @@ func opSuicide(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *
731738
evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
732739

733740
evm.StateDB.Suicide(contract.Address())
734-
735741
return nil, nil
736742
}
737743

‎core/vm/interpreter.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ 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)
215+
}
212216

213217
switch {
214218
case err != nil:

‎core/vm/jump_table.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ func NewMetropolisInstructionSet() [256]operation {
8989
memorySize: memoryReturnDataCopy,
9090
valid: true,
9191
}
92+
instructionSet[REVERT] = operation{
93+
execute: opRevert,
94+
gasCost: constGasFunc(GasFastestStep),
95+
validateStack: makeStackFunc(2, 0),
96+
valid: true,
97+
reverts: true,
98+
}
9299
return instructionSet
93100
}
94101

‎core/vm/opcodes.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ const (
204204
DELEGATECALL
205205
STATICCALL = 0xfa
206206

207+
REVERT = 0xfd
207208
SELFDESTRUCT = 0xff
208209
)
209210

@@ -360,6 +361,7 @@ var opCodeToString = map[OpCode]string{
360361
CALLCODE: "CALLCODE",
361362
DELEGATECALL: "DELEGATECALL",
362363
STATICCALL: "STATICCALL",
364+
REVERT: "REVERT",
363365
SELFDESTRUCT: "SELFDESTRUCT",
364366

365367
PUSH: "PUSH",
@@ -509,6 +511,7 @@ var stringToOp = map[string]OpCode{
509511
"CALL": CALL,
510512
"RETURN": RETURN,
511513
"CALLCODE": CALLCODE,
514+
"REVERT": REVERT,
512515
"SELFDESTRUCT": SELFDESTRUCT,
513516
}
514517

0 commit comments

Comments
 (0)