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@0f7c73a7.

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
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
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
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
@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
            # 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