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

61
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

62
MAX_CODE_SIZE = 0x6000

MAX_INIT_CODE_SIZE

63
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.
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]

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(
111
                gas_left=Uint(0),
112
                refund_counter=U256(0),
113
                logs=tuple(),
114
                accounts_to_delete=set(),
115
                error=AddressCollision(),
116
            )
117
        else:
118
            evm = process_create_message(message)
119
    else:
120
        evm = process_message(message)
121
122
    if evm.error:
123
        logs: Tuple[Log, ...] = ()
124
        accounts_to_delete = set()
125
    else:
126
        logs = evm.logs
127
        accounts_to_delete = evm.accounts_to_delete
128
        refund_counter += U256(evm.refund_counter)
129
130
    tx_end = TransactionEnd(
131
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
132
    )
133
    evm_trace(evm, tx_end)
134
135
    return MessageCallOutput(
136
        gas_left=evm.gas_left,
137
        refund_counter=refund_counter,
138
        logs=logs,
139
        accounts_to_delete=accounts_to_delete,
140
        error=evm.error,
141
    )

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 Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
145
    """
146
    Executes a call to create a smart contract.
147
148
    Parameters
149
    ----------
150
    message :
151
        Transaction specific items.
152
153
    Returns
154
    -------
155
    evm: :py:class:`~ethereum.forks.shanghai.vm.Evm`
156
        Items containing execution specific objects.
157
158
    """
159
    state = message.block_env.state
160
    # take snapshot of state before processing the message
161
    begin_transaction(state)
162
163
    # The list of created accounts is used by `get_storage_original`.
164
    mark_account_created(state, message.current_target)
165
166
    increment_nonce(state, message.current_target)
167
    evm = process_message(message)
168
    if not evm.error:
169
        contract_code = evm.output
170
        contract_code_gas = (
171
            Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE
172
        )
173
        try:
174
            if len(contract_code) > 0:
175
                if contract_code[0] == 0xEF:
176
                    raise InvalidContractPrefix
177
            charge_gas(evm, contract_code_gas)
178
            if len(contract_code) > MAX_CODE_SIZE:
179
                raise OutOfGasError
180
        except ExceptionalHalt as error:
181
            rollback_transaction(state)
182
            evm.gas_left = Uint(0)
183
            evm.output = b""
184
            evm.error = error
185
        else:
186
            set_code(state, message.current_target, contract_code)
187
            commit_transaction(state)
188
    else:
189
        rollback_transaction(state)
190
    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 Items containing execution specific objects

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