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

63
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

64
MAX_CODE_SIZE = 0x6000

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
198
    """
199
    Move ether and execute the relevant code.
200
201
    Parameters
202
    ----------
203
    message :
204
        Transaction specific items.
205
206
    Returns
207
    -------
208
    evm: :py:class:`~ethereum.paris.vm.Evm`
208
    evm: :py:class:`~ethereum.shanghai.vm.Evm`
209
        Items containing execution specific objects
210
    """
211
    state = message.block_env.state
212
    if message.depth > STACK_DEPTH_LIMIT:
213
        raise StackDepthLimitError("Stack depth limit reached")
214
215
    # take snapshot of state before processing the message
216
    begin_transaction(state)
217
218
    if message.should_transfer_value and message.value != 0:
219
        move_ether(
220
            state, message.caller, message.current_target, message.value
221
        )
222
223
    evm = execute_code(message)
224
    if evm.error:
225
        # revert state to the last saved checkpoint
226
        # since the message call resulted in an error
227
        rollback_transaction(state)
228
    else:
229
        commit_transaction(state)
230
    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:
234
    """
235
    Executes bytecode present in the `message`.
236
237
    Parameters
238
    ----------
239
    message :
240
        Transaction specific items.
241
242
    Returns
243
    -------
244
    evm: `ethereum.vm.EVM`
245
        Items containing execution specific objects
246
    """
247
    code = message.code
248
    valid_jump_destinations = get_valid_jump_destinations(code)
249
250
    evm = Evm(
251
        pc=Uint(0),
252
        stack=[],
253
        memory=bytearray(),
254
        code=code,
255
        gas_left=message.gas,
256
        valid_jump_destinations=valid_jump_destinations,
257
        logs=(),
258
        refund_counter=0,
259
        running=True,
260
        message=message,
261
        output=b"",
262
        accounts_to_delete=set(),
263
        return_data=b"",
264
        error=None,
265
        accessed_addresses=message.accessed_addresses,
266
        accessed_storage_keys=message.accessed_storage_keys,
267
    )
268
    try:
269
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
270
            evm_trace(evm, PrecompileStart(evm.message.code_address))
271
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
272
            evm_trace(evm, PrecompileEnd())
273
            return evm
274
275
        while evm.running and evm.pc < ulen(evm.code):
276
            try:
277
                op = Ops(evm.code[evm.pc])
278
            except ValueError:
279
                raise InvalidOpcode(evm.code[evm.pc])
280
281
            evm_trace(evm, OpStart(op))
282
            op_implementation[op](evm)
283
            evm_trace(evm, OpEnd())
284
285
        evm_trace(evm, EvmStop(Ops.STOP))
286
287
    except ExceptionalHalt as error:
288
        evm_trace(evm, OpException(error))
289
        evm.gas_left = Uint(0)
290
        evm.output = b""
291
        evm.error = error
292
    except Revert as error:
293
        evm_trace(evm, OpException(error))
294
        evm.error = error
295
    return evm