ethereum.forks.bpo5.vm.interpreterethereum.forks.amsterdam.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

70
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

65
MAX_CODE_SIZE = 0x6000
71
MAX_CODE_SIZE = 0x8000

MAX_INIT_CODE_SIZE

72
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.
  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.
  7. `regular_gas_used`: Regular gas used during execution.
  8. `state_gas_used`: State gas used during execution.
75
@dataclass
class MessageCallOutput:

gas_left

92
    gas_left: Uint

state_gas_left

93
    state_gas_left: Uint

refund_counter

94
    refund_counter: U256

logs

95
    logs: Tuple[Log, ...]

accounts_to_delete

96
    accounts_to_delete: Set[Address]

error

97
    error: Optional[EthereumException]

return_data

98
    return_data: Bytes

regular_gas_used

99
    regular_gas_used: Uint

state_gas_used

100
    state_gas_used: Uint

process_message_call

If message.target 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:
104
    """
105
    If `message.target` is empty then it creates a smart contract
106
    else it executes a call from the `message.caller` to the `message.target`.
107
108
    Parameters
109
    ----------
110
    message :
111
        Transaction specific items.
112
113
    Returns
114
    -------
115
    output : `MessageCallOutput`
116
        Output of the message call
117
118
    """
108
    block_env = message.block_env
119
    tx_state = message.tx_env.state
120
    refund_counter = U256(0)
121
    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)
122
        is_collision = account_has_code_or_nonce(
123
            tx_state, message.current_target
124
        ) or account_has_storage(tx_state, message.current_target)
125
        if is_collision:
126
            return MessageCallOutput(
127
                gas_left=Uint(0),
128
                state_gas_left=Uint(0),
129
                refund_counter=U256(0),
130
                logs=tuple(),
131
                accounts_to_delete=set(),
132
                error=AddressCollision(),
133
                return_data=Bytes(b""),
134
                regular_gas_used=Uint(0),
135
                state_gas_used=Uint(0),
136
            )
137
        else:
138
            evm = process_create_message(message)
139
    else:
140
        if message.tx_env.authorizations != ():
127
            refund_counter += set_delegation(message)
141
            set_delegation(message)
142
143
        delegated_address = get_delegated_code_address(message.code)
144
        if delegated_address is not None:
145
            message.disable_precompiles = True
146
            message.accessed_addresses.add(delegated_address)
133
            message.code = get_code(
134
                block_env.state,
135
                get_account(block_env.state, delegated_address).code_hash,
147
            message.code = get_code(
148
                tx_state,
149
                get_account(tx_state, delegated_address).code_hash,
150
            )
151
            message.code_address = delegated_address
152
153
        evm = process_message(message)
154
155
    if evm.error:
156
        logs: Tuple[Log, ...] = ()
157
        accounts_to_delete = set()
158
    else:
159
        logs = evm.logs
160
        accounts_to_delete = evm.accounts_to_delete
161
        refund_counter += U256(evm.refund_counter)
162
163
    tx_end = TransactionEnd(
164
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
165
    )
166
    evm_trace(evm, tx_end)
167
168
    return MessageCallOutput(
169
        gas_left=evm.gas_left,
170
        state_gas_left=evm.state_gas_left,
171
        refund_counter=refund_counter,
172
        logs=logs,
173
        accounts_to_delete=accounts_to_delete,
174
        error=evm.error,
175
        return_data=evm.output,
176
        regular_gas_used=evm.regular_gas_used,
177
        state_gas_used=evm.state_gas_used,
178
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
182
    """
183
    Executes a call to create a smart contract.
184
185
    Parameters
186
    ----------
187
    message :
188
        Transaction specific items.
189
190
    Returns
191
    -------
175
    evm: :py:class:`~ethereum.forks.bpo5.vm.Evm`
192
    evm: :py:class:`~ethereum.forks.amsterdam.vm.Evm`
193
        Items containing execution specific objects.
194
195
    """
179
    state = message.block_env.state
180
    transient_storage = message.tx_env.transient_storage
196
    tx_state = message.tx_env.state
197
    # take snapshot of state before processing the message
182
    begin_transaction(state, transient_storage)
198
    snapshot = copy_tx_state(tx_state)
199
184
    # The list of created accounts is used by `get_storage_original`.
185
    # Additionally, the list is needed to respect the constraints
200
    # If the address where the account is being created has storage, it is
201
    # destroyed. This can only happen in the following highly unlikely
202
    # circumstances:
203
    # * The address created by a `CREATE` call collides with a subsequent
204
    #   `CREATE` or `CREATE2` call.
205
    # * The first `CREATE` happened before Spurious Dragon and left empty
206
    #   code.
207
    destroy_storage(tx_state, message.current_target)
208
209
    # In the previously mentioned edge case the preexisting storage is ignored
210
    # for gas refund purposes. In order to do this we must track created
211
    # accounts. This tracking is also needed to respect the constraints
212
    # added to SELFDESTRUCT by EIP-6780.
187
    mark_account_created(state, message.current_target)
213
    mark_account_created(tx_state, message.current_target)
214
189
    increment_nonce(state, message.current_target)
215
    increment_nonce(tx_state, message.current_target)
216
217
    evm = process_message(message)
218
    if not evm.error:
219
        contract_code = evm.output
193
        contract_code_gas = (
194
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
195
        )
220
        try:
221
            if len(contract_code) > 0:
222
                if contract_code[0] == 0xEF:
223
                    raise InvalidContractPrefix
200
            charge_gas(evm, contract_code_gas)
224
            if len(contract_code) > MAX_CODE_SIZE:
202
                raise OutOfGasError
225
                raise OutOfGasError
226
            # Hash cost for computing keccak256 of deployed bytecode
227
            code_hash_gas = (
228
                GasCosts.OPCODE_KECCACK256_PER_WORD
229
                * ceil32(ulen(contract_code))
230
                // Uint(32)
231
            )
232
            charge_gas(evm, code_hash_gas)
233
            code_deposit_state_gas = ulen(contract_code) * COST_PER_STATE_BYTE
234
            charge_state_gas(evm, code_deposit_state_gas)
235
        except ExceptionalHalt as error:
204
            rollback_transaction(state, transient_storage)
236
            restore_tx_state(tx_state, snapshot)
237
            evm.regular_gas_used += evm.gas_left
238
            evm.gas_left = Uint(0)
239
            # On halt, restore the state gas reservoir to what was
240
            # passed into this frame. State-gas charges in excess of
241
            # the original reservoir came from gas_left (spill) or
242
            # from a child revert refund; either way they get
243
            # re-classified as regular gas usage on halt.
244
            total_state = evm.state_gas_used + evm.state_gas_left
245
            reservoir = evm.message.state_gas_reservoir
246
            if total_state > reservoir:
247
                evm.regular_gas_used += total_state - reservoir
248
            evm.state_gas_left = reservoir
249
            evm.state_gas_used = Uint(0)
250
            evm.state_gas_refund = Uint(0)
251
            evm.state_gas_refund_pending = Uint(0)
252
            evm.output = b""
253
            evm.error = error
254
        else:
209
            set_code(state, message.current_target, contract_code)
210
            commit_transaction(state, transient_storage)
255
            set_code(tx_state, message.current_target, contract_code)
256
    else:
212
        rollback_transaction(state, transient_storage)
257
        restore_tx_state(tx_state, snapshot)
258
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm Items containing execution specific objects

def process_message(message: Message) -> Evm:
262
    """
263
    Move ether and execute the relevant code.
264
265
    Parameters
266
    ----------
267
    message :
268
        Transaction specific items.
269
270
    Returns
271
    -------
227
    evm: :py:class:`~ethereum.forks.bpo5.vm.Evm`
272
    evm: :py:class:`~ethereum.forks.amsterdam.vm.Evm`
273
        Items containing execution specific objects
274
275
    """
231
    state = message.block_env.state
276
    tx_state = message.tx_env.state
277
    if message.depth > STACK_DEPTH_LIMIT:
278
        raise StackDepthLimitError("Stack depth limit reached")
279
235
    transient_storage = message.tx_env.transient_storage
280
    code = message.code
281
    valid_jump_destinations = get_valid_jump_destinations(code)
282
283
    evm = Evm(
284
        pc=Uint(0),
285
        stack=[],
286
        memory=bytearray(),
287
        code=code,
288
        gas_left=message.gas,
289
        state_gas_left=message.state_gas_reservoir,
290
        valid_jump_destinations=valid_jump_destinations,
291
        logs=(),
292
        refund_counter=0,
293
        running=True,
294
        message=message,
295
        output=b"",
296
        accounts_to_delete=set(),
297
        return_data=b"",
298
        error=None,
299
        accessed_addresses=message.accessed_addresses,
300
        accessed_storage_keys=message.accessed_storage_keys,
301
    )
302
303
    # take snapshot of state before processing the message
258
    begin_transaction(state, transient_storage)
304
    snapshot = copy_tx_state(tx_state)
305
306
    if message.should_transfer_value and message.value != 0:
261
        move_ether(
262
            state, message.caller, message.current_target, message.value
263
        )
307
        move_ether(
308
            tx_state,
309
            message.caller,
310
            message.current_target,
311
            message.value,
312
        )
313
        # EIP-7708: Only emit transfer log to a different account
314
        if message.caller != message.current_target:
315
            emit_transfer_log(
316
                evm, message.caller, message.current_target, message.value
317
            )
318
265
    try:
319
    # Execute message code and handle errors
320
    try:
321
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
322
            if not message.disable_precompiles:
323
                evm_trace(evm, PrecompileStart(evm.message.code_address))
324
                PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
325
                evm_trace(evm, PrecompileEnd())
326
        else:
327
            while evm.running and evm.pc < ulen(evm.code):
328
                try:
329
                    op = Ops(evm.code[evm.pc])
330
                except ValueError as e:
331
                    raise InvalidOpcode(evm.code[evm.pc]) from e
332
333
                evm_trace(evm, OpStart(op))
334
                op_implementation[op](evm)
335
                evm_trace(evm, OpEnd())
336
337
            evm_trace(evm, EvmStop(Ops.STOP))
338
339
    except ExceptionalHalt as error:
340
        evm_trace(evm, OpException(error))
341
        evm.regular_gas_used += evm.gas_left
342
        evm.gas_left = Uint(0)
343
        # On halt, restore the state gas reservoir to what was passed
344
        # into this frame. State-gas charges in excess of the original
345
        # reservoir came from gas_left (spill) or from a child revert
346
        # refund; either way they get re-classified as regular gas
347
        # usage on halt.
348
        total_state = evm.state_gas_used + evm.state_gas_left
349
        reservoir = evm.message.state_gas_reservoir
350
        if total_state > reservoir:
351
            evm.regular_gas_used += total_state - reservoir
352
        evm.state_gas_left = reservoir
353
        evm.state_gas_used = Uint(0)
354
        evm.state_gas_refund = Uint(0)
355
        evm.state_gas_refund_pending = Uint(0)
356
        evm.output = b""
357
        evm.error = error
358
    except Revert as error:
359
        evm_trace(evm, OpException(error))
360
        evm.error = error
361
362
    if evm.error:
294
        # revert state to the last saved checkpoint
295
        # since the message call resulted in an error
296
        rollback_transaction(state, transient_storage)
297
    else:
298
        commit_transaction(state, transient_storage)
363
        restore_tx_state(tx_state, snapshot)
364
    return evm