ethereum.forks.shanghai.vm.interpreterethereum.forks.cancun.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

61
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

62
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

63
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.
66
@final
67
@dataclass
class MessageCallOutput:

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Tuple[Log, ...]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

error

85
    error: Optional[EthereumException]

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:
89
    <snip>
104
    tx_state = message.tx_env.state
105
    refund_counter = U256(0)
106
    if message.target == Bytes0(b""):
107
        is_collision = account_has_code_or_nonce(
108
            tx_state, message.current_target
109
        ) or account_has_storage(tx_state, message.current_target)
110
        if is_collision:
111
            return MessageCallOutput(
112
                gas_left=Uint(0),
113
                refund_counter=U256(0),
114
                logs=tuple(),
115
                accounts_to_delete=set(),
116
                error=AddressCollision(),
117
            )
118
        else:
119
            evm = process_create_message(message)
120
    else:
121
        evm = process_message(message)
122
123
    if evm.error:
124
        logs: Tuple[Log, ...] = ()
125
        accounts_to_delete = set()
126
    else:
127
        logs = evm.logs
128
        accounts_to_delete = evm.accounts_to_delete
129
        refund_counter += U256(evm.refund_counter)
130
131
    tx_end = TransactionEnd(
132
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
133
    )
134
    evm_trace(evm, tx_end)
135
136
    return MessageCallOutput(
137
        gas_left=evm.gas_left,
138
        refund_counter=refund_counter,
139
        logs=logs,
140
        accounts_to_delete=accounts_to_delete,
141
        error=evm.error,
142
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.shanghai.vm.Evm~ethereum.forks.cancun.vm.Evm Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
146
    <snip>
160
    tx_state = message.tx_env.state
161
    # take snapshot of state before processing the message
162
    snapshot = copy_tx_state(tx_state)
163
164
    # If the address where the account is being created has storage, it is
165
    # destroyed. This can only happen in the following highly unlikely
166
    # circumstances:
167
    # * The address created by a `CREATE` call collides with a subsequent
168
    #   `CREATE` or `CREATE2` call.
169
    # * The first `CREATE` happened before Spurious Dragon and left empty
170
    #   code.
171
    destroy_storage(tx_state, message.current_target)
172
173
    # In the previously mentioned edge case the preexisting storage is ignored
174
    # for gas refund purposes. In order to do this we must track created
175
    # accounts.
175
    # accounts. This tracking is also needed to respect the constraints
176
    # added to SELFDESTRUCT by EIP-6780.
177
    mark_account_created(tx_state, message.current_target)
178
179
    increment_nonce(tx_state, message.current_target)
180
    evm = process_message(message)
181
    if not evm.error:
182
        contract_code = evm.output
183
        contract_code_gas = (
184
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
185
        )
186
        try:
187
            if len(contract_code) > 0:
188
                if contract_code[0] == 0xEF:
189
                    raise InvalidContractPrefix
190
            charge_gas(evm, contract_code_gas)
191
            if len(contract_code) > MAX_CODE_SIZE:
192
                raise OutOfGasError
193
        except ExceptionalHalt as error:
194
            restore_tx_state(tx_state, snapshot)
195
            evm.gas_left = Uint(0)
196
            evm.output = b""
197
            evm.error = error
198
        else:
199
            set_code(tx_state, message.current_target, contract_code)
200
    else:
201
        restore_tx_state(tx_state, snapshot)
202
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.shanghai.vm.Evm~ethereum.forks.cancun.vm.Evm Items containing execution specific objects

def process_message(message: Message) -> Evm:
206
    <snip>
220
    tx_state = message.tx_env.state
221
    if message.depth > STACK_DEPTH_LIMIT:
222
        raise StackDepthLimitError("Stack depth limit reached")
223
224
    code = message.code
225
    valid_jump_destinations = get_valid_jump_destinations(code)
226
    evm = Evm(
227
        pc=Uint(0),
228
        stack=[],
229
        memory=bytearray(),
230
        code=code,
231
        gas_left=message.gas,
232
        valid_jump_destinations=valid_jump_destinations,
233
        logs=(),
234
        refund_counter=0,
235
        running=True,
236
        message=message,
237
        output=b"",
238
        accounts_to_delete=set(),
239
        return_data=b"",
240
        error=None,
241
        accessed_addresses=message.accessed_addresses,
242
        accessed_storage_keys=message.accessed_storage_keys,
243
    )
244
245
    # take snapshot of state before processing the message
246
    snapshot = copy_tx_state(tx_state)
247
248
    if message.should_transfer_value and message.value != 0:
249
        move_ether(
250
            tx_state,
251
            message.caller,
252
            message.current_target,
253
            message.value,
254
        )
255
256
    try:
257
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
258
            evm_trace(evm, PrecompileStart(evm.message.code_address))
259
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
260
            evm_trace(evm, PrecompileEnd())
261
        else:
262
            while evm.running and evm.pc < ulen(evm.code):
263
                try:
264
                    op = Ops(evm.code[evm.pc])
265
                except ValueError as e:
266
                    raise InvalidOpcode(evm.code[evm.pc]) from e
267
268
                evm_trace(evm, OpStart(op))
269
                op_implementation[op](evm)
270
                evm_trace(evm, OpEnd())
271
272
            evm_trace(evm, EvmStop(Ops.STOP))
273
274
    except ExceptionalHalt as error:
275
        evm_trace(evm, OpException(error))
276
        evm.gas_left = Uint(0)
277
        evm.output = b""
278
        evm.error = error
279
    except Revert as error:
280
        evm_trace(evm, OpException(error))
281
        evm.error = error
282
283
    if evm.error:
284
        restore_tx_state(tx_state, snapshot)
285
    return evm