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@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

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
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
@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,
) -> None:
    """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
            # This payload is used to identify the number of blocks to be
            # subtracted from the latest block number
            caller_payload = Hash(1)
            caller_code_storage[call_return_data_size_slot] = 32
        case _:
            raise ValueError(f"Not implemented system contract: {system_contract}")

    # Setup the code to call the system contract
    match system_contract:
        case Address(0x0000F90827F1C53A10CB7A02335B175320002935):  # EIP-2935
            # Do a trick here to get the block number of the penultimate block
            # to ensure it is saved in the history contract
            check_block_number = Op.SUB(Op.NUMBER, Op.CALLDATALOAD(0))
            call_system_contract_code = Op.MSTORE(0, check_block_number) + Op.SSTORE(
                call_return_code_slot,
                call_opcode(address=auth_signer, value=call_value, args_size=32),
            )
        case _:
            # Call another system contract with fabricated payload
            call_system_contract_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),
            )

    caller_code = (
        call_system_contract_code
        + 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_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