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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
142
    """
143
    Halts execution returning output data.
144
145
    Parameters
146
    ----------
147
    evm :
148
        The current EVM frame.
149
150
    """
151
    # STACK
152
    memory_start_position = pop(evm.stack)
153
    memory_size = pop(evm.stack)
154
155
    # GAS
156
    extend_memory = calculate_gas_extend_memory(
157
        evm.memory, [(memory_start_position, memory_size)]
158
    )
159
160
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
161
162
    # OPERATION
163
    evm.memory += b"\x00" * extend_memory.expand_by
164
    evm.output = memory_read_bytes(
165
        evm.memory, memory_start_position, memory_size
166
    )
167
168
    evm.running = False
169
170
    # PROGRAM COUNTER
171
    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, ​​should_transfer_value: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
187
    """
188
    Perform the core logic of the `CALL*` family of opcodes.
189
    """
190
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
191
192
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
193
        evm.gas_left += gas
194
        push(evm.stack, U256(0))
195
        return
196
197
    call_data = memory_read_bytes(
198
        evm.memory, memory_input_start_position, memory_input_size
199
    )
200
    account = get_account(evm.message.block_env.state, code_address)
201
    code = get_code(evm.message.block_env.state, account.code_hash)
202
    child_message = Message(
203
        block_env=evm.message.block_env,
204
        tx_env=evm.message.tx_env,
205
        caller=caller,
206
        target=to,
207
        gas=gas,
208
        value=value,
209
        data=call_data,
210
        code=code,
211
        current_target=to,
212
        depth=evm.message.depth + Uint(1),
213
        code_address=code_address,
214
        should_transfer_value=should_transfer_value,
215
        parent_evm=evm,
216
    )
217
    child_evm = process_message(child_message)
218
219
    if child_evm.error:
220
        incorporate_child_on_error(evm, child_evm)
221
        push(evm.stack, U256(0))
222
    else:
223
        incorporate_child_on_success(evm, child_evm)
224
        push(evm.stack, U256(1))
225
226
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
227
    memory_write(
228
        evm.memory,
229
        memory_output_start_position,
230
        child_evm.output[:actual_output_size],
231
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
235
    """
236
    Message-call into an account.
237
238
    Parameters
239
    ----------
240
    evm :
241
        The current EVM frame.
242
243
    """
244
    # STACK
245
    gas = Uint(pop(evm.stack))
246
    to = to_address_masked(pop(evm.stack))
247
    value = pop(evm.stack)
248
    memory_input_start_position = pop(evm.stack)
249
    memory_input_size = pop(evm.stack)
250
    memory_output_start_position = pop(evm.stack)
251
    memory_output_size = pop(evm.stack)
252
253
    # GAS
254
    extend_memory = calculate_gas_extend_memory(
255
        evm.memory,
256
        [
257
            (memory_input_start_position, memory_input_size),
258
            (memory_output_start_position, memory_output_size),
259
        ],
260
    )
261
262
    code_address = to
263
264
    create_gas_cost = GasCosts.NEW_ACCOUNT
265
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
266
        create_gas_cost = Uint(0)
267
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
268
    message_call_gas = calculate_message_call_gas(
269
        value,
270
        gas,
271
        Uint(evm.gas_left),
272
        extend_memory.cost,
273
        GasCosts.OPCODE_CALL_BASE + create_gas_cost + transfer_gas_cost,
274
    )
275
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
276
277
    # OPERATION
278
    evm.memory += b"\x00" * extend_memory.expand_by
279
    sender_balance = get_account(
280
        evm.message.block_env.state, evm.message.current_target
281
    ).balance
282
    if sender_balance < value:
283
        push(evm.stack, U256(0))
284
        evm.gas_left += message_call_gas.sub_call
285
    else:
286
        generic_call(
287
            evm,
288
            message_call_gas.sub_call,
289
            value,
290
            evm.message.current_target,
291
            to,
292
            code_address,
293
            True,
294
            memory_input_start_position,
295
            memory_input_size,
296
            memory_output_start_position,
297
            memory_output_size,
298
        )
299
300
    # PROGRAM COUNTER
301
    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:
305
    """
306
    Message-call into this account with alternative account’s code.
307
308
    Parameters
309
    ----------
310
    evm :
311
        The current EVM frame.
312
313
    """
314
    # STACK
315
    gas = Uint(pop(evm.stack))
316
    code_address = to_address_masked(pop(evm.stack))
317
    value = pop(evm.stack)
318
    memory_input_start_position = pop(evm.stack)
319
    memory_input_size = pop(evm.stack)
320
    memory_output_start_position = pop(evm.stack)
321
    memory_output_size = pop(evm.stack)
322
323
    # GAS
324
    to = evm.message.current_target
325
326
    extend_memory = calculate_gas_extend_memory(
327
        evm.memory,
328
        [
329
            (memory_input_start_position, memory_input_size),
330
            (memory_output_start_position, memory_output_size),
331
        ],
332
    )
333
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
334
    message_call_gas = calculate_message_call_gas(
335
        value,
336
        gas,
337
        Uint(evm.gas_left),
338
        extend_memory.cost,
339
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
340
    )
341
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
342
343
    # OPERATION
344
    evm.memory += b"\x00" * extend_memory.expand_by
345
    sender_balance = get_account(
346
        evm.message.block_env.state, evm.message.current_target
347
    ).balance
348
    if sender_balance < value:
349
        push(evm.stack, U256(0))
350
        evm.gas_left += message_call_gas.sub_call
351
    else:
352
        generic_call(
353
            evm,
354
            message_call_gas.sub_call,
355
            value,
356
            evm.message.current_target,
357
            to,
358
            code_address,
359
            True,
360
            memory_input_start_position,
361
            memory_input_size,
362
            memory_output_start_position,
363
            memory_output_size,
364
        )
365
366
    # PROGRAM COUNTER
367
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
371
    """
372
    Halt execution and register account for later deletion.
373
374
    Parameters
375
    ----------
376
    evm :
377
        The current EVM frame.
378
379
    """
380
    # STACK
381
    beneficiary = to_address_masked(pop(evm.stack))
382
383
    # GAS
384
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
385
    if (
386
        not is_account_alive(evm.message.block_env.state, beneficiary)
387
        and get_account(
388
            evm.message.block_env.state, evm.message.current_target
389
        ).balance
390
        != 0
391
    ):
392
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
393
394
    originator = evm.message.current_target
395
396
    refunded_accounts = evm.accounts_to_delete
397
    parent_evm = evm.message.parent_evm
398
    while parent_evm is not None:
399
        refunded_accounts.update(parent_evm.accounts_to_delete)
400
        parent_evm = parent_evm.message.parent_evm
401
402
    if originator not in refunded_accounts:
403
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
404
405
    charge_gas(evm, gas_cost)
406
407
    beneficiary_balance = get_account(
408
        evm.message.block_env.state, beneficiary
409
    ).balance
410
    originator_balance = get_account(
411
        evm.message.block_env.state, originator
412
    ).balance
413
414
    # First Transfer to beneficiary
415
    set_account_balance(
416
        evm.message.block_env.state,
417
        beneficiary,
418
        beneficiary_balance + originator_balance,
419
    )
420
    # Next, Zero the balance of the address being deleted (must come after
421
    # sending to beneficiary in case the contract named itself as the
422
    # beneficiary).
423
    set_account_balance(evm.message.block_env.state, originator, U256(0))
424
425
    # register account for deletion
426
    evm.accounts_to_delete.add(originator)
427
428
    # mark beneficiary as touched
429
    if account_exists_and_is_empty(evm.message.block_env.state, beneficiary):
430
        evm.touched_accounts.add(beneficiary)
431
432
    # HALT the execution
433
    evm.running = False
434
435
    # PROGRAM COUNTER
436
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
440
    """
441
    Message-call into an account.
442
443
    Parameters
444
    ----------
445
    evm :
446
        The current EVM frame.
447
448
    """
449
    # STACK
450
    gas = Uint(pop(evm.stack))
451
    code_address = to_address_masked(pop(evm.stack))
452
    memory_input_start_position = pop(evm.stack)
453
    memory_input_size = pop(evm.stack)
454
    memory_output_start_position = pop(evm.stack)
455
    memory_output_size = pop(evm.stack)
456
457
    # GAS
458
    extend_memory = calculate_gas_extend_memory(
459
        evm.memory,
460
        [
461
            (memory_input_start_position, memory_input_size),
462
            (memory_output_start_position, memory_output_size),
463
        ],
464
    )
465
    message_call_gas = calculate_message_call_gas(
466
        U256(0),
467
        gas,
468
        Uint(evm.gas_left),
469
        extend_memory.cost,
470
        GasCosts.OPCODE_CALL_BASE,
471
    )
472
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
473
474
    # OPERATION
475
    evm.memory += b"\x00" * extend_memory.expand_by
476
    generic_call(
477
        evm,
478
        message_call_gas.sub_call,
479
        evm.message.value,
480
        evm.message.caller,
481
        evm.message.current_target,
482
        code_address,
483
        False,
484
        memory_input_start_position,
485
        memory_input_size,
486
        memory_output_start_position,
487
        memory_output_size,
488
    )
489
490
    # PROGRAM COUNTER
491
    evm.pc += Uint(1)