ethereum.forks.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:
61
    """
62
    Core logic used by the `CREATE*` family of opcodes.
63
    """
64
    # This import causes a circular import error
65
    # if it's not moved inside this method
66
    from ...vm.interpreter import (
67
        MAX_INIT_CODE_SIZE,
68
        STACK_DEPTH_LIMIT,
69
        process_create_message,
70
    )
71
72
    call_data = memory_read_bytes(
73
        evm.memory, memory_start_position, memory_size
74
    )
75
    if len(call_data) > MAX_INIT_CODE_SIZE:
76
        raise OutOfGasError
77
78
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
79
    evm.gas_left -= create_message_gas
80
    if evm.message.is_static:
81
        raise WriteInStaticContext
82
    evm.return_data = b""
83
84
    sender_address = evm.message.current_target
85
    sender = get_account(evm.message.block_env.state, sender_address)
86
87
    if (
88
        sender.balance < endowment
89
        or sender.nonce == Uint(2**64 - 1)
90
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
91
    ):
92
        evm.gas_left += create_message_gas
93
        push(evm.stack, U256(0))
94
        return
95
96
    evm.accessed_addresses.add(contract_address)
97
98
    if account_has_code_or_nonce(
99
        evm.message.block_env.state, contract_address
100
    ) or account_has_storage(evm.message.block_env.state, contract_address):
101
        increment_nonce(
102
            evm.message.block_env.state, evm.message.current_target
103
        )
104
        push(evm.stack, U256(0))
105
        return
106
107
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
108
109
    child_message = Message(
110
        block_env=evm.message.block_env,
111
        tx_env=evm.message.tx_env,
112
        caller=evm.message.current_target,
113
        target=Bytes0(),
114
        gas=create_message_gas,
115
        value=endowment,
116
        data=b"",
117
        code=call_data,
118
        current_target=contract_address,
119
        depth=evm.message.depth + Uint(1),
120
        code_address=None,
121
        should_transfer_value=True,
122
        is_static=False,
123
        accessed_addresses=evm.accessed_addresses.copy(),
124
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
125
        disable_precompiles=False,
126
        parent_evm=evm,
127
    )
128
    child_evm = process_create_message(child_message)
129
130
    if child_evm.error:
131
        incorporate_child_on_error(evm, child_evm)
132
        evm.return_data = child_evm.output
133
        push(evm.stack, U256(0))
134
    else:
135
        incorporate_child_on_success(evm, child_evm)
136
        evm.return_data = b""
137
        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:
141
    """
142
    Creates a new account with associated code.
143
144
    Parameters
145
    ----------
146
    evm :
147
        The current EVM frame.
148
149
    """
150
    # STACK
151
    endowment = pop(evm.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
    init_code_gas = init_code_cost(Uint(memory_size))
160
161
    charge_gas(
162
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
163
    )
164
165
    # OPERATION
166
    evm.memory += b"\x00" * extend_memory.expand_by
167
    contract_address = compute_contract_address(
168
        evm.message.current_target,
169
        get_account(
170
            evm.message.block_env.state, evm.message.current_target
171
        ).nonce,
172
    )
173
174
    generic_create(
175
        evm,
176
        endowment,
177
        contract_address,
178
        memory_start_position,
179
        memory_size,
180
    )
181
182
    # PROGRAM COUNTER
183
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

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

Parameters

evm : The current EVM frame.

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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
574
    """
575
    Message-call into an account.
576
577
    Parameters
578
    ----------
579
    evm :
580
        The current EVM frame.
581
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 = GasCosts.WARM_ACCESS
602
    else:
603
        evm.accessed_addresses.add(code_address)
604
        access_gas_cost = GasCosts.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
    """
652
    # STACK
653
    gas = Uint(pop(evm.stack))
654
    to = to_address_masked(pop(evm.stack))
655
    memory_input_start_position = pop(evm.stack)
656
    memory_input_size = pop(evm.stack)
657
    memory_output_start_position = pop(evm.stack)
658
    memory_output_size = pop(evm.stack)
659
660
    # GAS
661
    extend_memory = calculate_gas_extend_memory(
662
        evm.memory,
663
        [
664
            (memory_input_start_position, memory_input_size),
665
            (memory_output_start_position, memory_output_size),
666
        ],
667
    )
668
669
    if to in evm.accessed_addresses:
670
        access_gas_cost = GasCosts.WARM_ACCESS
671
    else:
672
        evm.accessed_addresses.add(to)
673
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
674
675
    code_address = to
676
    (
677
        disable_precompiles,
678
        code_address,
679
        code,
680
        delegated_access_gas_cost,
681
    ) = access_delegation(evm, code_address)
682
    access_gas_cost += delegated_access_gas_cost
683
684
    message_call_gas = calculate_message_call_gas(
685
        U256(0),
686
        gas,
687
        Uint(evm.gas_left),
688
        extend_memory.cost,
689
        access_gas_cost,
690
    )
691
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
692
693
    # OPERATION
694
    evm.memory += b"\x00" * extend_memory.expand_by
695
    generic_call(
696
        evm,
697
        message_call_gas.sub_call,
698
        U256(0),
699
        evm.message.current_target,
700
        to,
701
        code_address,
702
        True,
703
        True,
704
        memory_input_start_position,
705
        memory_input_size,
706
        memory_output_start_position,
707
        memory_output_size,
708
        code,
709
        disable_precompiles,
710
    )
711
712
    # PROGRAM COUNTER
713
    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:
717
    """
718
    Stop execution and revert state changes, without consuming all provided gas
719
    and also has the ability to return a reason.
720
721
    Parameters
722
    ----------
723
    evm :
724
        The current EVM frame.
725
726
    """
727
    # STACK
728
    memory_start_index = pop(evm.stack)
729
    size = pop(evm.stack)
730
731
    # GAS
732
    extend_memory = calculate_gas_extend_memory(
733
        evm.memory, [(memory_start_index, size)]
734
    )
735
736
    charge_gas(evm, extend_memory.cost)
737
738
    # OPERATION
739
    evm.memory += b"\x00" * extend_memory.expand_by
740
    output = memory_read_bytes(evm.memory, memory_start_index, size)
741
    evm.output = Bytes(output)
742
    raise Revert
743
744
    # PROGRAM COUNTER
745
    # no-op