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
@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~ethereum.forks.cancun.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`
155
    evm: :py:class:`~ethereum.forks.cancun.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.
174
    # accounts. This tracking is also needed to respect the constraints
175
    # added to SELFDESTRUCT by EIP-6780.
176
    mark_account_created(tx_state, message.current_target)
177
178
    increment_nonce(tx_state, message.current_target)
179
    evm = process_message(message)
180
    if not evm.error:
181
        contract_code = evm.output
182
        contract_code_gas = (
183
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
184
        )
185
        try:
186
            if len(contract_code) > 0:
187
                if contract_code[0] == 0xEF:
188
                    raise InvalidContractPrefix
189
            charge_gas(evm, contract_code_gas)
190
            if len(contract_code) > MAX_CODE_SIZE:
191
                raise OutOfGasError
192
        except ExceptionalHalt as error:
193
            restore_tx_state(tx_state, snapshot)
194
            evm.gas_left = Uint(0)
195
            evm.output = b""
196
            evm.error = error
197
        else:
198
            set_code(tx_state, message.current_target, contract_code)
199
    else:
200
        restore_tx_state(tx_state, snapshot)
201
    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:
205
    """
206
    Move ether and execute the relevant code.
207
208
    Parameters
209
    ----------
210
    message :
211
        Transaction specific items.
212
213
    Returns
214
    -------
214
    evm: :py:class:`~ethereum.forks.shanghai.vm.Evm`
215
    evm: :py:class:`~ethereum.forks.cancun.vm.Evm`
216
        Items containing execution specific objects
217
218
    """
219
    tx_state = message.tx_env.state
220
    if message.depth > STACK_DEPTH_LIMIT:
221
        raise StackDepthLimitError("Stack depth limit reached")
222
223
    code = message.code
224
    valid_jump_destinations = get_valid_jump_destinations(code)
225
    evm = Evm(
226
        pc=Uint(0),
227
        stack=[],
228
        memory=bytearray(),
229
        code=code,
230
        gas_left=message.gas,
231
        valid_jump_destinations=valid_jump_destinations,
232
        logs=(),
233
        refund_counter=0,
234
        running=True,
235
        message=message,
236
        output=b"",
237
        accounts_to_delete=set(),
238
        return_data=b"",
239
        error=None,
240
        accessed_addresses=message.accessed_addresses,
241
        accessed_storage_keys=message.accessed_storage_keys,
242
    )
243
244
    # take snapshot of state before processing the message
245
    snapshot = copy_tx_state(tx_state)
246
247
    if message.should_transfer_value and message.value != 0:
248
        move_ether(
249
            tx_state,
250
            message.caller,
251
            message.current_target,
252
            message.value,
253
        )
254
255
    try:
256
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
257
            evm_trace(evm, PrecompileStart(evm.message.code_address))
258
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
259
            evm_trace(evm, PrecompileEnd())
260
        else:
261
            while evm.running and evm.pc < ulen(evm.code):
262
                try:
263
                    op = Ops(evm.code[evm.pc])
264
                except ValueError as e:
265
                    raise InvalidOpcode(evm.code[evm.pc]) from e
266
267
                evm_trace(evm, OpStart(op))
268
                op_implementation[op](evm)
269
                evm_trace(evm, OpEnd())
270
271
            evm_trace(evm, EvmStop(Ops.STOP))
272
273
    except ExceptionalHalt as error:
274
        evm_trace(evm, OpException(error))
275
        evm.gas_left = Uint(0)
276
        evm.output = b""
277
        evm.error = error
278
    except Revert as error:
279
        evm_trace(evm, OpException(error))
280
        evm.error = error
281
282
    if evm.error:
283
        restore_tx_state(tx_state, snapshot)
284
    return evm