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__¶
| 33 | __all__ = ("Environment", "Evm", "Message") |
|---|
TRANSFER_TOPIC¶
| 34 | TRANSFER_TOPIC = keccak256(b"Transfer(address,address,uint256)") |
|---|
BURN_TOPIC¶
| 35 | BURN_TOPIC = keccak256(b"Burn(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 | @dataclass |
|---|
class BlockEnvironment:
chain_id¶
| 48 | chain_id: U64 |
|---|
state¶
| 49 | state: BlockState |
|---|
block_gas_limit¶
| 50 | block_gas_limit: Uint |
|---|
block_hashes¶
| 51 | block_hashes: List[Hash32] |
|---|
coinbase¶
| 52 | coinbase: Address |
|---|
number¶
| 53 | number: Uint |
|---|
base_fee_per_gas¶
| 54 | base_fee_per_gas: Uint |
|---|
time¶
| 55 | time: U256 |
|---|
prev_randao¶
| 56 | prev_randao: Bytes32 |
|---|
excess_blob_gas¶
| 57 | excess_blob_gas: U64 |
|---|
parent_beacon_block_root¶
| 58 | parent_beacon_block_root: Hash32 |
|---|
block_access_list_builder¶
| 59 | block_access_list_builder: BlockAccessListBuilder |
|---|
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.
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.
| 62 | @dataclass |
|---|
class BlockOutput:
block_gas_used¶
| 90 | block_gas_used: Uint = Uint(0) |
|---|
cumulative_gas_used¶
| 91 | cumulative_gas_used: Uint = Uint(0) |
|---|
transactions_trie¶
| 92 | transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = ( |
|---|---|
| 93 | field(default_factory=lambda: Trie(secured=False, default=None)) |
| 94 | ) |
receipts_trie¶
| 95 | receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field( |
|---|---|
| 96 | default_factory=lambda: Trie(secured=False, default=None) |
| 97 | ) |
receipt_keys¶
| 98 | receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple) |
|---|
block_logs¶
| 99 | block_logs: Tuple[Log, ...] = field(default_factory=tuple) |
|---|
withdrawals_trie¶
| 100 | withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field( |
|---|---|
| 101 | default_factory=lambda: Trie(secured=False, default=None) |
| 102 | ) |
blob_gas_used¶
| 103 | blob_gas_used: U64 = U64(0) |
|---|
requests¶
| 104 | requests: List[Bytes] = field(default_factory=list) |
|---|
block_access_list¶
| 105 | block_access_list: BlockAccessList = field(default_factory=list) |
|---|
TransactionEnvironment ¶
Items that are used by contract creation or message call.
| 108 | @dataclass |
|---|
class TransactionEnvironment:
origin¶
| 114 | origin: Address |
|---|
gas_price¶
| 115 | gas_price: Uint |
|---|
gas¶
| 116 | gas: Uint |
|---|
access_list_addresses¶
| 117 | access_list_addresses: Set[Address] |
|---|
access_list_storage_keys¶
| 118 | access_list_storage_keys: Set[Tuple[Address, Bytes32]] |
|---|
state¶
| 119 | state: TransactionState |
|---|
blob_versioned_hashes¶
| 120 | blob_versioned_hashes: Tuple[VersionedHash, ...] |
|---|
index_in_block¶
| 122 | index_in_block: Optional[Uint] |
|---|
tx_hash¶
| 123 | tx_hash: Optional[Hash32] |
|---|
Message ¶
Items that are used by contract creation or message call.
| 126 | @dataclass |
|---|
class Message:
block_env¶
| 132 | block_env: BlockEnvironment |
|---|
tx_env¶
| 133 | tx_env: TransactionEnvironment |
|---|
caller¶
| 134 | caller: Address |
|---|
target¶
| 135 | target: Bytes0 | Address |
|---|
current_target¶
| 136 | current_target: Address |
|---|
gas¶
| 137 | gas: Uint |
|---|
value¶
| 138 | value: U256 |
|---|
data¶
| 139 | data: Bytes |
|---|
code_address¶
| 140 | code_address: Optional[Address] |
|---|
code¶
| 141 | code: Bytes |
|---|
depth¶
| 142 | depth: Uint |
|---|
should_transfer_value¶
| 143 | should_transfer_value: bool |
|---|
is_static¶
| 144 | is_static: bool |
|---|
accessed_addresses¶
| 145 | accessed_addresses: Set[Address] |
|---|
accessed_storage_keys¶
| 146 | accessed_storage_keys: Set[Tuple[Address, Bytes32]] |
|---|
disable_precompiles¶
| 147 | disable_precompiles: bool |
|---|
parent_evm¶
| 148 | parent_evm: Optional["Evm"] |
|---|
Evm ¶
The internal state of the virtual machine.
| 151 | @dataclass |
|---|
class Evm:
pc¶
| 155 | pc: Uint |
|---|
stack¶
| 156 | stack: List[U256] |
|---|
memory¶
| 157 | memory: bytearray |
|---|
code¶
| 158 | code: Bytes |
|---|
gas_left¶
| 159 | gas_left: Uint |
|---|
valid_jump_destinations¶
| 160 | valid_jump_destinations: Set[Uint] |
|---|
logs¶
| 161 | logs: Tuple[Log, ...] |
|---|
refund_counter¶
| 162 | refund_counter: int |
|---|
running¶
| 163 | running: bool |
|---|
message¶
| 164 | message: Message |
|---|
output¶
| 165 | output: Bytes |
|---|
accounts_to_delete¶
| 166 | accounts_to_delete: Set[Address] |
|---|
return_data¶
| 167 | return_data: Bytes |
|---|
error¶
| 168 | error: Optional[EthereumException] |
|---|
accessed_addresses¶
| 169 | accessed_addresses: Set[Address] |
|---|
accessed_storage_keys¶
| 170 | accessed_storage_keys: Set[Tuple[Address, Bytes32]] |
|---|
incorporate_child_on_success ¶
Incorporate the state of a successful child_evm into the parent evm.
Parameters
evm :
The parent EVM.
child_evm :
The child evm to incorporate.
def incorporate_child_on_success(evm: Evm, child_evm: Evm) -> None:
| 174 | """ |
|---|---|
| 175 | Incorporate the state of a successful `child_evm` into the parent `evm`. |
| 176 | |
| 177 | Parameters |
| 178 | ---------- |
| 179 | evm : |
| 180 | The parent `EVM`. |
| 181 | child_evm : |
| 182 | The child evm to incorporate. |
| 183 | |
| 184 | """ |
| 185 | evm.gas_left += child_evm.gas_left |
| 186 | evm.logs += child_evm.logs |
| 187 | evm.refund_counter += child_evm.refund_counter |
| 188 | evm.accounts_to_delete.update(child_evm.accounts_to_delete) |
| 189 | evm.accessed_addresses.update(child_evm.accessed_addresses) |
| 190 | evm.accessed_storage_keys.update(child_evm.accessed_storage_keys) |
incorporate_child_on_error ¶
Incorporate the state of an unsuccessful child_evm into the parent evm.
Parameters
evm :
The parent EVM.
child_evm :
The child evm to incorporate.
def incorporate_child_on_error(evm: Evm, child_evm: Evm) -> None:
| 194 | """ |
|---|---|
| 195 | Incorporate the state of an unsuccessful `child_evm` into the parent `evm`. |
| 196 | |
| 197 | Parameters |
| 198 | ---------- |
| 199 | evm : |
| 200 | The parent `EVM`. |
| 201 | child_evm : |
| 202 | The child evm to incorporate. |
| 203 | |
| 204 | """ |
| 205 | evm.gas_left += child_evm.gas_left |
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:
| 214 | """ |
|---|---|
| 215 | Emit a LOG3 for all ETH transfers satisfying EIP-7708. |
| 216 | |
| 217 | Parameters |
| 218 | ---------- |
| 219 | evm : |
| 220 | The state of the ethereum virtual machine |
| 221 | sender : |
| 222 | The account address sending the transfer |
| 223 | recipient : |
| 224 | The account address receiving the transfer |
| 225 | transfer_amount : |
| 226 | The amount of ETH transacted |
| 227 | |
| 228 | """ |
| 229 | if transfer_amount == 0: |
| 230 | return |
| 231 | |
| 232 | padded_sender = left_pad_zero_bytes(sender, 32) |
| 233 | padded_recipient = left_pad_zero_bytes(recipient, 32) |
| 234 | log_entry = Log( |
| 235 | address=SYSTEM_ADDRESS, |
| 236 | topics=( |
| 237 | TRANSFER_TOPIC, |
| 238 | Hash32(padded_sender), |
| 239 | Hash32(padded_recipient), |
| 240 | ), |
| 241 | data=transfer_amount.to_be_bytes32(), |
| 242 | ) |
| 243 | |
| 244 | evm.logs = evm.logs + (log_entry,) |
emit_burn_log ¶
Emit a LOG2 for ETH burn per EIP-7708.
Parameters
evm : The state of the ethereum virtual machine account : The account address whose ETH is being burned amount : The amount of ETH being burned
def emit_burn_log(evm: Evm, account: Address, amount: U256) -> None:
| 252 | """ |
|---|---|
| 253 | Emit a LOG2 for ETH burn per EIP-7708. |
| 254 | |
| 255 | Parameters |
| 256 | ---------- |
| 257 | evm : |
| 258 | The state of the ethereum virtual machine |
| 259 | account : |
| 260 | The account address whose ETH is being burned |
| 261 | amount : |
| 262 | The amount of ETH being burned |
| 263 | |
| 264 | """ |
| 265 | if amount == 0: |
| 266 | return |
| 267 | |
| 268 | padded_account = left_pad_zero_bytes(account, 32) |
| 269 | log_entry = Log( |
| 270 | address=SYSTEM_ADDRESS, |
| 271 | topics=( |
| 272 | BURN_TOPIC, |
| 273 | Hash32(padded_account), |
| 274 | ), |
| 275 | data=amount.to_be_bytes32(), |
| 276 | ) |
| 277 | |
| 278 | evm.logs = evm.logs + (log_entry,) |