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:
63
    <snip>
66
    # This import causes a circular import error
67
    # if it's not moved inside this method
68
    from ...vm.interpreter import (
69
        MAX_INIT_CODE_SIZE,
70
        STACK_DEPTH_LIMIT,
71
        process_create_message,
72
    )
73
74
    call_data = memory_read_bytes(
75
        evm.memory, memory_start_position, memory_size
76
    )
77
    if len(call_data) > MAX_INIT_CODE_SIZE:
78
        raise OutOfGasError
79
80
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
81
    evm.gas_left -= create_message_gas
82
    if evm.message.is_static:
83
        raise WriteInStaticContext
84
    evm.return_data = b""
85
86
    sender_address = evm.message.current_target
87
    sender = get_account(evm.message.tx_env.state, sender_address)
88
89
    if (
90
        sender.balance < endowment
91
        or sender.nonce == Uint(2**64 - 1)
92
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
93
    ):
94
        evm.gas_left += create_message_gas
95
        push(evm.stack, U256(0))
96
        return
97
98
    evm.accessed_addresses.add(contract_address)
99
100
    if not account_deployable(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
    <snip>
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
    <snip>
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_KECCAK256_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
    <snip>
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

GenericCall

Parameters for the core logic of the CALL* family of opcodes.

271
@final
272
@dataclass
class GenericCall:

gas

278
    gas: Uint

value

279
    value: U256

caller

280
    caller: Address

to

281
    to: Address

code_address

282
    code_address: Address

should_transfer_value

283
    should_transfer_value: bool

is_staticcall

284
    is_staticcall: bool

memory_input_start_position

285
    memory_input_start_position: U256

memory_input_size

286
    memory_input_size: U256

memory_output_start_position

287
    memory_output_start_position: U256

memory_output_size

288
    memory_output_size: U256

code

289
    code: Bytes

disable_precompiles

290
    disable_precompiles: bool

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
294
    <snip>
297
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
298
299
    evm.return_data = b""
300
301
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
302
        evm.gas_left += params.gas
303
        push(evm.stack, U256(0))
304
        return
305
306
    call_data = memory_read_bytes(
307
        evm.memory,
308
        params.memory_input_start_position,
309
        params.memory_input_size,
310
    )
311
312
    child_message = Message(
313
        block_env=evm.message.block_env,
314
        tx_env=evm.message.tx_env,
315
        caller=params.caller,
316
        target=params.to,
317
        gas=params.gas,
318
        value=params.value,
319
        data=call_data,
320
        code=params.code,
321
        current_target=params.to,
322
        depth=evm.message.depth + Uint(1),
323
        code_address=params.code_address,
324
        should_transfer_value=params.should_transfer_value,
325
        is_static=params.is_staticcall or evm.message.is_static,
326
        accessed_addresses=evm.accessed_addresses.copy(),
327
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
328
        disable_precompiles=params.disable_precompiles,
329
        parent_evm=evm,
330
    )
331
    child_evm = process_message(child_message)
332
333
    if child_evm.error:
334
        incorporate_child_on_error(evm, child_evm)
335
        evm.return_data = child_evm.output
336
        push(evm.stack, U256(0))
337
    else:
338
        incorporate_child_on_success(evm, child_evm)
339
        evm.return_data = child_evm.output
340
        push(evm.stack, U256(1))
341
342
    actual_output_size = min(
343
        params.memory_output_size, U256(len(child_evm.output))
344
    )
345
    memory_write(
346
        evm.memory,
347
        params.memory_output_start_position,
348
        child_evm.output[:actual_output_size],
349
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
353
    <snip>
362
    # STACK
363
    gas = Uint(pop(evm.stack))
364
    to = to_address_masked(pop(evm.stack))
365
    value = pop(evm.stack)
366
    memory_input_start_position = pop(evm.stack)
367
    memory_input_size = pop(evm.stack)
368
    memory_output_start_position = pop(evm.stack)
369
    memory_output_size = pop(evm.stack)
370
371
    # GAS
372
    extend_memory = calculate_gas_extend_memory(
373
        evm.memory,
374
        [
375
            (memory_input_start_position, memory_input_size),
376
            (memory_output_start_position, memory_output_size),
377
        ],
378
    )
379
380
    if to in evm.accessed_addresses:
381
        access_gas_cost = GasCosts.WARM_ACCESS
382
    else:
383
        evm.accessed_addresses.add(to)
384
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
385
386
    code_address = to
387
    (
388
        disable_precompiles,
389
        code_address,
390
        code,
391
        delegated_access_gas_cost,
392
    ) = access_delegation(evm, code_address)
393
    access_gas_cost += delegated_access_gas_cost
394
395
    create_gas_cost = GasCosts.NEW_ACCOUNT
396
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
397
        create_gas_cost = Uint(0)
398
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
399
    message_call_gas = calculate_message_call_gas(
400
        value,
401
        gas,
402
        Uint(evm.gas_left),
403
        memory_cost=extend_memory.cost,
404
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
405
    )
406
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
407
    if evm.message.is_static and value != U256(0):
408
        raise WriteInStaticContext
409
    evm.memory += b"\x00" * extend_memory.expand_by
410
    sender_balance = get_account(
411
        evm.message.tx_env.state, evm.message.current_target
412
    ).balance
413
    if sender_balance < value:
414
        push(evm.stack, U256(0))
415
        evm.return_data = b""
416
        evm.gas_left += message_call_gas.sub_call
417
    else:
418
        generic_call(
419
            evm,
420
            GenericCall(
421
                gas=message_call_gas.sub_call,
422
                value=value,
423
                caller=evm.message.current_target,
424
                to=to,
425
                code_address=code_address,
426
                should_transfer_value=True,
427
                is_staticcall=False,
428
                memory_input_start_position=memory_input_start_position,
429
                memory_input_size=memory_input_size,
430
                memory_output_start_position=memory_output_start_position,
431
                memory_output_size=memory_output_size,
432
                code=code,
433
                disable_precompiles=disable_precompiles,
434
            ),
435
        )
436
437
    # PROGRAM COUNTER
438
    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:
442
    <snip>
451
    # STACK
452
    gas = Uint(pop(evm.stack))
453
    code_address = to_address_masked(pop(evm.stack))
454
    value = pop(evm.stack)
455
    memory_input_start_position = pop(evm.stack)
456
    memory_input_size = pop(evm.stack)
457
    memory_output_start_position = pop(evm.stack)
458
    memory_output_size = pop(evm.stack)
459
460
    # GAS
461
    to = evm.message.current_target
462
463
    extend_memory = calculate_gas_extend_memory(
464
        evm.memory,
465
        [
466
            (memory_input_start_position, memory_input_size),
467
            (memory_output_start_position, memory_output_size),
468
        ],
469
    )
470
471
    if code_address in evm.accessed_addresses:
472
        access_gas_cost = GasCosts.WARM_ACCESS
473
    else:
474
        evm.accessed_addresses.add(code_address)
475
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
476
477
    (
478
        disable_precompiles,
479
        code_address,
480
        code,
481
        delegated_access_gas_cost,
482
    ) = access_delegation(evm, code_address)
483
    access_gas_cost += delegated_access_gas_cost
484
485
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
486
    message_call_gas = calculate_message_call_gas(
487
        value,
488
        gas,
489
        Uint(evm.gas_left),
490
        extend_memory.cost,
491
        access_gas_cost + transfer_gas_cost,
492
    )
493
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
494
495
    # OPERATION
496
    evm.memory += b"\x00" * extend_memory.expand_by
497
    sender_balance = get_account(
498
        evm.message.tx_env.state,
499
        evm.message.current_target,
500
    ).balance
501
    if sender_balance < value:
502
        push(evm.stack, U256(0))
503
        evm.return_data = b""
504
        evm.gas_left += message_call_gas.sub_call
505
    else:
506
        generic_call(
507
            evm,
508
            GenericCall(
509
                gas=message_call_gas.sub_call,
510
                value=value,
511
                caller=evm.message.current_target,
512
                to=to,
513
                code_address=code_address,
514
                should_transfer_value=True,
515
                is_staticcall=False,
516
                memory_input_start_position=memory_input_start_position,
517
                memory_input_size=memory_input_size,
518
                memory_output_start_position=memory_output_start_position,
519
                memory_output_size=memory_output_size,
520
                code=code,
521
                disable_precompiles=disable_precompiles,
522
            ),
523
        )
524
525
    # PROGRAM COUNTER
526
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
530
    <snip>
539
    # STACK
540
    beneficiary = to_address_masked(pop(evm.stack))
541
542
    # GAS
543
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
544
    if beneficiary not in evm.accessed_addresses:
545
        evm.accessed_addresses.add(beneficiary)
546
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
547
548
    if (
549
        not is_account_alive(evm.message.tx_env.state, beneficiary)
550
        and get_account(
551
            evm.message.tx_env.state,
552
            evm.message.current_target,
553
        ).balance
554
        != 0
555
    ):
556
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
557
558
    charge_gas(evm, gas_cost)
559
    if evm.message.is_static:
560
        raise WriteInStaticContext
561
562
    originator = evm.message.current_target
563
    originator_balance = get_account(
564
        evm.message.tx_env.state, originator
565
    ).balance
566
567
    move_ether(
568
        evm.message.tx_env.state,
569
        originator,
570
        beneficiary,
571
        originator_balance,
572
    )
573
574
    # register account for deletion only if it was created
575
    # in the same transaction
576
    if originator in evm.message.tx_env.state.created_accounts:
577
        # If beneficiary is the same as originator, then
578
        # the ether is burnt.
579
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
580
        evm.accounts_to_delete.add(originator)
581
582
    # HALT the execution
583
    evm.running = False
584
585
    # PROGRAM COUNTER
586
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
590
    <snip>
599
    # STACK
600
    gas = Uint(pop(evm.stack))
601
    code_address = to_address_masked(pop(evm.stack))
602
    memory_input_start_position = pop(evm.stack)
603
    memory_input_size = pop(evm.stack)
604
    memory_output_start_position = pop(evm.stack)
605
    memory_output_size = pop(evm.stack)
606
607
    # GAS
608
    extend_memory = calculate_gas_extend_memory(
609
        evm.memory,
610
        [
611
            (memory_input_start_position, memory_input_size),
612
            (memory_output_start_position, memory_output_size),
613
        ],
614
    )
615
616
    if code_address in evm.accessed_addresses:
617
        access_gas_cost = GasCosts.WARM_ACCESS
618
    else:
619
        evm.accessed_addresses.add(code_address)
620
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
621
622
    (
623
        disable_precompiles,
624
        code_address,
625
        code,
626
        delegated_access_gas_cost,
627
    ) = access_delegation(evm, code_address)
628
    access_gas_cost += delegated_access_gas_cost
629
630
    message_call_gas = calculate_message_call_gas(
631
        U256(0),
632
        gas,
633
        Uint(evm.gas_left),
634
        extend_memory.cost,
635
        access_gas_cost,
636
    )
637
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
638
639
    # OPERATION
640
    evm.memory += b"\x00" * extend_memory.expand_by
641
    generic_call(
642
        evm,
643
        GenericCall(
644
            gas=message_call_gas.sub_call,
645
            value=evm.message.value,
646
            caller=evm.message.caller,
647
            to=evm.message.current_target,
648
            code_address=code_address,
649
            should_transfer_value=False,
650
            is_staticcall=False,
651
            memory_input_start_position=memory_input_start_position,
652
            memory_input_size=memory_input_size,
653
            memory_output_start_position=memory_output_start_position,
654
            memory_output_size=memory_output_size,
655
            code=code,
656
            disable_precompiles=disable_precompiles,
657
        ),
658
    )
659
660
    # PROGRAM COUNTER
661
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
665
    <snip>
674
    # STACK
675
    gas = Uint(pop(evm.stack))
676
    to = to_address_masked(pop(evm.stack))
677
    memory_input_start_position = pop(evm.stack)
678
    memory_input_size = pop(evm.stack)
679
    memory_output_start_position = pop(evm.stack)
680
    memory_output_size = pop(evm.stack)
681
682
    # GAS
683
    extend_memory = calculate_gas_extend_memory(
684
        evm.memory,
685
        [
686
            (memory_input_start_position, memory_input_size),
687
            (memory_output_start_position, memory_output_size),
688
        ],
689
    )
690
691
    if to in evm.accessed_addresses:
692
        access_gas_cost = GasCosts.WARM_ACCESS
693
    else:
694
        evm.accessed_addresses.add(to)
695
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
696
697
    code_address = to
698
    (
699
        disable_precompiles,
700
        code_address,
701
        code,
702
        delegated_access_gas_cost,
703
    ) = access_delegation(evm, code_address)
704
    access_gas_cost += delegated_access_gas_cost
705
706
    message_call_gas = calculate_message_call_gas(
707
        U256(0),
708
        gas,
709
        Uint(evm.gas_left),
710
        extend_memory.cost,
711
        access_gas_cost,
712
    )
713
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
714
715
    # OPERATION
716
    evm.memory += b"\x00" * extend_memory.expand_by
717
    generic_call(
718
        evm,
719
        GenericCall(
720
            gas=message_call_gas.sub_call,
721
            value=U256(0),
722
            caller=evm.message.current_target,
723
            to=to,
724
            code_address=code_address,
725
            should_transfer_value=True,
726
            is_staticcall=True,
727
            memory_input_start_position=memory_input_start_position,
728
            memory_input_size=memory_input_size,
729
            memory_output_start_position=memory_output_start_position,
730
            memory_output_size=memory_output_size,
731
            code=code,
732
            disable_precompiles=disable_precompiles,
733
        ),
734
    )
735
736
    # PROGRAM COUNTER
737
    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:
741
    <snip>
751
    # STACK
752
    memory_start_index = pop(evm.stack)
753
    size = pop(evm.stack)
754
755
    # GAS
756
    extend_memory = calculate_gas_extend_memory(
757
        evm.memory, [(memory_start_index, size)]
758
    )
759
760
    charge_gas(evm, extend_memory.cost)
761
762
    # OPERATION
763
    evm.memory += b"\x00" * extend_memory.expand_by
764
    output = memory_read_bytes(evm.memory, memory_start_index, size)
765
    evm.output = Bytes(output)
766
    raise Revert
767
768
    # PROGRAM COUNTER
769
    # no-op