ethereum.forks.osaka.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,
163
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
164
    )
165
166
    # OPERATION
167
    evm.memory += b"\x00" * extend_memory.expand_by
168
    contract_address = compute_contract_address(
169
        evm.message.current_target,
170
        get_account(
171
            evm.message.block_env.state, evm.message.current_target
172
        ).nonce,
173
    )
174
175
    generic_create(
176
        evm,
177
        endowment,
178
        contract_address,
179
        memory_start_position,
180
        memory_size,
181
    )
182
183
    # PROGRAM COUNTER
184
    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:
188
    """
189
    Creates a new account with associated code.
190
191
    It's similar to the CREATE opcode except that the address of the new
192
    account depends on the init_code instead of the nonce of sender.
193
194
    Parameters
195
    ----------
196
    evm :
197
        The current EVM frame.
198
199
    """
200
    # STACK
201
    endowment = pop(evm.stack)
202
    memory_start_position = pop(evm.stack)
203
    memory_size = pop(evm.stack)
204
    salt = pop(evm.stack).to_be_bytes32()
205
206
    # GAS
207
    extend_memory = calculate_gas_extend_memory(
208
        evm.memory, [(memory_start_position, memory_size)]
209
    )
210
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
211
    init_code_gas = init_code_cost(Uint(memory_size))
212
    charge_gas(
213
        evm,
214
        GasCosts.OPCODE_CREATE_BASE
215
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
216
        + extend_memory.cost
217
        + init_code_gas,
218
    )
219
220
    # OPERATION
221
    evm.memory += b"\x00" * extend_memory.expand_by
222
    contract_address = compute_create2_contract_address(
223
        evm.message.current_target,
224
        salt,
225
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
226
    )
227
228
    generic_create(
229
        evm,
230
        endowment,
231
        contract_address,
232
        memory_start_position,
233
        memory_size,
234
    )
235
236
    # PROGRAM COUNTER
237
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
241
    """
242
    Halts execution returning output data.
243
244
    Parameters
245
    ----------
246
    evm :
247
        The current EVM frame.
248
249
    """
250
    # STACK
251
    memory_start_position = pop(evm.stack)
252
    memory_size = pop(evm.stack)
253
254
    # GAS
255
    extend_memory = calculate_gas_extend_memory(
256
        evm.memory, [(memory_start_position, memory_size)]
257
    )
258
259
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
260
261
    # OPERATION
262
    evm.memory += b"\x00" * extend_memory.expand_by
263
    evm.output = memory_read_bytes(
264
        evm.memory, memory_start_position, memory_size
265
    )
266
267
    evm.running = False
268
269
    # PROGRAM COUNTER
270
    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:
289
    """
290
    Perform the core logic of the `CALL*` family of opcodes.
291
    """
292
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
293
294
    evm.return_data = b""
295
296
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
297
        evm.gas_left += gas
298
        push(evm.stack, U256(0))
299
        return
300
301
    call_data = memory_read_bytes(
302
        evm.memory, memory_input_start_position, memory_input_size
303
    )
304
305
    child_message = Message(
306
        block_env=evm.message.block_env,
307
        tx_env=evm.message.tx_env,
308
        caller=caller,
309
        target=to,
310
        gas=gas,
311
        value=value,
312
        data=call_data,
313
        code=code,
314
        current_target=to,
315
        depth=evm.message.depth + Uint(1),
316
        code_address=code_address,
317
        should_transfer_value=should_transfer_value,
318
        is_static=True if is_staticcall else evm.message.is_static,
319
        accessed_addresses=evm.accessed_addresses.copy(),
320
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
321
        disable_precompiles=disable_precompiles,
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 = GasCosts.WARM_ACCESS
373
    else:
374
        evm.accessed_addresses.add(to)
375
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
376
377
    code_address = to
378
    (
379
        disable_precompiles,
380
        code_address,
381
        code,
382
        delegated_access_gas_cost,
383
    ) = access_delegation(evm, code_address)
384
    access_gas_cost += delegated_access_gas_cost
385
386
    create_gas_cost = GasCosts.NEW_ACCOUNT
387
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
388
        create_gas_cost = Uint(0)
389
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
390
    message_call_gas = calculate_message_call_gas(
391
        value,
392
        gas,
393
        Uint(evm.gas_left),
394
        extend_memory.cost,
395
        access_gas_cost + create_gas_cost + transfer_gas_cost,
396
    )
397
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
398
    if evm.message.is_static and value != U256(0):
399
        raise WriteInStaticContext
400
    evm.memory += b"\x00" * extend_memory.expand_by
401
    sender_balance = get_account(
402
        evm.message.block_env.state, evm.message.current_target
403
    ).balance
404
    if sender_balance < value:
405
        push(evm.stack, U256(0))
406
        evm.return_data = b""
407
        evm.gas_left += message_call_gas.sub_call
408
    else:
409
        generic_call(
410
            evm,
411
            message_call_gas.sub_call,
412
            value,
413
            evm.message.current_target,
414
            to,
415
            code_address,
416
            True,
417
            False,
418
            memory_input_start_position,
419
            memory_input_size,
420
            memory_output_start_position,
421
            memory_output_size,
422
            code,
423
            disable_precompiles,
424
        )
425
426
    # PROGRAM COUNTER
427
    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:
431
    """
432
    Message-call into this account with alternative account’s code.
433
434
    Parameters
435
    ----------
436
    evm :
437
        The current EVM frame.
438
439
    """
440
    # STACK
441
    gas = Uint(pop(evm.stack))
442
    code_address = to_address_masked(pop(evm.stack))
443
    value = pop(evm.stack)
444
    memory_input_start_position = pop(evm.stack)
445
    memory_input_size = pop(evm.stack)
446
    memory_output_start_position = pop(evm.stack)
447
    memory_output_size = pop(evm.stack)
448
449
    # GAS
450
    to = evm.message.current_target
451
452
    extend_memory = calculate_gas_extend_memory(
453
        evm.memory,
454
        [
455
            (memory_input_start_position, memory_input_size),
456
            (memory_output_start_position, memory_output_size),
457
        ],
458
    )
459
460
    if code_address in evm.accessed_addresses:
461
        access_gas_cost = GasCosts.WARM_ACCESS
462
    else:
463
        evm.accessed_addresses.add(code_address)
464
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
465
466
    (
467
        disable_precompiles,
468
        code_address,
469
        code,
470
        delegated_access_gas_cost,
471
    ) = access_delegation(evm, code_address)
472
    access_gas_cost += delegated_access_gas_cost
473
474
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
475
    message_call_gas = calculate_message_call_gas(
476
        value,
477
        gas,
478
        Uint(evm.gas_left),
479
        extend_memory.cost,
480
        access_gas_cost + transfer_gas_cost,
481
    )
482
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
483
484
    # OPERATION
485
    evm.memory += b"\x00" * extend_memory.expand_by
486
    sender_balance = get_account(
487
        evm.message.block_env.state,
488
        evm.message.current_target,
489
    ).balance
490
    if sender_balance < value:
491
        push(evm.stack, U256(0))
492
        evm.return_data = b""
493
        evm.gas_left += message_call_gas.sub_call
494
    else:
495
        generic_call(
496
            evm,
497
            message_call_gas.sub_call,
498
            value,
499
            evm.message.current_target,
500
            to,
501
            code_address,
502
            True,
503
            False,
504
            memory_input_start_position,
505
            memory_input_size,
506
            memory_output_start_position,
507
            memory_output_size,
508
            code,
509
            disable_precompiles,
510
        )
511
512
    # PROGRAM COUNTER
513
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
577
    """
578
    Message-call into an account.
579
580
    Parameters
581
    ----------
582
    evm :
583
        The current EVM frame.
584
585
    """
586
    # STACK
587
    gas = Uint(pop(evm.stack))
588
    code_address = to_address_masked(pop(evm.stack))
589
    memory_input_start_position = pop(evm.stack)
590
    memory_input_size = pop(evm.stack)
591
    memory_output_start_position = pop(evm.stack)
592
    memory_output_size = pop(evm.stack)
593
594
    # GAS
595
    extend_memory = calculate_gas_extend_memory(
596
        evm.memory,
597
        [
598
            (memory_input_start_position, memory_input_size),
599
            (memory_output_start_position, memory_output_size),
600
        ],
601
    )
602
603
    if code_address in evm.accessed_addresses:
604
        access_gas_cost = GasCosts.WARM_ACCESS
605
    else:
606
        evm.accessed_addresses.add(code_address)
607
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
608
609
    (
610
        disable_precompiles,
611
        code_address,
612
        code,
613
        delegated_access_gas_cost,
614
    ) = access_delegation(evm, code_address)
615
    access_gas_cost += delegated_access_gas_cost
616
617
    message_call_gas = calculate_message_call_gas(
618
        U256(0),
619
        gas,
620
        Uint(evm.gas_left),
621
        extend_memory.cost,
622
        access_gas_cost,
623
    )
624
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
625
626
    # OPERATION
627
    evm.memory += b"\x00" * extend_memory.expand_by
628
    generic_call(
629
        evm,
630
        message_call_gas.sub_call,
631
        evm.message.value,
632
        evm.message.caller,
633
        evm.message.current_target,
634
        code_address,
635
        False,
636
        False,
637
        memory_input_start_position,
638
        memory_input_size,
639
        memory_output_start_position,
640
        memory_output_size,
641
        code,
642
        disable_precompiles,
643
    )
644
645
    # PROGRAM COUNTER
646
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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