ethereum.istanbul.vmethereum.muir_glacier.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 |
---|
time
47 | time: U256 |
---|
difficulty
48 | 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.
51 | @dataclass |
---|
class BlockOutput:
block_gas_used
71 | block_gas_used: Uint = Uint(0) |
---|
transactions_trie
72 | transactions_trie: Trie[Bytes, Optional[Transaction]] = field( |
---|---|
73 | default_factory=lambda: Trie(secured=False, default=None) |
74 | ) |
receipts_trie
75 | receipts_trie: Trie[Bytes, Optional[Receipt]] = field( |
---|---|
76 | default_factory=lambda: Trie(secured=False, default=None) |
77 | ) |
receipt_keys
78 | receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple) |
---|
block_logs
79 | block_logs: Tuple[Log, ...] = field(default_factory=tuple) |
---|
TransactionEnvironment
Items that are used by contract creation or message call.
82 | @dataclass |
---|
class TransactionEnvironment:
origin
88 | origin: Address |
---|
gas_price
89 | gas_price: Uint |
---|
gas
90 | gas: Uint |
---|
index_in_block
91 | index_in_block: Uint |
---|
tx_hash
92 | tx_hash: Optional[Hash32] |
---|
traces
93 | traces: List[dict] |
---|
Message
Items that are used by contract creation or message call.
96 | @dataclass |
---|
class Message:
block_env
102 | block_env: BlockEnvironment |
---|
tx_env
103 | tx_env: TransactionEnvironment |
---|
caller
104 | caller: Address |
---|
target
105 | target: Union[Bytes0, Address] |
---|
current_target
106 | current_target: Address |
---|
gas
107 | gas: Uint |
---|
value
108 | value: U256 |
---|
data
109 | data: Bytes |
---|
code_address
110 | code_address: Optional[Address] |
---|
code
111 | code: Bytes |
---|
depth
112 | depth: Uint |
---|
should_transfer_value
113 | should_transfer_value: bool |
---|
is_static
114 | is_static: bool |
---|
parent_evm
115 | parent_evm: Optional["Evm"] |
---|
Evm
The internal state of the virtual machine.
118 | @dataclass |
---|
class Evm:
pc
122 | pc: Uint |
---|
stack
123 | stack: List[U256] |
---|
memory
124 | memory: bytearray |
---|
code
125 | code: Bytes |
---|
gas_left
126 | gas_left: Uint |
---|
valid_jump_destinations
127 | valid_jump_destinations: Set[Uint] |
---|
logs
128 | logs: Tuple[Log, ...] |
---|
refund_counter
129 | refund_counter: int |
---|
running
130 | running: bool |
---|
message
131 | message: Message |
---|
output
132 | output: Bytes |
---|
accounts_to_delete
133 | accounts_to_delete: Set[Address] |
---|
touched_accounts
134 | touched_accounts: Set[Address] |
---|
return_data
135 | return_data: Bytes |
---|
error
136 | error: Optional[EthereumException] |
---|
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:
140 | """ |
---|---|
141 | Incorporate the state of a successful `child_evm` into the parent `evm`. |
142 |
|
143 | Parameters |
144 | ---------- |
145 | evm : |
146 | The parent `EVM`. |
147 | child_evm : |
148 | The child evm to incorporate. |
149 | """ |
150 | evm.gas_left += child_evm.gas_left |
151 | evm.logs += child_evm.logs |
152 | evm.refund_counter += child_evm.refund_counter |
153 | evm.accounts_to_delete.update(child_evm.accounts_to_delete) |
154 | evm.touched_accounts.update(child_evm.touched_accounts) |
155 | if account_exists_and_is_empty( |
156 | evm.message.block_env.state, child_evm.message.current_target |
157 | ): |
158 | evm.touched_accounts.add(child_evm.message.current_target) |
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:
162 | """ |
---|---|
163 | Incorporate the state of an unsuccessful `child_evm` into the parent `evm`. |
164 |
|
165 | Parameters |
166 | ---------- |
167 | evm : |
168 | The parent `EVM`. |
169 | child_evm : |
170 | The child evm to incorporate. |
171 | """ |
172 | # In block 2675119, the empty account at 0x3 (the RIPEMD160 precompile) was |
173 | # cleared despite running out of gas. This is an obscure edge case that can |
174 | # only happen to a precompile. |
175 | # According to the general rules governing clearing of empty accounts, the |
176 | # touch should have been reverted. Due to client bugs, this event went |
177 | # unnoticed and 0x3 has been exempted from the rule that touches are |
178 | # reverted in order to preserve this historical behaviour. |
179 | if RIPEMD160_ADDRESS in child_evm.touched_accounts: |
180 | evm.touched_accounts.add(RIPEMD160_ADDRESS) |
181 | if child_evm.message.current_target == RIPEMD160_ADDRESS: |
182 | if account_exists_and_is_empty( |
183 | evm.message.block_env.state, child_evm.message.current_target |
184 | ): |
185 | evm.touched_accounts.add(RIPEMD160_ADDRESS) |
186 | evm.gas_left += child_evm.gas_left |