ethereum.tangerine_whistle.vm.interpreterethereum.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

62
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

63
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.
  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.
66
@dataclass
class MessageCallOutput:

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Tuple[Log, ...]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

touched_accounts

85
    touched_accounts: Set[Address]

error

86
    error: Optional[EthereumException]

process_message_call

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

def process_create_message(message: Message) -> Evm:
149
    """
150
    Executes a call to create a smart contract.
151
152
    Parameters
153
    ----------
154
    message :
155
        Transaction specific items.
156
157
    Returns
158
    -------
146
    evm: :py:class:`~ethereum.tangerine_whistle.vm.Evm`
159
    evm: :py:class:`~ethereum.spurious_dragon.vm.Evm`
160
        Items containing execution specific objects.
161
    """
162
    state = message.block_env.state
163
    # take snapshot of state before processing the message
164
    begin_transaction(state)
165
166
    # If the address where the account is being created has storage, it is
167
    # destroyed. This can only happen in the following highly unlikely
168
    # circumstances:
169
    # * The address created by two `CREATE` calls collide.
157
    # * The first `CREATE` left empty code.
170
    # * The first `CREATE` happened before Spurious Dragon and left empty
171
    #   code.
172
    destroy_storage(state, message.current_target)
173
174
    increment_nonce(state, message.current_target)
175
    evm = process_message(message)
176
    if not evm.error:
177
        contract_code = evm.output
178
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
179
        try:
165
            charge_gas(evm, contract_code_gas)
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)
185
            evm.gas_left = Uint(0)
186
            evm.error = error
187
        else:
188
            set_code(state, message.current_target, contract_code)
189
            commit_transaction(state)
190
    else:
191
        rollback_transaction(state)
192
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
196
    """
197
    Move ether and execute the relevant code.
198
199
    Parameters
200
    ----------
201
    message :
202
        Transaction specific items.
203
204
    Returns
205
    -------
189
    evm: :py:class:`~ethereum.tangerine_whistle.vm.Evm`
206
    evm: :py:class:`~ethereum.spurious_dragon.vm.Evm`
207
        Items containing execution specific objects
208
    """
209
    state = message.block_env.state
210
    if message.depth > STACK_DEPTH_LIMIT:
211
        raise StackDepthLimitError("Stack depth limit reached")
212
213
    # take snapshot of state before processing the message
214
    begin_transaction(state)
215
216
    touch_account(state, message.current_target)
217
218
    if message.should_transfer_value and message.value != 0:
219
        move_ether(
220
            state, message.caller, message.current_target, message.value
221
        )
222
223
    evm = execute_code(message)
224
    if evm.error:
225
        # revert state to the last saved checkpoint
226
        # since the message call resulted in an error
227
        rollback_transaction(state)
228
    else:
229
        commit_transaction(state)
230
    return evm

execute_code

Executes bytecode present in the message.

Parameters

message : Transaction specific items.

Returns

evm: ethereum.vm.EVM Items containing execution specific objects

def execute_code(message: Message) -> Evm:
234
    """
235
    Executes bytecode present in the `message`.
236
237
    Parameters
238
    ----------
239
    message :
240
        Transaction specific items.
241
242
    Returns
243
    -------
244
    evm: `ethereum.vm.EVM`
245
        Items containing execution specific objects
246
    """
247
    code = message.code
248
    valid_jump_destinations = get_valid_jump_destinations(code)
249
250
    evm = Evm(
251
        pc=Uint(0),
252
        stack=[],
253
        memory=bytearray(),
254
        code=code,
255
        gas_left=message.gas,
256
        valid_jump_destinations=valid_jump_destinations,
257
        logs=(),
258
        refund_counter=0,
259
        running=True,
260
        message=message,
261
        output=b"",
262
        accounts_to_delete=set(),
263
        touched_accounts=set(),
264
        error=None,
265
    )
266
    try:
267
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
268
            evm_trace(evm, PrecompileStart(evm.message.code_address))
269
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
270
            evm_trace(evm, PrecompileEnd())
271
            return evm
272
273
        while evm.running and evm.pc < ulen(evm.code):
274
            try:
275
                op = Ops(evm.code[evm.pc])
276
            except ValueError:
277
                raise InvalidOpcode(evm.code[evm.pc])
278
279
            evm_trace(evm, OpStart(op))
280
            op_implementation[op](evm)
281
            evm_trace(evm, OpEnd())
282
283
        evm_trace(evm, EvmStop(Ops.STOP))
284
285
    except ExceptionalHalt as error:
286
        evm_trace(evm, OpException(error))
287
        evm.gas_left = Uint(0)
288
        evm.error = error
289
    return evm