ethereum.forks.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

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
    tx_state = message.tx_env.state
98
    refund_counter = U256(0)
99
    if message.target == Bytes0(b""):
100
        is_collision = account_has_code_or_nonce(
101
            tx_state, message.current_target
102
        ) or account_has_storage(tx_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.dao_fork.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.dao_fork.vm.Evm`
150
        Items containing execution specific objects.
151
152
    """
153
    tx_state = message.tx_env.state
154
    # take snapshot of state before processing the message
155
    snapshot = copy_tx_state(tx_state)
156
157
    # If the address where the account is being created has storage, it is
158
    # destroyed. This can only happen in the following highly unlikely
159
    # circumstances:
160
    # * The address created by a `CREATE` call collides with a subsequent
161
    #   `CREATE` call.
162
    destroy_storage(tx_state, message.current_target)
163
164
    evm = process_message(message)
165
    if not evm.error:
166
        contract_code = evm.output
167
        contract_code_gas = (
168
            ulen(contract_code) * GasCosts.CODE_DEPOSIT_PER_BYTE
169
        )
170
        try:
171
            charge_gas(evm, contract_code_gas)
172
        except ExceptionalHalt as error:
173
            restore_tx_state(tx_state, snapshot)
174
            evm.gas_left = Uint(0)
175
            evm.error = error
176
        else:
177
            set_code(tx_state, message.current_target, contract_code)
178
    else:
179
        restore_tx_state(tx_state, snapshot)
180
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
184
    """
185
    Move ether and execute the relevant code.
186
187
    Parameters
188
    ----------
189
    message :
190
        Transaction specific items.
191
192
    Returns
193
    -------
194
    evm: :py:class:`~ethereum.forks.dao_fork.vm.Evm`
195
        Items containing execution specific objects
196
197
    """
198
    tx_state = message.tx_env.state
199
    if message.depth > STACK_DEPTH_LIMIT:
200
        raise StackDepthLimitError("Stack depth limit reached")
201
202
    code = message.code
203
    valid_jump_destinations = get_valid_jump_destinations(code)
204
    evm = Evm(
205
        pc=Uint(0),
206
        stack=[],
207
        memory=bytearray(),
208
        code=code,
209
        gas_left=message.gas,
210
        valid_jump_destinations=valid_jump_destinations,
211
        logs=(),
212
        refund_counter=0,
213
        running=True,
214
        message=message,
215
        output=b"",
216
        accounts_to_delete=set(),
217
        error=None,
218
    )
219
220
    # take snapshot of state before processing the message
221
    snapshot = copy_tx_state(tx_state)
222
223
    touch_account(tx_state, message.current_target)
224
225
    if message.should_transfer_value and message.value != 0:
226
        move_ether(
227
            tx_state,
228
            message.caller,
229
            message.current_target,
230
            message.value,
231
        )
232
233
    try:
234
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
235
            evm_trace(evm, PrecompileStart(evm.message.code_address))
236
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
237
            evm_trace(evm, PrecompileEnd())
238
        else:
239
            while evm.running and evm.pc < ulen(evm.code):
240
                try:
241
                    op = Ops(evm.code[evm.pc])
242
                except ValueError as e:
243
                    raise InvalidOpcode(evm.code[evm.pc]) from e
244
245
                evm_trace(evm, OpStart(op))
246
                op_implementation[op](evm)
247
                evm_trace(evm, OpEnd())
248
249
            evm_trace(evm, EvmStop(Ops.STOP))
250
251
    except ExceptionalHalt as error:
252
        evm_trace(evm, OpException(error))
253
        evm.gas_left = Uint(0)
254
        evm.error = error
255
256
    if evm.error:
257
        restore_tx_state(tx_state, snapshot)
258
    return evm