ethereum.forks.berlin.vmethereum.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
@final
39
@dataclass
class BlockEnvironment:

chain_id

45
    chain_id: U64

state

46
    state: BlockState

block_gas_limit

47
    block_gas_limit: Uint

block_hashes

48
    block_hashes: List[Hash32]

coinbase

49
    coinbase: Address

number

50
    number: Uint

base_fee_per_gas

51
    base_fee_per_gas: Uint

time

52
    time: U256

difficulty

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

56
@final
57
@dataclass
class BlockOutput:

block_gas_used

77
    block_gas_used: Uint = Uint(0)

transactions_trie

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

receipts_trie

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

receipt_keys

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

block_logs

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

TransactionEnvironment

Items that are used while processing a transaction.

88
@final
89
@dataclass
class TransactionEnvironment:

origin

95
    origin: Address

gas_price

96
    gas_price: Uint

gas

97
    gas: Uint

access_list_addresses

98
    access_list_addresses: Set[Address]

access_list_storage_keys

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

state

100
    state: TransactionState

index_in_block

101
    index_in_block: Optional[Uint]

tx_hash

102
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

105
@final
106
@dataclass
class Message:

block_env

112
    block_env: BlockEnvironment

tx_env

113
    tx_env: TransactionEnvironment

caller

114
    caller: Address

target

115
    target: Bytes0 | Address

current_target

116
    current_target: Address

gas

117
    gas: Uint

value

118
    value: U256

data

119
    data: Bytes

code_address

120
    code_address: Optional[Address]

code

121
    code: Bytes

depth

122
    depth: Uint

should_transfer_value

123
    should_transfer_value: bool

is_static

124
    is_static: bool

accessed_addresses

125
    accessed_addresses: Set[Address]

accessed_storage_keys

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

parent_evm

127
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

130
@final
131
@dataclass
class Evm:

pc

135
    pc: Uint

stack

136
    stack: List[U256]

memory

137
    memory: bytearray

code

138
    code: Bytes

gas_left

139
    gas_left: Uint

valid_jump_destinations

140
    valid_jump_destinations: Set[Uint]

logs

141
    logs: Tuple[Log, ...]

refund_counter

142
    refund_counter: int

running

143
    running: bool

message

144
    message: Message

output

145
    output: Bytes

accounts_to_delete

146
    accounts_to_delete: Set[Address]

touched_accounts

147
    touched_accounts: Set[Address]

return_data

148
    return_data: Bytes

error

149
    error: Optional[EthereumException]

accessed_addresses

150
    accessed_addresses: Set[Address]

accessed_storage_keys

151
    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:
155
    <snip>
166
    evm.gas_left += child_evm.gas_left
167
    evm.logs += child_evm.logs
168
    evm.refund_counter += child_evm.refund_counter
169
    evm.accounts_to_delete.update(child_evm.accounts_to_delete)
170
    evm.touched_accounts.update(child_evm.touched_accounts)
171
    if account_exists_and_is_empty(
172
        evm.message.tx_env.state, child_evm.message.current_target
173
    ):
174
        evm.touched_accounts.add(child_evm.message.current_target)
175
    evm.accessed_addresses.update(child_evm.accessed_addresses)
176
    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:
180
    <snip>
191
    # In block 2675119, the empty account at 0x3 (the RIPEMD160 precompile) was
192
    # cleared despite running out of gas. This is an obscure edge case that can
193
    # only happen to a precompile.
194
    # According to the general rules governing clearing of empty accounts, the
195
    # touch should have been reverted. Due to client bugs, this event went
196
    # unnoticed and 0x3 has been exempted from the rule that touches are
197
    # reverted in order to preserve this historical behaviour.
198
    if RIPEMD160_ADDRESS in child_evm.touched_accounts:
199
        evm.touched_accounts.add(RIPEMD160_ADDRESS)
200
    if child_evm.message.current_target == RIPEMD160_ADDRESS:
201
        if account_exists_and_is_empty(
202
            evm.message.tx_env.state, child_evm.message.current_target
203
        ):
204
            evm.touched_accounts.add(RIPEMD160_ADDRESS)
205
    evm.gas_left += child_evm.gas_left