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__

34
__all__ = ("Environment", "Evm")

TRANSFER_TOPIC

35
TRANSFER_TOPIC = keccak256(b"Transfer(address,address,uint256)")

SYSTEM_ADDRESS

36
SYSTEM_ADDRESS = Address(
37
    bytes.fromhex("fffffffffffffffffffffffffffffffffffffffe")
38
)

CALL_SUCCESS

39
CALL_SUCCESS = U256(1)

BlockEnvironment

Items external to the virtual machine itself, provided by the environment.

42
@final
43
@dataclass
class BlockEnvironment:

chain_id

49
    chain_id: U64

state

50
    state: BlockState

block_gas_limit

51
    block_gas_limit: Uint

block_hashes

52
    block_hashes: List[Hash32]

coinbase

53
    coinbase: Address

number

54
    number: Uint

base_fee_per_gas

55
    base_fee_per_gas: Uint

time

56
    time: U256

prev_randao

57
    prev_randao: Bytes32

excess_blob_gas

58
    excess_blob_gas: U64

parent_beacon_block_root

59
    parent_beacon_block_root: Hash32

block_access_list_builder

60
    block_access_list_builder: BlockAccessListBuilder

slot_number

61
    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.

64
@final
65
@dataclass
class BlockOutput:

block_gas_used

98
    block_gas_used: ExecutionGas = ExecutionGas(Uint(0))

block_state_gas_used

99
    block_state_gas_used: StateGas = StateGas(Uint(0))

cumulative_gas_used

100
    cumulative_gas_used: Uint = Uint(0)

transactions_trie

101
    transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = (
102
        field(default_factory=lambda: Trie(secured=False, default=None))
103
    )

receipts_trie

104
    receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
105
        default_factory=lambda: Trie(secured=False, default=None)
106
    )

receipt_keys

107
    receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple)

block_logs

108
    block_logs: Tuple[Log, ...] = field(default_factory=tuple)

withdrawals_trie

109
    withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field(
110
        default_factory=lambda: Trie(secured=False, default=None)
111
    )

blob_gas_used

112
    blob_gas_used: U64 = U64(0)

requests

113
    requests: List[Bytes] = field(default_factory=list)

block_access_list

114
    block_access_list: BlockAccessList = field(default_factory=list)

TransactionEnvironment

Items that are used while processing a transaction.

117
@final
118
@dataclass
class TransactionEnvironment:

origin

124
    origin: Address

recipient

126
    recipient: Address

is_create

127
    is_create: bool

data

128
    data: Bytes

value

129
    value: U256

gas_limit

130
    gas_limit: Uint

effective_gas_price

131
    effective_gas_price: Uint

execution_gas_grant

132
    execution_gas_grant: ExecutionGas

state_gas_reservoir

133
    state_gas_reservoir: StateGas

calldata_floor

134
    calldata_floor: Uint

access_list_addresses

135
    access_list_addresses: Set[Address]

access_list_storage_keys

136
    access_list_storage_keys: Set[Tuple[Address, Bytes32]]

accounts_with_paid_writes

137
    accounts_with_paid_writes: Set[Address]

state

138
    state: TransactionState

blob_versioned_hashes

139
    blob_versioned_hashes: Tuple[VersionedHash, ...]

authorizations

140
    authorizations: Tuple[Authorization, ...]

index_in_block

141
    index_in_block: Optional[Uint]

tx_hash

142
    tx_hash: Optional[Hash32]

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.

145
@final
146
@dataclass
class Evm:

pc

156
    pc: Uint

stack

157
    stack: List[U256]

memory

158
    memory: bytearray

code

160
    code: Bytes

gas_meter

161
    gas_meter: GasMeter

valid_jump_destinations

162
    valid_jump_destinations: Set[Uint]

logs

163
    logs: Tuple[Log, ...]

running

164
    running: bool

block_env

167
    block_env: BlockEnvironment

tx_env

168
    tx_env: TransactionEnvironment

caller

169
    caller: Address

current_target

170
    current_target: Address

value

171
    value: U256

call_data

172
    call_data: Bytes

code_address

173
    code_address: Optional[Address]

depth

174
    depth: Uint

should_transfer_value

175
    should_transfer_value: bool

is_static

176
    is_static: bool

disable_precompiles

177
    disable_precompiles: bool

parent_evm

178
    parent_evm: Optional["Evm"]

output

180
    output: Bytes

accounts_to_delete

181
    accounts_to_delete: Set[Address]

return_data

182
    return_data: Bytes

error

183
    error: Optional[EthereumException]

accessed_addresses

184
    accessed_addresses: Set[Address]

accessed_storage_keys

185
    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 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:
189
    <snip>
219
    child_meter = child_evm.gas_meter
220
    # Only the top frame commits state gas; a child never carries any.
221
    assert child_meter.state_gas_committed_spill == Uint(0)
222
223
    if child_evm.error:
224
        # A failed child arrives settled: rolled back to its baseline,
225
        # spill refilled, refunds discarded.
226
        assert child_meter.state_gas_spilled == Uint(0)
227
        assert child_meter.refund_counter == 0
228
        assert child_meter.state_gas_left == child_meter.state_gas_baseline
229
230
    # Gas returns to the parent regardless of the child's fate.
231
    # Note that upon failure, the child already arrives settled.
232
    gas_meter = evm.gas_meter
233
    gas_meter.gas_left += child_meter.gas_left
234
    gas_meter.state_gas_left += child_meter.state_gas_left
235
    gas_meter.state_gas_spilled += child_meter.state_gas_spilled
236
    gas_meter.refund_counter += child_meter.refund_counter
237
238
    # Everything else survives only on success.
239
    if not child_evm.error:
240
        repay_state_gas_spill(gas_meter)
241
        evm.logs += child_evm.logs
242
        evm.accounts_to_delete.update(child_evm.accounts_to_delete)
243
        evm.accessed_addresses.update(child_evm.accessed_addresses)
244
        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:
253
    <snip>
268
    if transfer_amount == 0:
269
        return
270
271
    padded_sender = left_pad_zero_bytes(sender, 32)
272
    padded_recipient = left_pad_zero_bytes(recipient, 32)
273
    log_entry = Log(
274
        address=SYSTEM_ADDRESS,
275
        topics=(
276
            TRANSFER_TOPIC,
277
            Hash32(padded_sender),
278
            Hash32(padded_recipient),
279
        ),
280
        data=transfer_amount.to_be_bytes32(),
281
    )
282
283
    evm.logs = evm.logs + (log_entry,)