ethereum.forks.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__

35
__all__ = ("Environment", "Evm", "Message")

BlockEnvironment

Items external to the virtual machine itself, provided by the environment.

38
@dataclass
class BlockEnvironment:

chain_id

44
    chain_id: U64

state

45
    state: BlockState

block_gas_limit

46
    block_gas_limit: Uint

block_hashes

47
    block_hashes: List[Hash32]

coinbase

48
    coinbase: Address

number

49
    number: Uint

base_fee_per_gas

50
    base_fee_per_gas: Uint

time

51
    time: U256

difficulty

52
    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 : 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.

55
@dataclass
class BlockOutput:

block_gas_used

75
    block_gas_used: Uint = Uint(0)

transactions_trie

76
    transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = (
77
        field(default_factory=lambda: Trie(secured=False, default=None))
78
    )

receipts_trie

79
    receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
80
        default_factory=lambda: Trie(secured=False, default=None)
81
    )

receipt_keys

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

block_logs

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

TransactionEnvironment

Items that are used by contract creation or message call.

86
@dataclass
class TransactionEnvironment:

origin

92
    origin: Address

gas_price

93
    gas_price: Uint

gas

94
    gas: Uint

access_list_addresses

95
    access_list_addresses: Set[Address]

access_list_storage_keys

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

state

97
    state: TransactionState

index_in_block

98
    index_in_block: Optional[Uint]

tx_hash

99
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

102
@dataclass
class Message:

block_env

108
    block_env: BlockEnvironment

tx_env

109
    tx_env: TransactionEnvironment

caller

110
    caller: Address

target

111
    target: Bytes0 | Address

current_target

112
    current_target: Address

gas

113
    gas: Uint

value

114
    value: U256

data

115
    data: Bytes

code_address

116
    code_address: Optional[Address]

code

117
    code: Bytes

depth

118
    depth: Uint

should_transfer_value

119
    should_transfer_value: bool

is_static

120
    is_static: bool

accessed_addresses

121
    accessed_addresses: Set[Address]

accessed_storage_keys

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

parent_evm

123
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

126
@dataclass
class Evm:

pc

130
    pc: Uint

stack

131
    stack: List[U256]

memory

132
    memory: bytearray

code

133
    code: Bytes

gas_left

134
    gas_left: Uint

valid_jump_destinations

135
    valid_jump_destinations: Set[Uint]

logs

136
    logs: Tuple[Log, ...]

refund_counter

137
    refund_counter: int

running

138
    running: bool

message

139
    message: Message

output

140
    output: Bytes

accounts_to_delete

141
    accounts_to_delete: Set[Address]

touched_accounts

142
    touched_accounts: Set[Address]

return_data

143
    return_data: Bytes

error

144
    error: Optional[EthereumException]

accessed_addresses

145
    accessed_addresses: Set[Address]

accessed_storage_keys

146
    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:
150
    """
151
    Incorporate the state of a successful `child_evm` into the parent `evm`.
152
153
    Parameters
154
    ----------
155
    evm :
156
        The parent `EVM`.
157
    child_evm :
158
        The child evm to incorporate.
159
160
    """
161
    evm.gas_left += child_evm.gas_left
162
    evm.logs += child_evm.logs
163
    evm.refund_counter += child_evm.refund_counter
164
    evm.accounts_to_delete.update(child_evm.accounts_to_delete)
165
    evm.touched_accounts.update(child_evm.touched_accounts)
166
    if account_exists_and_is_empty(
167
        evm.message.tx_env.state, child_evm.message.current_target
168
    ):
169
        evm.touched_accounts.add(child_evm.message.current_target)
170
    evm.accessed_addresses.update(child_evm.accessed_addresses)
171
    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:
175
    """
176
    Incorporate the state of an unsuccessful `child_evm` into the parent `evm`.
177
178
    Parameters
179
    ----------
180
    evm :
181
        The parent `EVM`.
182
    child_evm :
183
        The child evm to incorporate.
184
185
    """
186
    # In block 2675119, the empty account at 0x3 (the RIPEMD160 precompile) was
187
    # cleared despite running out of gas. This is an obscure edge case that can
188
    # only happen to a precompile.
189
    # According to the general rules governing clearing of empty accounts, the
190
    # touch should have been reverted. Due to client bugs, this event went
191
    # unnoticed and 0x3 has been exempted from the rule that touches are
192
    # reverted in order to preserve this historical behaviour.
193
    if RIPEMD160_ADDRESS in child_evm.touched_accounts:
194
        evm.touched_accounts.add(RIPEMD160_ADDRESS)
195
    if child_evm.message.current_target == RIPEMD160_ADDRESS:
196
        if account_exists_and_is_empty(
197
            evm.message.tx_env.state, child_evm.message.current_target
198
        ):
199
            evm.touched_accounts.add(RIPEMD160_ADDRESS)
200
    evm.gas_left += child_evm.gas_left