ethereum.forks.gray_glacier.vm.interpreterethereum.forks.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.
  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]

touched_accounts

87
    touched_accounts: 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
    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(
115
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
111
                Uint(0), U256(0), tuple(), set(), AddressCollision()
112
            )
113
        else:
114
            evm = process_create_message(message)
115
    else:
120
        evm = process_message(message)
121
        if account_exists_and_is_empty(
122
            block_env.state, Address(message.target)
123
        ):
124
            evm.touched_accounts.add(Address(message.target))
116
        evm = process_message(message)
117
118
    if evm.error:
119
        logs: Tuple[Log, ...] = ()
128
        accounts_to_delete = set()
129
        touched_accounts = set()
120
        accounts_to_delete = set()
121
    else:
122
        logs = evm.logs
123
        accounts_to_delete = evm.accounts_to_delete
133
        touched_accounts = evm.touched_accounts
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,
146
        touched_accounts=touched_accounts,
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.forks.gray_glacier.vm.Evm~ethereum.forks.paris.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
    -------
162
    evm: :py:class:`~ethereum.forks.gray_glacier.vm.Evm`
151
    evm: :py:class:`~ethereum.forks.paris.vm.Evm`
152
        Items containing execution specific objects.
153
154
    """
155
    state = message.block_env.state
156
    # take snapshot of state before processing the message
157
    begin_transaction(state)
158
159
    # If the address where the account is being created has storage, it is
160
    # destroyed. This can only happen in the following highly unlikely
161
    # circumstances:
162
    # * The address created by a `CREATE` call collides with a subsequent
163
    #   `CREATE` or `CREATE2` call.
164
    # * The first `CREATE` happened before Spurious Dragon and left empty
165
    #   code.
166
    destroy_storage(state, message.current_target)
167
168
    # In the previously mentioned edge case the preexisting storage is ignored
169
    # for gas refund purposes. In order to do this we must track created
170
    # accounts.
171
    mark_account_created(state, message.current_target)
172
173
    increment_nonce(state, message.current_target)
174
    evm = process_message(message)
175
    if not evm.error:
176
        contract_code = evm.output
177
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
178
        try:
179
            if len(contract_code) > 0:
180
                if contract_code[0] == 0xEF:
181
                    raise InvalidContractPrefix
182
            charge_gas(evm, contract_code_gas)
183
            if len(contract_code) > MAX_CODE_SIZE:
184
                raise OutOfGasError
185
        except ExceptionalHalt as error:
186
            rollback_transaction(state)
187
            evm.gas_left = Uint(0)
188
            evm.output = b""
189
            evm.error = error
190
        else:
191
            set_code(state, message.current_target, contract_code)
192
            commit_transaction(state)
193
    else:
194
        rollback_transaction(state)
195
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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