ethereum.shanghai.vm.instructions.systemethereum.cancun.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:
69
    """
70
    Core logic used by the `CREATE*` family of opcodes.
71
    """
72
    # This import causes a circular import error
73
    # if it's not moved inside this method
74
    from ...vm.interpreter import (
75
        MAX_INIT_CODE_SIZE,
76
        STACK_DEPTH_LIMIT,
77
        process_create_message,
78
    )
79
80
    call_data = memory_read_bytes(
81
        evm.memory, memory_start_position, memory_size
82
    )
83
    if len(call_data) > MAX_INIT_CODE_SIZE:
84
        raise OutOfGasError
85
86
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
87
    evm.gas_left -= create_message_gas
88
    if evm.message.is_static:
89
        raise WriteInStaticContext
90
    evm.return_data = b""
91
92
    sender_address = evm.message.current_target
93
    sender = get_account(evm.message.block_env.state, sender_address)
94
95
    if (
96
        sender.balance < endowment
97
        or sender.nonce == Uint(2**64 - 1)
98
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
99
    ):
100
        evm.gas_left += create_message_gas
101
        push(evm.stack, U256(0))
102
        return
103
104
    evm.accessed_addresses.add(contract_address)
105
106
    if account_has_code_or_nonce(
107
        evm.message.block_env.state, contract_address
108
    ) or account_has_storage(evm.message.block_env.state, contract_address):
109
        increment_nonce(
110
            evm.message.block_env.state, evm.message.current_target
111
        )
112
        push(evm.stack, U256(0))
113
        return
114
115
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
116
117
    child_message = Message(
118
        block_env=evm.message.block_env,
119
        tx_env=evm.message.tx_env,
120
        caller=evm.message.current_target,
121
        target=Bytes0(),
122
        gas=create_message_gas,
123
        value=endowment,
124
        data=b"",
125
        code=call_data,
126
        current_target=contract_address,
127
        depth=evm.message.depth + Uint(1),
128
        code_address=None,
129
        should_transfer_value=True,
130
        is_static=False,
131
        accessed_addresses=evm.accessed_addresses.copy(),
132
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
133
        parent_evm=evm,
134
    )
135
    child_evm = process_create_message(child_message)
136
137
    if child_evm.error:
138
        incorporate_child_on_error(evm, child_evm)
139
        evm.return_data = child_evm.output
140
        push(evm.stack, U256(0))
141
    else:
142
        incorporate_child_on_success(evm, child_evm)
143
        evm.return_data = b""
144
        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:
148
    """
149
    Creates a new account with associated code.
150
151
    Parameters
152
    ----------
153
    evm :
154
        The current EVM frame.
155
    """
156
    # STACK
157
    endowment = pop(evm.stack)
158
    memory_start_position = pop(evm.stack)
159
    memory_size = pop(evm.stack)
160
161
    # GAS
162
    extend_memory = calculate_gas_extend_memory(
163
        evm.memory, [(memory_start_position, memory_size)]
164
    )
165
    init_code_gas = init_code_cost(Uint(memory_size))
166
167
    charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas)
168
169
    # OPERATION
170
    evm.memory += b"\x00" * extend_memory.expand_by
171
    contract_address = compute_contract_address(
172
        evm.message.current_target,
173
        get_account(
174
            evm.message.block_env.state, evm.message.current_target
175
        ).nonce,
176
    )
177
178
    generic_create(
179
        evm,
180
        endowment,
181
        contract_address,
182
        memory_start_position,
183
        memory_size,
184
    )
185
186
    # PROGRAM COUNTER
187
    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:
191
    """
192
    Creates a new account with associated code.
193
194
    It's similar to CREATE opcode except that the address of new account
195
    depends on the init_code instead of the nonce of sender.
196
197
    Parameters
198
    ----------
199
    evm :
200
        The current EVM frame.
201
    """
202
    # STACK
203
    endowment = pop(evm.stack)
204
    memory_start_position = pop(evm.stack)
205
    memory_size = pop(evm.stack)
206
    salt = pop(evm.stack).to_be_bytes32()
207
208
    # GAS
209
    extend_memory = calculate_gas_extend_memory(
210
        evm.memory, [(memory_start_position, memory_size)]
211
    )
212
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
213
    init_code_gas = init_code_cost(Uint(memory_size))
214
    charge_gas(
215
        evm,
216
        GAS_CREATE
217
        + GAS_KECCAK256_WORD * call_data_words
218
        + extend_memory.cost
219
        + init_code_gas,
220
    )
221
222
    # OPERATION
223
    evm.memory += b"\x00" * extend_memory.expand_by
224
    contract_address = compute_create2_contract_address(
225
        evm.message.current_target,
226
        salt,
227
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
228
    )
229
230
    generic_create(
231
        evm,
232
        endowment,
233
        contract_address,
234
        memory_start_position,
235
        memory_size,
236
    )
237
238
    # PROGRAM COUNTER
239
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
243
    """
244
    Halts execution returning output data.
245
246
    Parameters
247
    ----------
248
    evm :
249
        The current EVM frame.
250
    """
251
    # STACK
252
    memory_start_position = pop(evm.stack)
253
    memory_size = pop(evm.stack)
254
255
    # GAS
256
    extend_memory = calculate_gas_extend_memory(
257
        evm.memory, [(memory_start_position, memory_size)]
258
    )
259
260
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
261
262
    # OPERATION
263
    evm.memory += b"\x00" * extend_memory.expand_by
264
    evm.output = memory_read_bytes(
265
        evm.memory, memory_start_position, memory_size
266
    )
267
268
    evm.running = False
269
270
    # PROGRAM COUNTER
271
    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) -> 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
    code = get_account(evm.message.block_env.state, code_address).code
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
        parent_evm=evm,
321
    )
322
    child_evm = process_message(child_message)
323
324
    if child_evm.error:
325
        incorporate_child_on_error(evm, child_evm)
326
        evm.return_data = child_evm.output
327
        push(evm.stack, U256(0))
328
    else:
329
        incorporate_child_on_success(evm, child_evm)
330
        evm.return_data = child_evm.output
331
        push(evm.stack, U256(1))
332
333
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
334
    memory_write(
335
        evm.memory,
336
        memory_output_start_position,
337
        child_evm.output[:actual_output_size],
338
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
493
    """
494
    Halt execution and register account for later deletion.
495
496
    Parameters
497
    ----------
498
    evm :
499
        The current EVM frame.
500
    """
501
    # STACK
502
    beneficiary = to_address_masked(pop(evm.stack))
503
504
    # GAS
505
    gas_cost = GAS_SELF_DESTRUCT
506
    if beneficiary not in evm.accessed_addresses:
507
        evm.accessed_addresses.add(beneficiary)
508
        gas_cost += GAS_COLD_ACCOUNT_ACCESS
509
510
    if (
511
        not is_account_alive(evm.message.block_env.state, beneficiary)
512
        and get_account(
513
            evm.message.block_env.state, evm.message.current_target
514
        ).balance
515
        != 0
516
    ):
517
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
518
519
    charge_gas(evm, gas_cost)
520
    if evm.message.is_static:
521
        raise WriteInStaticContext
522
523
    originator = evm.message.current_target
523
    beneficiary_balance = get_account(
524
        evm.message.block_env.state, beneficiary
525
    ).balance
524
    originator_balance = get_account(
525
        evm.message.block_env.state, originator
526
    ).balance
527
530
    # First Transfer to beneficiary
531
    set_account_balance(
528
    move_ether(
529
        evm.message.block_env.state,
530
        originator,
531
        beneficiary,
534
        beneficiary_balance + originator_balance,
532
        originator_balance,
533
    )
536
    # Next, Zero the balance of the address being deleted (must come after
537
    # sending to beneficiary in case the contract named itself as the
538
    # beneficiary).
539
    set_account_balance(evm.message.block_env.state, originator, U256(0))
534
541
    # register account for deletion
542
    evm.accounts_to_delete.add(originator)
535
    # register account for deletion only if it was created
536
    # in the same transaction
537
    if originator in evm.message.block_env.state.created_accounts:
538
        # If beneficiary is the same as originator, then
539
        # the ether is burnt.
540
        set_account_balance(evm.message.block_env.state, originator, U256(0))
541
        evm.accounts_to_delete.add(originator)
542
543
    # HALT the execution
544
    evm.running = False
545
546
    # PROGRAM COUNTER
547
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
551
    """
552
    Message-call into an account.
553
554
    Parameters
555
    ----------
556
    evm :
557
        The current EVM frame.
558
    """
559
    # STACK
560
    gas = Uint(pop(evm.stack))
561
    code_address = to_address_masked(pop(evm.stack))
562
    memory_input_start_position = pop(evm.stack)
563
    memory_input_size = pop(evm.stack)
564
    memory_output_start_position = pop(evm.stack)
565
    memory_output_size = pop(evm.stack)
566
567
    # GAS
568
    extend_memory = calculate_gas_extend_memory(
569
        evm.memory,
570
        [
571
            (memory_input_start_position, memory_input_size),
572
            (memory_output_start_position, memory_output_size),
573
        ],
574
    )
575
576
    if code_address in evm.accessed_addresses:
577
        access_gas_cost = GAS_WARM_ACCESS
578
    else:
579
        evm.accessed_addresses.add(code_address)
580
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
581
582
    message_call_gas = calculate_message_call_gas(
583
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
584
    )
585
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
586
587
    # OPERATION
588
    evm.memory += b"\x00" * extend_memory.expand_by
589
    generic_call(
590
        evm,
591
        message_call_gas.sub_call,
592
        evm.message.value,
593
        evm.message.caller,
594
        evm.message.current_target,
595
        code_address,
596
        False,
597
        False,
598
        memory_input_start_position,
599
        memory_input_size,
600
        memory_output_start_position,
601
        memory_output_size,
602
    )
603
604
    # PROGRAM COUNTER
605
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
609
    """
610
    Message-call into an account.
611
612
    Parameters
613
    ----------
614
    evm :
615
        The current EVM frame.
616
    """
617
    # STACK
618
    gas = Uint(pop(evm.stack))
619
    to = to_address_masked(pop(evm.stack))
620
    memory_input_start_position = pop(evm.stack)
621
    memory_input_size = pop(evm.stack)
622
    memory_output_start_position = pop(evm.stack)
623
    memory_output_size = pop(evm.stack)
624
625
    # GAS
626
    extend_memory = calculate_gas_extend_memory(
627
        evm.memory,
628
        [
629
            (memory_input_start_position, memory_input_size),
630
            (memory_output_start_position, memory_output_size),
631
        ],
632
    )
633
634
    if to in evm.accessed_addresses:
635
        access_gas_cost = GAS_WARM_ACCESS
636
    else:
637
        evm.accessed_addresses.add(to)
638
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
639
640
    code_address = to
641
642
    message_call_gas = calculate_message_call_gas(
643
        U256(0),
644
        gas,
645
        Uint(evm.gas_left),
646
        extend_memory.cost,
647
        access_gas_cost,
648
    )
649
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
650
651
    # OPERATION
652
    evm.memory += b"\x00" * extend_memory.expand_by
653
    generic_call(
654
        evm,
655
        message_call_gas.sub_call,
656
        U256(0),
657
        evm.message.current_target,
658
        to,
659
        code_address,
660
        True,
661
        True,
662
        memory_input_start_position,
663
        memory_input_size,
664
        memory_output_start_position,
665
        memory_output_size,
666
    )
667
668
    # PROGRAM COUNTER
669
    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:
673
    """
674
    Stop execution and revert state changes, without consuming all provided gas
675
    and also has the ability to return a reason
676
    Parameters
677
    ----------
678
    evm :
679
        The current EVM frame.
680
    """
681
    # STACK
682
    memory_start_index = pop(evm.stack)
683
    size = pop(evm.stack)
684
685
    # GAS
686
    extend_memory = calculate_gas_extend_memory(
687
        evm.memory, [(memory_start_index, size)]
688
    )
689
690
    charge_gas(evm, extend_memory.cost)
691
692
    # OPERATION
693
    evm.memory += b"\x00" * extend_memory.expand_by
694
    output = memory_read_bytes(evm.memory, memory_start_index, size)
695
    evm.output = Bytes(output)
696
    raise Revert