ethereum.berlin.vmethereum.london.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__
32 | __all__ = ("Environment", "Evm", "Message") |
---|
BlockEnvironment
Items external to the virtual machine itself, provided by the environment.
35 | @dataclass |
---|
class BlockEnvironment:
chain_id
41 | chain_id: U64 |
---|
state
42 | state: State |
---|
block_gas_limit
43 | block_gas_limit: Uint |
---|
block_hashes
44 | block_hashes: List[Hash32] |
---|
coinbase
45 | coinbase: Address |
---|
number
46 | number: Uint |
---|
base_fee_per_gas
47 | base_fee_per_gas: Uint |
---|
time
48 | time: U256 |
---|
difficulty
49 | difficulty: Uint |
---|
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 :
Key of all the receipts in the block.
block_logs : Bloom
Logs bloom of all the logs included in all the transactions of the
block.
52 | @dataclass |
---|
class BlockOutput:
block_gas_used
72 | block_gas_used: Uint = Uint(0) |
---|
transactions_trie
73 | transactions_trie: Trie[ |
---|---|
74 | Bytes, Optional[Union[Bytes, LegacyTransaction]] |
75 | ] = field(default_factory=lambda: Trie(secured=False, default=None)) |
receipts_trie
76 | receipts_trie: Trie[Bytes, Optional[Union[Bytes, Receipt]]] = field( |
---|---|
77 | default_factory=lambda: Trie(secured=False, default=None) |
78 | ) |
receipt_keys
79 | receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple) |
---|
block_logs
80 | block_logs: Tuple[Log, ...] = field(default_factory=tuple) |
---|
TransactionEnvironment
Items that are used by contract creation or message call.
83 | @dataclass |
---|
class TransactionEnvironment:
origin
89 | origin: Address |
---|
gas_price
90 | gas_price: Uint |
---|
gas
91 | gas: Uint |
---|
access_list_addresses
92 | access_list_addresses: Set[Address] |
---|
access_list_storage_keys
93 | access_list_storage_keys: Set[Tuple[Address, Bytes32]] |
---|
index_in_block
94 | index_in_block: Optional[Uint] |
---|
tx_hash
95 | tx_hash: Optional[Hash32] |
---|
traces
96 | traces: List[dict] |
---|
Message
Items that are used by contract creation or message call.
99 | @dataclass |
---|
class Message:
block_env
105 | block_env: BlockEnvironment |
---|
tx_env
106 | tx_env: TransactionEnvironment |
---|
caller
107 | caller: Address |
---|
target
108 | target: Union[Bytes0, Address] |
---|
current_target
109 | current_target: Address |
---|
gas
110 | gas: Uint |
---|
value
111 | value: U256 |
---|
data
112 | data: Bytes |
---|
code_address
113 | code_address: Optional[Address] |
---|
code
114 | code: Bytes |
---|
depth
115 | depth: Uint |
---|
should_transfer_value
116 | should_transfer_value: bool |
---|
is_static
117 | is_static: bool |
---|
accessed_addresses
118 | accessed_addresses: Set[Address] |
---|
accessed_storage_keys
119 | accessed_storage_keys: Set[Tuple[Address, Bytes32]] |
---|
parent_evm
120 | parent_evm: Optional["Evm"] |
---|
Evm
The internal state of the virtual machine.
123 | @dataclass |
---|
class Evm:
pc
127 | pc: Uint |
---|
stack
128 | stack: List[U256] |
---|
memory
129 | memory: bytearray |
---|
code
130 | code: Bytes |
---|
gas_left
131 | gas_left: Uint |
---|
valid_jump_destinations
132 | valid_jump_destinations: Set[Uint] |
---|
logs
133 | logs: Tuple[Log, ...] |
---|
refund_counter
134 | refund_counter: int |
---|
running
135 | running: bool |
---|
message
136 | message: Message |
---|
output
137 | output: Bytes |
---|
accounts_to_delete
138 | accounts_to_delete: Set[Address] |
---|
touched_accounts
139 | touched_accounts: Set[Address] |
---|
return_data
140 | return_data: Bytes |
---|
error
141 | error: Optional[EthereumException] |
---|
accessed_addresses
142 | accessed_addresses: Set[Address] |
---|
accessed_storage_keys
143 | 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:
147 | """ |
---|---|
148 | Incorporate the state of a successful `child_evm` into the parent `evm`. |
149 |
|
150 | Parameters |
151 | ---------- |
152 | evm : |
153 | The parent `EVM`. |
154 | child_evm : |
155 | The child evm to incorporate. |
156 | """ |
157 | evm.gas_left += child_evm.gas_left |
158 | evm.logs += child_evm.logs |
159 | evm.refund_counter += child_evm.refund_counter |
160 | evm.accounts_to_delete.update(child_evm.accounts_to_delete) |
161 | evm.touched_accounts.update(child_evm.touched_accounts) |
162 | if account_exists_and_is_empty( |
163 | evm.message.block_env.state, child_evm.message.current_target |
164 | ): |
165 | evm.touched_accounts.add(child_evm.message.current_target) |
166 | evm.accessed_addresses.update(child_evm.accessed_addresses) |
167 | 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:
171 | """ |
---|---|
172 | Incorporate the state of an unsuccessful `child_evm` into the parent `evm`. |
173 |
|
174 | Parameters |
175 | ---------- |
176 | evm : |
177 | The parent `EVM`. |
178 | child_evm : |
179 | The child evm to incorporate. |
180 | """ |
181 | # In block 2675119, the empty account at 0x3 (the RIPEMD160 precompile) was |
182 | # cleared despite running out of gas. This is an obscure edge case that can |
183 | # only happen to a precompile. |
184 | # According to the general rules governing clearing of empty accounts, the |
185 | # touch should have been reverted. Due to client bugs, this event went |
186 | # unnoticed and 0x3 has been exempted from the rule that touches are |
187 | # reverted in order to preserve this historical behaviour. |
188 | if RIPEMD160_ADDRESS in child_evm.touched_accounts: |
189 | evm.touched_accounts.add(RIPEMD160_ADDRESS) |
190 | if child_evm.message.current_target == RIPEMD160_ADDRESS: |
191 | if account_exists_and_is_empty( |
192 | evm.message.block_env.state, child_evm.message.current_target |
193 | ): |
194 | evm.touched_accounts.add(RIPEMD160_ADDRESS) |
195 | evm.gas_left += child_evm.gas_left |