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.
  9. `state_refund`: State gas refunded by `set_delegation` for
     authorities that already existed in state. Subtracted from
     `tx_state_gas` in block accounting so `block.gas_used`
     matches the receipt `cumulative_gas_used`.
75
@final
76
@dataclass
class MessageCallOutput:

gas_left

97
    gas_left: Uint

refund_counter

98
    refund_counter: U256

logs

99
    logs: Tuple[Log, ...]

accounts_to_delete

100
    accounts_to_delete: Set[Address]

error

101
    error: Optional[EthereumException]

return_data

102
    return_data: Bytes

state_gas_left

103
    state_gas_left: Uint

regular_gas_used

104
    regular_gas_used: Uint

state_gas_used

105
    state_gas_used: int

state_refund

106
    state_refund: 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:
110
    <snip>
125
    tx_state = message.tx_env.state
126
    refund_counter = U256(0)
127
    state_refund = Uint(0)
128
    if message.target == Bytes0(b""):
129
        is_collision = account_has_code_or_nonce(
130
            tx_state, message.current_target
131
        ) or account_has_storage(tx_state, message.current_target)
132
        if is_collision:
133
            return MessageCallOutput(
134
                gas_left=Uint(0),
135
                refund_counter=U256(0),
136
                logs=tuple(),
137
                accounts_to_delete=set(),
138
                error=AddressCollision(),
139
                return_data=Bytes(b""),
140
                state_gas_left=message.state_gas_reservoir,
141
                regular_gas_used=message.gas,
142
                state_gas_used=0,
143
                state_refund=Uint(0),
144
            )
145
        else:
146
            evm = process_create_message(message)
147
    else:
148
        if message.tx_env.authorizations != ():
128
            refund_counter += set_delegation(message)
149
            state_refund += set_delegation(message)
150
151
        delegated_address = get_delegated_code_address(message.code)
152
        if delegated_address is not None:
153
            message.disable_precompiles = True
154
            message.accessed_addresses.add(delegated_address)
155
            message.code = get_code(
156
                tx_state,
157
                get_account(tx_state, delegated_address).code_hash,
158
            )
159
            message.code_address = delegated_address
160
161
        evm = process_message(message)
162
163
    if evm.error:
164
        logs: Tuple[Log, ...] = ()
165
        accounts_to_delete = set()
166
    else:
167
        logs = evm.logs
168
        accounts_to_delete = evm.accounts_to_delete
169
        refund_counter += U256(evm.refund_counter)
170
171
    tx_end = TransactionEnd(
172
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
173
    )
174
    evm_trace(evm, tx_end)
175
176
    return MessageCallOutput(
177
        gas_left=evm.gas_left,
178
        refund_counter=refund_counter,
179
        logs=logs,
180
        accounts_to_delete=accounts_to_delete,
181
        error=evm.error,
182
        return_data=evm.output,
183
        state_gas_left=evm.state_gas_left,
184
        regular_gas_used=evm.regular_gas_used,
185
        state_gas_used=evm.state_gas_used,
186
        state_refund=state_refund,
187
    )

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:
191
    <snip>
205
    tx_state = message.tx_env.state
206
    # take snapshot of state before processing the message
207
    snapshot = copy_tx_state(tx_state)
208
209
    # If the address where the account is being created has storage, it is
210
    # destroyed. This can only happen in the following highly unlikely
211
    # circumstances:
212
    # * The address created by a `CREATE` call collides with a subsequent
213
    #   `CREATE` or `CREATE2` call.
214
    # * The first `CREATE` happened before Spurious Dragon and left empty
215
    #   code.
216
    destroy_storage(tx_state, message.current_target)
217
218
    # In the previously mentioned edge case the preexisting storage is ignored
219
    # for gas refund purposes. In order to do this we must track created
220
    # accounts. This tracking is also needed to respect the constraints
221
    # added to SELFDESTRUCT by EIP-6780.
222
    mark_account_created(tx_state, message.current_target)
223
224
    increment_nonce(tx_state, message.current_target)
225
226
    evm = process_message(message)
227
    if not evm.error:
228
        contract_code = evm.output
203
        contract_code_gas = (
204
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
205
        )
229
        try:
230
            if len(contract_code) > 0:
231
                if contract_code[0] == 0xEF:
232
                    raise InvalidContractPrefix
210
            charge_gas(evm, contract_code_gas)
233
            if len(contract_code) > MAX_CODE_SIZE:
212
                raise OutOfGasError
234
                raise OutOfGasError
235
            # Hash cost for computing keccak256 of deployed bytecode
236
            code_hash_gas = (
237
                GasCosts.OPCODE_KECCAK256_PER_WORD
238
                * ceil32(ulen(contract_code))
239
                // Uint(32)
240
            )
241
            charge_gas(evm, code_hash_gas)
242
            code_deposit_state_gas = (
243
                ulen(contract_code) * StateGasCosts.COST_PER_STATE_BYTE
244
            )
245
            charge_state_gas(evm, code_deposit_state_gas)
246
        except ExceptionalHalt as error:
247
            restore_tx_state(tx_state, snapshot)
248
            evm.regular_gas_used += evm.gas_left
249
            evm.gas_left = Uint(0)
250
            evm.output = b""
251
            evm.error = error
252
        else:
253
            set_code(tx_state, message.current_target, contract_code)
254
    else:
255
        restore_tx_state(tx_state, snapshot)
256
    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:
260
    <snip>
274
    tx_state = message.tx_env.state
275
    if message.depth > STACK_DEPTH_LIMIT:
276
        raise StackDepthLimitError("Stack depth limit reached")
277
278
    code = message.code
279
    valid_jump_destinations = get_valid_jump_destinations(code)
280
    evm = Evm(
281
        pc=Uint(0),
282
        stack=[],
283
        memory=bytearray(),
284
        code=code,
285
        gas_left=message.gas,
286
        state_gas_left=message.state_gas_reservoir,
287
        valid_jump_destinations=valid_jump_destinations,
288
        logs=(),
289
        refund_counter=0,
290
        running=True,
291
        message=message,
292
        output=b"",
293
        accounts_to_delete=set(),
294
        return_data=b"",
295
        error=None,
296
        accessed_addresses=message.accessed_addresses,
297
        accessed_storage_keys=message.accessed_storage_keys,
298
    )
299
265
    # take snapshot of state before processing the message
266
    snapshot = copy_tx_state(tx_state)
300
    snapshot = copy_tx_state(tx_state)
301
302
    if message.should_transfer_value and message.value != 0:
303
        move_ether(
304
            tx_state,
305
            message.caller,
306
            message.current_target,
307
            message.value,
274
        )
308
        )
309
        if message.caller != message.current_target:
310
            emit_transfer_log(
311
                evm, message.caller, message.current_target, message.value
312
            )
313
276
    try:
314
    # Execute message code and handle errors
315
    try:
316
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
317
            if not message.disable_precompiles:
318
                evm_trace(evm, PrecompileStart(evm.message.code_address))
319
                PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
320
                evm_trace(evm, PrecompileEnd())
321
        else:
322
            while evm.running and evm.pc < ulen(evm.code):
323
                try:
324
                    op = Ops(evm.code[evm.pc])
325
                except ValueError as e:
326
                    raise InvalidOpcode(evm.code[evm.pc]) from e
327
328
                evm_trace(evm, OpStart(op))
329
                op_implementation[op](evm)
330
                evm_trace(evm, OpEnd())
331
332
            evm_trace(evm, EvmStop(Ops.STOP))
333
334
    except ExceptionalHalt as error:
335
        evm_trace(evm, OpException(error))
336
        evm.regular_gas_used += evm.gas_left
337
        evm.gas_left = Uint(0)
338
        evm.output = b""
339
        evm.error = error
340
    except Revert as error:
341
        evm_trace(evm, OpException(error))
342
        evm.error = error
343
344
    if evm.error:
345
        restore_tx_state(tx_state, snapshot)
346
    return evm