Skip to content

test_set_code_to_system_contract()

Documentation for tests/prague/eip7702_set_code_tx/test_set_code_txs.py::test_set_code_to_system_contract@21fb11c8.

Generate fixtures for these test cases for Prague with:

fill -v tests/prague/eip7702_set_code_tx/test_set_code_txs.py::test_set_code_to_system_contract --fork Prague

Test setting the code of an account to a system contract.

Source code in tests/prague/eip7702_set_code_tx/test_set_code_txs.py
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
@pytest.mark.with_all_call_opcodes(
    selector=(
        lambda opcode: opcode
        not in [Op.STATICCALL, Op.CALLCODE, Op.DELEGATECALL, Op.EXTDELEGATECALL, Op.EXTSTATICCALL]
    )
)
@pytest.mark.with_all_system_contracts
def test_set_code_to_system_contract(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    system_contract: int,
    call_opcode: Op,
):
    """Test setting the code of an account to a system contract."""
    caller_code_storage = Storage()
    call_return_code_slot = caller_code_storage.store_next(
        call_return_code(
            opcode=call_opcode,
            success=True,
        )
    )
    call_return_data_size_slot = caller_code_storage.store_next(0)

    call_value = 0

    # Setup the initial storage of the account to mimic the system contract if required
    match system_contract:
        case Address(0x00000000219AB540356CBB839CBE05303D7705FA):  # EIP-6110
            # Deposit contract needs specific storage values, so we set them on the account
            auth_signer = pre.fund_eoa(
                auth_account_start_balance, storage=deposit_contract_initial_storage()
            )
        case Address(0x000F3DF6D732807EF1319FB7B8BB8522D0BEAC02):  # EIP-4788
            auth_signer = pre.fund_eoa(auth_account_start_balance, storage=Storage({1: 1}))
        case _:
            # Pre-fund without storage
            auth_signer = pre.fund_eoa(auth_account_start_balance)

    # Fabricate the payload for the system contract
    match system_contract:
        case Address(0x000F3DF6D732807EF1319FB7B8BB8522D0BEAC02):  # EIP-4788
            caller_payload = Hash(1)
            caller_code_storage[call_return_data_size_slot] = 32
        case Address(0x00000000219AB540356CBB839CBE05303D7705FA):  # EIP-6110
            # Fabricate a valid deposit request to the set-code account
            deposit_request = DepositRequest(
                pubkey=0x01,
                withdrawal_credentials=0x02,
                amount=1_000_000_000,
                signature=0x03,
                index=0x0,
            )
            caller_payload = deposit_request.calldata
            call_value = deposit_request.value
        case Address(0x00000961EF480EB55E80D19AD83579A64C007002):  # EIP-7002
            # Fabricate a valid withdrawal request to the set-code account
            withdrawal_request = WithdrawalRequest(
                source_address=0x01,
                validator_pubkey=0x02,
                amount=0x03,
                fee=0x01,
            )
            caller_payload = withdrawal_request.calldata
            call_value = withdrawal_request.value
        case Address(0x0000BBDDC7CE488642FB579F8B00F3A590007251):  # EIP-7251
            # Fabricate a valid consolidation request to the set-code account
            consolidation_request = ConsolidationRequest(
                source_address=0x01,
                source_pubkey=0x02,
                target_pubkey=0x03,
                fee=0x01,
            )
            caller_payload = consolidation_request.calldata
            call_value = consolidation_request.value
        case Address(0x0000F90827F1C53A10CB7A02335B175320002935):  # EIP-2935
            caller_payload = Hash(0)
            caller_code_storage[call_return_data_size_slot] = 32
        case _:
            raise ValueError(f"Not implemented system contract: {system_contract}")

    caller_code = (
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.SSTORE(
            call_return_code_slot,
            call_opcode(address=auth_signer, value=call_value, args_size=Op.CALLDATASIZE),
        )
        + Op.SSTORE(call_return_data_size_slot, Op.RETURNDATASIZE)
        + Op.STOP
    )
    caller_code_address = pre.deploy_contract(caller_code)
    sender = pre.fund_eoa()
    if call_value > 0:
        pre.fund_address(sender, call_value)

    txs = [
        Transaction(
            sender=sender,
            gas_limit=500_000,
            to=caller_code_address,
            value=call_value,
            data=caller_payload,
            authorization_list=[
                AuthorizationTuple(
                    address=Address(system_contract),
                    nonce=auth_signer.nonce,
                    signer=auth_signer,
                ),
            ],
        )
    ]

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=txs,
                requests_hash=Requests(),  # Verify nothing slipped into the requests trie
            )
        ],
        post={
            auth_signer: Account(
                nonce=auth_signer.nonce + 1,
                code=Spec.delegation_designation(Address(system_contract)),
            ),
            caller_code_address: Account(
                storage=caller_code_storage,
            ),
        },
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) call_opcode evm_code_type system_contract
...fork_Prague-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x00000000219ab540356cbb839cbe05303d7705fa-blockchain_test CALL LEGACY 0x00000000219ab540356cbb839cbe05 303d7705fa
...fork_Prague-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x00000961ef480eb55e80d19ad83579a64c007002-blockchain_test CALL LEGACY 0x00000961ef480eb55e80d19ad83579 a64c007002
...fork_Prague-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x0000bbddc7ce488642fb579f8b00f3a590007251-blockchain_test CALL LEGACY 0x0000bbddc7ce488642fb579f8b00f3 a590007251
...fork_Prague-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x0000f90827f1c53a10cb7a02335b175320002935-blockchain_test CALL LEGACY 0x0000f90827f1c53a10cb7a02335b17 5320002935
...fork_Prague-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x000f3df6d732807ef1319fb7b8bb8522d0beac02-blockchain_test CALL LEGACY 0x000f3df6d732807ef1319fb7b8bb85 22d0beac02
...fork_Osaka-call_opcode_EXTCALL-evm_code_type_EOF_V1-system_contract_0x00000000219ab540356cbb839cbe05303d7705fa-blockchain_test EXTCALL EOF_V1 0x00000000219ab540356cbb839cbe05 303d7705fa
...fork_Osaka-call_opcode_EXTCALL-evm_code_type_EOF_V1-system_contract_0x00000961ef480eb55e80d19ad83579a64c007002-blockchain_test EXTCALL EOF_V1 0x00000961ef480eb55e80d19ad83579 a64c007002
...fork_Osaka-call_opcode_EXTCALL-evm_code_type_EOF_V1-system_contract_0x0000bbddc7ce488642fb579f8b00f3a590007251-blockchain_test EXTCALL EOF_V1 0x0000bbddc7ce488642fb579f8b00f3 a590007251
...fork_Osaka-call_opcode_EXTCALL-evm_code_type_EOF_V1-system_contract_0x0000f90827f1c53a10cb7a02335b175320002935-blockchain_test EXTCALL EOF_V1 0x0000f90827f1c53a10cb7a02335b17 5320002935
...fork_Osaka-call_opcode_EXTCALL-evm_code_type_EOF_V1-system_contract_0x000f3df6d732807ef1319fb7b8bb8522d0beac02-blockchain_test EXTCALL EOF_V1 0x000f3df6d732807ef1319fb7b8bb85 22d0beac02
...fork_Osaka-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x00000000219ab540356cbb839cbe05303d7705fa-blockchain_test CALL LEGACY 0x00000000219ab540356cbb839cbe05 303d7705fa
...fork_Osaka-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x00000961ef480eb55e80d19ad83579a64c007002-blockchain_test CALL LEGACY 0x00000961ef480eb55e80d19ad83579 a64c007002
...fork_Osaka-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x0000bbddc7ce488642fb579f8b00f3a590007251-blockchain_test CALL LEGACY 0x0000bbddc7ce488642fb579f8b00f3 a590007251
...fork_Osaka-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x0000f90827f1c53a10cb7a02335b175320002935-blockchain_test CALL LEGACY 0x0000f90827f1c53a10cb7a02335b17 5320002935
...fork_Osaka-call_opcode_CALL-evm_code_type_LEGACY-system_contract_0x000f3df6d732807ef1319fb7b8bb8522d0beac02-blockchain_test CALL LEGACY 0x000f3df6d732807ef1319fb7b8bb85 22d0beac02