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

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

91
    block_gas_used: Uint = Uint(0)

cumulative_gas_used

92
    cumulative_gas_used: Uint = Uint(0)

transactions_trie

93
    transactions_trie: Trie[Bytes, Optional[Bytes | LegacyTransaction]] = (
94
        field(default_factory=lambda: Trie(secured=False, default=None))
95
    )

receipts_trie

96
    receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
97
        default_factory=lambda: Trie(secured=False, default=None)
98
    )

receipt_keys

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

block_logs

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

withdrawals_trie

101
    withdrawals_trie: Trie[Bytes, Optional[Bytes | Withdrawal]] = field(
102
        default_factory=lambda: Trie(secured=False, default=None)
103
    )

blob_gas_used

104
    blob_gas_used: U64 = U64(0)

requests

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

block_access_list

106
    block_access_list: BlockAccessList = field(default_factory=list)

TransactionEnvironment

Items that are used by contract creation or message call.

109
@dataclass
class TransactionEnvironment:

origin

115
    origin: Address

gas_price

116
    gas_price: Uint

gas

117
    gas: Uint

access_list_addresses

118
    access_list_addresses: Set[Address]

access_list_storage_keys

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

state

120
    state: TransactionState

blob_versioned_hashes

121
    blob_versioned_hashes: Tuple[VersionedHash, ...]

authorizations

122
    authorizations: Tuple[Authorization, ...]

index_in_block

123
    index_in_block: Optional[Uint]

tx_hash

124
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

127
@dataclass
class Message:

block_env

133
    block_env: BlockEnvironment

tx_env

134
    tx_env: TransactionEnvironment

caller

135
    caller: Address

target

136
    target: Bytes0 | Address

current_target

137
    current_target: Address

gas

138
    gas: Uint

value

139
    value: U256

data

140
    data: Bytes

code_address

141
    code_address: Optional[Address]

code

142
    code: Bytes

depth

143
    depth: Uint

should_transfer_value

144
    should_transfer_value: bool

is_static

145
    is_static: bool

accessed_addresses

146
    accessed_addresses: Set[Address]

accessed_storage_keys

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

disable_precompiles

148
    disable_precompiles: bool

parent_evm

149
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

152
@dataclass
class Evm:

pc

156
    pc: Uint

stack

157
    stack: List[U256]

memory

158
    memory: bytearray

code

159
    code: Bytes

gas_left

160
    gas_left: Uint

valid_jump_destinations

161
    valid_jump_destinations: Set[Uint]

logs

162
    logs: Tuple[Log, ...]

refund_counter

163
    refund_counter: int

running

164
    running: bool

message

165
    message: Message

output

166
    output: Bytes

accounts_to_delete

167
    accounts_to_delete: Set[Address]

return_data

168
    return_data: Bytes

error

169
    error: Optional[EthereumException]

accessed_addresses

170
    accessed_addresses: Set[Address]

accessed_storage_keys

171
    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:
175
    <snip>
186
    evm.gas_left += child_evm.gas_left
187
    evm.logs += child_evm.logs
188
    evm.refund_counter += child_evm.refund_counter
189
    evm.accounts_to_delete.update(child_evm.accounts_to_delete)
190
    evm.accessed_addresses.update(child_evm.accessed_addresses)
191
    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:
195
    <snip>
206
    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:
215
    <snip>
230
    if transfer_amount == 0:
231
        return
232
233
    padded_sender = left_pad_zero_bytes(sender, 32)
234
    padded_recipient = left_pad_zero_bytes(recipient, 32)
235
    log_entry = Log(
236
        address=SYSTEM_ADDRESS,
237
        topics=(
238
            TRANSFER_TOPIC,
239
            Hash32(padded_sender),
240
            Hash32(padded_recipient),
241
        ),
242
        data=transfer_amount.to_be_bytes32(),
243
    )
244
245
    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:
253
    <snip>
266
    if amount == 0:
267
        return
268
269
    padded_account = left_pad_zero_bytes(account, 32)
270
    log_entry = Log(
271
        address=SYSTEM_ADDRESS,
272
        topics=(
273
            BURN_TOPIC,
274
            Hash32(padded_account),
275
        ),
276
        data=amount.to_be_bytes32(),
277
    )
278
279
    evm.logs = evm.logs + (log_entry,)