ethereum.forks.frontier.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 = 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.
61
@dataclass
class MessageCallOutput:

gas_left

75
    gas_left: Uint

refund_counter

76
    refund_counter: U256

logs

77
    logs: Tuple[Log, ...]

accounts_to_delete

78
    accounts_to_delete: Set[Address]

error

79
    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:
83
    """
84
    If `message.target` 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
    Returns
93
    -------
94
    output : `MessageCallOutput`
95
        Output of the message call
96
97
    """
98
    block_env = message.block_env
99
    refund_counter = U256(0)
100
    if message.target == Bytes0(b""):
101
        is_collision = account_has_code_or_nonce(
102
            block_env.state, message.current_target
103
        ) or account_has_storage(block_env.state, message.current_target)
104
        if is_collision:
105
            return MessageCallOutput(
106
                Uint(0), U256(0), tuple(), set(), AddressCollision()
107
            )
108
        else:
109
            evm = process_create_message(message)
110
    else:
111
        evm = process_message(message)
112
113
    if evm.error:
114
        logs: Tuple[Log, ...] = ()
115
        accounts_to_delete = set()
116
    else:
117
        logs = evm.logs
118
        accounts_to_delete = evm.accounts_to_delete
119
        refund_counter += U256(evm.refund_counter)
120
121
    tx_end = TransactionEnd(
122
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
123
    )
124
    evm_trace(evm, tx_end)
125
126
    return MessageCallOutput(
127
        gas_left=evm.gas_left,
128
        refund_counter=refund_counter,
129
        logs=logs,
130
        accounts_to_delete=accounts_to_delete,
131
        error=evm.error,
132
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

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

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

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