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

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

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:
139
    """
140
    Executes a call to create a smart contract.
141
142
    Parameters
143
    ----------
144
    message :
145
        Transaction specific items.
146
147
    Returns
148
    -------
149
    evm: :py:class:`~ethereum.forks.frontier.vm.Evm`
150
        Items containing execution specific objects.
151
152
    """
153
    state = message.block_env.state
154
    # take snapshot of state before processing the message
155
    begin_transaction(state)
156
157
    evm = process_message(message)
158
    if not evm.error:
159
        contract_code = evm.output
160
        contract_code_gas = (
161
            Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE
162
        )
163
        try:
164
            charge_gas(evm, contract_code_gas)
165
        except ExceptionalHalt:
166
            evm.output = b""
167
        else:
168
            set_code(state, message.current_target, contract_code)
169
        commit_transaction(state)
170
    else:
171
        rollback_transaction(state)
172
    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:
176
    """
177
    Move ether and execute the relevant code.
178
179
    Parameters
180
    ----------
181
    message :
182
        Transaction specific items.
183
184
    Returns
185
    -------
186
    evm: :py:class:`~ethereum.forks.frontier.vm.Evm`
187
        Items containing execution specific objects
188
189
    """
190
    state = message.block_env.state
191
    if message.depth > STACK_DEPTH_LIMIT:
192
        raise StackDepthLimitError("Stack depth limit reached")
193
194
    code = message.code
195
    valid_jump_destinations = get_valid_jump_destinations(code)
196
    evm = Evm(
197
        pc=Uint(0),
198
        stack=[],
199
        memory=bytearray(),
200
        code=code,
201
        gas_left=message.gas,
202
        valid_jump_destinations=valid_jump_destinations,
203
        logs=(),
204
        refund_counter=0,
205
        running=True,
206
        message=message,
207
        output=b"",
208
        accounts_to_delete=set(),
209
        error=None,
210
    )
211
212
    # take snapshot of state before processing the message
213
    begin_transaction(state)
214
215
    touch_account(state, message.current_target)
216
217
    if message.value != 0:
218
        move_ether(
219
            state, message.caller, message.current_target, message.value
220
        )
221
222
    try:
223
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
224
            evm_trace(evm, PrecompileStart(evm.message.code_address))
225
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
226
            evm_trace(evm, PrecompileEnd())
227
        else:
228
            while evm.running and evm.pc < ulen(evm.code):
229
                try:
230
                    op = Ops(evm.code[evm.pc])
231
                except ValueError as e:
232
                    raise InvalidOpcode(evm.code[evm.pc]) from e
233
234
                evm_trace(evm, OpStart(op))
235
                op_implementation[op](evm)
236
                evm_trace(evm, OpEnd())
237
238
            evm_trace(evm, EvmStop(Ops.STOP))
239
240
    except ExceptionalHalt as error:
241
        evm_trace(evm, OpException(error))
242
        evm.gas_left = Uint(0)
243
        evm.error = error
244
245
    if evm.error:
246
        # revert state to the last saved checkpoint
247
        # since the message call resulted in an error
248
        rollback_transaction(state)
249
    else:
250
        commit_transaction(state)
251
    return evm