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

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: Iterable[Address]

error

84
    error: Optional[Exception]

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:
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
    env :
100
        External items required for EVM execution.
101
102
    Returns
103
    -------
104
    output : `MessageCallOutput`
105
        Output of the message call
106
    """
107
    if message.target == Bytes0(b""):
108
        is_collision = account_has_code_or_nonce(
109
            env.state, message.current_target
110
        )
111
        if is_collision:
112
            return MessageCallOutput(
113
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
114
            )
115
        else:
116
            evm = process_create_message(message, env)
117
    else:
118
        evm = process_message(message, env)
119
        if account_exists_and_is_empty(env.state, Address(message.target)):
120
            evm.touched_accounts.add(Address(message.target))
121
122
    if evm.error:
123
        logs: Tuple[Log, ...] = ()
124
        accounts_to_delete = set()
125
        touched_accounts = set()
126
        refund_counter = U256(0)
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. env : External items required for EVM execution.

Returns

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

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

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