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

58
STACK_DEPTH_LIMIT = U256(1024)

MAX_CODE_SIZE

59
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.
62
@dataclass
class MessageCallOutput:

gas_left

77
    gas_left: Uint

refund_counter

78
    refund_counter: U256

logs

79
    logs: Tuple[Log, ...]

accounts_to_delete

80
    accounts_to_delete: Set[Address]

touched_accounts

81
    touched_accounts: Iterable[Address]

error

82
    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:
88
    """
89
    If `message.current` 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
    env :
98
        External items required for EVM execution.
99
100
    Returns
101
    -------
102
    output : `MessageCallOutput`
103
        Output of the message call
104
    """
105
    if message.target == Bytes0(b""):
106
        is_collision = account_has_code_or_nonce(
107
            env.state, message.current_target
108
        )
109
        if is_collision:
110
            return MessageCallOutput(
111
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
112
            )
113
        else:
114
            evm = process_create_message(message, env)
115
    else:
116
        evm = process_message(message, env)
117
        if account_exists_and_is_empty(env.state, Address(message.target)):
118
            evm.touched_accounts.add(Address(message.target))
119
120
    if evm.error:
121
        logs: Tuple[Log, ...] = ()
122
        accounts_to_delete = set()
123
        touched_accounts = set()
124
        refund_counter = U256(0)
125
    else:
126
        logs = evm.logs
127
        accounts_to_delete = evm.accounts_to_delete
128
        touched_accounts = evm.touched_accounts
129
        refund_counter = U256(evm.refund_counter)
130
131
    tx_end = TransactionEnd(message.gas - evm.gas_left, evm.output, evm.error)
132
    evm_trace(evm, tx_end)
133
134
    return MessageCallOutput(
135
        gas_left=evm.gas_left,
136
        refund_counter=refund_counter,
137
        logs=logs,
138
        accounts_to_delete=accounts_to_delete,
139
        touched_accounts=touched_accounts,
140
        error=evm.error,
141
    )

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 Items containing execution specific objects.

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

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