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

62
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

63
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. `touched_accounts`: Accounts that have been touched.
  6. `error`: The error from the execution if any.
66
@dataclass
class MessageCallOutput:

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Union[Tuple[()], Tuple[Log, ...]]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

touched_accounts

85
    touched_accounts: Iterable[Address]

error

86
    error: Optional[Exception]

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.

env : External items required for EVM execution.

Returns

output : MessageCallOutput Output of the message call

def process_message_call(message: Message, ​​env: Environment) -> MessageCallOutput:
92
    """
93
    If `message.current` is empty then it creates a smart contract
94
    else it executes a call from the `message.caller` to the `message.target`.
95
96
    Parameters
97
    ----------
98
    message :
99
        Transaction specific items.
100
101
    env :
102
        External items required for EVM execution.
103
104
    Returns
105
    -------
106
    output : `MessageCallOutput`
107
        Output of the message call
108
    """
109
    if message.target == Bytes0(b""):
110
        is_collision = account_has_code_or_nonce(
111
            env.state, message.current_target
112
        )
113
        if is_collision:
114
            return MessageCallOutput(
115
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
116
            )
117
        else:
118
            evm = process_create_message(message, env)
119
    else:
120
        evm = process_message(message, env)
121
        if account_exists_and_is_empty(env.state, Address(message.target)):
122
            evm.touched_accounts.add(Address(message.target))
123
124
    if evm.error:
125
        logs: Tuple[Log, ...] = ()
126
        accounts_to_delete = set()
127
        touched_accounts = set()
128
        refund_counter = U256(0)
129
    else:
130
        logs = evm.logs
131
        accounts_to_delete = evm.accounts_to_delete
132
        touched_accounts = evm.touched_accounts
133
        refund_counter = U256(evm.refund_counter)
134
135
    tx_end = TransactionEnd(
136
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
137
    )
138
    evm_trace(evm, tx_end)
139
140
    return MessageCallOutput(
141
        gas_left=evm.gas_left,
142
        refund_counter=refund_counter,
143
        logs=logs,
144
        accounts_to_delete=accounts_to_delete,
145
        touched_accounts=touched_accounts,
146
        error=evm.error,
147
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

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

process_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

def process_message(message: Message, ​​env: Environment) -> Evm:
209
    """
210
    Executes a call to create a smart contract.
211
212
    Parameters
213
    ----------
214
    message :
215
        Transaction specific items.
216
    env :
217
        External items required for EVM execution.
218
219
    Returns
220
    -------
221
    evm: :py:class:`~ethereum.paris.vm.Evm`
222
        Items containing execution specific objects
223
    """
224
    if message.depth > STACK_DEPTH_LIMIT:
225
        raise StackDepthLimitError("Stack depth limit reached")
226
227
    # take snapshot of state before processing the message
228
    begin_transaction(env.state)
229
230
    touch_account(env.state, message.current_target)
231
232
    if message.should_transfer_value and message.value != 0:
233
        move_ether(
234
            env.state, message.caller, message.current_target, message.value
235
        )
236
237
    evm = execute_code(message, env)
238
    if evm.error:
239
        # revert state to the last saved checkpoint
240
        # since the message call resulted in an error
241
        rollback_transaction(env.state)
242
    else:
243
        commit_transaction(env.state)
244
    return evm

execute_code

Executes bytecode present in the message.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

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