ethereum.cancun.vm.instructions.systemethereum.prague.vm.instructions.system

Ethereum Virtual Machine (EVM) System Instructions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

Introduction

Implementations of the EVM system related instructions.

generic_create

Core logic used by the CREATE* family of opcodes.

def generic_create(evm: Evm, ​​endowment: U256, ​​contract_address: Address, ​​memory_start_position: U256, ​​memory_size: U256) -> None:
70
    """
71
    Core logic used by the `CREATE*` family of opcodes.
72
    """
73
    # This import causes a circular import error
74
    # if it's not moved inside this method
75
    from ...vm.interpreter import (
76
        MAX_INIT_CODE_SIZE,
77
        STACK_DEPTH_LIMIT,
78
        process_create_message,
79
    )
80
81
    call_data = memory_read_bytes(
82
        evm.memory, memory_start_position, memory_size
83
    )
84
    if len(call_data) > MAX_INIT_CODE_SIZE:
85
        raise OutOfGasError
86
87
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
88
    evm.gas_left -= create_message_gas
89
    if evm.message.is_static:
90
        raise WriteInStaticContext
91
    evm.return_data = b""
92
93
    sender_address = evm.message.current_target
94
    sender = get_account(evm.message.block_env.state, sender_address)
95
96
    if (
97
        sender.balance < endowment
98
        or sender.nonce == Uint(2**64 - 1)
99
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
100
    ):
101
        evm.gas_left += create_message_gas
102
        push(evm.stack, U256(0))
103
        return
104
105
    evm.accessed_addresses.add(contract_address)
106
107
    if account_has_code_or_nonce(
108
        evm.message.block_env.state, contract_address
109
    ) or account_has_storage(evm.message.block_env.state, contract_address):
110
        increment_nonce(
111
            evm.message.block_env.state, evm.message.current_target
112
        )
113
        push(evm.stack, U256(0))
114
        return
115
116
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
117
118
    child_message = Message(
119
        block_env=evm.message.block_env,
120
        tx_env=evm.message.tx_env,
121
        caller=evm.message.current_target,
122
        target=Bytes0(),
123
        gas=create_message_gas,
124
        value=endowment,
125
        data=b"",
126
        code=call_data,
127
        current_target=contract_address,
128
        depth=evm.message.depth + Uint(1),
129
        code_address=None,
130
        should_transfer_value=True,
131
        is_static=False,
132
        accessed_addresses=evm.accessed_addresses.copy(),
133
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
134
        disable_precompiles=False,
135
        parent_evm=evm,
136
    )
137
    child_evm = process_create_message(child_message)
138
139
    if child_evm.error:
140
        incorporate_child_on_error(evm, child_evm)
141
        evm.return_data = child_evm.output
142
        push(evm.stack, U256(0))
143
    else:
144
        incorporate_child_on_success(evm, child_evm)
145
        evm.return_data = b""
146
        push(evm.stack, U256.from_be_bytes(child_evm.message.current_target))

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
150
    """
151
    Creates a new account with associated code.
152
153
    Parameters
154
    ----------
155
    evm :
156
        The current EVM frame.
157
    """
158
    # STACK
159
    endowment = pop(evm.stack)
160
    memory_start_position = pop(evm.stack)
161
    memory_size = pop(evm.stack)
162
163
    # GAS
164
    extend_memory = calculate_gas_extend_memory(
165
        evm.memory, [(memory_start_position, memory_size)]
166
    )
167
    init_code_gas = init_code_cost(Uint(memory_size))
168
169
    charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas)
170
171
    # OPERATION
172
    evm.memory += b"\x00" * extend_memory.expand_by
173
    contract_address = compute_contract_address(
174
        evm.message.current_target,
175
        get_account(
176
            evm.message.block_env.state, evm.message.current_target
177
        ).nonce,
178
    )
179
180
    generic_create(
181
        evm,
182
        endowment,
183
        contract_address,
184
        memory_start_position,
185
        memory_size,
186
    )
187
188
    # PROGRAM COUNTER
189
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

It's similar to CREATE opcode except that the address of new account depends on the init_code instead of the nonce of sender.

Parameters

evm : The current EVM frame.

def create2(evm: Evm) -> None:
193
    """
194
    Creates a new account with associated code.
195
196
    It's similar to CREATE opcode except that the address of new account
197
    depends on the init_code instead of the nonce of sender.
198
199
    Parameters
200
    ----------
201
    evm :
202
        The current EVM frame.
203
    """
204
    # STACK
205
    endowment = pop(evm.stack)
206
    memory_start_position = pop(evm.stack)
207
    memory_size = pop(evm.stack)
208
    salt = pop(evm.stack).to_be_bytes32()
209
210
    # GAS
211
    extend_memory = calculate_gas_extend_memory(
212
        evm.memory, [(memory_start_position, memory_size)]
213
    )
214
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
215
    init_code_gas = init_code_cost(Uint(memory_size))
216
    charge_gas(
217
        evm,
218
        GAS_CREATE
219
        + GAS_KECCAK256_WORD * call_data_words
220
        + extend_memory.cost
221
        + init_code_gas,
222
    )
223
224
    # OPERATION
225
    evm.memory += b"\x00" * extend_memory.expand_by
226
    contract_address = compute_create2_contract_address(
227
        evm.message.current_target,
228
        salt,
229
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
230
    )
231
232
    generic_create(
233
        evm,
234
        endowment,
235
        contract_address,
236
        memory_start_position,
237
        memory_size,
238
    )
239
240
    # PROGRAM COUNTER
241
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
245
    """
246
    Halts execution returning output data.
247
248
    Parameters
249
    ----------
250
    evm :
251
        The current EVM frame.
252
    """
253
    # STACK
254
    memory_start_position = pop(evm.stack)
255
    memory_size = pop(evm.stack)
256
257
    # GAS
258
    extend_memory = calculate_gas_extend_memory(
259
        evm.memory, [(memory_start_position, memory_size)]
260
    )
261
262
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
263
264
    # OPERATION
265
    evm.memory += b"\x00" * extend_memory.expand_by
266
    evm.output = memory_read_bytes(
267
        evm.memory, memory_start_position, memory_size
268
    )
269
270
    evm.running = False
271
272
    # PROGRAM COUNTER
273
    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, ​​is_staticcall: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256, ​​code: Bytesdisable_precompiles: bool) -> None:
292
    """
293
    Perform the core logic of the `CALL*` family of opcodes.
294
    """
295
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
296
297
    evm.return_data = b""
298
299
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
300
        evm.gas_left += gas
301
        push(evm.stack, U256(0))
302
        return
303
304
    call_data = memory_read_bytes(
305
        evm.memory, memory_input_start_position, memory_input_size
306
    )
303
    code = get_account(evm.message.block_env.state, code_address).code
307
308
    child_message = Message(
309
        block_env=evm.message.block_env,
310
        tx_env=evm.message.tx_env,
311
        caller=caller,
312
        target=to,
313
        gas=gas,
314
        value=value,
315
        data=call_data,
316
        code=code,
317
        current_target=to,
318
        depth=evm.message.depth + Uint(1),
319
        code_address=code_address,
320
        should_transfer_value=should_transfer_value,
321
        is_static=True if is_staticcall else evm.message.is_static,
322
        accessed_addresses=evm.accessed_addresses.copy(),
323
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
324
        disable_precompiles=disable_precompiles,
325
        parent_evm=evm,
326
    )
327
    child_evm = process_message(child_message)
328
329
    if child_evm.error:
330
        incorporate_child_on_error(evm, child_evm)
331
        evm.return_data = child_evm.output
332
        push(evm.stack, U256(0))
333
    else:
334
        incorporate_child_on_success(evm, child_evm)
335
        evm.return_data = child_evm.output
336
        push(evm.stack, U256(1))
337
338
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
339
    memory_write(
340
        evm.memory,
341
        memory_output_start_position,
342
        child_evm.output[:actual_output_size],
343
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
347
    """
348
    Message-call into an account.
349
350
    Parameters
351
    ----------
352
    evm :
353
        The current EVM frame.
354
    """
355
    # STACK
356
    gas = Uint(pop(evm.stack))
357
    to = to_address_masked(pop(evm.stack))
358
    value = pop(evm.stack)
359
    memory_input_start_position = pop(evm.stack)
360
    memory_input_size = pop(evm.stack)
361
    memory_output_start_position = pop(evm.stack)
362
    memory_output_size = pop(evm.stack)
363
364
    # GAS
365
    extend_memory = calculate_gas_extend_memory(
366
        evm.memory,
367
        [
368
            (memory_input_start_position, memory_input_size),
369
            (memory_output_start_position, memory_output_size),
370
        ],
371
    )
372
373
    if to in evm.accessed_addresses:
374
        access_gas_cost = GAS_WARM_ACCESS
375
    else:
376
        evm.accessed_addresses.add(to)
377
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
378
379
    code_address = to
380
    (
381
        disable_precompiles,
382
        code_address,
383
        code,
384
        delegated_access_gas_cost,
385
    ) = access_delegation(evm, code_address)
386
    access_gas_cost += delegated_access_gas_cost
387
388
    create_gas_cost = GAS_NEW_ACCOUNT
389
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
390
        create_gas_cost = Uint(0)
391
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
392
    message_call_gas = calculate_message_call_gas(
393
        value,
394
        gas,
395
        Uint(evm.gas_left),
396
        extend_memory.cost,
397
        access_gas_cost + create_gas_cost + transfer_gas_cost,
398
    )
399
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
400
    if evm.message.is_static and value != U256(0):
401
        raise WriteInStaticContext
402
    evm.memory += b"\x00" * extend_memory.expand_by
403
    sender_balance = get_account(
404
        evm.message.block_env.state, evm.message.current_target
405
    ).balance
406
    if sender_balance < value:
407
        push(evm.stack, U256(0))
408
        evm.return_data = b""
409
        evm.gas_left += message_call_gas.sub_call
410
    else:
411
        generic_call(
412
            evm,
413
            message_call_gas.sub_call,
414
            value,
415
            evm.message.current_target,
416
            to,
417
            code_address,
418
            True,
419
            False,
420
            memory_input_start_position,
421
            memory_input_size,
422
            memory_output_start_position,
423
            memory_output_size,
424
            code,
425
            disable_precompiles,
426
        )
427
428
    # PROGRAM COUNTER
429
    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:
433
    """
434
    Message-call into this account with alternative account’s code.
435
436
    Parameters
437
    ----------
438
    evm :
439
        The current EVM frame.
440
    """
441
    # STACK
442
    gas = Uint(pop(evm.stack))
443
    code_address = to_address_masked(pop(evm.stack))
444
    value = pop(evm.stack)
445
    memory_input_start_position = pop(evm.stack)
446
    memory_input_size = pop(evm.stack)
447
    memory_output_start_position = pop(evm.stack)
448
    memory_output_size = pop(evm.stack)
449
450
    # GAS
451
    to = evm.message.current_target
452
453
    extend_memory = calculate_gas_extend_memory(
454
        evm.memory,
455
        [
456
            (memory_input_start_position, memory_input_size),
457
            (memory_output_start_position, memory_output_size),
458
        ],
459
    )
460
461
    if code_address in evm.accessed_addresses:
462
        access_gas_cost = GAS_WARM_ACCESS
463
    else:
464
        evm.accessed_addresses.add(code_address)
465
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
466
467
    (
468
        disable_precompiles,
469
        code_address,
470
        code,
471
        delegated_access_gas_cost,
472
    ) = access_delegation(evm, code_address)
473
    access_gas_cost += delegated_access_gas_cost
474
475
    transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE
476
    message_call_gas = calculate_message_call_gas(
477
        value,
478
        gas,
479
        Uint(evm.gas_left),
480
        extend_memory.cost,
481
        access_gas_cost + transfer_gas_cost,
482
    )
483
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
484
485
    # OPERATION
486
    evm.memory += b"\x00" * extend_memory.expand_by
487
    sender_balance = get_account(
488
        evm.message.block_env.state, evm.message.current_target
489
    ).balance
490
    if sender_balance < value:
491
        push(evm.stack, U256(0))
492
        evm.return_data = b""
493
        evm.gas_left += message_call_gas.sub_call
494
    else:
495
        generic_call(
496
            evm,
497
            message_call_gas.sub_call,
498
            value,
499
            evm.message.current_target,
500
            to,
501
            code_address,
502
            True,
503
            False,
504
            memory_input_start_position,
505
            memory_input_size,
506
            memory_output_start_position,
507
            memory_output_size,
508
            code,
509
            disable_precompiles,
510
        )
511
512
    # PROGRAM COUNTER
513
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
517
    """
518
    Halt execution and register account for later deletion.
519
520
    Parameters
521
    ----------
522
    evm :
523
        The current EVM frame.
524
    """
525
    # STACK
526
    beneficiary = to_address_masked(pop(evm.stack))
527
528
    # GAS
529
    gas_cost = GAS_SELF_DESTRUCT
530
    if beneficiary not in evm.accessed_addresses:
531
        evm.accessed_addresses.add(beneficiary)
532
        gas_cost += GAS_COLD_ACCOUNT_ACCESS
533
534
    if (
535
        not is_account_alive(evm.message.block_env.state, beneficiary)
536
        and get_account(
537
            evm.message.block_env.state, evm.message.current_target
538
        ).balance
539
        != 0
540
    ):
541
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
542
543
    charge_gas(evm, gas_cost)
544
    if evm.message.is_static:
545
        raise WriteInStaticContext
546
547
    originator = evm.message.current_target
548
    originator_balance = get_account(
549
        evm.message.block_env.state, originator
550
    ).balance
551
552
    move_ether(
553
        evm.message.block_env.state,
554
        originator,
555
        beneficiary,
556
        originator_balance,
557
    )
558
559
    # register account for deletion only if it was created
560
    # in the same transaction
561
    if originator in evm.message.block_env.state.created_accounts:
562
        # If beneficiary is the same as originator, then
563
        # the ether is burnt.
564
        set_account_balance(evm.message.block_env.state, originator, U256(0))
565
        evm.accounts_to_delete.add(originator)
566
567
    # HALT the execution
568
    evm.running = False
569
570
    # PROGRAM COUNTER
571
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
575
    """
576
    Message-call into an account.
577
578
    Parameters
579
    ----------
580
    evm :
581
        The current EVM frame.
582
    """
583
    # STACK
584
    gas = Uint(pop(evm.stack))
585
    code_address = to_address_masked(pop(evm.stack))
586
    memory_input_start_position = pop(evm.stack)
587
    memory_input_size = pop(evm.stack)
588
    memory_output_start_position = pop(evm.stack)
589
    memory_output_size = pop(evm.stack)
590
591
    # GAS
592
    extend_memory = calculate_gas_extend_memory(
593
        evm.memory,
594
        [
595
            (memory_input_start_position, memory_input_size),
596
            (memory_output_start_position, memory_output_size),
597
        ],
598
    )
599
600
    if code_address in evm.accessed_addresses:
601
        access_gas_cost = GAS_WARM_ACCESS
602
    else:
603
        evm.accessed_addresses.add(code_address)
604
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
605
606
    (
607
        disable_precompiles,
608
        code_address,
609
        code,
610
        delegated_access_gas_cost,
611
    ) = access_delegation(evm, code_address)
612
    access_gas_cost += delegated_access_gas_cost
613
614
    message_call_gas = calculate_message_call_gas(
615
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
616
    )
617
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
618
619
    # OPERATION
620
    evm.memory += b"\x00" * extend_memory.expand_by
621
    generic_call(
622
        evm,
623
        message_call_gas.sub_call,
624
        evm.message.value,
625
        evm.message.caller,
626
        evm.message.current_target,
627
        code_address,
628
        False,
629
        False,
630
        memory_input_start_position,
631
        memory_input_size,
632
        memory_output_start_position,
633
        memory_output_size,
634
        code,
635
        disable_precompiles,
636
    )
637
638
    # PROGRAM COUNTER
639
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
643
    """
644
    Message-call into an account.
645
646
    Parameters
647
    ----------
648
    evm :
649
        The current EVM frame.
650
    """
651
    # STACK
652
    gas = Uint(pop(evm.stack))
653
    to = to_address_masked(pop(evm.stack))
654
    memory_input_start_position = pop(evm.stack)
655
    memory_input_size = pop(evm.stack)
656
    memory_output_start_position = pop(evm.stack)
657
    memory_output_size = pop(evm.stack)
658
659
    # GAS
660
    extend_memory = calculate_gas_extend_memory(
661
        evm.memory,
662
        [
663
            (memory_input_start_position, memory_input_size),
664
            (memory_output_start_position, memory_output_size),
665
        ],
666
    )
667
668
    if to in evm.accessed_addresses:
669
        access_gas_cost = GAS_WARM_ACCESS
670
    else:
671
        evm.accessed_addresses.add(to)
672
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
673
674
    code_address = to
675
    (
676
        disable_precompiles,
677
        code_address,
678
        code,
679
        delegated_access_gas_cost,
680
    ) = access_delegation(evm, code_address)
681
    access_gas_cost += delegated_access_gas_cost
682
683
    message_call_gas = calculate_message_call_gas(
684
        U256(0),
685
        gas,
686
        Uint(evm.gas_left),
687
        extend_memory.cost,
688
        access_gas_cost,
689
    )
690
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
691
692
    # OPERATION
693
    evm.memory += b"\x00" * extend_memory.expand_by
694
    generic_call(
695
        evm,
696
        message_call_gas.sub_call,
697
        U256(0),
698
        evm.message.current_target,
699
        to,
700
        code_address,
701
        True,
702
        True,
703
        memory_input_start_position,
704
        memory_input_size,
705
        memory_output_start_position,
706
        memory_output_size,
707
        code,
708
        disable_precompiles,
709
    )
710
711
    # PROGRAM COUNTER
712
    evm.pc += Uint(1)

revert

Stop execution and revert state changes, without consuming all provided gas and also has the ability to return a reason Parameters

evm : The current EVM frame.

def revert(evm: Evm) -> None:
716
    """
717
    Stop execution and revert state changes, without consuming all provided gas
718
    and also has the ability to return a reason
719
    Parameters
720
    ----------
721
    evm :
722
        The current EVM frame.
723
    """
724
    # STACK
725
    memory_start_index = pop(evm.stack)
726
    size = pop(evm.stack)
727
728
    # GAS
729
    extend_memory = calculate_gas_extend_memory(
730
        evm.memory, [(memory_start_index, size)]
731
    )
732
733
    charge_gas(evm, extend_memory.cost)
734
735
    # OPERATION
736
    evm.memory += b"\x00" * extend_memory.expand_by
737
    output = memory_read_bytes(evm.memory, memory_start_index, size)
738
    evm.output = Bytes(output)
739
    raise Revert