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

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
@final
60
@dataclass
class MessageCallOutput:

gas_left

74
    gas_left: Uint

refund_counter

75
    refund_counter: U256

logs

76
    logs: Tuple[Log, ...]

accounts_to_delete

77
    accounts_to_delete: Set[Address]

error

78
    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:
82
    <snip>
97
    tx_state = message.tx_env.state
98
    refund_counter = U256(0)
99
    if message.target == Bytes0(b""):
100
        if account_deployable(tx_state, message.current_target):
101
            evm = process_create_message(message)
102
        else:
103
            return MessageCallOutput(
104
                gas_left=Uint(0),
105
                refund_counter=U256(0),
106
                logs=tuple(),
107
                accounts_to_delete=set(),
108
                error=AddressCollision(),
109
            )
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
    <snip>
150
    tx_state = message.tx_env.state
151
    # take snapshot of state before processing the message
152
    snapshot = copy_tx_state(tx_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 a `CREATE` call collides with a subsequent
158
    #   `CREATE` call.
159
    destroy_storage(tx_state, message.current_target)
160
161
    evm = process_message(message)
162
    if not evm.error:
163
        contract_code = evm.output
164
        contract_code_gas = (
165
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
166
        )
167
        try:
168
            charge_gas(evm, contract_code_gas)
169
        except ExceptionalHalt:
170
            evm.output = b""
171
        else:
172
            set_code(tx_state, message.current_target, contract_code)
173
    else:
174
        restore_tx_state(tx_state, snapshot)
175
    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:
179
    <snip>
193
    tx_state = message.tx_env.state
194
    if message.depth > STACK_DEPTH_LIMIT:
195
        raise StackDepthLimitError("Stack depth limit reached")
196
197
    code = message.code
198
    valid_jump_destinations = get_valid_jump_destinations(code)
199
    evm = Evm(
200
        pc=Uint(0),
201
        stack=[],
202
        memory=bytearray(),
203
        code=code,
204
        gas_left=message.gas,
205
        valid_jump_destinations=valid_jump_destinations,
206
        logs=(),
207
        refund_counter=0,
208
        running=True,
209
        message=message,
210
        output=b"",
211
        accounts_to_delete=set(),
212
        error=None,
213
    )
214
215
    # take snapshot of state before processing the message
216
    snapshot = copy_tx_state(tx_state)
217
218
    touch_account(tx_state, message.current_target)
219
220
    if message.value != 0:
221
        move_ether(
222
            tx_state,
223
            message.caller,
224
            message.current_target,
225
            message.value,
226
        )
227
228
    try:
229
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
230
            evm_trace(evm, PrecompileStart(evm.message.code_address))
231
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
232
            evm_trace(evm, PrecompileEnd())
233
        else:
234
            while evm.running and evm.pc < ulen(evm.code):
235
                try:
236
                    op = Ops(evm.code[evm.pc])
237
                except ValueError as e:
238
                    raise InvalidOpcode(evm.code[evm.pc]) from e
239
240
                evm_trace(evm, OpStart(op))
241
                op_implementation[op](evm)
242
                evm_trace(evm, OpEnd())
243
244
            evm_trace(evm, EvmStop(Ops.STOP))
245
246
    except ExceptionalHalt as error:
247
        evm_trace(evm, OpException(error))
248
        evm.gas_left = Uint(0)
249
        evm.error = error
250
251
    if evm.error:
252
        restore_tx_state(tx_state, snapshot)
253
    return evm