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.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
        disable_precompiles=False,
124
        parent_evm=evm,
125
    )
126
    child_evm = process_create_message(child_message)
127
128
    if child_evm.error:
129
        incorporate_child_on_error(evm, child_evm)
130
        evm.return_data = child_evm.output
131
        push(evm.stack, U256(0))
132
    else:
133
        incorporate_child_on_success(evm, child_evm)
134
        evm.return_data = b""
135
        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:
139
    """
140
    Creates a new account with associated code.
141
142
    Parameters
143
    ----------
144
    evm :
145
        The current EVM frame.
146
147
    """
148
    # STACK
149
    endowment = pop(evm.stack)
150
    memory_start_position = pop(evm.stack)
151
    memory_size = pop(evm.stack)
152
153
    # GAS
154
    extend_memory = calculate_gas_extend_memory(
155
        evm.memory, [(memory_start_position, memory_size)]
156
    )
157
    init_code_gas = init_code_cost(Uint(memory_size))
158
159
    charge_gas(
160
        evm,
161
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
162
    )
163
164
    # OPERATION
165
    evm.memory += b"\x00" * extend_memory.expand_by
166
    contract_address = compute_contract_address(
167
        evm.message.current_target,
168
        get_account(
169
            evm.message.tx_env.state, evm.message.current_target
170
        ).nonce,
171
    )
172
173
    generic_create(
174
        evm,
175
        endowment,
176
        contract_address,
177
        memory_start_position,
178
        memory_size,
179
    )
180
181
    # PROGRAM COUNTER
182
    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:
186
    """
187
    Creates a new account with associated code.
188
189
    It's similar to the CREATE opcode except that the address of the new
190
    account depends on the init_code instead of the nonce of sender.
191
192
    Parameters
193
    ----------
194
    evm :
195
        The current EVM frame.
196
197
    """
198
    # STACK
199
    endowment = pop(evm.stack)
200
    memory_start_position = pop(evm.stack)
201
    memory_size = pop(evm.stack)
202
    salt = pop(evm.stack).to_be_bytes32()
203
204
    # GAS
205
    extend_memory = calculate_gas_extend_memory(
206
        evm.memory, [(memory_start_position, memory_size)]
207
    )
208
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
209
    init_code_gas = init_code_cost(Uint(memory_size))
210
    charge_gas(
211
        evm,
212
        GasCosts.OPCODE_CREATE_BASE
213
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
214
        + extend_memory.cost
215
        + init_code_gas,
216
    )
217
218
    # OPERATION
219
    evm.memory += b"\x00" * extend_memory.expand_by
220
    contract_address = compute_create2_contract_address(
221
        evm.message.current_target,
222
        salt,
223
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
224
    )
225
226
    generic_create(
227
        evm,
228
        endowment,
229
        contract_address,
230
        memory_start_position,
231
        memory_size,
232
    )
233
234
    # PROGRAM COUNTER
235
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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