2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-05-22 21:01:19 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-08 14:52:10 +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.
|
|
|
|
|
|
|
|
import
|
2024-06-07 08:24:32 +00:00
|
|
|
"."/[stack, memory, code_stream, evm_errors],
|
2021-06-01 11:27:05 +00:00
|
|
|
./interpreter/[gas_costs, op_codes],
|
2023-12-12 19:12:56 +00:00
|
|
|
../db/ledger,
|
2022-12-02 04:35:41 +00:00
|
|
|
../common/[common, evmforks]
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2023-08-28 12:10:31 +00:00
|
|
|
# this import not guarded by `when defined(evmc_enabled)`
|
|
|
|
# because we want to use evmc types such as evmc_call_kind
|
|
|
|
# and evmc_flags
|
|
|
|
import
|
|
|
|
evmc/evmc
|
|
|
|
|
|
|
|
export evmc
|
|
|
|
|
2023-02-14 14:37:21 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-09-26 04:56:54 +00:00
|
|
|
when defined(evmc_enabled):
|
|
|
|
import
|
|
|
|
./evmc_api
|
|
|
|
|
|
|
|
# Select between small-stack recursion and no recursion. Both are good, fast,
|
|
|
|
# low resource using methods. Keep both here because true EVMC API requires
|
|
|
|
# the small-stack method, but Chronos `async` is better without recursion.
|
|
|
|
const vm_use_recursion* = defined(evmc_enabled)
|
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
type
|
|
|
|
VMFlag* = enum
|
|
|
|
ExecutionOK
|
2024-06-08 08:05:00 +00:00
|
|
|
CollectWitnessData
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2023-09-24 15:25:41 +00:00
|
|
|
BlockContext* = object
|
2023-08-28 12:10:31 +00:00
|
|
|
timestamp* : EthTime
|
|
|
|
gasLimit* : GasInt
|
2024-06-14 07:31:08 +00:00
|
|
|
baseFeePerGas* : Opt[UInt256]
|
2023-08-28 12:10:31 +00:00
|
|
|
prevRandao* : Hash256
|
2023-09-24 15:25:41 +00:00
|
|
|
difficulty* : UInt256
|
|
|
|
coinbase* : EthAddress
|
|
|
|
excessBlobGas* : uint64
|
|
|
|
|
2023-10-01 07:24:15 +00:00
|
|
|
TxContext* = object
|
|
|
|
origin* : EthAddress
|
|
|
|
gasPrice* : GasInt
|
|
|
|
versionedHashes*: VersionedHashes
|
|
|
|
blobBaseFee* : UInt256
|
|
|
|
|
2023-09-24 15:25:41 +00:00
|
|
|
BaseVMState* = ref object of RootObj
|
|
|
|
com* : CommonRef
|
2023-12-12 19:12:56 +00:00
|
|
|
stateDB* : LedgerRef
|
2023-09-24 15:25:41 +00:00
|
|
|
gasPool* : GasInt
|
|
|
|
parent* : BlockHeader
|
|
|
|
blockCtx* : BlockContext
|
2023-10-01 07:24:15 +00:00
|
|
|
txCtx* : TxContext
|
2023-08-28 12:10:31 +00:00
|
|
|
flags* : set[VMFlag]
|
2023-10-01 07:24:15 +00:00
|
|
|
fork* : EVMFork
|
2023-08-28 12:10:31 +00:00
|
|
|
tracer* : TracerRef
|
|
|
|
receipts* : seq[Receipt]
|
2021-04-08 14:52:10 +00:00
|
|
|
cumulativeGasUsed*: GasInt
|
2023-08-28 12:10:31 +00:00
|
|
|
gasCosts* : GasCosts
|
2024-06-24 05:56:24 +00:00
|
|
|
blobGasUsed* : uint64
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
Computation* = ref object
|
|
|
|
# The execution computation
|
|
|
|
vmState*: BaseVMState
|
|
|
|
msg*: Message
|
2024-06-07 08:24:32 +00:00
|
|
|
memory*: EvmMemoryRef
|
|
|
|
stack*: EvmStackRef
|
2021-04-08 14:52:10 +00:00
|
|
|
returnStack*: seq[int]
|
|
|
|
gasMeter*: GasMeter
|
|
|
|
code*: CodeStream
|
|
|
|
output*: seq[byte]
|
|
|
|
returnData*: seq[byte]
|
|
|
|
error*: Error
|
2023-12-12 19:12:56 +00:00
|
|
|
savePoint*: LedgerSpRef
|
2021-04-08 14:52:10 +00:00
|
|
|
instr*: Op
|
|
|
|
opIndex*: int
|
2022-09-26 04:56:54 +00:00
|
|
|
when defined(evmc_enabled):
|
|
|
|
host*: HostContext
|
|
|
|
child*: ref nimbus_message
|
|
|
|
res*: nimbus_result
|
|
|
|
else:
|
|
|
|
parent*, child*: Computation
|
2024-06-07 08:24:32 +00:00
|
|
|
continuation*: proc(): EvmResultVoid {.gcsafe, raises: [].}
|
2023-09-20 13:10:16 +00:00
|
|
|
sysCall*: bool
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
Error* = ref object
|
2024-05-30 09:01:07 +00:00
|
|
|
evmcStatus*: evmc_status_code
|
2023-08-28 12:10:31 +00:00
|
|
|
info* : string
|
|
|
|
burnsGas* : bool
|
2024-06-14 07:31:08 +00:00
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
GasMeter* = object
|
2024-07-07 06:52:11 +00:00
|
|
|
gasRefunded*: int64
|
2021-04-08 14:52:10 +00:00
|
|
|
gasRemaining*: GasInt
|
|
|
|
|
2023-08-28 12:10:31 +00:00
|
|
|
CallKind* = evmc_call_kind
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2023-08-28 12:10:31 +00:00
|
|
|
MsgFlags* = evmc_flags
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
Message* = ref object
|
|
|
|
kind*: CallKind
|
|
|
|
depth*: int
|
|
|
|
gas*: GasInt
|
|
|
|
sender*: EthAddress
|
|
|
|
contractAddress*: EthAddress
|
|
|
|
codeAddress*: EthAddress
|
|
|
|
value*: UInt256
|
|
|
|
data*: seq[byte]
|
|
|
|
flags*: MsgFlags
|
2023-08-02 10:17:40 +00:00
|
|
|
|
|
|
|
TracerFlags* {.pure.} = enum
|
|
|
|
DisableStorage
|
|
|
|
DisableMemory
|
|
|
|
DisableStack
|
|
|
|
DisableState
|
|
|
|
DisableStateDiff
|
|
|
|
EnableAccount
|
|
|
|
DisableReturnData
|
2024-07-04 12:23:53 +00:00
|
|
|
|
2023-08-02 10:17:40 +00:00
|
|
|
TracerRef* = ref object of RootObj
|
|
|
|
flags*: set[TracerFlags]
|
|
|
|
|
|
|
|
# Transaction level
|
2023-08-25 09:07:20 +00:00
|
|
|
# This is called once fo each transaction
|
2023-08-02 10:17:40 +00:00
|
|
|
method captureTxStart*(ctx: TracerRef, gasLimit: GasInt) {.base, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
method captureTxEnd*(ctx: TracerRef, restGas: GasInt) {.base, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
# Top call frame
|
2023-08-25 09:07:20 +00:00
|
|
|
method captureStart*(ctx: TracerRef, comp: Computation,
|
2023-08-02 10:17:40 +00:00
|
|
|
sender: EthAddress, to: EthAddress,
|
|
|
|
create: bool, input: openArray[byte],
|
2023-08-25 09:07:20 +00:00
|
|
|
gasLimit: GasInt, value: UInt256) {.base, gcsafe.} =
|
2023-08-02 10:17:40 +00:00
|
|
|
discard
|
|
|
|
|
2023-08-25 09:07:20 +00:00
|
|
|
method captureEnd*(ctx: TracerRef, comp: Computation, output: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
gasUsed: GasInt, error: Opt[string]) {.base, gcsafe.} =
|
2023-08-02 10:17:40 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
# Rest of call frames
|
2023-08-25 09:07:20 +00:00
|
|
|
method captureEnter*(ctx: TracerRef, comp: Computation, op: Op,
|
2023-08-02 10:17:40 +00:00
|
|
|
sender: EthAddress, to: EthAddress,
|
2023-08-25 09:07:20 +00:00
|
|
|
input: openArray[byte], gasLimit: GasInt,
|
2023-08-02 10:17:40 +00:00
|
|
|
value: UInt256) {.base, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
2023-08-25 09:07:20 +00:00
|
|
|
method captureExit*(ctx: TracerRef, comp: Computation, output: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
gasUsed: GasInt, error: Opt[string]) {.base, gcsafe.} =
|
2023-08-02 10:17:40 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
# Opcode level
|
2023-08-27 06:13:37 +00:00
|
|
|
method captureOpStart*(ctx: TracerRef, comp: Computation,
|
|
|
|
fixed: bool, pc: int, op: Op, gas: GasInt,
|
2023-08-02 10:17:40 +00:00
|
|
|
depth: int): int {.base, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
method captureGasCost*(ctx: TracerRef, comp: Computation,
|
|
|
|
fixed: bool, op: Op, gasCost: GasInt,
|
|
|
|
gasRemaining: GasInt, depth: int) {.base, gcsafe.} =
|
2023-08-23 09:29:33 +00:00
|
|
|
discard
|
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
method captureOpEnd*(ctx: TracerRef, comp: Computation,
|
2024-07-07 06:52:11 +00:00
|
|
|
fixed: bool, pc: int, op: Op, gas: GasInt, refund: int64,
|
2023-08-02 10:17:40 +00:00
|
|
|
rData: openArray[byte],
|
|
|
|
depth: int, opIndex: int) {.base, gcsafe.} =
|
|
|
|
discard
|
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
method captureFault*(ctx: TracerRef, comp: Computation,
|
2024-07-07 06:52:11 +00:00
|
|
|
fixed: bool, pc: int, op: Op, gas: GasInt, refund: int64,
|
2023-08-02 10:17:40 +00:00
|
|
|
rData: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
depth: int, error: Opt[string]) {.base, gcsafe.} =
|
2023-08-02 10:17:40 +00:00
|
|
|
discard
|
|
|
|
|
2023-08-27 06:13:37 +00:00
|
|
|
# Called at the start of EVM interpreter loop
|
2023-08-25 09:07:20 +00:00
|
|
|
method capturePrepare*(ctx: TracerRef, comp: Computation, depth: int) {.base, gcsafe.} =
|
2023-08-02 10:17:40 +00:00
|
|
|
discard
|