ethereum.forks.frontier.vm.instructions.system

Ethereum Virtual Machine (EVM) System Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM system related instructions.

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
45
    """
46
    Creates a new account with associated code.
47
48
    Parameters
49
    ----------
50
    evm :
51
        The current EVM frame.
52
53
    """
54
    # This import causes a circular import error
55
    # if it's not moved inside this method
56
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
57
58
    # STACK
59
    endowment = pop(evm.stack)
60
    memory_start_position = pop(evm.stack)
61
    memory_size = pop(evm.stack)
62
63
    # GAS
64
    extend_memory = calculate_gas_extend_memory(
65
        evm.memory, [(memory_start_position, memory_size)]
66
    )
67
68
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
69
70
    create_message_gas = evm.gas_left
71
    evm.gas_left = Uint(0)
72
73
    # OPERATION
74
    evm.memory += b"\x00" * extend_memory.expand_by
75
    sender_address = evm.message.current_target
76
    sender = get_account(evm.message.tx_env.state, sender_address)
77
78
    contract_address = compute_contract_address(
79
        evm.message.current_target,
80
        get_account(
81
            evm.message.tx_env.state, evm.message.current_target
82
        ).nonce,
83
    )
84
85
    if (
86
        sender.balance < endowment
87
        or sender.nonce == Uint(2**64 - 1)
88
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
89
    ):
90
        push(evm.stack, U256(0))
91
        evm.gas_left += create_message_gas
92
    elif account_has_code_or_nonce(
93
        evm.message.tx_env.state, contract_address
94
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
95
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
96
        push(evm.stack, U256(0))
97
    else:
98
        call_data = memory_read_bytes(
99
            evm.memory, memory_start_position, memory_size
100
        )
101
102
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
103
104
        child_message = Message(
105
            block_env=evm.message.block_env,
106
            tx_env=evm.message.tx_env,
107
            caller=evm.message.current_target,
108
            target=Bytes0(),
109
            gas=create_message_gas,
110
            value=endowment,
111
            data=b"",
112
            code=call_data,
113
            current_target=contract_address,
114
            depth=evm.message.depth + Uint(1),
115
            code_address=None,
116
            parent_evm=evm,
117
        )
118
        child_evm = process_create_message(child_message)
119
120
        if child_evm.error:
121
            incorporate_child_on_error(evm, child_evm)
122
            push(evm.stack, U256(0))
123
        else:
124
            incorporate_child_on_success(evm, child_evm)
125
            push(
126
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
127
            )
128
129
    # PROGRAM COUNTER
130
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
134
    """
135
    Halts execution returning output data.
136
137
    Parameters
138
    ----------
139
    evm :
140
        The current EVM frame.
141
142
    """
143
    # STACK
144
    memory_start_position = pop(evm.stack)
145
    memory_size = pop(evm.stack)
146
147
    # GAS
148
    extend_memory = calculate_gas_extend_memory(
149
        evm.memory, [(memory_start_position, memory_size)]
150
    )
151
152
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
153
154
    # OPERATION
155
    evm.memory += b"\x00" * extend_memory.expand_by
156
    evm.output = memory_read_bytes(
157
        evm.memory, memory_start_position, memory_size
158
    )
159
160
    evm.running = False
161
162
    # PROGRAM COUNTER
163
    pass

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​gas: Uint, ​​value: U256, ​​caller: Address, ​​to: Address, ​​code_address: Address, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
178
    """
179
    Perform the core logic of the `CALL*` family of opcodes.
180
    """
181
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
182
183
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
184
        evm.gas_left += gas
185
        push(evm.stack, U256(0))
186
        return
187
188
    call_data = memory_read_bytes(
189
        evm.memory, memory_input_start_position, memory_input_size
190
    )
191
    account_to_call = get_account(evm.message.tx_env.state, code_address)
192
    code = get_code(evm.message.tx_env.state, account_to_call.code_hash)
193
    child_message = Message(
194
        block_env=evm.message.block_env,
195
        tx_env=evm.message.tx_env,
196
        caller=caller,
197
        target=to,
198
        gas=gas,
199
        value=value,
200
        data=call_data,
201
        code=code,
202
        current_target=to,
203
        depth=evm.message.depth + Uint(1),
204
        code_address=code_address,
205
        parent_evm=evm,
206
    )
207
    child_evm = process_message(child_message)
208
209
    if child_evm.error:
210
        incorporate_child_on_error(evm, child_evm)
211
        push(evm.stack, U256(0))
212
    else:
213
        incorporate_child_on_success(evm, child_evm)
214
        push(evm.stack, U256(1))
215
216
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
217
    memory_write(
218
        evm.memory,
219
        memory_output_start_position,
220
        child_evm.output[:actual_output_size],
221
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
225
    """
226
    Message-call into an account.
227
228
    Parameters
229
    ----------
230
    evm :
231
        The current EVM frame.
232
233
    """
234
    # STACK
235
    gas = Uint(pop(evm.stack))
236
    to = to_address_masked(pop(evm.stack))
237
    value = pop(evm.stack)
238
    memory_input_start_position = pop(evm.stack)
239
    memory_input_size = pop(evm.stack)
240
    memory_output_start_position = pop(evm.stack)
241
    memory_output_size = pop(evm.stack)
242
243
    # GAS
244
    extend_memory = calculate_gas_extend_memory(
245
        evm.memory,
246
        [
247
            (memory_input_start_position, memory_input_size),
248
            (memory_output_start_position, memory_output_size),
249
        ],
250
    )
251
252
    code_address = to
253
254
    message_call_gas = calculate_message_call_gas(
255
        evm.message.tx_env.state, gas, to, value
256
    )
257
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
258
259
    # OPERATION
260
    evm.memory += b"\x00" * extend_memory.expand_by
261
    sender_balance = get_account(
262
        evm.message.tx_env.state, evm.message.current_target
263
    ).balance
264
    if sender_balance < value:
265
        push(evm.stack, U256(0))
266
        evm.gas_left += message_call_gas.sub_call
267
    else:
268
        generic_call(
269
            evm,
270
            message_call_gas.sub_call,
271
            value,
272
            evm.message.current_target,
273
            to,
274
            code_address,
275
            memory_input_start_position,
276
            memory_input_size,
277
            memory_output_start_position,
278
            memory_output_size,
279
        )
280
281
    # PROGRAM COUNTER
282
    evm.pc += Uint(1)

callcode

Message-call into this account with alternative account’s code.

Parameters

evm : The current EVM frame.

def callcode(evm: Evm) -> None:
286
    """
287
    Message-call into this account with alternative account’s code.
288
289
    Parameters
290
    ----------
291
    evm :
292
        The current EVM frame.
293
294
    """
295
    # STACK
296
    gas = Uint(pop(evm.stack))
297
    code_address = to_address_masked(pop(evm.stack))
298
    value = pop(evm.stack)
299
    memory_input_start_position = pop(evm.stack)
300
    memory_input_size = pop(evm.stack)
301
    memory_output_start_position = pop(evm.stack)
302
    memory_output_size = pop(evm.stack)
303
304
    # GAS
305
    to = evm.message.current_target
306
307
    extend_memory = calculate_gas_extend_memory(
308
        evm.memory,
309
        [
310
            (memory_input_start_position, memory_input_size),
311
            (memory_output_start_position, memory_output_size),
312
        ],
313
    )
314
    message_call_gas = calculate_message_call_gas(
315
        evm.message.tx_env.state, gas, to, value
316
    )
317
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
318
319
    # OPERATION
320
    evm.memory += b"\x00" * extend_memory.expand_by
321
    sender_balance = get_account(
322
        evm.message.tx_env.state, evm.message.current_target
323
    ).balance
324
    if sender_balance < value:
325
        push(evm.stack, U256(0))
326
        evm.gas_left += message_call_gas.sub_call
327
    else:
328
        generic_call(
329
            evm,
330
            message_call_gas.sub_call,
331
            value,
332
            evm.message.current_target,
333
            to,
334
            code_address,
335
            memory_input_start_position,
336
            memory_input_size,
337
            memory_output_start_position,
338
            memory_output_size,
339
        )
340
341
    # PROGRAM COUNTER
342
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
346
    """
347
    Halt execution and register account for later deletion.
348
349
    Parameters
350
    ----------
351
    evm :
352
        The current EVM frame.
353
354
    """
355
    # STACK
356
    beneficiary = to_address_masked(pop(evm.stack))
357
358
    # GAS
359
    gas_cost = GasCosts.ZERO
360
361
    originator = evm.message.current_target
362
363
    refunded_accounts = evm.accounts_to_delete
364
    parent_evm = evm.message.parent_evm
365
    while parent_evm is not None:
366
        refunded_accounts.update(parent_evm.accounts_to_delete)
367
        parent_evm = parent_evm.message.parent_evm
368
369
    if originator not in refunded_accounts:
370
        evm.refund_counter += int(GasCosts.REFUND_SELF_DESTRUCT)
371
372
    charge_gas(evm, gas_cost)
373
374
    # OPERATION
375
    beneficiary_balance = get_account(
376
        evm.message.tx_env.state, beneficiary
377
    ).balance
378
    originator_balance = get_account(
379
        evm.message.tx_env.state, originator
380
    ).balance
381
382
    # First Transfer to beneficiary
383
    set_account_balance(
384
        evm.message.tx_env.state,
385
        beneficiary,
386
        beneficiary_balance + originator_balance,
387
    )
388
    # Next, Zero the balance of the address being deleted (must come after
389
    # sending to beneficiary in case the contract named itself as the
390
    # beneficiary).
391
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
392
393
    # register account for deletion
394
    evm.accounts_to_delete.add(originator)
395
396
    # HALT the execution
397
    evm.running = False
398
399
    # PROGRAM COUNTER
400
    pass