ethereum.forks.bogota.vm
Ethereum Virtual Machine (EVM).
.. contents:: Table of Contents :backlinks: none :local:
Introduction
The abstract computer which runs the code stored in an
.fork_types.Account.
__all__¶
| 53 | __all__ = ("Environment", "Evm") |
|---|
TRANSFER_TOPIC¶
| 54 | TRANSFER_TOPIC = keccak256(b"Transfer(address,address,uint256)") |
|---|
SYSTEM_ADDRESS¶
| 55 | SYSTEM_ADDRESS = Address( |
|---|---|
| 56 | bytes.fromhex("fffffffffffffffffffffffffffffffffffffffe") |
| 57 | ) |
FRAME_ENTRY_POINT¶
| 58 | FRAME_ENTRY_POINT = Address( |
|---|---|
| 59 | bytes.fromhex("00000000000000000000000000000000000000aa") |
| 60 | ) |
CALL_SUCCESS¶
| 61 | CALL_SUCCESS = U256(1) |
|---|
BlockEnvironment ¶
Items external to the virtual machine itself, provided by the environment.
| 64 | @final |
|---|
| 65 | @dataclass |
|---|
class BlockEnvironment:
chain_id¶
| 71 | chain_id: U64 |
|---|
state¶
| 72 | state: BlockState |
|---|
block_gas_limit¶
| 73 | block_gas_limit: Uint |
|---|
block_hashes¶
| 74 | block_hashes: List[Hash32] |
|---|
coinbase¶
| 75 | coinbase: Address |
|---|
number¶
| 76 | number: Uint |
|---|
base_fee_per_gas¶
| 77 | base_fee_per_gas: Uint |
|---|
time¶
| 78 | time: U256 |
|---|
prev_randao¶
| 79 | prev_randao: Bytes32 |
|---|
excess_blob_gas¶
| 80 | excess_blob_gas: U64 |
|---|
parent_beacon_block_root¶
| 81 | parent_beacon_block_root: Hash32 |
|---|
block_access_list_builder¶
| 82 | block_access_list_builder: BlockAccessListBuilder |
|---|
slot_number¶
| 83 | slot_number: U64 |
|---|
BlockOutput ¶
Output from applying the block body to the present state.
Contains the following:
block_gas_used : ExecutionGas
Execution gas used for executing all transactions. EIP-8037
names this counter block_execution_gas_used.
block_state_gas_used : StateGas
State gas used for executing all transactions.
cumulative_gas_used : ethereum.base_types.Uint
Cumulative gas paid by users (post-refund, post-floor).
transactions_trie : ethereum.fork_types.Root
Trie of all the transactions in the block.
receipts_trie : ethereum.fork_types.Root
Trie root of all the receipts in the block.
receipt_keys :
Keys of all the receipts in the block.
block_logs : Bloom
Logs bloom of all the logs included in all the transactions of the
block.
withdrawals_trie : ethereum.fork_types.Root
Trie root of all the withdrawals in the block.
blob_gas_used : ethereum.base_types.U64
Total blob gas used in the block.
requests : Bytes
Hash of all the requests in the block.
block_access_list: BlockAccessList
The block access list for the block.
| 86 | @final |
|---|
| 87 | @dataclass |
|---|
class BlockOutput:
block_gas_used¶
| 120 | block_gas_used: ExecutionGas = ExecutionGas(Uint(0)) |
|---|
block_state_gas_used¶
| 121 | block_state_gas_used: StateGas = StateGas(Uint(0)) |
|---|
cumulative_gas_used¶
| 122 | cumulative_gas_used: Uint = Uint(0) |
|---|
transactions_trie¶
| 123 | transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = ( |
|---|---|
| 124 | field(default_factory=lambda: Trie(secured=False, default=None)) |
| 125 | ) |
receipts_trie¶
| 126 | receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field( |
|---|---|
| 127 | default_factory=lambda: Trie(secured=False, default=None) |
| 128 | ) |
receipt_keys¶
| 129 | receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple) |
|---|
block_logs¶
| 130 | block_logs: Tuple[Log, ...] = field(default_factory=tuple) |
|---|
withdrawals_trie¶
| 131 | withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field( |
|---|---|
| 132 | default_factory=lambda: Trie(secured=False, default=None) |
| 133 | ) |
blob_gas_used¶
| 134 | blob_gas_used: U64 = U64(0) |
|---|
requests¶
| 135 | requests: List[Bytes] = field(default_factory=list) |
|---|
block_access_list¶
| 136 | block_access_list: BlockAccessList = field(default_factory=list) |
|---|
TopLevelContext ¶
The single top-level call or creation a non-frame transaction describes.
Unlike FrameContext, this is a frozen one-shot descriptor:
consumed once when the transaction's top-level frame is built;
instructions never read it.
| 139 | @final |
|---|
| 140 | @slotted_freezable |
|---|
| 141 | @dataclass |
|---|
class TopLevelContext:
recipient¶
The address the transaction calls; for a creation, the address the contract deploys to.
| 152 | recipient: Address |
|---|
is_create¶
Whether the transaction is a contract creation.
| 158 | is_create: bool |
|---|
data¶
The transaction's data payload: call data for a call, init code for a creation.
| 163 | data: Bytes |
|---|
value¶
The amount of ether (in wei) sent with the transaction.
| 169 | value: U256 |
|---|
FrameContext ¶
Frame-transaction state, alive for the whole transaction and visible at every call depth through the transaction environment.
The state gas dimension lives here rather than on the gas meters:
each frame's pool is shared by every call depth within the frame,
never forwarded or split like execution gas. While a frame
executes only its pool moves; its attributed state gas is written
once, at frame exit, into its receipt — the frame's budget less
the remaining pool. A later frame's cross-frame refill lowers the
owning frame's receipt entry in place. The pool, the receipts, and
the outstanding-charge ownership map roll back with the
transaction state, through the same snapshots
(copy_frame_context / restore_frame_context) that already
accompany every state snapshot.
| 175 | @final |
|---|
| 176 | @dataclass |
|---|
class FrameContext:
tx¶
The frame transaction being executed.
| 195 | tx: FrameTransaction |
|---|
signature_hash¶
The transaction's canonical signature hash.
| 200 | signature_hash: Hash32 |
|---|
resolved_signers¶
The signer each signature entry resolved to; None for
ARBITRARY entries, to which the protocol assigns no signer.
| 205 | resolved_signers: Tuple[Optional[Address], ...] |
|---|
standard_gas_limit¶
Settlement anchor: the transaction's intrinsic cost plus the sum
of the frames' gas budgets in both dimensions. The environment's
gas_limit carries the inclusion-facing max_gas instead.
| 211 | standard_gas_limit: Uint |
|---|
max_cost¶
The escrow collected from the payer when a frame approves payment:
the worst-case base fee charge at the block's base fees, plus the
largest tip the transaction's max_fee budget allows.
| 218 | max_cost: Uint |
|---|
current_frame_index¶
Index of the frame currently executing, advanced by the frame loop.
| 225 | current_frame_index: Uint |
|---|
frame_receipts¶
Receipts of the completed frames, growing as frames complete.
Entries are not final until the transaction ends: a later frame's
refill of a state charge lowers the owning frame's
gas_used.state, and an atomic batch unroll zeroes the state gas
and empties the logs of the unrolled frames' receipts.
| 231 | frame_receipts: List[FrameReceipt] |
|---|
payer¶
The account that approved paying for the transaction's gas, once one has.
| 241 | payer: Optional[Address] |
|---|
sender_approved¶
Whether the sender has approved future frames executing on its behalf.
| 247 | sender_approved: bool |
|---|
state_gas_left¶
State gas remaining in the executing frame's pool, seeded from the frame's declared state budget at frame entry. EVM call frames at every depth within the frame draw from it directly; a charge exceeding it halts the current call frame exceptionally.
| 253 | state_gas_left: StateGas |
|---|
outstanding_charge_owners¶
For each storage slot whose creation charge is outstanding, the index of the frame that paid it. A refill of the slot lowers the owner frame's attributed state gas — the executing frame's pool when the owner is still executing, its receipt entry otherwise — and clears the entry either way.
| 261 | outstanding_charge_owners: Dict[Tuple[Address, Bytes32], Uint] |
|---|
TransactionEnvironment ¶
Items that are used while processing a transaction.
Fields shared by every transaction type, plus exactly one of the
two type-specific contexts: top_level_context for a regular or
system transaction, frame_context for a frame transaction.
| 271 | @final |
|---|
| 272 | @dataclass |
|---|
class TransactionEnvironment:
origin¶
| 282 | origin: Address |
|---|
gas_limit¶
| 283 | gas_limit: Uint |
|---|
effective_gas_price¶
| 284 | effective_gas_price: Uint |
|---|
execution_gas_grant¶
| 285 | execution_gas_grant: ExecutionGas |
|---|
state_gas_reservoir¶
| 286 | state_gas_reservoir: StateGas |
|---|
calldata_floor¶
| 287 | calldata_floor: Uint |
|---|
access_list_addresses¶
| 288 | access_list_addresses: Set[Address] |
|---|
access_list_storage_keys¶
| 289 | access_list_storage_keys: Set[Tuple[Address, Bytes32]] |
|---|
accounts_with_paid_writes¶
| 290 | accounts_with_paid_writes: Set[Address] |
|---|
state¶
| 291 | state: TransactionState |
|---|
blob_versioned_hashes¶
| 292 | blob_versioned_hashes: Tuple[VersionedHash, ...] |
|---|
index_in_block¶
| 294 | index_in_block: Optional[Uint] |
|---|
tx_hash¶
| 295 | tx_hash: Optional[Hash32] |
|---|
top_level_context¶
Present iff the transaction describes a single top-level call or
creation. Exactly one of this and frame_context is set; both
flow entries assert it.
| 297 | top_level_context: Optional[TopLevelContext] |
|---|
frame_context¶
Present iff this is a frame transaction. The frame-only opcodes
exceptionally halt when this is None.
| 304 | frame_context: Optional[FrameContext] |
|---|
copy_frame_context ¶
Copy a frame transaction's context, to be restored on failure.
Paired with every transaction-state snapshot taken while a frame
executes: APPROVE's state-side effects (the sender's nonce
increment and the payment escrow) are transaction-state writes
that roll back with the state, so the context fields recording the
approval must roll back in the same motion — as must the state gas
pool, the receipts a cross-frame refill may have edited, and the
outstanding-charge ownership map. Return None for other
transaction types, whose environments carry no frame context.
def copy_frame_context(tx_env: TransactionEnvironment) -> Optional[FrameContext]:
| 314 | <snip> |
|---|---|
| 326 | frame_context = tx_env.frame_context |
| 327 | if frame_context is None: |
| 328 | return None |
| 329 | return replace( |
| 330 | frame_context, |
| 331 | frame_receipts=list(frame_context.frame_receipts), |
| 332 | outstanding_charge_owners=dict( |
| 333 | frame_context.outstanding_charge_owners |
| 334 | ), |
| 335 | ) |
restore_frame_context ¶
Restore the mutable fields of a frame transaction's context from a
copy taken by copy_frame_context; a no-op for other transaction
types.
def restore_frame_context(tx_env: TransactionEnvironment, snapshot: Optional[FrameContext]) -> None:
| 342 | <snip> |
|---|---|
| 347 | if snapshot is None: |
| 348 | return |
| 349 | frame_context = tx_env.frame_context |
| 350 | assert frame_context is not None |
| 351 | frame_context.current_frame_index = snapshot.current_frame_index |
| 352 | frame_context.frame_receipts = snapshot.frame_receipts |
| 353 | frame_context.payer = snapshot.payer |
| 354 | frame_context.sender_approved = snapshot.sender_approved |
| 355 | frame_context.state_gas_left = snapshot.state_gas_left |
| 356 | frame_context.outstanding_charge_owners = ( |
| 357 | snapshot.outstanding_charge_owners |
| 358 | ) |
attempt_approval ¶
Attempt an APPROVE of scope on behalf of the executing frame's
resolved target, applying its effects on success.
The scope must be non-empty and within the frame's allowed approval flags. Approving execution requires that execution is not already approved and that the frame's resolved target is the transaction's sender. Approving payment requires that no payer is set, that execution is approved (by this same scope or earlier), and that the resolved target can cover the transaction's maximum cost; it increments the sender's nonce and collects the maximum cost from the resolved target, which becomes the payer. The payer needs no warming here: it is the frame's resolved target, whose access the frame charged and warmed at frame entry.
When incrementing the nonce creates the sender account, the account creation is charged from the executing frame's state gas pool immediately before the increment. A pool that cannot cover the charge halts the current call frame exceptionally — the halt's rollback discards every approval effect, including an execution approval this same call already recorded.
Return whether the approval was granted; a refusal reverts the requesting call frame, which is the frame itself only when the protocol default code is the caller.
def attempt_approval(tx_env: TransactionEnvironment, scope: FrameFlag) -> bool:
| 365 | <snip> |
|---|---|
| 391 | frame_context = tx_env.frame_context |
| 392 | assert frame_context is not None |
| 393 | tx = frame_context.tx |
| 394 | frame = tx.frames[int(frame_context.current_frame_index)] |
| 395 | resolved_target = resolve_frame_target(tx, frame) |
| 396 | |
| 397 | allowed_scope = frame.flags & APPROVE_SCOPE_MASK |
| 398 | # An empty scope, or one beyond the frame's allowed flags. |
| 399 | if not scope or scope & ~allowed_scope: |
| 400 | return False |
| 401 | |
| 402 | approves_execution = FrameFlag.APPROVE_EXECUTION in scope |
| 403 | approves_payment = FrameFlag.APPROVE_PAYMENT in scope |
| 404 | |
| 405 | if approves_execution: |
| 406 | # Execution is already approved. |
| 407 | if frame_context.sender_approved: |
| 408 | return False |
| 409 | # Only the sender may approve execution on its own behalf. |
| 410 | if resolved_target != tx.sender: |
| 411 | return False |
| 412 | |
| 413 | if approves_payment: |
| 414 | # Payment is already approved. |
| 415 | if frame_context.payer is not None: |
| 416 | return False |
| 417 | # Payment approval requires execution approval first. |
| 418 | if not (frame_context.sender_approved or approves_execution): |
| 419 | return False |
| 420 | payer_balance = get_account(tx_env.state, resolved_target).balance |
| 421 | # The payer cannot cover the transaction's maximum cost. |
| 422 | if Uint(payer_balance) < frame_context.max_cost: |
| 423 | return False |
| 424 | |
| 425 | if approves_execution: |
| 426 | frame_context.sender_approved = True |
| 427 | if approves_payment: |
| 428 | if not is_account_alive(tx_env.state, tx.sender): |
| 429 | charge_frame_state_gas(frame_context, StateGasCosts.NEW_ACCOUNT) |
| 430 | increment_nonce(tx_env.state, tx.sender) |
| 431 | set_account_balance( |
| 432 | tx_env.state, |
| 433 | resolved_target, |
| 434 | U256(Uint(payer_balance) - frame_context.max_cost), |
| 435 | ) |
| 436 | frame_context.payer = resolved_target |
| 437 | |
| 438 | return True |
Evm ¶
A single call frame: its parameters, gas meter, machine state, and accrued effects.
A call spawns a child frame and each top-level call is a frame at depth zero, so one dataclass describes them all.
| 441 | @final |
|---|
| 442 | @dataclass |
|---|
class Evm:
pc¶
| 452 | pc: Uint |
|---|
stack¶
| 453 | stack: List[U256] |
|---|
memory¶
| 454 | memory: bytearray |
|---|
code¶
| 456 | code: Bytes |
|---|
gas_meter¶
| 457 | gas_meter: GasMeter |
|---|
valid_jump_destinations¶
| 458 | valid_jump_destinations: Set[Uint] |
|---|
logs¶
| 459 | logs: Tuple[Log, ...] |
|---|
running¶
| 460 | running: bool |
|---|
block_env¶
| 463 | block_env: BlockEnvironment |
|---|
tx_env¶
| 464 | tx_env: TransactionEnvironment |
|---|
caller¶
| 465 | caller: Address |
|---|
current_target¶
| 466 | current_target: Address |
|---|
value¶
| 467 | value: U256 |
|---|
call_data¶
| 468 | call_data: Bytes |
|---|
code_address¶
| 469 | code_address: Optional[Address] |
|---|
depth¶
| 470 | depth: Uint |
|---|
should_transfer_value¶
| 471 | should_transfer_value: bool |
|---|
is_static¶
| 472 | is_static: bool |
|---|
disable_precompiles¶
| 473 | disable_precompiles: bool |
|---|
parent_evm¶
| 474 | parent_evm: Optional["Evm"] |
|---|
output¶
| 476 | output: Bytes |
|---|
accounts_to_delete¶
| 477 | accounts_to_delete: Set[Address] |
|---|
return_data¶
| 478 | return_data: Bytes |
|---|
error¶
| 479 | error: Optional[EthereumException] |
|---|
accessed_addresses¶
| 480 | accessed_addresses: Set[Address] |
|---|
accessed_storage_keys¶
| 481 | accessed_storage_keys: Set[Tuple[Address, Bytes32]] |
|---|
incorporate_child ¶
Incorporate the state of a returning child_evm into the parent
evm.
Gas flows back to the parent regardless of the child's fate. A failed child settles its own meter before returning -- its state gas rolled back to the baseline, its spill refilled, and its refunds discarded -- so absorbing the meter unconditionally reclaims exactly the gas the child gives back. Everything else the child accumulated -- logs, scheduled self-destructs, refunds, and warmed access sets -- survives only on success, dying with a failed child's reverted state.
A call tree never mixes gas models: within a frame transaction no meter carries a reservoir -- state gas lives on the frame context, shared by every call depth, so none of it moves between meters here -- while in the single-gas-field model every meter carries one.
In the reservoir model, a successful merge ends with the reservoir
repaying any spill still outstanding: a cross-frame refund lands
in the reservoir while the gas_left that funded the charge stays
reduced, and the merge is where the claim and the credit first
share a meter. A failed child repays nothing -- its rollback
restored the state whose removal any refund credited.
Parameters
evm :
The parent EVM.
child_evm :
The child evm to incorporate.
def incorporate_child(evm: Evm, child_evm: Evm) -> None:
| 485 | <snip> # noqa: E501 |
|---|---|
| 521 | child_meter = child_evm.gas_meter |
| 522 | gas_meter = evm.gas_meter |
| 523 | |
| 524 | if child_evm.error: |
| 525 | # A failed child arrives settled: refunds discarded. |
| 526 | assert child_meter.refund_counter == 0 |
| 527 | |
| 528 | # Execution gas returns to the parent regardless of the child's |
| 529 | # fate. Note that upon failure, the child already arrives settled. |
| 530 | gas_meter.gas_left += child_meter.gas_left |
| 531 | gas_meter.refund_counter += child_meter.refund_counter |
| 532 | |
| 533 | child_reservoir = child_meter.reservoir |
| 534 | if child_reservoir is None: |
| 535 | assert gas_meter.reservoir is None |
| 536 | else: |
| 537 | parent_reservoir = gas_meter.reservoir |
| 538 | assert parent_reservoir is not None |
| 539 | |
| 540 | # Only the top frame commits state gas; a child never carries |
| 541 | # any. |
| 542 | assert child_reservoir.state_gas_committed_spill == Uint(0) |
| 543 | |
| 544 | if child_evm.error: |
| 545 | # A failed child arrives settled: rolled back to its |
| 546 | # baseline, spill refilled. |
| 547 | assert child_reservoir.state_gas_spilled == Uint(0) |
| 548 | assert ( |
| 549 | child_reservoir.state_gas_left |
| 550 | == child_reservoir.state_gas_baseline |
| 551 | ) |
| 552 | |
| 553 | parent_reservoir.state_gas_left += child_reservoir.state_gas_left |
| 554 | parent_reservoir.state_gas_spilled += child_reservoir.state_gas_spilled |
| 555 | if not child_evm.error: |
| 556 | repay_state_gas_spill(gas_meter) |
| 557 | |
| 558 | # Everything else survives only on success. |
| 559 | if not child_evm.error: |
| 560 | evm.logs += child_evm.logs |
| 561 | evm.accounts_to_delete.update(child_evm.accounts_to_delete) |
| 562 | evm.accessed_addresses.update(child_evm.accessed_addresses) |
| 563 | evm.accessed_storage_keys.update(child_evm.accessed_storage_keys) |
emit_transfer_log ¶
Emit a LOG3 for all ETH transfers satisfying EIP-7708.
Parameters
evm : The state of the ethereum virtual machine sender : The account address sending the transfer recipient : The account address receiving the transfer transfer_amount : The amount of ETH transacted
def emit_transfer_log(evm: Evm, sender: Address, recipient: Address, transfer_amount: U256) -> None:
| 572 | <snip> |
|---|---|
| 587 | if transfer_amount == 0: |
| 588 | return |
| 589 | |
| 590 | padded_sender = left_pad_zero_bytes(sender, 32) |
| 591 | padded_recipient = left_pad_zero_bytes(recipient, 32) |
| 592 | log_entry = Log( |
| 593 | address=SYSTEM_ADDRESS, |
| 594 | topics=( |
| 595 | TRANSFER_TOPIC, |
| 596 | Hash32(padded_sender), |
| 597 | Hash32(padded_recipient), |
| 598 | ), |
| 599 | data=transfer_amount.to_be_bytes32(), |
| 600 | ) |
| 601 | |
| 602 | evm.logs = evm.logs + (log_entry,) |