Skip to content

test_selfdestruct_pre_existing()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_selfdestruct_pre_existing@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_selfdestruct_pre_existing --fork Amsterdam

Test calling a previously created account that contains a selfdestruct, and verify its balance is sent to the destination address.

After EIP-6780, the balance should be sent to the send-all recipient address, similar to the behavior before the EIP, but the account is not deleted.

Test using: - Different send-all recipient addresses: single, multiple, including self - Different initial balances for the self-destructing contract

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
 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
 925
 926
 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
@pytest.mark.parametrize(
    "call_times,sendall_recipient_addresses",
    [
        pytest.param(
            1,
            [PRE_DEPLOY_CONTRACT_1],
            id="single_call",
        ),
        pytest.param(
            1,
            [SELF_ADDRESS],
            id="single_call_self",
        ),
        pytest.param(
            2,
            [PRE_DEPLOY_CONTRACT_1],
            id="multiple_calls_single_sendall_recipient",
        ),
        pytest.param(
            2,
            [SELF_ADDRESS],
            id="multiple_calls_single_self_recipient",
        ),
        pytest.param(
            3,
            [
                PRE_DEPLOY_CONTRACT_1,
                PRE_DEPLOY_CONTRACT_2,
                PRE_DEPLOY_CONTRACT_3,
            ],
            id="multiple_calls_multiple_sendall_recipients",
        ),
        pytest.param(
            3,
            [SELF_ADDRESS, PRE_DEPLOY_CONTRACT_2, PRE_DEPLOY_CONTRACT_3],
            id="multiple_calls_multiple_sendall_recipients_including_self",
        ),
        pytest.param(
            3,
            [PRE_DEPLOY_CONTRACT_1, PRE_DEPLOY_CONTRACT_2, SELF_ADDRESS],
            id="multiple_calls_multiple_sendall_recipients_including_self_last",
        ),
        pytest.param(
            6,
            [SELF_ADDRESS, PRE_DEPLOY_CONTRACT_2, PRE_DEPLOY_CONTRACT_3],
            id="multiple_calls_multiple_repeating_sendall_recipients_including_self",
        ),
        pytest.param(
            6,
            [PRE_DEPLOY_CONTRACT_1, PRE_DEPLOY_CONTRACT_2, SELF_ADDRESS],
            id="multiple_calls_multiple_repeating_sendall_recipients_including_self_last",
        ),
    ],
    indirect=["sendall_recipient_addresses"],
)
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 100_000])
@pytest.mark.valid_from("Shanghai")
def test_selfdestruct_pre_existing(
    state_test: StateTestFiller,
    eip_enabled: bool,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    selfdestruct_code: Bytecode,
    selfdestruct_contract_initial_balance: int,
    sendall_recipient_addresses: List[Address],
    call_times: int,
) -> None:
    """
    Test calling a previously created account that contains a selfdestruct, and
    verify its balance is sent to the destination address.

    After EIP-6780, the balance should be sent to the send-all recipient
    address, similar to the behavior before the EIP, but the account is not
    deleted.

    Test using:
    - Different send-all recipient addresses: single, multiple,
       including self
    - Different initial balances for the self-destructing contract
    """
    selfdestruct_contract_address = pre.deploy_contract(
        selfdestruct_code, balance=selfdestruct_contract_initial_balance
    )
    entry_code_storage = Storage()

    for i, addr in enumerate(sendall_recipient_addresses):
        if addr == SELF_ADDRESS:
            sendall_recipient_addresses[i] = selfdestruct_contract_address

    # Create a dict to record the expected final balances
    sendall_final_balances = dict(
        zip(
            sendall_recipient_addresses,
            [0] * len(sendall_recipient_addresses),
            strict=False,
        )
    )
    selfdestruct_contract_current_balance = (
        selfdestruct_contract_initial_balance
    )

    # Entry code in this case will simply call the pre-existing self-
    # destructing contract, as many times as required
    entry_code = Bytecode()

    # Pre-compute the entry_code_address to use for Transfer log attribution.
    entry_code_address = compute_create_address(address=sender, nonce=0)

    # Call the self-destructing contract multiple times as required, increasing
    # the wei sent each time
    expected_logs_after_tx_value: list = []
    entry_code_balance = 0
    for i, sendall_recipient in zip(
        range(call_times), cycle(sendall_recipient_addresses)
    ):
        entry_code += Op.MSTORE(0, sendall_recipient)
        entry_code += Op.SSTORE(
            entry_code_storage.store_next(1),
            Op.CALL(
                Op.GASLIMIT,  # Gas
                selfdestruct_contract_address,  # Address
                i,  # Value
                0,
                32,
                0,
                0,
            ),
        )
        entry_code_balance += i
        selfdestruct_contract_current_balance += i

        # CALL with nonzero value emits Transfer(entry_code -> contract).
        if i > 0:
            expected_logs_after_tx_value.append(
                transfer_log(
                    entry_code_address, selfdestruct_contract_address, i
                )
            )

        # SELFDESTRUCT emits Transfer to a different recipient; for a
        # pre-existing contract sending to itself, no log is emitted (balance
        # stays). Pre-Cancun, SD also burns on self, but EIP-7708 is
        # Amsterdam+, long after EIP-6780 is enabled, so the self-keep path
        # applies here.
        if (
            sendall_recipient != selfdestruct_contract_address
            and selfdestruct_contract_current_balance > 0
        ):
            expected_logs_after_tx_value.append(
                transfer_log(
                    selfdestruct_contract_address,
                    sendall_recipient,
                    selfdestruct_contract_current_balance,
                )
            )

        # Balance is always sent to other contracts
        if sendall_recipient != selfdestruct_contract_address:
            sendall_final_balances[sendall_recipient] += (
                selfdestruct_contract_current_balance
            )

        # Balance is only kept by the self-destructing contract if we are
        # sending to self and the EIP is activated, otherwise the balance is
        # destroyed
        if (
            sendall_recipient != selfdestruct_contract_address
            or not eip_enabled
        ):
            selfdestruct_contract_current_balance = 0

        entry_code += Op.SSTORE(
            entry_code_storage.store_next(
                selfdestruct_contract_current_balance
            ),
            Op.BALANCE(selfdestruct_contract_address),
        )

    # Check the EXTCODE* properties of the self-destructing contract
    entry_code += Op.SSTORE(
        entry_code_storage.store_next(len(selfdestruct_code)),
        Op.EXTCODESIZE(selfdestruct_contract_address),
    )

    entry_code += Op.SSTORE(
        entry_code_storage.store_next(selfdestruct_code.keccak256()),
        Op.EXTCODEHASH(selfdestruct_contract_address),
    )

    # Lastly return zero so the entry point contract is created and we can
    # retain the stored values for verification.
    entry_code += Op.RETURN(32, 1)

    tx = Transaction(
        value=entry_code_balance,
        data=entry_code,
        sender=sender,
        to=None,
    )

    assert tx.created_contract == entry_code_address

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            storage=entry_code_storage,
        ),
    }

    # Check the balances of the sendall recipients
    for address, balance in sendall_final_balances.items():
        if address != selfdestruct_contract_address:
            post[address] = Account(balance=balance, storage={0: 1})

    if eip_enabled:
        balance = selfdestruct_contract_current_balance
        post[selfdestruct_contract_address] = Account(
            balance=balance,
            storage={0: call_times},
        )
    else:
        post[selfdestruct_contract_address] = Account.NONEXISTENT  # type: ignore

    if fork.is_eip_enabled(7708):
        expected_logs = []
        if entry_code_balance > 0:
            expected_logs.append(
                transfer_log(sender, entry_code_address, entry_code_balance)
            )
        expected_logs.extend(expected_logs_after_tx_value)
        tx.expected_receipt = TransactionReceipt(logs=expected_logs)

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 18 parametrized test cases across 5 forks.