Skip to content

test_selfdestruct_to_self()

Documentation for tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_selfdestruct_to_self@b47f0253.

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_self --fork Amsterdam

Test SELFDESTRUCT where beneficiary is the executing contract itself.

Uses Op.SELFDESTRUCT(Op.ADDRESS) - the victim selfdestructs to itself.

Key characteristics: - Beneficiary is always warm (it's the executing contract) - Beneficiary is always alive (EIP-161 nonce=1) - No NEW_ACCOUNT charge - No cold access charge (>=Berlin) - Balance is "transferred" to self (no net change until destruction)

Gas boundary: - exact_gas: SELFDESTRUCT completes successfully - exact_gas_minus_1: OOG, SELFDESTRUCT fails

Post-destruction behavior (is_success=True only): - Pre-Cancun or same_tx: contract destroyed, balance = 0 - >=Cancun pre-existing: contract NOT destroyed, balance preserved

Source code in tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
@pytest.mark.parametrize(
    "is_success", [True, False], ids=["exact_gas", "exact_gas_minus_1"]
)
@pytest.mark.parametrize(
    "originator_balance",
    [0, 1],
    ids=["no_balance", "has_balance"],
)
@pytest.mark.parametrize(
    "same_tx", [False, True], ids=["pre_deploy", "same_tx"]
)
@pytest.mark.valid_from("TangerineWhistle")
def test_selfdestruct_to_self(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    is_success: bool,
    originator_balance: int,
    same_tx: bool,
) -> None:
    """
    Test SELFDESTRUCT where beneficiary is the executing contract itself.

    Uses Op.SELFDESTRUCT(Op.ADDRESS) - the victim selfdestructs to itself.

    Key characteristics:
    - Beneficiary is always warm (it's the executing contract)
    - Beneficiary is always alive (EIP-161 nonce=1)
    - No NEW_ACCOUNT charge
    - No cold access charge (>=Berlin)
    - Balance is "transferred" to self (no net change until destruction)

    Gas boundary:
    - exact_gas: SELFDESTRUCT completes successfully
    - exact_gas_minus_1: OOG, SELFDESTRUCT fails

    Post-destruction behavior (is_success=True only):
    - Pre-Cancun or same_tx: contract destroyed, balance = 0
    - >=Cancun pre-existing: contract NOT destroyed, balance preserved
    """
    alice = pre.fund_eoa()
    victim_code = Op.SELFDESTRUCT(Op.ADDRESS)

    # Gas: ADDRESS + SELFDESTRUCT (no cold access, no NEW_ACCOUNT)
    base_gas = Op.SELFDESTRUCT(
        Op.ADDRESS, address_warm=True, account_new=False
    ).gas_cost(fork)
    inner_call_gas = base_gas if is_success else base_gas - 1

    if same_tx:
        # Deploy and selfdestruct in same transaction via factory
        initcode = Initcode(deploy_code=victim_code)
        initcode_len = len(initcode)

        factory_code = Om.MSTORE(initcode, 0) + Op.CALL(
            gas=inner_call_gas,
            address=Op.CREATE(
                value=originator_balance, offset=0, size=initcode_len
            ),
        )
        caller = pre.deploy_contract(
            code=factory_code,
            balance=originator_balance,
        )
        victim = compute_create_address(address=caller, nonce=1)
    else:
        # Pre-existing contract
        victim = pre.deploy_contract(
            code=victim_code, balance=originator_balance
        )
        caller_code = Op.CALL(gas=inner_call_gas, address=victim)
        caller = pre.deploy_contract(code=caller_code)

    tx = Transaction(
        sender=alice,
        to=caller,
        gas_limit=500_000,
        protected=fork.supports_protected_txs(),
    )

    # Build BAL expectations
    expected_bal: BlockAccessListExpectation | None = None
    if fork.is_eip_enabled(7928):
        if same_tx:
            if is_success:
                # Created and destroyed in same tx - no net changes for victim
                victim_expectation = BalAccountExpectation.empty()
            else:
                # OOG: CREATE succeeded 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(victim_code),
                        )
                    ],
                )
                if originator_balance > 0:
                    victim_expectation.balance_changes.append(
                        BalBalanceChange(
                            block_access_index=1,
                            post_balance=originator_balance,
                        )
                    )

            caller_expectation = BalAccountExpectation(
                nonce_changes=[
                    BalNonceChange(block_access_index=1, post_nonce=2)
                ],
            )
            if originator_balance > 0:
                caller_expectation.balance_changes.append(
                    BalBalanceChange(block_access_index=1, post_balance=0)
                )
        else:
            # Pre-existing: victim in BAL
            if not is_success:
                # OOG: victim accessed but no state changes
                victim_expectation = BalAccountExpectation.empty()
            elif fork >= Cancun:
                # >=Cancun success: contract survives with original balance
                victim_expectation = BalAccountExpectation.empty()
            elif originator_balance > 0:
                # Pre-Cancun success: contract destroyed
                victim_expectation = BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(block_access_index=1, post_balance=0)
                    ],
                )
            else:
                victim_expectation = BalAccountExpectation.empty()
            caller_expectation = BalAccountExpectation.empty()

        expected_bal = BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                ),
                caller: caller_expectation,
                victim: victim_expectation,
            }
        )

    # Build post state
    caller_nonce = 2 if same_tx else 1

    if not is_success:
        # OOG: SELFDESTRUCT failed, contract survives
        if same_tx:
            post: dict = {
                alice: Account(nonce=1),
                caller: Account(nonce=caller_nonce, balance=0),
                victim: Account(balance=originator_balance, code=victim_code),
            }
        else:
            post = {
                alice: Account(nonce=1),
                caller: Account(nonce=caller_nonce),
                victim: Account(balance=originator_balance, code=victim_code),
            }
    else:
        contract_destroyed = fork < Cancun or same_tx
        if contract_destroyed:
            post = {
                alice: Account(nonce=1),
                caller: Account(nonce=caller_nonce),
                victim: Account.NONEXISTENT,
            }
        else:
            # >=Cancun pre-existing: code preserved, balance preserved
            post = {
                alice: Account(nonce=1),
                caller: Account(nonce=caller_nonce),
                victim: Account(balance=originator_balance, code=victim_code),
            }

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

Parametrized Test Cases

This test generates 8 parametrized test cases across 12 forks.