2021-04-12 17:13:30 +00:00
|
|
|
# Nimbus
|
2024-02-20 07:16:12 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-04-12 17:13:30 +00:00
|
|
|
# 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
|
|
|
|
## ======================================
|
|
|
|
##
|
|
|
|
|
2024-05-22 21:01:19 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
import
|
2024-06-18 00:55:35 +00:00
|
|
|
stew/assign2,
|
2024-06-07 08:24:32 +00:00
|
|
|
../../evm_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
|
|
|
../../types,
|
2021-04-21 17:04:54 +00:00
|
|
|
../gas_costs,
|
|
|
|
../op_codes,
|
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
|
|
|
|
|
2022-09-28 06:09:33 +00:00
|
|
|
when not defined(evmc_enabled):
|
|
|
|
import
|
|
|
|
../../state,
|
2023-12-12 19:12:56 +00:00
|
|
|
../../../db/ledger
|
2022-09-28 06:09:33 +00:00
|
|
|
|
2021-04-12 17:13:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc returnOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## 0xf3, Halt execution returning output data.
|
2024-07-19 01:44:01 +00:00
|
|
|
? cpt.stack.lsCheck(2)
|
2024-07-18 11:59:53 +00:00
|
|
|
let
|
|
|
|
pos = cpt.stack.lsPeekMemRef(^1)
|
|
|
|
len = cpt.stack.lsPeekMemRef(^2)
|
|
|
|
cpt.stack.lsShrink(2)
|
2024-06-17 16:13:38 +00:00
|
|
|
|
2024-07-18 11:59:53 +00:00
|
|
|
? cpt.opcodeGasCost(Return,
|
|
|
|
cpt.gasCosts[Return].m_handler(cpt.memory.len, pos, len),
|
2024-06-17 16:13:38 +00:00
|
|
|
reason = "RETURN")
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
cpt.memory.extend(pos, len)
|
|
|
|
assign(cpt.output, cpt.memory.read(pos, len))
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc revertOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## 0xfd, Halt execution reverting state changes but returning data
|
|
|
|
## and remaining gas.
|
2024-07-19 01:44:01 +00:00
|
|
|
? cpt.stack.lsCheck(2)
|
2024-07-18 11:59:53 +00:00
|
|
|
let
|
|
|
|
pos = cpt.stack.lsPeekMemRef(^1)
|
|
|
|
len = cpt.stack.lsPeekMemRef(^2)
|
|
|
|
cpt.stack.lsShrink(2)
|
2024-06-17 16:13:38 +00:00
|
|
|
|
2024-07-18 11:59:53 +00:00
|
|
|
? cpt.opcodeGasCost(Revert,
|
|
|
|
cpt.gasCosts[Revert].m_handler(cpt.memory.len, pos, len),
|
2024-06-17 16:13:38 +00:00
|
|
|
reason = "REVERT")
|
|
|
|
|
2024-07-18 11:59:53 +00:00
|
|
|
cpt.memory.extend(pos, len)
|
|
|
|
assign(cpt.output, cpt.memory.read(pos, len))
|
2024-06-17 16:13:38 +00:00
|
|
|
# setError(msg, false) will signal cheap revert
|
2024-07-18 11:59:53 +00:00
|
|
|
cpt.setError(EVMC_REVERT, "REVERT opcode executed", false)
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc invalidOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
err(opErr(InvalidInstruction))
|
|
|
|
|
|
|
|
# -----------
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc selfDestructOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## 0xff, Halt execution and register account for later deletion.
|
2024-07-19 01:44:01 +00:00
|
|
|
let beneficiary = ? cpt.stack.popAddress()
|
2024-07-18 11:59:53 +00:00
|
|
|
|
2024-06-17 16:13:38 +00:00
|
|
|
when defined(evmc_enabled):
|
2024-07-19 01:44:01 +00:00
|
|
|
cpt.selfDestruct(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
else:
|
2024-07-19 01:44:01 +00:00
|
|
|
cpt.selfDestruct(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc selfDestructEIP150Op(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## selfDestructEip150 (auto generated comment)
|
2024-07-05 04:55:13 +00:00
|
|
|
let
|
|
|
|
beneficiary = ? cpt.stack.popAddress()
|
|
|
|
condition = not cpt.accountExists(beneficiary)
|
|
|
|
gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
? cpt.opcodeGasCost(SelfDestruct,
|
2024-07-05 04:55:13 +00:00
|
|
|
gasCost, reason = "SELFDESTRUCT EIP150")
|
|
|
|
cpt.selfDestruct(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc selfDestructEIP161Op(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## selfDestructEip161 (auto generated comment)
|
2024-07-19 01:44:01 +00:00
|
|
|
? cpt.checkInStaticContext()
|
2024-06-17 16:13:38 +00:00
|
|
|
|
2024-07-05 04:55:13 +00:00
|
|
|
let
|
|
|
|
beneficiary = ? cpt.stack.popAddress()
|
2024-07-19 01:44:01 +00:00
|
|
|
isDead = not cpt.accountExists(beneficiary)
|
|
|
|
balance = cpt.getBalance(cpt.msg.contractAddress)
|
|
|
|
condition = isDead and not balance.isZero
|
|
|
|
gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition)
|
2024-07-05 04:55:13 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
? cpt.opcodeGasCost(SelfDestruct,
|
2024-07-05 04:55:13 +00:00
|
|
|
gasCost, reason = "SELFDESTRUCT EIP161")
|
|
|
|
cpt.selfDestruct(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
|
|
|
|
2024-07-19 01:44:01 +00:00
|
|
|
proc selfDestructEIP2929Op(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 16:13:38 +00:00
|
|
|
## selfDestructEIP2929 (auto generated comment)
|
2024-07-19 01:44:01 +00:00
|
|
|
? cpt.checkInStaticContext()
|
2024-06-17 16:13:38 +00:00
|
|
|
|
2024-07-05 04:55:13 +00:00
|
|
|
let
|
|
|
|
beneficiary = ? cpt.stack.popAddress()
|
|
|
|
isDead = not cpt.accountExists(beneficiary)
|
|
|
|
balance = cpt.getBalance(cpt.msg.contractAddress)
|
|
|
|
condition = isDead and not balance.isZero
|
2021-04-15 17:42:43 +00:00
|
|
|
|
2024-07-05 04:55:13 +00:00
|
|
|
var
|
|
|
|
gasCost = cpt.gasCosts[SelfDestruct].sc_handler(condition)
|
2021-04-15 17:42:43 +00:00
|
|
|
|
2024-07-05 04:55:13 +00:00
|
|
|
when evmc_enabled:
|
|
|
|
if cpt.host.accessAccount(beneficiary) == EVMC_ACCESS_COLD:
|
|
|
|
gasCost = gasCost + ColdAccountAccessCost
|
|
|
|
else:
|
|
|
|
cpt.vmState.mutateStateDB:
|
|
|
|
if not db.inAccessList(beneficiary):
|
|
|
|
db.accessList(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
gasCost = gasCost + ColdAccountAccessCost
|
2024-07-05 04:55:13 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
? cpt.opcodeGasCost(SelfDestruct,
|
2024-07-05 04:55:13 +00:00
|
|
|
gasCost, reason = "SELFDESTRUCT EIP2929")
|
|
|
|
cpt.selfDestruct(beneficiary)
|
2024-06-17 16:13:38 +00:00
|
|
|
ok()
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public, op exec table entries
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
2024-06-15 16:18:53 +00:00
|
|
|
VmOpExecSysOp*: seq[VmOpExec] = @[
|
2021-04-12 17:13:30 +00:00
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
(opCode: Return, ## 0xf3, Halt execution returning output data.
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "returnOp",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "Halt execution returning output data",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: returnOp),
|
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
(opCode: Revert, ## 0xfd, Halt and revert state changes
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpByzantiumAndLater,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "revert",
|
2021-04-15 17:42:43 +00:00
|
|
|
info: "Halt execution reverting state changes but returning data " &
|
|
|
|
"and remaining gas",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: revertOp),
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
(opCode: Invalid, ## 0xfe, invalid instruction.
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
name: "invalidInstruction",
|
2021-04-12 17:13:30 +00:00
|
|
|
info: "Designated invalid instruction",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: invalidOp),
|
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, Halt execution, prep for later deletion
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpAllForks - VmOpTangerineAndLater,
|
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",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: selfDestructOp),
|
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP150: self destruct, Tangerine
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpTangerineAndLater - VmOpSpuriousAndLater,
|
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",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: selfDestructEIP150Op),
|
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP161: self destruct, Spurious and later
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpSpuriousAndLater - VmOpBerlinAndLater,
|
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",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: selfDestructEIP161Op),
|
|
|
|
|
2021-04-15 17:42:43 +00:00
|
|
|
|
|
|
|
(opCode: SelfDestruct, ## 0xff, EIP2929: self destruct, Berlin and later
|
2024-06-15 16:18:53 +00:00
|
|
|
forks: VmOpBerlinAndLater,
|
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",
|
2024-06-24 05:58:15 +00:00
|
|
|
exec: selfDestructEIP2929Op)]
|
2021-04-12 17:13:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|