ethereum.forks.shanghai.vm.instructions.systemethereum.forks.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:
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.tx_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.tx_env.state, contract_address
100
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
101
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
        push(evm.stack, U256(0))
103
        return
104
105
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
106
107
    child_message = Message(
108
        block_env=evm.message.block_env,
109
        tx_env=evm.message.tx_env,
110
        caller=evm.message.current_target,
111
        target=Bytes0(),
112
        gas=create_message_gas,
113
        value=endowment,
114
        data=b"",
115
        code=call_data,
116
        current_target=contract_address,
117
        depth=evm.message.depth + Uint(1),
118
        code_address=None,
119
        should_transfer_value=True,
120
        is_static=False,
121
        accessed_addresses=evm.accessed_addresses.copy(),
122
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
123
        parent_evm=evm,
124
    )
125
    child_evm = process_create_message(child_message)
126
127
    if child_evm.error:
128
        incorporate_child_on_error(evm, child_evm)
129
        evm.return_data = child_evm.output
130
        push(evm.stack, U256(0))
131
    else:
132
        incorporate_child_on_success(evm, child_evm)
133
        evm.return_data = b""
134
        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:
138
    """
139
    Creates a new account with associated code.
140
141
    Parameters
142
    ----------
143
    evm :
144
        The current EVM frame.
145
146
    """
147
    # STACK
148
    endowment = pop(evm.stack)
149
    memory_start_position = pop(evm.stack)
150
    memory_size = pop(evm.stack)
151
152
    # GAS
153
    extend_memory = calculate_gas_extend_memory(
154
        evm.memory, [(memory_start_position, memory_size)]
155
    )
156
    init_code_gas = init_code_cost(Uint(memory_size))
157
158
    charge_gas(
159
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
160
    )
161
162
    # OPERATION
163
    evm.memory += b"\x00" * extend_memory.expand_by
164
    contract_address = compute_contract_address(
165
        evm.message.current_target,
166
        get_account(
167
            evm.message.tx_env.state, evm.message.current_target
168
        ).nonce,
169
    )
170
171
    generic_create(
172
        evm,
173
        endowment,
174
        contract_address,
175
        memory_start_position,
176
        memory_size,
177
    )
178
179
    # PROGRAM COUNTER
180
    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:
184
    """
185
    Creates a new account with associated code.
186
187
    It's similar to the CREATE opcode except that the address of the new
188
    account depends on the init_code instead of the nonce of sender.
189
190
    Parameters
191
    ----------
192
    evm :
193
        The current EVM frame.
194
195
    """
196
    # STACK
197
    endowment = pop(evm.stack)
198
    memory_start_position = pop(evm.stack)
199
    memory_size = pop(evm.stack)
200
    salt = pop(evm.stack).to_be_bytes32()
201
202
    # GAS
203
    extend_memory = calculate_gas_extend_memory(
204
        evm.memory, [(memory_start_position, memory_size)]
205
    )
206
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
207
    init_code_gas = init_code_cost(Uint(memory_size))
208
    charge_gas(
209
        evm,
210
        GasCosts.OPCODE_CREATE_BASE
211
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
212
        + extend_memory.cost
213
        + init_code_gas,
214
    )
215
216
    # OPERATION
217
    evm.memory += b"\x00" * extend_memory.expand_by
218
    contract_address = compute_create2_contract_address(
219
        evm.message.current_target,
220
        salt,
221
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
222
    )
223
224
    generic_create(
225
        evm,
226
        endowment,
227
        contract_address,
228
        memory_start_position,
229
        memory_size,
230
    )
231
232
    # PROGRAM COUNTER
233
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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