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

63
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

64
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. `touched_accounts`: Accounts that have been touched.
  6. `error`: The error from the execution if any.
67
@dataclass
class MessageCallOutput:

gas_left

82
    gas_left: Uint

refund_counter

83
    refund_counter: U256

logs

84
    logs: Tuple[Log, ...]

accounts_to_delete

85
    accounts_to_delete: Set[Address]

touched_accounts

86
    touched_accounts: Set[Address]

error

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
205
    """
206
    Move ether and execute the relevant code.
207
208
    Parameters
209
    ----------
210
    message :
211
        Transaction specific items.
212
213
    Returns
214
    -------
215
    evm: :py:class:`~ethereum.forks.arrow_glacier.vm.Evm`
216
        Items containing execution specific objects
217
218
    """
219
    state = message.block_env.state
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
        touched_accounts=set(),
239
        return_data=b"",
240
        error=None,
241
        accessed_addresses=message.accessed_addresses,
242
        accessed_storage_keys=message.accessed_storage_keys,
243
    )
244
245
    # take snapshot of state before processing the message
246
    begin_transaction(state)
247
248
    touch_account(state, message.current_target)
249
250
    if message.should_transfer_value and message.value != 0:
251
        move_ether(
252
            state, message.caller, message.current_target, message.value
253
        )
254
255
    try:
256
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
257
            evm_trace(evm, PrecompileStart(evm.message.code_address))
258
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
259
            evm_trace(evm, PrecompileEnd())
260
        else:
261
            while evm.running and evm.pc < ulen(evm.code):
262
                try:
263
                    op = Ops(evm.code[evm.pc])
264
                except ValueError as e:
265
                    raise InvalidOpcode(evm.code[evm.pc]) from e
266
267
                evm_trace(evm, OpStart(op))
268
                op_implementation[op](evm)
269
                evm_trace(evm, OpEnd())
270
271
            evm_trace(evm, EvmStop(Ops.STOP))
272
273
    except ExceptionalHalt as error:
274
        evm_trace(evm, OpException(error))
275
        evm.gas_left = Uint(0)
276
        evm.output = b""
277
        evm.error = error
278
    except Revert as error:
279
        evm_trace(evm, OpException(error))
280
        evm.error = error
281
282
    if evm.error:
283
        # revert state to the last saved checkpoint
284
        # since the message call resulted in an error
285
        rollback_transaction(state)
286
    else:
287
        commit_transaction(state)
288
    return evm