2021-04-12 17:13:30 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## EVM Opcode Handlers: System Operations
|
|
|
|
## ======================================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2021-04-21 17:04:54 +00:00
|
|
|
../../../db/accounts_cache,
|
2021-04-15 17:42:43 +00:00
|
|
|
../../../errors,
|
2021-04-26 16:00:46 +00:00
|
|
|
../../computation,
|
2021-04-22 16:05:58 +00:00
|
|
|
../../memory,
|
2021-04-21 17:04:54 +00:00
|
|
|
../../stack,
|
2021-04-22 16:05:58 +00:00
|
|
|
../../state,
|
|
|
|
../../types,
|
2021-04-21 17:04:54 +00:00
|
|
|
../gas_costs,
|
|
|
|
../gas_meter,
|
|
|
|
../op_codes,
|
2021-04-22 16:05:58 +00:00
|
|
|
../utils/utils_numeric,
|
2021-04-15 17:42:43 +00:00
|
|
|
./oph_defs,
|
|
|
|
./oph_helpers,
|
2021-04-21 17:04:54 +00:00
|
|
|
eth/common,
|
2021-04-15 17:42:43 +00:00
|
|
|
stint
|
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2021-04-20 12:07:01 +00:00
|
|
|
returnOp: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## 0xf3, Halt execution returning output data.
|
|
|
|
let (startPos, size) = k.cpt.stack.popInt(2)
|
|
|
|
|
|
|
|
let (pos, len) = (startPos.cleanMemRef, size.cleanMemRef)
|
|
|
|
k.cpt.gasMeter.consumeGas(
|
|
|
|
k.cpt.gasCosts[Return].m_handler(k.cpt.memory.len, pos, len),
|
|
|
|
reason = "RETURN")
|
|
|
|
k.cpt.memory.extend(pos, len)
|
|
|
|
k.cpt.output = k.cpt.memory.read(pos, len)
|
|
|
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
revertOp: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## 0xfd, Halt execution reverting state changes but returning data
|
|
|
|
## and remaining gas.
|
|
|
|
let (startPos, size) = k.cpt.stack.popInt(2)
|
|
|
|
|
|
|
|
let (pos, len) = (startPos.cleanMemRef, size.cleanMemRef)
|
|
|
|
k.cpt.gasMeter.consumeGas(
|
|
|
|
k.cpt.gasCosts[Revert].m_handler(k.cpt.memory.len, pos, len),
|
|
|
|
reason = "REVERT")
|
|
|
|
|
|
|
|
k.cpt.memory.extend(pos, len)
|
|
|
|
k.cpt.output = k.cpt.memory.read(pos, len)
|
|
|
|
# setError(msg, false) will signal cheap revert
|
|
|
|
k.cpt.setError("REVERT opcode executed", false)
|
|
|
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
invalidOp: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
raise newException(InvalidInstruction,
|
|
|
|
"Invalid instruction, received an opcode " &
|
|
|
|
"not implemented in the current fork.")
|
|
|
|
|
|
|
|
# -----------
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
selfDestructOp: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## 0xff, Halt execution and register account for later deletion.
|
|
|
|
let beneficiary = k.cpt.stack.popAddress()
|
|
|
|
k.cpt.selfDestruct(beneficiary)
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
selfDestructEIP150Op: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## selfDestructEip150 (auto generated comment)
|
|
|
|
let beneficiary = k.cpt.stack.popAddress()
|
|
|
|
|
|
|
|
let gasParams = GasParams(
|
|
|
|
kind: SelfDestruct,
|
|
|
|
sd_condition: not k.cpt.accountExists(beneficiary))
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
let gasCost =
|
|
|
|
k.cpt.gasCosts[SelfDestruct].c_handler(0.u256, gasParams).gasCost
|
|
|
|
k.cpt.gasMeter.consumeGas(
|
|
|
|
gasCost, reason = "SELFDESTRUCT EIP150")
|
|
|
|
k.cpt.selfDestruct(beneficiary)
|
|
|
|
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
selfDestructEIP161Op: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## selfDestructEip161 (auto generated comment)
|
|
|
|
checkInStaticContext(k.cpt)
|
|
|
|
|
|
|
|
let
|
|
|
|
beneficiary = k.cpt.stack.popAddress()
|
|
|
|
isDead = not k.cpt.accountExists(beneficiary)
|
|
|
|
balance = k.cpt.getBalance(k.cpt.msg.contractAddress)
|
|
|
|
|
|
|
|
let gasParams = GasParams(
|
|
|
|
kind: SelfDestruct,
|
|
|
|
sd_condition: isDead and not balance.isZero)
|
|
|
|
|
|
|
|
let gasCost =
|
|
|
|
k.cpt.gasCosts[SelfDestruct].c_handler(0.u256, gasParams).gasCost
|
|
|
|
k.cpt.gasMeter.consumeGas(
|
|
|
|
gasCost, reason = "SELFDESTRUCT EIP161")
|
|
|
|
k.cpt.selfDestruct(beneficiary)
|
|
|
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
selfDestructEIP2929Op: Vm2OpFn = proc(k: var Vm2Ctx) =
|
2021-04-15 17:42:43 +00:00
|
|
|
## selfDestructEIP2929 (auto generated comment)
|
|
|
|
checkInStaticContext(k.cpt)
|
|
|
|
|
|
|
|
let
|
|
|
|
beneficiary = k.cpt.stack.popAddress()
|
|
|
|
isDead = not k.cpt.accountExists(beneficiary)
|
|
|
|
balance = k.cpt.getBalance(k.cpt.msg.contractAddress)
|
|
|
|
|
|
|
|
let gasParams = GasParams(
|
|
|
|
kind: SelfDestruct,
|
|
|
|
sd_condition: isDead and not balance.isZero)
|
|
|
|
|
|
|
|
var gasCost =
|
|
|
|
k.cpt.gasCosts[SelfDestruct].c_handler(0.u256, gasParams).gasCost
|
|
|
|
|
|
|
|
k.cpt.vmState.mutateStateDB:
|
|
|
|
if not db.inAccessList(beneficiary):
|
|
|
|
db.accessList(beneficiary)
|
|
|
|
gasCost = gasCost + ColdAccountAccessCost
|
|
|
|
|
|
|
|
k.cpt.gasMeter.consumeGas(
|
|
|
|
gasCost, reason = "SELFDESTRUCT EIP161")
|
|
|
|
k.cpt.selfDestruct(beneficiary)
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public, op exec table entries
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2021-04-15 17:42:43 +00:00
|
|
|
vm2OpExecSysOp*: seq[Vm2OpExec] = @[
|
2021-04-12 17:13:30 +00:00
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
(opCode: Return, ## 0xf3, Halt execution returning output data.
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "returnOp",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "Halt execution returning output data",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: returnOp,
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
(opCode: Revert, ## 0xfd, Halt and revert state changes
|
2021-04-19 09:15:35 +00:00
|
|
|
forks: Vm2OpByzantiumAndLater,
|
|
|
|
name: "revert",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "Halt execution reverting state changes but returning data " &
|
|
|
|
"and remaining gas",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: revertOp,
|
|
|
|
post: vm2OpIgnore)),
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
(opCode: Invalid, ## 0xfe, invalid instruction.
|
2021-04-12 17:13:30 +00:00
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "invalidInstruction",
|
2021-04-12 17:13:30 +00:00
|
|
|
info: "Designated invalid instruction",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: invalidOp,
|
2021-04-15 17:42:43 +00:00
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, Halt execution, prep for later deletion
|
|
|
|
forks: Vm2OpAllForks - Vm2OpTangerineAndLater,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "selfDestruct",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "Halt execution and register account for later deletion",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: selfDestructOp,
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP150: self destruct, Tangerine
|
|
|
|
forks: Vm2OpTangerineAndLater - Vm2OpSpuriousAndLater,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "selfDestructEIP150",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "EIP150: Halt execution and register account for later deletion",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: selfDestructEIP150Op,
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP161: self destruct, Spurious and later
|
|
|
|
forks: Vm2OpSpuriousAndLater - Vm2OpBerlinAndLater,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "selfDestructEIP161",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "EIP161: Halt execution and register account for later deletion",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: selfDestructEIP161Op,
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP2929: self destruct, Berlin and later
|
|
|
|
forks: Vm2OpBerlinAndLater,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "selfDestructEIP2929",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "EIP2929: Halt execution and register account for later deletion",
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
run: selfDestructEIP2929Op,
|
2021-04-12 17:13:30 +00:00
|
|
|
post: vm2OpIgnore))]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|