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

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.cancun.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.cancun.vm.Evm`
156
        Items containing execution specific objects.
157
158
    """
159
    state = message.block_env.state
160
    transient_storage = message.tx_env.transient_storage
161
    # take snapshot of state before processing the message
162
    begin_transaction(state, transient_storage)
163
164
    # The list of created accounts is used by `get_storage_original`.
165
    # Additionally, the list is needed to respect the constraints
166
    # added to SELFDESTRUCT by EIP-6780.
167
    mark_account_created(state, message.current_target)
168
169
    increment_nonce(state, message.current_target)
170
    evm = process_message(message)
171
    if not evm.error:
172
        contract_code = evm.output
173
        contract_code_gas = (
174
            Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE
175
        )
176
        try:
177
            if len(contract_code) > 0:
178
                if contract_code[0] == 0xEF:
179
                    raise InvalidContractPrefix
180
            charge_gas(evm, contract_code_gas)
181
            if len(contract_code) > MAX_CODE_SIZE:
182
                raise OutOfGasError
183
        except ExceptionalHalt as error:
184
            rollback_transaction(state, transient_storage)
185
            evm.gas_left = Uint(0)
186
            evm.output = b""
187
            evm.error = error
188
        else:
189
            set_code(state, message.current_target, contract_code)
190
            commit_transaction(state, transient_storage)
191
    else:
192
        rollback_transaction(state, transient_storage)
193
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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