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

56
STACK_DEPTH_LIMIT = Uint(1024)

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

gas_left

73
    gas_left: Uint

refund_counter

74
    refund_counter: U256

logs

75
    logs: Tuple[Log, ...]

accounts_to_delete

76
    accounts_to_delete: Set[Address]

error

77
    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:
83
    """
84
    If `message.current` is empty then it creates a smart contract
85
    else it executes a call from the `message.caller` to the `message.target`.
86
87
    Parameters
88
    ----------
89
    message :
90
        Transaction specific items.
91
92
    env :
93
        External items required for EVM execution.
94
95
    Returns
96
    -------
97
    output : `MessageCallOutput`
98
        Output of the message call
99
    """
100
    if message.target == Bytes0(b""):
101
        is_collision = account_has_code_or_nonce(
102
            env.state, message.current_target
103
        )
104
        if is_collision:
105
            return MessageCallOutput(
106
                Uint(0), U256(0), tuple(), set(), AddressCollision()
107
            )
108
        else:
109
            evm = process_create_message(message, env)
110
    else:
111
        evm = process_message(message, env)
112
113
    if evm.error:
114
        logs: Tuple[Log, ...] = ()
115
        accounts_to_delete = set()
116
        refund_counter = U256(0)
117
    else:
118
        logs = evm.logs
119
        accounts_to_delete = evm.accounts_to_delete
120
        refund_counter = U256(evm.refund_counter)
121
122
    tx_end = TransactionEnd(
123
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
124
    )
125
    evm_trace(evm, tx_end)
126
127
    return MessageCallOutput(
128
        gas_left=evm.gas_left,
129
        refund_counter=refund_counter,
130
        logs=logs,
131
        accounts_to_delete=accounts_to_delete,
132
        error=evm.error,
133
    )

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.homestead.vm.Evm~ethereum.dao_fork.vm.Evm Items containing execution specific objects.

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

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