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

60
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

61
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

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

gas_left

80
    gas_left: Uint

refund_counter

81
    refund_counter: U256

logs

82
    logs: Tuple[Log, ...]

accounts_to_delete

83
    accounts_to_delete: Set[Address]

error

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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