Skip to content

test_selfdestruct_to_system_contract()

Documentation for tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_selfdestruct_to_system_contract@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_selfdestruct_to_system_contract --fork Amsterdam

Test SELFDESTRUCT success boundary for system contract beneficiaries.

System contracts are always warm (no cold access charge) and always have code (so beneficiary is never dead, no NEW_ACCOUNT charge).

  • exact_gas: succeeds, balance transferred
  • exact_gas_minus_1: OOG, operation fails
Source code in tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
@pytest.mark.parametrize(
    "is_success", [True, False], ids=["exact_gas", "exact_gas_minus_1"]
)
@pytest.mark.with_all_system_contracts
@pytest.mark.parametrize(
    "same_tx", [False, True], ids=["pre_deploy", "same_tx"]
)
@pytest.mark.parametrize(
    "originator_balance",
    [0, 1],
    ids=["no_balance", "has_balance"],
)
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_to_system_contract(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    is_success: bool,
    system_contract: Address,
    same_tx: bool,
    originator_balance: int,
) -> None:
    """
    Test SELFDESTRUCT success boundary for system contract beneficiaries.

    System contracts are always warm (no cold access charge) and always have
    code (so beneficiary is never dead, no NEW_ACCOUNT charge).

    - exact_gas: succeeds, balance transferred
    - exact_gas_minus_1: OOG, operation fails
    """
    # Calculate exact gas for success
    # System contracts are always warm and never dead (have code)
    inner_call_gas = calculate_selfdestruct_gas(
        fork,
        beneficiary_warm=True,
        beneficiary_dead=False,
        originator_balance=originator_balance,
    )
    if not is_success:
        inner_call_gas -= 1

    alice, caller, victim, tx = setup_selfdestruct_test(
        pre,
        fork,
        system_contract,
        originator_balance,
        same_tx,
        beneficiary_warm=True,
        inner_call_gas=inner_call_gas,
    )

    # Build minimal BAL expectations for test-specific accounts only
    expected_bal: BlockAccessListExpectation | None = None
    if fork.is_eip_enabled(7928):
        account_expectations: Dict[Address, BalAccountExpectation | None] = {
            alice: BalAccountExpectation(
                nonce_changes=[
                    BalNonceChange(block_access_index=1, post_nonce=1)
                ],
            ),
        }

        # Victim expectation
        if same_tx:
            if is_success:
                # Created and destroyed in same tx - no net changes
                victim_expectation = BalAccountExpectation.empty()
            else:
                # OOG: contract created but selfdestruct failed
                victim_expectation = BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                    code_changes=[
                        BalCodeChange(
                            block_access_index=1,
                            new_code=bytes(Op.SELFDESTRUCT(system_contract)),
                        )
                    ],
                )
                if originator_balance > 0:
                    victim_expectation.balance_changes.append(
                        BalBalanceChange(
                            block_access_index=1,
                            post_balance=originator_balance,
                        )
                    )
            # Caller nonce incremented for CREATE
            caller_expectation = BalAccountExpectation(
                nonce_changes=[
                    BalNonceChange(block_access_index=1, post_nonce=2)
                ],
            )
            if originator_balance > 0 and is_success:
                caller_expectation.balance_changes.append(
                    BalBalanceChange(block_access_index=1, post_balance=0)
                )
            account_expectations[caller] = caller_expectation
        else:
            # Pre-existing victim
            if is_success and originator_balance > 0:
                victim_expectation = BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(block_access_index=1, post_balance=0)
                    ],
                )
            else:
                victim_expectation = BalAccountExpectation.empty()
            account_expectations[caller] = BalAccountExpectation.empty()

        account_expectations[victim] = victim_expectation

        # System contract receives balance if success and originator
        # had balance
        if is_success and originator_balance > 0:
            account_expectations[system_contract] = BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(
                        block_access_index=1, post_balance=originator_balance
                    )
                ],
            )

        expected_bal = BlockAccessListExpectation(
            account_expectations=account_expectations
        )

    post = build_post_state(
        fork,
        alice,
        caller,
        victim,
        system_contract,
        originator_balance,
        beneficiary_initial_balance=0,
        same_tx=same_tx,
        success=is_success,
        beneficiary_has_code=True,
    )

    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
        post=post,
    )

Parametrized Test Cases

This test generates 40 parametrized test cases across 4 forks.