ethereum.forks.shanghai.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
@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
    """
89
    If `message.target` is empty then it creates a smart contract
90
    else it executes a call from the `message.caller` to the `message.target`.
91
92
    Parameters
93
    ----------
94
    message :
95
        Transaction specific items.
96
97
    Returns
98
    -------
99
    output : `MessageCallOutput`
100
        Output of the message call
101
102
    """
103
    tx_state = message.tx_env.state
104
    refund_counter = U256(0)
105
    if message.target == Bytes0(b""):
106
        is_collision = account_has_code_or_nonce(
107
            tx_state, message.current_target
108
        ) or account_has_storage(tx_state, message.current_target)
109
        if is_collision:
110
            return MessageCallOutput(
111
                gas_left=Uint(0),
112
                refund_counter=U256(0),
113
                logs=tuple(),
114
                accounts_to_delete=set(),
115
                error=AddressCollision(),
116
            )
117
        else:
118
            evm = process_create_message(message)
119
    else:
120
        evm = process_message(message)
121
122
    if evm.error:
123
        logs: Tuple[Log, ...] = ()
124
        accounts_to_delete = set()
125
    else:
126
        logs = evm.logs
127
        accounts_to_delete = evm.accounts_to_delete
128
        refund_counter += U256(evm.refund_counter)
129
130
    tx_end = TransactionEnd(
131
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
132
    )
133
    evm_trace(evm, tx_end)
134
135
    return MessageCallOutput(
136
        gas_left=evm.gas_left,
137
        refund_counter=refund_counter,
138
        logs=logs,
139
        accounts_to_delete=accounts_to_delete,
140
        error=evm.error,
141
    )

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 Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
145
    """
146
    Executes a call to create a smart contract.
147
148
    Parameters
149
    ----------
150
    message :
151
        Transaction specific items.
152
153
    Returns
154
    -------
155
    evm: :py:class:`~ethereum.forks.shanghai.vm.Evm`
156
        Items containing execution specific objects.
157
158
    """
159
    tx_state = message.tx_env.state
160
    # take snapshot of state before processing the message
161
    snapshot = copy_tx_state(tx_state)
162
163
    # If the address where the account is being created has storage, it is
164
    # destroyed. This can only happen in the following highly unlikely
165
    # circumstances:
166
    # * The address created by a `CREATE` call collides with a subsequent
167
    #   `CREATE` or `CREATE2` call.
168
    # * The first `CREATE` happened before Spurious Dragon and left empty
169
    #   code.
170
    destroy_storage(tx_state, message.current_target)
171
172
    # In the previously mentioned edge case the preexisting storage is ignored
173
    # for gas refund purposes. In order to do this we must track created
174
    # accounts.
175
    mark_account_created(tx_state, message.current_target)
176
177
    increment_nonce(tx_state, message.current_target)
178
    evm = process_message(message)
179
    if not evm.error:
180
        contract_code = evm.output
181
        contract_code_gas = (
182
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
183
        )
184
        try:
185
            if len(contract_code) > 0:
186
                if contract_code[0] == 0xEF:
187
                    raise InvalidContractPrefix
188
            charge_gas(evm, contract_code_gas)
189
            if len(contract_code) > MAX_CODE_SIZE:
190
                raise OutOfGasError
191
        except ExceptionalHalt as error:
192
            restore_tx_state(tx_state, snapshot)
193
            evm.gas_left = Uint(0)
194
            evm.output = b""
195
            evm.error = error
196
        else:
197
            set_code(tx_state, message.current_target, contract_code)
198
    else:
199
        restore_tx_state(tx_state, snapshot)
200
    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 Items containing execution specific objects

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