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

62
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

63
MAX_CODE_SIZE = 0x6000

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. `touched_accounts`: Accounts that have been touched.
  6. `error`: The error from the execution if any.
66
@dataclass
class MessageCallOutput:

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Tuple[Log, ...]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

touched_accounts

85
    touched_accounts: Set[Address]

error

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

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

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

def process_create_message(message: Message) -> Evm:
155
    """
156
    Executes a call to create a smart contract.
157
158
    Parameters
159
    ----------
160
    message :
161
        Transaction specific items.
162
163
    Returns
164
    -------
165
    evm: :py:class:`~ethereum.forks.berlin.vm.Evm`
166
        Items containing execution specific objects.
167
168
    """
169
    state = message.block_env.state
170
    # take snapshot of state before processing the message
171
    begin_transaction(state)
172
173
    # The list of created accounts is used by `get_storage_original`.
174
    mark_account_created(state, message.current_target)
175
176
    increment_nonce(state, message.current_target)
177
    evm = process_message(message)
178
    if not evm.error:
179
        contract_code = evm.output
180
        contract_code_gas = (
181
            Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE
182
        )
183
        try:
184
            charge_gas(evm, contract_code_gas)
185
            if len(contract_code) > MAX_CODE_SIZE:
186
                raise OutOfGasError
187
        except ExceptionalHalt as error:
188
            rollback_transaction(state)
189
            evm.gas_left = Uint(0)
190
            evm.output = b""
191
            evm.error = error
192
        else:
193
            set_code(state, message.current_target, contract_code)
194
            commit_transaction(state)
195
    else:
196
        rollback_transaction(state)
197
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

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

def process_message(message: Message) -> Evm:
201
    """
202
    Move ether and execute the relevant code.
203
204
    Parameters
205
    ----------
206
    message :
207
        Transaction specific items.
208
209
    Returns
210
    -------
211
    evm: :py:class:`~ethereum.forks.berlin.vm.Evm`
212
        Items containing execution specific objects
213
214
    """
215
    state = message.block_env.state
216
    if message.depth > STACK_DEPTH_LIMIT:
217
        raise StackDepthLimitError("Stack depth limit reached")
218
219
    code = message.code
220
    valid_jump_destinations = get_valid_jump_destinations(code)
221
    evm = Evm(
222
        pc=Uint(0),
223
        stack=[],
224
        memory=bytearray(),
225
        code=code,
226
        gas_left=message.gas,
227
        valid_jump_destinations=valid_jump_destinations,
228
        logs=(),
229
        refund_counter=0,
230
        running=True,
231
        message=message,
232
        output=b"",
233
        accounts_to_delete=set(),
234
        touched_accounts=set(),
235
        return_data=b"",
236
        error=None,
237
        accessed_addresses=message.accessed_addresses,
238
        accessed_storage_keys=message.accessed_storage_keys,
239
    )
240
241
    # take snapshot of state before processing the message
242
    begin_transaction(state)
243
244
    touch_account(state, message.current_target)
245
246
    if message.should_transfer_value and message.value != 0:
247
        move_ether(
248
            state, message.caller, message.current_target, message.value
249
        )
250
251
    try:
252
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
253
            evm_trace(evm, PrecompileStart(evm.message.code_address))
254
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
255
            evm_trace(evm, PrecompileEnd())
256
        else:
257
            while evm.running and evm.pc < ulen(evm.code):
258
                try:
259
                    op = Ops(evm.code[evm.pc])
260
                except ValueError as e:
261
                    raise InvalidOpcode(evm.code[evm.pc]) from e
262
263
                evm_trace(evm, OpStart(op))
264
                op_implementation[op](evm)
265
                evm_trace(evm, OpEnd())
266
267
            evm_trace(evm, EvmStop(Ops.STOP))
268
269
    except ExceptionalHalt as error:
270
        evm_trace(evm, OpException(error))
271
        evm.gas_left = Uint(0)
272
        evm.output = b""
273
        evm.error = error
274
    except Revert as error:
275
        evm_trace(evm, OpException(error))
276
        evm.error = error
277
278
    if evm.error:
279
        # revert state to the last saved checkpoint
280
        # since the message call resulted in an error
281
        rollback_transaction(state)
282
    else:
283
        commit_transaction(state)
284
    return evm