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

60
STACK_DEPTH_LIMIT = U256(1024)

MAX_CODE_SIZE

61
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.
64
@dataclass
class MessageCallOutput:

gas_left

79
    gas_left: Uint

refund_counter

80
    refund_counter: U256

logs

81
    logs: Tuple[Log, ...]

accounts_to_delete

82
    accounts_to_delete: Set[Address]

touched_accounts

83
    touched_accounts: Iterable[Address]

error

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

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.berlin.vm.Evm~ethereum.london.vm.Evm Items containing execution specific objects.

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

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