ethereum.forks.bpo1.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
    """
108
    tx_state = message.tx_env.state
109
    refund_counter = U256(0)
110
    if message.target == Bytes0(b""):
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
                gas_left=Uint(0),
117
                refund_counter=U256(0),
118
                logs=tuple(),
119
                accounts_to_delete=set(),
120
                error=AddressCollision(),
121
                return_data=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)
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.bpo1.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
    -------
175
    evm: :py:class:`~ethereum.forks.bpo1.vm.Evm`
176
        Items containing execution specific objects.
177
178
    """
179
    tx_state = message.tx_env.state
180
    # take snapshot of state before processing the message
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.
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.
196
    mark_account_created(tx_state, message.current_target)
197
198
    increment_nonce(tx_state, message.current_target)
199
    evm = process_message(message)
200
    if not evm.error:
201
        contract_code = evm.output
202
        contract_code_gas = (
203
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
204
        )
205
        try:
206
            if len(contract_code) > 0:
207
                if contract_code[0] == 0xEF:
208
                    raise InvalidContractPrefix
209
            charge_gas(evm, contract_code_gas)
210
            if len(contract_code) > MAX_CODE_SIZE:
211
                raise OutOfGasError
212
        except ExceptionalHalt as error:
213
            restore_tx_state(tx_state, snapshot)
214
            evm.gas_left = Uint(0)
215
            evm.output = b""
216
            evm.error = error
217
        else:
218
            set_code(tx_state, message.current_target, contract_code)
219
    else:
220
        restore_tx_state(tx_state, snapshot)
221
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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