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

time

50
    time: U256

difficulty

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

54
@dataclass
class BlockOutput:

block_gas_used

74
    block_gas_used: Uint = Uint(0)

transactions_trie

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

receipts_trie

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

receipt_keys

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

block_logs

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

TransactionEnvironment

Items that are used by contract creation or message call.

85
@dataclass
class TransactionEnvironment:

origin

91
    origin: Address

gas_price

92
    gas_price: Uint

gas

93
    gas: Uint

state

94
    state: TransactionState

index_in_block

95
    index_in_block: Uint

tx_hash

96
    tx_hash: Optional[Hash32]

Message

Items that are used by contract creation or message call.

99
@dataclass
class Message:

block_env

105
    block_env: BlockEnvironment

tx_env

106
    tx_env: TransactionEnvironment

caller

107
    caller: Address

target

108
    target: Bytes0 | Address

current_target

109
    current_target: Address

gas

110
    gas: Uint

value

111
    value: U256

data

112
    data: Bytes

code_address

113
    code_address: Optional[Address]

code

114
    code: Bytes

depth

115
    depth: Uint

should_transfer_value

116
    should_transfer_value: bool

is_static

117
    is_static: bool

parent_evm

118
    parent_evm: Optional["Evm"]

Evm

The internal state of the virtual machine.

121
@dataclass
class Evm:

pc

125
    pc: Uint

stack

126
    stack: List[U256]

memory

127
    memory: bytearray

code

128
    code: Bytes

gas_left

129
    gas_left: Uint

valid_jump_destinations

130
    valid_jump_destinations: Set[Uint]

logs

131
    logs: Tuple[Log, ...]

refund_counter

132
    refund_counter: int

running

133
    running: bool

message

134
    message: Message

output

135
    output: Bytes

accounts_to_delete

136
    accounts_to_delete: Set[Address]

touched_accounts

137
    touched_accounts: Set[Address]

return_data

138
    return_data: Bytes

error

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