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

65
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

66
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

67
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.
  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.
70
@dataclass
class MessageCallOutput:

gas_left

85
    gas_left: Uint

refund_counter

86
    refund_counter: U256

logs

87
    logs: Tuple[Log, ...]

accounts_to_delete

88
    accounts_to_delete: Set[Address]

error

89
    error: Optional[EthereumException]

return_data

90
    return_data: Bytes

process_message_call

If message.current 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:
94
    """
95
    If `message.current` is empty then it creates a smart contract
96
    else it executes a call from the `message.caller` to the `message.target`.
97
98
    Parameters
99
    ----------
100
    message :
101
        Transaction specific items.
102
103
    Returns
104
    -------
105
    output : `MessageCallOutput`
106
        Output of the message call
107
    """
108
    block_env = message.block_env
109
    refund_counter = U256(0)
110
    if message.target == Bytes0(b""):
111
        is_collision = account_has_code_or_nonce(
112
            block_env.state, message.current_target
113
        ) or account_has_storage(block_env.state, message.current_target)
114
        if is_collision:
115
            return MessageCallOutput(
112
                Uint(0), U256(0), tuple(), set(), AddressCollision()
116
                Uint(0),
117
                U256(0),
118
                tuple(),
119
                set(),
120
                AddressCollision(),
121
                Bytes(b""),
122
            )
123
        else:
124
            evm = process_create_message(message)
125
    else:
117
        evm = process_message(message)
126
        if message.tx_env.authorizations != ():
127
            refund_counter += set_delegation(message)
128
129
        delegated_address = get_delegated_code_address(message.code)
130
        if delegated_address is not None:
131
            message.disable_precompiles = True
132
            message.accessed_addresses.add(delegated_address)
133
            message.code = get_account(block_env.state, delegated_address).code
134
            message.code_address = delegated_address
135
136
        evm = process_message(message)
137
138
    if evm.error:
139
        logs: Tuple[Log, ...] = ()
140
        accounts_to_delete = set()
141
    else:
142
        logs = evm.logs
143
        accounts_to_delete = evm.accounts_to_delete
144
        refund_counter += U256(evm.refund_counter)
145
146
    tx_end = TransactionEnd(
147
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
148
    )
149
    evm_trace(evm, tx_end)
150
151
    return MessageCallOutput(
152
        gas_left=evm.gas_left,
153
        refund_counter=refund_counter,
154
        logs=logs,
155
        accounts_to_delete=accounts_to_delete,
156
        error=evm.error,
157
        return_data=evm.output,
158
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

def process_create_message(message: Message) -> Evm:
162
    """
163
    Executes a call to create a smart contract.
164
165
    Parameters
166
    ----------
167
    message :
168
        Transaction specific items.
169
170
    Returns
171
    -------
152
    evm: :py:class:`~ethereum.cancun.vm.Evm`
172
    evm: :py:class:`~ethereum.prague.vm.Evm`
173
        Items containing execution specific objects.
174
    """
175
    state = message.block_env.state
176
    transient_storage = message.tx_env.transient_storage
177
    # take snapshot of state before processing the message
178
    begin_transaction(state, transient_storage)
179
180
    # If the address where the account is being created has storage, it is
181
    # destroyed. This can only happen in the following highly unlikely
182
    # circumstances:
183
    # * The address created by a `CREATE` call collides with a subsequent
184
    #   `CREATE` or `CREATE2` call.
185
    # * The first `CREATE` happened before Spurious Dragon and left empty
186
    #   code.
187
    destroy_storage(state, message.current_target)
188
189
    # In the previously mentioned edge case the preexisting storage is ignored
190
    # for gas refund purposes. In order to do this we must track created
191
    # accounts.
192
    mark_account_created(state, message.current_target)
193
194
    increment_nonce(state, message.current_target)
195
    evm = process_message(message)
196
    if not evm.error:
197
        contract_code = evm.output
198
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
199
        try:
200
            if len(contract_code) > 0:
201
                if contract_code[0] == 0xEF:
202
                    raise InvalidContractPrefix
203
            charge_gas(evm, contract_code_gas)
204
            if len(contract_code) > MAX_CODE_SIZE:
205
                raise OutOfGasError
206
        except ExceptionalHalt as error:
207
            rollback_transaction(state, transient_storage)
208
            evm.gas_left = Uint(0)
209
            evm.output = b""
210
            evm.error = error
211
        else:
212
            set_code(state, message.current_target, contract_code)
213
            commit_transaction(state, transient_storage)
214
    else:
215
        rollback_transaction(state, transient_storage)
216
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
220
    """
221
    Move ether and execute the relevant code.
222
223
    Parameters
224
    ----------
225
    message :
226
        Transaction specific items.
227
228
    Returns
229
    -------
210
    evm: :py:class:`~ethereum.cancun.vm.Evm`
230
    evm: :py:class:`~ethereum.prague.vm.Evm`
231
        Items containing execution specific objects
232
    """
233
    state = message.block_env.state
234
    transient_storage = message.tx_env.transient_storage
235
    if message.depth > STACK_DEPTH_LIMIT:
236
        raise StackDepthLimitError("Stack depth limit reached")
237
238
    # take snapshot of state before processing the message
239
    begin_transaction(state, transient_storage)
240
241
    if message.should_transfer_value and message.value != 0:
242
        move_ether(
243
            state, message.caller, message.current_target, message.value
244
        )
245
246
    evm = execute_code(message)
247
    if evm.error:
248
        # revert state to the last saved checkpoint
249
        # since the message call resulted in an error
250
        rollback_transaction(state, transient_storage)
251
    else:
252
        commit_transaction(state, transient_storage)
253
    return evm

execute_code

Executes bytecode present in the message.

Parameters

message : Transaction specific items.

Returns

evm: ethereum.vm.EVM Items containing execution specific objects

def execute_code(message: Message) -> Evm:
257
    """
258
    Executes bytecode present in the `message`.
259
260
    Parameters
261
    ----------
262
    message :
263
        Transaction specific items.
264
265
    Returns
266
    -------
267
    evm: `ethereum.vm.EVM`
268
        Items containing execution specific objects
269
    """
270
    code = message.code
271
    valid_jump_destinations = get_valid_jump_destinations(code)
272
273
    evm = Evm(
274
        pc=Uint(0),
275
        stack=[],
276
        memory=bytearray(),
277
        code=code,
278
        gas_left=message.gas,
279
        valid_jump_destinations=valid_jump_destinations,
280
        logs=(),
281
        refund_counter=0,
282
        running=True,
283
        message=message,
284
        output=b"",
285
        accounts_to_delete=set(),
286
        return_data=b"",
287
        error=None,
288
        accessed_addresses=message.accessed_addresses,
289
        accessed_storage_keys=message.accessed_storage_keys,
290
    )
291
    try:
292
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
273
            evm_trace(evm, PrecompileStart(evm.message.code_address))
293
            if message.disable_precompiles:
294
                return evm
295
            evm_trace(evm, PrecompileStart(evm.message.code_address))
296
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
297
            evm_trace(evm, PrecompileEnd())
298
            return evm
299
300
        while evm.running and evm.pc < ulen(evm.code):
301
            try:
302
                op = Ops(evm.code[evm.pc])
303
            except ValueError:
304
                raise InvalidOpcode(evm.code[evm.pc])
305
306
            evm_trace(evm, OpStart(op))
307
            op_implementation[op](evm)
308
            evm_trace(evm, OpEnd())
309
310
        evm_trace(evm, EvmStop(Ops.STOP))
311
312
    except ExceptionalHalt as error:
313
        evm_trace(evm, OpException(error))
314
        evm.gas_left = Uint(0)
315
        evm.output = b""
316
        evm.error = error
317
    except Revert as error:
318
        evm_trace(evm, OpException(error))
319
        evm.error = error
320
    return evm