ethereum.osaka.vm.interpreter

Ethereum Virtual Machine (EVM) Interpreter ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

A straightforward interpreter that executes EVM code.

STACK_DEPTH_LIMIT

65
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

66
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

67
MAX_INIT_CODE_SIZE = 2 * MAX_CODE_SIZE

MessageCallOutput

Output of a particular message call

Contains the following:

  1. `gas_left`: remaining gas after execution.
  2. `refund_counter`: gas to refund after execution.
  3. `logs`: list of `Log` generated during execution.
  4. `accounts_to_delete`: Contracts which have self-destructed.
  5. `error`: The error from the execution if any.
  6. `return_data`: The output of the execution.
70
@dataclass
class MessageCallOutput:

gas_left

85
    gas_left: Uint

refund_counter

86
    refund_counter: U256

logs

87
    logs: Tuple[Log, ...]

accounts_to_delete

88
    accounts_to_delete: Set[Address]

error

89
    error: Optional[EthereumException]

return_data

90
    return_data: Bytes

process_message_call

If message.current is empty then it creates a smart contract else it executes a call from the message.caller to the message.target.

Parameters

message : Transaction specific items.

Returns

output : MessageCallOutput Output of the message call

def process_message_call(message: Message) -> MessageCallOutput:
94
    """
95
    If `message.current` is empty then it creates a smart contract
96
    else it executes a call from the `message.caller` to the `message.target`.
97
98
    Parameters
99
    ----------
100
    message :
101
        Transaction specific items.
102
103
    Returns
104
    -------
105
    output : `MessageCallOutput`
106
        Output of the message call
107
    """
108
    block_env = message.block_env
109
    refund_counter = U256(0)
110
    if message.target == Bytes0(b""):
111
        is_collision = account_has_code_or_nonce(
112
            block_env.state, message.current_target
113
        ) or account_has_storage(block_env.state, message.current_target)
114
        if is_collision:
115
            return MessageCallOutput(
116
                Uint(0),
117
                U256(0),
118
                tuple(),
119
                set(),
120
                AddressCollision(),
121
                Bytes(b""),
122
            )
123
        else:
124
            evm = process_create_message(message)
125
    else:
126
        if message.tx_env.authorizations != ():
127
            refund_counter += set_delegation(message)
128
129
        delegated_address = get_delegated_code_address(message.code)
130
        if delegated_address is not None:
131
            message.disable_precompiles = True
132
            message.accessed_addresses.add(delegated_address)
133
            message.code = get_account(block_env.state, delegated_address).code
134
            message.code_address = delegated_address
135
136
        evm = process_message(message)
137
138
    if evm.error:
139
        logs: Tuple[Log, ...] = ()
140
        accounts_to_delete = set()
141
    else:
142
        logs = evm.logs
143
        accounts_to_delete = evm.accounts_to_delete
144
        refund_counter += U256(evm.refund_counter)
145
146
    tx_end = TransactionEnd(
147
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
148
    )
149
    evm_trace(evm, tx_end)
150
151
    return MessageCallOutput(
152
        gas_left=evm.gas_left,
153
        refund_counter=refund_counter,
154
        logs=logs,
155
        accounts_to_delete=accounts_to_delete,
156
        error=evm.error,
157
        return_data=evm.output,
158
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.osaka.vm.Evm Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
162
    """
163
    Executes a call to create a smart contract.
164
165
    Parameters
166
    ----------
167
    message :
168
        Transaction specific items.
169
170
    Returns
171
    -------
172
    evm: :py:class:`~ethereum.osaka.vm.Evm`
173
        Items containing execution specific objects.
174
    """
175
    state = message.block_env.state
176
    transient_storage = message.tx_env.transient_storage
177
    # take snapshot of state before processing the message
178
    begin_transaction(state, transient_storage)
179
180
    # If the address where the account is being created has storage, it is
181
    # destroyed. This can only happen in the following highly unlikely
182
    # circumstances:
183
    # * The address created by a `CREATE` call collides with a subsequent
184
    #   `CREATE` or `CREATE2` call.
185
    # * The first `CREATE` happened before Spurious Dragon and left empty
186
    #   code.
187
    destroy_storage(state, message.current_target)
188
189
    # In the previously mentioned edge case the preexisting storage is ignored
190
    # for gas refund purposes. In order to do this we must track created
191
    # accounts. This tracking is also needed to respect the constraints
192
    # added to SELFDESTRUCT by EIP-6780.
193
    mark_account_created(state, message.current_target)
194
195
    increment_nonce(state, message.current_target)
196
    evm = process_message(message)
197
    if not evm.error:
198
        contract_code = evm.output
199
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
200
        try:
201
            if len(contract_code) > 0:
202
                if contract_code[0] == 0xEF:
203
                    raise InvalidContractPrefix
204
            charge_gas(evm, contract_code_gas)
205
            if len(contract_code) > MAX_CODE_SIZE:
206
                raise OutOfGasError
207
        except ExceptionalHalt as error:
208
            rollback_transaction(state, transient_storage)
209
            evm.gas_left = Uint(0)
210
            evm.output = b""
211
            evm.error = error
212
        else:
213
            set_code(state, message.current_target, contract_code)
214
            commit_transaction(state, transient_storage)
215
    else:
216
        rollback_transaction(state, transient_storage)
217
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.osaka.vm.Evm Items containing execution specific objects

def process_message(message: Message) -> Evm:
221
    """
222
    Move ether and execute the relevant code.
223
224
    Parameters
225
    ----------
226
    message :
227
        Transaction specific items.
228
229
    Returns
230
    -------
231
    evm: :py:class:`~ethereum.osaka.vm.Evm`
232
        Items containing execution specific objects
233
    """
234
    state = message.block_env.state
235
    transient_storage = message.tx_env.transient_storage
236
    if message.depth > STACK_DEPTH_LIMIT:
237
        raise StackDepthLimitError("Stack depth limit reached")
238
239
    # take snapshot of state before processing the message
240
    begin_transaction(state, transient_storage)
241
242
    if message.should_transfer_value and message.value != 0:
243
        move_ether(
244
            state, message.caller, message.current_target, message.value
245
        )
246
247
    evm = execute_code(message)
248
    if evm.error:
249
        # revert state to the last saved checkpoint
250
        # since the message call resulted in an error
251
        rollback_transaction(state, transient_storage)
252
    else:
253
        commit_transaction(state, transient_storage)
254
    return evm

execute_code

Executes bytecode present in the message.

Parameters

message : Transaction specific items.

Returns

evm: ethereum.vm.EVM Items containing execution specific objects

def execute_code(message: Message) -> Evm:
258
    """
259
    Executes bytecode present in the `message`.
260
261
    Parameters
262
    ----------
263
    message :
264
        Transaction specific items.
265
266
    Returns
267
    -------
268
    evm: `ethereum.vm.EVM`
269
        Items containing execution specific objects
270
    """
271
    code = message.code
272
    valid_jump_destinations = get_valid_jump_destinations(code)
273
274
    evm = Evm(
275
        pc=Uint(0),
276
        stack=[],
277
        memory=bytearray(),
278
        code=code,
279
        gas_left=message.gas,
280
        valid_jump_destinations=valid_jump_destinations,
281
        logs=(),
282
        refund_counter=0,
283
        running=True,
284
        message=message,
285
        output=b"",
286
        accounts_to_delete=set(),
287
        return_data=b"",
288
        error=None,
289
        accessed_addresses=message.accessed_addresses,
290
        accessed_storage_keys=message.accessed_storage_keys,
291
    )
292
    try:
293
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
294
            if message.disable_precompiles:
295
                return evm
296
            evm_trace(evm, PrecompileStart(evm.message.code_address))
297
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
298
            evm_trace(evm, PrecompileEnd())
299
            return evm
300
301
        while evm.running and evm.pc < ulen(evm.code):
302
            try:
303
                op = Ops(evm.code[evm.pc])
304
            except ValueError:
305
                raise InvalidOpcode(evm.code[evm.pc])
306
307
            evm_trace(evm, OpStart(op))
308
            op_implementation[op](evm)
309
            evm_trace(evm, OpEnd())
310
311
        evm_trace(evm, EvmStop(Ops.STOP))
312
313
    except ExceptionalHalt as error:
314
        evm_trace(evm, OpException(error))
315
        evm.gas_left = Uint(0)
316
        evm.output = b""
317
        evm.error = error
318
    except Revert as error:
319
        evm_trace(evm, OpException(error))
320
        evm.error = error
321
    return evm