ethereum.forks.tangerine_whistle.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.tangerine_whistle.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.tangerine_whistle.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 as error:
166
            rollback_transaction(state)
167
            evm.gas_left = Uint(0)
168
            evm.error = error
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.tangerine_whistle.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.tangerine_whistle.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
    code = message.code
197
    valid_jump_destinations = get_valid_jump_destinations(code)
198
    evm = Evm(
199
        pc=Uint(0),
200
        stack=[],
201
        memory=bytearray(),
202
        code=code,
203
        gas_left=message.gas,
204
        valid_jump_destinations=valid_jump_destinations,
205
        logs=(),
206
        refund_counter=0,
207
        running=True,
208
        message=message,
209
        output=b"",
210
        accounts_to_delete=set(),
211
        error=None,
212
    )
213
214
    # take snapshot of state before processing the message
215
    begin_transaction(state)
216
217
    touch_account(state, message.current_target)
218
219
    if message.should_transfer_value and message.value != 0:
220
        move_ether(
221
            state, message.caller, message.current_target, message.value
222
        )
223
224
    try:
225
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
226
            evm_trace(evm, PrecompileStart(evm.message.code_address))
227
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
228
            evm_trace(evm, PrecompileEnd())
229
        else:
230
            while evm.running and evm.pc < ulen(evm.code):
231
                try:
232
                    op = Ops(evm.code[evm.pc])
233
                except ValueError as e:
234
                    raise InvalidOpcode(evm.code[evm.pc]) from e
235
236
                evm_trace(evm, OpStart(op))
237
                op_implementation[op](evm)
238
                evm_trace(evm, OpEnd())
239
240
            evm_trace(evm, EvmStop(Ops.STOP))
241
242
    except ExceptionalHalt as error:
243
        evm_trace(evm, OpException(error))
244
        evm.gas_left = Uint(0)
245
        evm.error = error
246
247
    if evm.error:
248
        # revert state to the last saved checkpoint
249
        # since the message call resulted in an error
250
        rollback_transaction(state)
251
    else:
252
        commit_transaction(state)
253
    return evm