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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
244
    """
245
    Halts execution returning output data.
246
247
    Parameters
248
    ----------
249
    evm :
250
        The current EVM frame.
251
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) -> None:
290
    """
291
    Perform the core logic of the `CALL*` family of opcodes.
292
    """
293
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
294
295
    evm.return_data = b""
296
297
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
298
        evm.gas_left += gas
299
        push(evm.stack, U256(0))
300
        return
301
302
    call_data = memory_read_bytes(
303
        evm.memory, memory_input_start_position, memory_input_size
304
    )
305
    code = get_account(evm.message.block_env.state, code_address).code
306
    child_message = Message(
307
        block_env=evm.message.block_env,
308
        tx_env=evm.message.tx_env,
309
        caller=caller,
310
        target=to,
311
        gas=gas,
312
        value=value,
313
        data=call_data,
314
        code=code,
315
        current_target=to,
316
        depth=evm.message.depth + Uint(1),
317
        code_address=code_address,
318
        should_transfer_value=should_transfer_value,
319
        is_static=True if is_staticcall else evm.message.is_static,
320
        accessed_addresses=evm.accessed_addresses.copy(),
321
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
322
        parent_evm=evm,
323
    )
324
    child_evm = process_message(child_message)
325
326
    if child_evm.error:
327
        incorporate_child_on_error(evm, child_evm)
328
        evm.return_data = child_evm.output
329
        push(evm.stack, U256(0))
330
    else:
331
        incorporate_child_on_success(evm, child_evm)
332
        evm.return_data = child_evm.output
333
        push(evm.stack, U256(1))
334
335
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
336
    memory_write(
337
        evm.memory,
338
        memory_output_start_position,
339
        child_evm.output[:actual_output_size],
340
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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