ethereum.forks.bpo5.vm.interpreterethereum.forks.amsterdam.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

64
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

65
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

66
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.
  6. `return_data`: The output of the execution.
69
@dataclass
class MessageCallOutput:

gas_left

84
    gas_left: Uint

refund_counter

85
    refund_counter: U256

logs

86
    logs: Tuple[Log, ...]

accounts_to_delete

87
    accounts_to_delete: Set[Address]

error

88
    error: Optional[EthereumException]

return_data

89
    return_data: Bytes

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:
93
    """
94
    If `message.target` is empty then it creates a smart contract
95
    else it executes a call from the `message.caller` to the `message.target`.
96
97
    Parameters
98
    ----------
99
    message :
100
        Transaction specific items.
101
102
    Returns
103
    -------
104
    output : `MessageCallOutput`
105
        Output of the message call
106
107
    """
109
    block_env = message.block_env
108
    tx_state = message.tx_env.state
109
    refund_counter = U256(0)
110
    if message.target == Bytes0(b""):
112
        is_collision = account_has_code_or_nonce(
113
            block_env.state, message.current_target
114
        ) or account_has_storage(block_env.state, message.current_target)
111
        is_collision = account_has_code_or_nonce(
112
            tx_state, message.current_target
113
        ) or account_has_storage(tx_state, message.current_target)
114
        if is_collision:
115
            return MessageCallOutput(
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:
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)
134
            message.code = get_code(
135
                block_env.state,
136
                get_account(block_env.state, delegated_address).code_hash,
133
            message.code = get_code(
134
                tx_state,
135
                get_account(tx_state, delegated_address).code_hash,
136
            )
137
            message.code_address = delegated_address
138
139
        evm = process_message(message)
140
141
    if evm.error:
142
        logs: Tuple[Log, ...] = ()
143
        accounts_to_delete = set()
144
    else:
145
        logs = evm.logs
146
        accounts_to_delete = evm.accounts_to_delete
147
        refund_counter += U256(evm.refund_counter)
148
149
    tx_end = TransactionEnd(
150
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
151
    )
152
    evm_trace(evm, tx_end)
153
154
    return MessageCallOutput(
155
        gas_left=evm.gas_left,
156
        refund_counter=refund_counter,
157
        logs=logs,
158
        accounts_to_delete=accounts_to_delete,
159
        error=evm.error,
160
        return_data=evm.output,
161
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm Items containing execution specific objects.

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm Items containing execution specific objects

def process_message(message: Message) -> Evm:
226
    """
227
    Move ether and execute the relevant code.
228
229
    Parameters
230
    ----------
231
    message :
232
        Transaction specific items.
233
234
    Returns
235
    -------
238
    evm: :py:class:`~ethereum.forks.bpo5.vm.Evm`
236
    evm: :py:class:`~ethereum.forks.amsterdam.vm.Evm`
237
        Items containing execution specific objects
238
239
    """
242
    state = message.block_env.state
240
    tx_state = message.tx_env.state
241
    if message.depth > STACK_DEPTH_LIMIT:
242
        raise StackDepthLimitError("Stack depth limit reached")
243
246
    transient_storage = message.tx_env.transient_storage
244
    code = message.code
245
    valid_jump_destinations = get_valid_jump_destinations(code)
246
    evm = Evm(
247
        pc=Uint(0),
248
        stack=[],
249
        memory=bytearray(),
250
        code=code,
251
        gas_left=message.gas,
252
        valid_jump_destinations=valid_jump_destinations,
253
        logs=(),
254
        refund_counter=0,
255
        running=True,
256
        message=message,
257
        output=b"",
258
        accounts_to_delete=set(),
259
        return_data=b"",
260
        error=None,
261
        accessed_addresses=message.accessed_addresses,
262
        accessed_storage_keys=message.accessed_storage_keys,
263
    )
264
265
    # take snapshot of state before processing the message
269
    begin_transaction(state, transient_storage)
266
    snapshot = copy_tx_state(tx_state)
267
268
    if message.should_transfer_value and message.value != 0:
272
        move_ether(
273
            state, message.caller, message.current_target, message.value
269
        move_ether(
270
            tx_state,
271
            message.caller,
272
            message.current_target,
273
            message.value,
274
        )
275
276
    try:
277
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
278
            if not message.disable_precompiles:
279
                evm_trace(evm, PrecompileStart(evm.message.code_address))
280
                PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
281
                evm_trace(evm, PrecompileEnd())
282
        else:
283
            while evm.running and evm.pc < ulen(evm.code):
284
                try:
285
                    op = Ops(evm.code[evm.pc])
286
                except ValueError as e:
287
                    raise InvalidOpcode(evm.code[evm.pc]) from e
288
289
                evm_trace(evm, OpStart(op))
290
                op_implementation[op](evm)
291
                evm_trace(evm, OpEnd())
292
293
            evm_trace(evm, EvmStop(Ops.STOP))
294
295
    except ExceptionalHalt as error:
296
        evm_trace(evm, OpException(error))
297
        evm.gas_left = Uint(0)
298
        evm.output = b""
299
        evm.error = error
300
    except Revert as error:
301
        evm_trace(evm, OpException(error))
302
        evm.error = error
303
304
    if evm.error:
305
        # revert state to the last saved checkpoint
306
        # since the message call resulted in an error
307
        rollback_transaction(state, transient_storage)
308
    else:
309
        commit_transaction(state, transient_storage)
305
        restore_tx_state(tx_state, snapshot)
306
    return evm