Skip to content

test_recreate_self_destructed_contract_different_txs()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_recreate_self_destructed_contract_different_txs@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

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

Test that a contract can be recreated after it has self-destructed, over the lapse of multiple transactions.

Behavior should be the same before and after EIP-6780.

Test using
  • Different initial balances for the self-destructing contract
  • Contract creating opcodes that are not CREATE
Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
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
@pytest.mark.parametrize("create_opcode", [Op.CREATE2])
@pytest.mark.parametrize(
    "sendall_recipient_addresses",
    [
        pytest.param(
            [PRE_DEPLOY_CONTRACT_1],
            id="selfdestruct_other_address",
        ),
        pytest.param(
            [SELF_ADDRESS],
            id="selfdestruct_to_self",
        ),
    ],
    indirect=["sendall_recipient_addresses"],
)
@pytest.mark.parametrize(
    "selfdestruct_contract_initial_balance",
    [0, 100_000],
)
@pytest.mark.parametrize("recreate_times", [1])
@pytest.mark.parametrize("call_times", [1])
@pytest.mark.valid_from("Shanghai")
def test_recreate_self_destructed_contract_different_txs(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    selfdestruct_code: Bytecode,
    selfdestruct_contract_initial_balance: int,
    sendall_recipient_addresses: List[Address],
    create_opcode: Op,
    # Number of times to recreate the contract in different transactions
    recreate_times: int,
    # Number of times to call the self-destructing contract in the same tx
    call_times: int,
) -> None:
    """
    Test that a contract can be recreated after it has self-destructed, over
    the lapse of multiple transactions.

    Behavior should be the same before and after EIP-6780.

    Test using:
      - Different initial balances for the self-destructing contract
      - Contract creating opcodes that are not CREATE
    """
    selfdestruct_contract_initcode = Initcode(deploy_code=selfdestruct_code)
    initcode_copy_from_address = pre.deploy_contract(
        selfdestruct_contract_initcode
    )
    entry_code_storage = Storage()
    sendall_amount = selfdestruct_contract_initial_balance

    # Validate bytecode used to create the contract
    assert create_opcode != Op.CREATE, (
        "cannot recreate contract using CREATE opcode"
    )
    create_bytecode = create_opcode(size=len(selfdestruct_contract_initcode))

    # Entry code that will be executed, creates the contract and then calls it
    entry_code = (
        # Initcode is already deployed at initcode_copy_from_address, so just
        # copy it
        Op.EXTCODECOPY(
            initcode_copy_from_address,
            0,
            0,
            len(selfdestruct_contract_initcode),
        )
        + Op.MSTORE(0, create_bytecode)
        + Op.SSTORE(
            Op.CALLDATALOAD(0),
            Op.MLOAD(0),
        )
    )

    for i in range(call_times):
        entry_code += Op.CALL(
            Op.GASLIMIT,
            Op.MLOAD(0),
            i,
            0,
            0,
            0,
            0,
        )
        sendall_amount += i

    entry_code += Op.STOP

    entry_code_address = pre.deploy_contract(code=entry_code)
    selfdestruct_contract_address = compute_create_address(
        address=entry_code_address,
        initcode=selfdestruct_contract_initcode,
        opcode=create_opcode,
    )
    if selfdestruct_contract_initial_balance > 0:
        pre.fund_address(
            selfdestruct_contract_address,
            selfdestruct_contract_initial_balance,
        )
    for i, addr in enumerate(sendall_recipient_addresses):
        if addr == SELF_ADDRESS:
            sendall_recipient_addresses[i] = selfdestruct_contract_address

    txs: List[Transaction] = []
    for i in range(recreate_times + 1):
        expected_receipt = None
        if fork.is_eip_enabled(7708):
            # First tx: contract is recreated at the pre-funded address, then
            # SELFDESTRUCTs transferring initial_balance to the recipient
            # (or emitting a Burn log when SD to self). Subsequent txs see
            # address with 0 balance (destroyed+cleared), so no log.
            tx_logs: list = []
            if i == 0 and selfdestruct_contract_initial_balance > 0:
                if (
                    sendall_recipient_addresses[0]
                    == selfdestruct_contract_address
                ):
                    tx_logs.append(
                        burn_log(
                            selfdestruct_contract_address,
                            selfdestruct_contract_initial_balance,
                        )
                    )
                else:
                    tx_logs.append(
                        transfer_log(
                            selfdestruct_contract_address,
                            sendall_recipient_addresses[0],
                            selfdestruct_contract_initial_balance,
                        )
                    )
            expected_receipt = TransactionReceipt(logs=tx_logs)
        txs.append(
            Transaction(
                data=Hash(i),
                sender=sender,
                to=entry_code_address,
                gas_limit=500_000,
                expected_receipt=expected_receipt,
            )
        )
        entry_code_storage[i] = selfdestruct_contract_address

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            storage=entry_code_storage,
        ),
        selfdestruct_contract_address: Account.NONEXISTENT,  # type: ignore
    }
    if sendall_recipient_addresses[0] != selfdestruct_contract_address:
        post[sendall_recipient_addresses[0]] = Account(
            balance=sendall_amount, storage={0: 1}
        )

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 5 forks.