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 Gas used for executing all transactions. 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

97
    block_gas_used: Uint = Uint(0)

block_state_gas_used

98
    block_state_gas_used: Uint = Uint(0)

cumulative_gas_used

99
    cumulative_gas_used: Uint = Uint(0)

transactions_trie

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

receipts_trie

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

receipt_keys

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

block_logs

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

withdrawals_trie

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

blob_gas_used

111
    blob_gas_used: U64 = U64(0)

requests

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

block_access_list

113
    block_access_list: BlockAccessList = field(default_factory=list)

TransactionEnvironment

Items that are used while processing a transaction.

116
@final
117
@dataclass
class TransactionEnvironment:

origin

123
    origin: Address

recipient

124
    recipient: Bytes0 | Address

value

125
    value: U256

gas_price

126
    gas_price: Uint

gas

127
    gas: Uint

state_gas_reservoir

128
    state_gas_reservoir: Uint

access_list_addresses

129
    access_list_addresses: Set[Address]

access_list_storage_keys

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

state

131
    state: TransactionState

blob_versioned_hashes

132
    blob_versioned_hashes: Tuple[VersionedHash, ...]

authorizations

133
    authorizations: Tuple[Authorization, ...]

index_in_block

134
    index_in_block: Optional[Uint]

tx_hash

135
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

138
@final
139
@dataclass
class Message:

block_env

145
    block_env: BlockEnvironment

tx_env

146
    tx_env: TransactionEnvironment

caller

147
    caller: Address

target

148
    target: Bytes0 | Address

current_target

149
    current_target: Address

gas

150
    gas: Uint

state_gas_reservoir

151
    state_gas_reservoir: Uint

value

152
    value: U256

data

153
    data: Bytes

code_address

154
    code_address: Optional[Address]

code

155
    code: Optional[Bytes]

depth

156
    depth: Uint

should_transfer_value

157
    should_transfer_value: bool

is_static

158
    is_static: bool

accessed_addresses

159
    accessed_addresses: Set[Address]

accessed_storage_keys

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

disable_precompiles

161
    disable_precompiles: bool

parent_evm

162
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

165
@final
166
@dataclass
class Evm:

pc

170
    pc: Uint

stack

171
    stack: List[U256]

memory

172
    memory: bytearray

code

173
    code: Bytes

gas_meter

174
    gas_meter: GasMeter

valid_jump_destinations

175
    valid_jump_destinations: Set[Uint]

logs

176
    logs: Tuple[Log, ...]

running

177
    running: bool

message

178
    message: Message

output

179
    output: Bytes

accounts_to_delete

180
    accounts_to_delete: Set[Address]

return_data

181
    return_data: Bytes

error

182
    error: Optional[EthereumException]

accessed_addresses

183
    accessed_addresses: Set[Address]

accessed_storage_keys

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