ethereum.forks.bpo5.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
    block_env = message.block_env
109
    refund_counter = U256(0)
110
    if message.target == Bytes0(b""):
111
        is_collision = account_has_code_or_nonce(
112
            block_env.state, message.current_target
113
        ) or account_has_storage(block_env.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
                block_env.state,
135
                get_account(block_env.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.bpo5.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.bpo5.vm.Evm`
176
        Items containing execution specific objects.
177
178
    """
179
    state = message.block_env.state
180
    transient_storage = message.tx_env.transient_storage
181
    # take snapshot of state before processing the message
182
    begin_transaction(state, transient_storage)
183
184
    # The list of created accounts is used by `get_storage_original`.
185
    # Additionally, the list is needed to respect the constraints
186
    # added to SELFDESTRUCT by EIP-6780.
187
    mark_account_created(state, message.current_target)
188
189
    increment_nonce(state, message.current_target)
190
    evm = process_message(message)
191
    if not evm.error:
192
        contract_code = evm.output
193
        contract_code_gas = (
194
            Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE
195
        )
196
        try:
197
            if len(contract_code) > 0:
198
                if contract_code[0] == 0xEF:
199
                    raise InvalidContractPrefix
200
            charge_gas(evm, contract_code_gas)
201
            if len(contract_code) > MAX_CODE_SIZE:
202
                raise OutOfGasError
203
        except ExceptionalHalt as error:
204
            rollback_transaction(state, transient_storage)
205
            evm.gas_left = Uint(0)
206
            evm.output = b""
207
            evm.error = error
208
        else:
209
            set_code(state, message.current_target, contract_code)
210
            commit_transaction(state, transient_storage)
211
    else:
212
        rollback_transaction(state, transient_storage)
213
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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