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

60
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

61
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.
64
@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]

touched_accounts

83
    touched_accounts: 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
                touched_accounts=set(),
116
                error=AddressCollision(),
117
            )
118
        else:
119
            evm = process_create_message(message)
120
    else:
121
        evm = process_message(message)
122
        if account_exists_and_is_empty(
123
            block_env.state, Address(message.target)
124
        ):
125
            evm.touched_accounts.add(Address(message.target))
126
127
    if evm.error:
128
        logs: Tuple[Log, ...] = ()
129
        accounts_to_delete = set()
130
        touched_accounts = set()
131
    else:
132
        logs = evm.logs
133
        accounts_to_delete = evm.accounts_to_delete
134
        touched_accounts = evm.touched_accounts
135
        refund_counter += U256(evm.refund_counter)
136
137
    tx_end = TransactionEnd(
138
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
139
    )
140
    evm_trace(evm, tx_end)
141
142
    return MessageCallOutput(
143
        gas_left=evm.gas_left,
144
        refund_counter=refund_counter,
145
        logs=logs,
146
        accounts_to_delete=accounts_to_delete,
147
        touched_accounts=touched_accounts,
148
        error=evm.error,
149
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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