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

61
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

62
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. `error`: The error from the execution if any.
65
@dataclass
class MessageCallOutput:

gas_left

79
    gas_left: Uint

refund_counter

80
    refund_counter: U256

logs

81
    logs: Tuple[Log, ...]

accounts_to_delete

82
    accounts_to_delete: Set[Address]

error

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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