ethereum.forks.amsterdam.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", "Message")

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 : ethereum.base_types.Uint Execution gas used for executing all transactions. EIP-8037 names this counter block_execution_gas_used. block_state_gas_used : ethereum.base_types.Uint 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: Uint = Uint(0)

block_state_gas_used

99
    block_state_gas_used: Uint = 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

125
    recipient: Bytes0 | Address

value

126
    value: U256

gas_price

127
    gas_price: Uint

gas

128
    gas: Uint

state_gas_reservoir

129
    state_gas_reservoir: Uint

access_list_addresses

130
    access_list_addresses: Set[Address]

access_list_storage_keys

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

state

132
    state: TransactionState

blob_versioned_hashes

133
    blob_versioned_hashes: Tuple[VersionedHash, ...]

authorizations

134
    authorizations: Tuple[Authorization, ...]

index_in_block

135
    index_in_block: Optional[Uint]

tx_hash

136
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

139
@final
140
@dataclass
class Message:

block_env

146
    block_env: BlockEnvironment

tx_env

147
    tx_env: TransactionEnvironment

caller

148
    caller: Address

target

149
    target: Bytes0 | Address

current_target

150
    current_target: Address

gas

151
    gas: Uint

state_gas_reservoir

152
    state_gas_reservoir: Uint

value

153
    value: U256

data

154
    data: Bytes

code_address

155
    code_address: Optional[Address]

code

156
    code: Optional[Bytes]

depth

157
    depth: Uint

should_transfer_value

158
    should_transfer_value: bool

is_static

159
    is_static: bool

accessed_addresses

160
    accessed_addresses: Set[Address]

accessed_storage_keys

161
    accessed_storage_keys: Set[Tuple[Address, Bytes32]]

disable_precompiles

162
    disable_precompiles: bool

parent_evm

163
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

166
@final
167
@dataclass
class Evm:

pc

171
    pc: Uint

stack

172
    stack: List[U256]

memory

173
    memory: bytearray

code

174
    code: Bytes

gas_meter

175
    gas_meter: GasMeter

valid_jump_destinations

176
    valid_jump_destinations: Set[Uint]

logs

177
    logs: Tuple[Log, ...]

running

178
    running: bool

message

179
    message: Message

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.

Parameters

evm : The parent EVM. child_evm : The child evm to incorporate.

def incorporate_child(evm: Evm, ​​child_evm: Evm) -> None:
189
    <snip>
212
    child_meter = child_evm.gas_meter
213
    # Only the top frame commits state gas; a child never carries any.
214
    assert child_meter.state_gas_committed_spill == Uint(0)
215
216
    if child_evm.error:
217
        # A failed child arrives settled: rolled back to its baseline,
218
        # spill refilled, refunds discarded.
219
        assert child_meter.state_gas_spilled == Uint(0)
220
        assert child_meter.refund_counter == 0
221
        assert child_meter.state_gas_left == child_meter.state_gas_baseline
222
223
    # Gas returns to the parent regardless of the child's fate.
224
    # Note that upon failure, the child already arrives settled.
225
    gas_meter = evm.gas_meter
226
    gas_meter.gas_left += child_meter.gas_left
227
    gas_meter.state_gas_left += child_meter.state_gas_left
228
    gas_meter.state_gas_spilled += child_meter.state_gas_spilled
229
    gas_meter.refund_counter += child_meter.refund_counter
230
231
    # Everything else survives only on success.
232
    if not child_evm.error:
233
        evm.logs += child_evm.logs
234
        evm.accounts_to_delete.update(child_evm.accounts_to_delete)
235
        evm.accessed_addresses.update(child_evm.accessed_addresses)
236
        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:
245
    <snip>
260
    if transfer_amount == 0:
261
        return
262
263
    padded_sender = left_pad_zero_bytes(sender, 32)
264
    padded_recipient = left_pad_zero_bytes(recipient, 32)
265
    log_entry = Log(
266
        address=SYSTEM_ADDRESS,
267
        topics=(
268
            TRANSFER_TOPIC,
269
            Hash32(padded_sender),
270
            Hash32(padded_recipient),
271
        ),
272
        data=transfer_amount.to_be_bytes32(),
273
    )
274
275
    evm.logs = evm.logs + (log_entry,)