ethereum.forks.bpo5.vmethereum.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

41
    state: 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

slot_number

60
    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.

63
@dataclass
class BlockOutput:

block_gas_used

95
    block_gas_used: Uint = Uint(0)

block_state_gas_used

96
    block_state_gas_used: Uint = Uint(0)

cumulative_gas_used

97
    cumulative_gas_used: Uint = Uint(0)

transactions_trie

80
    transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = (
81
        field(default_factory=lambda: Trie(secured=False, default=None))
98
    transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = (
99
        field(default_factory=lambda: Trie(secured=False, default=None))
100
    )

receipts_trie

83
    receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
84
        default_factory=lambda: Trie(secured=False, default=None)
101
    receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
102
        default_factory=lambda: Trie(secured=False, default=None)
103
    )

receipt_keys

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

block_logs

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

withdrawals_trie

88
    withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field(
89
        default_factory=lambda: Trie(secured=False, default=None)
106
    withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field(
107
        default_factory=lambda: Trie(secured=False, default=None)
108
    )

blob_gas_used

109
    blob_gas_used: U64 = U64(0)

requests

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

block_access_list

111
    block_access_list: BlockAccessList = field(default_factory=list)

TransactionEnvironment

Items that are used by contract creation or message call.

114
@dataclass
class TransactionEnvironment:

origin

120
    origin: Address

gas_price

121
    gas_price: Uint

gas

122
    gas: Uint

state_gas_reservoir

123
    state_gas_reservoir: Uint

access_list_addresses

124
    access_list_addresses: Set[Address]

access_list_storage_keys

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

transient_storage

106
    transient_storage: TransientStorage

state

126
    state: TransactionState

blob_versioned_hashes

127
    blob_versioned_hashes: Tuple[VersionedHash, ...]

authorizations

128
    authorizations: Tuple[Authorization, ...]

index_in_block

129
    index_in_block: Optional[Uint]

tx_hash

130
    tx_hash: Optional[Hash32]

intrinsic_regular_gas

131
    intrinsic_regular_gas: Uint

intrinsic_state_gas

132
    intrinsic_state_gas: Uint

Message

Items that are used by contract creation or message call.

135
@dataclass
class Message:

block_env

141
    block_env: BlockEnvironment

tx_env

142
    tx_env: TransactionEnvironment

caller

143
    caller: Address

target

144
    target: Bytes0 | Address

current_target

145
    current_target: Address

gas

146
    gas: Uint

state_gas_reservoir

147
    state_gas_reservoir: Uint

value

148
    value: U256

data

149
    data: Bytes

code_address

150
    code_address: Optional[Address]

code

151
    code: Bytes

depth

152
    depth: Uint

should_transfer_value

153
    should_transfer_value: bool

is_static

154
    is_static: bool

accessed_addresses

155
    accessed_addresses: Set[Address]

accessed_storage_keys

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

disable_precompiles

157
    disable_precompiles: bool

parent_evm

158
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

161
@dataclass
class Evm:

pc

165
    pc: Uint

stack

166
    stack: List[U256]

memory

167
    memory: bytearray

code

168
    code: Bytes

gas_left

169
    gas_left: Uint

state_gas_reservoir

170
    state_gas_reservoir: Uint

valid_jump_destinations

171
    valid_jump_destinations: Set[Uint]

logs

172
    logs: Tuple[Log, ...]

refund_counter

173
    refund_counter: int

running

174
    running: bool

message

175
    message: Message

output

176
    output: Bytes

accounts_to_delete

177
    accounts_to_delete: Set[Address]

return_data

178
    return_data: Bytes

error

179
    error: Optional[EthereumException]

accessed_addresses

180
    accessed_addresses: Set[Address]

accessed_storage_keys

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

regular_gas_used

182
    regular_gas_used: Uint = Uint(0)

state_gas_used

187
    state_gas_used: int = 0

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:
191
    """
192
    Incorporate the state of a successful `child_evm` into the parent `evm`.
193
194
    Parameters
195
    ----------
196
    evm :
197
        The parent `EVM`.
198
    child_evm :
199
        The child evm to incorporate.
200
201
    """
202
    evm.gas_left += child_evm.gas_left
203
    evm.state_gas_reservoir += child_evm.state_gas_reservoir
204
    evm.logs += child_evm.logs
205
    evm.refund_counter += child_evm.refund_counter
206
    evm.accounts_to_delete.update(child_evm.accounts_to_delete)
207
    evm.accessed_addresses.update(child_evm.accessed_addresses)
177
    evm.accessed_storage_keys.update(child_evm.accessed_storage_keys)
208
    evm.accessed_storage_keys.update(child_evm.accessed_storage_keys)
209
    evm.regular_gas_used += child_evm.regular_gas_used
210
    evm.state_gas_used += child_evm.state_gas_used

incorporate_child_on_error

Incorporate the state of an unsuccessful child_evm into the parent evm.

All state gas the child held is returned to the parent state_gas_reservoir.

Parameters

evm : The parent EVM. child_evm : The child evm to incorporate.

def incorporate_child_on_error(evm: Evm, ​​child_evm: Evm) -> None:
217
    """
218
    Incorporate the state of an unsuccessful `child_evm` into the parent `evm`.
219
220
    All state gas the child held is returned to the parent
221
    `state_gas_reservoir`.
222
223
    Parameters
224
    ----------
225
    evm :
226
        The parent `EVM`.
227
    child_evm :
228
        The child evm to incorporate.
229
230
    """
192
    evm.gas_left += child_evm.gas_left
231
    evm.gas_left += child_evm.gas_left
232
    evm.state_gas_reservoir += (
233
        Uint(max(0, child_evm.state_gas_used)) + child_evm.state_gas_reservoir
234
    )
235
    evm.regular_gas_used += child_evm.regular_gas_used

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
    """
245
    Emit a LOG3 for all ETH transfers satisfying EIP-7708.
246
247
    Parameters
248
    ----------
249
    evm :
250
        The state of the ethereum virtual machine
251
    sender :
252
        The account address sending the transfer
253
    recipient :
254
        The account address receiving the transfer
255
    transfer_amount :
256
        The amount of ETH transacted
257
258
    """
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,)

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:
282
    """
283
    Emit a LOG2 for ETH burn per EIP-7708.
284
285
    Parameters
286
    ----------
287
    evm :
288
        The state of the ethereum virtual machine
289
    account :
290
        The account address whose ETH is being burned
291
    amount :
292
        The amount of ETH being burned
293
294
    """
295
    if amount == 0:
296
        return
297
298
    padded_account = left_pad_zero_bytes(account, 32)
299
    log_entry = Log(
300
        address=SYSTEM_ADDRESS,
301
        topics=(
302
            BURN_TOPIC,
303
            Hash32(padded_account),
304
        ),
305
        data=amount.to_be_bytes32(),
306
    )
307
308
    evm.logs = evm.logs + (log_entry,)