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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
490
    """
491
    Halt execution and register account for later deletion.
492
493
    Parameters
494
    ----------
495
    evm :
496
        The current EVM frame.
497
498
    """
499
    # STACK
500
    beneficiary = to_address_masked(pop(evm.stack))
501
502
    # GAS
503
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
504
    if beneficiary not in evm.accessed_addresses:
505
        evm.accessed_addresses.add(beneficiary)
506
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
507
508
    if (
509
        not is_account_alive(evm.message.tx_env.state, beneficiary)
510
        and get_account(
511
            evm.message.tx_env.state, evm.message.current_target
512
        ).balance
513
        != 0
514
    ):
515
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
516
517
    charge_gas(evm, gas_cost)
518
    if evm.message.is_static:
519
        raise WriteInStaticContext
520
521
    originator = evm.message.current_target
522
    beneficiary_balance = get_account(
523
        evm.message.tx_env.state, beneficiary
524
    ).balance
525
    originator_balance = get_account(
526
        evm.message.tx_env.state, originator
527
    ).balance
528
529
    # First Transfer to beneficiary
530
    set_account_balance(
531
        evm.message.tx_env.state,
532
        beneficiary,
533
        beneficiary_balance + originator_balance,
534
    )
535
    # Next, Zero the balance of the address being deleted (must come after
536
    # sending to beneficiary in case the contract named itself as the
537
    # beneficiary).
538
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
539
540
    # register account for deletion
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
    """
560
    # STACK
561
    gas = Uint(pop(evm.stack))
562
    code_address = to_address_masked(pop(evm.stack))
563
    memory_input_start_position = pop(evm.stack)
564
    memory_input_size = pop(evm.stack)
565
    memory_output_start_position = pop(evm.stack)
566
    memory_output_size = pop(evm.stack)
567
568
    # GAS
569
    extend_memory = calculate_gas_extend_memory(
570
        evm.memory,
571
        [
572
            (memory_input_start_position, memory_input_size),
573
            (memory_output_start_position, memory_output_size),
574
        ],
575
    )
576
577
    if code_address in evm.accessed_addresses:
578
        access_gas_cost = GasCosts.WARM_ACCESS
579
    else:
580
        evm.accessed_addresses.add(code_address)
581
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
582
583
    message_call_gas = calculate_message_call_gas(
584
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
585
    )
586
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
587
588
    # OPERATION
589
    evm.memory += b"\x00" * extend_memory.expand_by
590
    generic_call(
591
        evm,
592
        message_call_gas.sub_call,
593
        evm.message.value,
594
        evm.message.caller,
595
        evm.message.current_target,
596
        code_address,
597
        False,
598
        False,
599
        memory_input_start_position,
600
        memory_input_size,
601
        memory_output_start_position,
602
        memory_output_size,
603
    )
604
605
    # PROGRAM COUNTER
606
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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