ethereum.constantinople.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.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.

env : External items required for EVM execution.

Returns

output : MessageCallOutput Output of the message call

def process_message_call(message: Message, ​​env: Environment) -> MessageCallOutput:
93
    """
94
    If `message.current` 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
    env :
103
        External items required for EVM execution.
104
105
    Returns
106
    -------
107
    output : `MessageCallOutput`
108
        Output of the message call
109
    """
110
    if message.target == Bytes0(b""):
111
        is_collision = account_has_code_or_nonce(
112
            env.state, message.current_target
113
        ) or account_has_storage(env.state, message.current_target)
114
        if is_collision:
115
            return MessageCallOutput(
116
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
117
            )
118
        else:
119
            evm = process_create_message(message, env)
120
    else:
121
        evm = process_message(message, env)
122
        if account_exists_and_is_empty(env.state, Address(message.target)):
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
        refund_counter = U256(0)
130
    else:
131
        logs = evm.logs
132
        accounts_to_delete = evm.accounts_to_delete
133
        touched_accounts = evm.touched_accounts
134
        refund_counter = U256(evm.refund_counter)
135
136
    tx_end = TransactionEnd(
137
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
138
    )
139
    evm_trace(evm, tx_end)
140
141
    return MessageCallOutput(
142
        gas_left=evm.gas_left,
143
        refund_counter=refund_counter,
144
        logs=logs,
145
        accounts_to_delete=accounts_to_delete,
146
        touched_accounts=touched_accounts,
147
        error=evm.error,
148
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

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

process_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

def process_message(message: Message, ​​env: Environment) -> Evm:
202
    """
203
    Executes a call to create a smart contract.
204
205
    Parameters
206
    ----------
207
    message :
208
        Transaction specific items.
209
    env :
210
        External items required for EVM execution.
211
212
    Returns
213
    -------
214
    evm: :py:class:`~ethereum.constantinople.vm.Evm`
215
        Items containing execution specific objects
216
    """
217
    if message.depth > STACK_DEPTH_LIMIT:
218
        raise StackDepthLimitError("Stack depth limit reached")
219
220
    # take snapshot of state before processing the message
221
    begin_transaction(env.state)
222
223
    touch_account(env.state, message.current_target)
224
225
    if message.should_transfer_value and message.value != 0:
226
        move_ether(
227
            env.state, message.caller, message.current_target, message.value
228
        )
229
230
    evm = execute_code(message, env)
231
    if evm.error:
232
        # revert state to the last saved checkpoint
233
        # since the message call resulted in an error
234
        rollback_transaction(env.state)
235
    else:
236
        commit_transaction(env.state)
237
    return evm

execute_code

Executes bytecode present in the message.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

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

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