Skip to content

test_selfdestruct_created_same_block_different_tx()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_selfdestruct_created_same_block_different_tx@49a16fac.

Generate fixtures for these test cases for Prague with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_selfdestruct_created_same_block_different_tx --fork Prague

Test that if an account created in the same block that contains a selfdestruct is called, its balance is sent to the send-all address, but the account is not deleted.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
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
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 1])
@pytest.mark.parametrize("call_times", [1, 10])
@pytest.mark.valid_from("Shanghai")
def test_selfdestruct_created_same_block_different_tx(
    blockchain_test: BlockchainTestFiller,
    eip_enabled: bool,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    selfdestruct_contract_initial_balance: int,
    sendall_recipient_addresses: List[Address],
    call_times: int,
):
    """
    Test that if an account created in the same block that contains a selfdestruct is
    called, its balance is sent to the send-all address, but the account is not deleted.
    """
    selfdestruct_code = selfdestruct_code_preset(
        sendall_recipient_addresses=sendall_recipient_addresses,
    )
    selfdestruct_contract_initcode = Initcode(deploy_code=selfdestruct_code)
    selfdestruct_contract_address = compute_create_address(address=sender, nonce=0)
    entry_code_address = compute_create_address(address=sender, nonce=1)
    entry_code_storage = Storage()
    sendall_amount = selfdestruct_contract_initial_balance
    entry_code = Bytecode()

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

    # Call the self-destructing contract multiple times as required, increasing the wei sent each
    # time
    entry_code_balance = 0
    for i in range(call_times):
        entry_code += Op.SSTORE(
            entry_code_storage.store_next(1),
            Op.CALL(
                Op.GASLIMIT,  # Gas
                selfdestruct_contract_address,  # Address
                i,  # Value
                0,
                0,
                0,
                0,
            ),
        )
        entry_code_balance += i
        sendall_amount += i

        entry_code += Op.SSTORE(
            entry_code_storage.store_next(0),
            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)

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            storage=entry_code_storage,
        ),
        sendall_recipient_addresses[0]: Account(balance=sendall_amount, storage={0: 1}),
    }

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

    txs = [
        Transaction(
            value=selfdestruct_contract_initial_balance,
            data=selfdestruct_contract_initcode,
            sender=sender,
            to=None,
            gas_limit=500_000,
        ),
        Transaction(
            value=entry_code_balance,
            data=entry_code,
            sender=sender,
            to=None,
            gas_limit=500_000,
        ),
    ]

    blockchain_test(genesis_environment=env, pre=pre, post=post, blocks=[Block(txs=txs)])

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) call_times selfdestruct_contract_initial_balance
...fork_Shanghai-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_0 1 0
...fork_Shanghai-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_1 1 1
...fork_Shanghai-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_0 10 0
...fork_Shanghai-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_1 10 1
...fork_Cancun-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_0 1 0
...fork_Cancun-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_1 1 1
...fork_Cancun-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_0 10 0
...fork_Cancun-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_1 10 1
...fork_Prague-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_0 1 0
...fork_Prague-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_1 1 1
...fork_Prague-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_0 10 0
...fork_Prague-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_1 10 1
...fork_Osaka-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_0 1 0
...fork_Osaka-blockchain_test-call_times_1-selfdestruct_contract_initial_balance_1 1 1
...fork_Osaka-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_0 10 0
...fork_Osaka-blockchain_test-call_times_10-selfdestruct_contract_initial_balance_1 10 1