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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

def process_create_message(message: Message) -> Evm:
151
    """
152
    Executes a call to create a smart contract.
153
154
    Parameters
155
    ----------
156
    message :
157
        Transaction specific items.
158
159
    Returns
160
    -------
161
    evm: :py:class:`~ethereum.forks.istanbul.vm.Evm`
162
        Items containing execution specific objects.
163
164
    """
165
    state = message.block_env.state
166
    # take snapshot of state before processing the message
167
    begin_transaction(state)
168
169
    # If the address where the account is being created has storage, it is
170
    # destroyed. This can only happen in the following highly unlikely
171
    # circumstances:
172
    # * The address created by a `CREATE` call collides with a subsequent
173
    #   `CREATE` or `CREATE2` call.
174
    # * The first `CREATE` happened before Spurious Dragon and left empty
175
    #   code.
176
    destroy_storage(state, message.current_target)
177
178
    # In the previously mentioned edge case the preexisting storage is ignored
179
    # for gas refund purposes. In order to do this we must track created
180
    # accounts.
181
    mark_account_created(state, message.current_target)
182
183
    increment_nonce(state, message.current_target)
184
    evm = process_message(message)
185
    if not evm.error:
186
        contract_code = evm.output
187
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
188
        try:
189
            charge_gas(evm, contract_code_gas)
190
            if len(contract_code) > MAX_CODE_SIZE:
191
                raise OutOfGasError
192
        except ExceptionalHalt as error:
193
            rollback_transaction(state)
194
            evm.gas_left = Uint(0)
195
            evm.output = b""
196
            evm.error = error
197
        else:
198
            set_code(state, message.current_target, contract_code)
199
            commit_transaction(state)
200
    else:
201
        rollback_transaction(state)
202
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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