ethereum.forks.shanghai.vm.interpreterethereum.forks.cancun.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

MAX_INIT_CODE_SIZE

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

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Tuple[Log, ...]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

error

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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