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@21fb11c8.

Generate fixtures for these test cases for Prague with:

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

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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
@pytest.mark.parametrize("create_opcode", [Op.CREATE2])  # Can only recreate using 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,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    selfdestruct_code: Bytecode,
    selfdestruct_contract_initial_balance: int,
    sendall_recipient_addresses: List[Address],
    create_opcode: Op,
    recreate_times: int,  # Number of times to recreate the contract in different transactions
    call_times: int,  # Number of times to call the self-destructing contract in the same tx
):
    """
    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

    # 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
    )
    pre.fund_address(selfdestruct_contract_address, selfdestruct_contract_initial_balance)
    for i in range(len(sendall_recipient_addresses)):
        if sendall_recipient_addresses[i] == SELF_ADDRESS:
            sendall_recipient_addresses[i] = selfdestruct_contract_address

    txs: List[Transaction] = []
    for i in range(recreate_times + 1):
        txs.append(
            Transaction(
                data=Hash(i),
                sender=sender,
                to=entry_code_address,
                gas_limit=500_000,
            )
        )
        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(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) sendall_recipient_addresses call_times recreate_times selfdestruct_contract_initial_balance create_opcode
...fork_Shanghai-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 0 CREATE2
...fork_Shanghai-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 0 CREATE2
...fork_Shanghai-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 100000 CREATE2
...fork_Shanghai-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 100000 CREATE2
...fork_Cancun-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 0 CREATE2
...fork_Cancun-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 0 CREATE2
...fork_Cancun-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 100000 CREATE2
...fork_Cancun-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 100000 CREATE2
...fork_Prague-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 0 CREATE2
...fork_Prague-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 0 CREATE2
...fork_Prague-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 100000 CREATE2
...fork_Prague-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 100000 CREATE2
...fork_Osaka-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 0 CREATE2
...fork_Osaka-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_0-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 0 CREATE2
...fork_Osaka-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_other_address-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 1 100000 CREATE2
...fork_Osaka-blockchain_test-call_times_1-recreate_times_1-selfdestruct_contract_initial_balance_100000-selfdestruct_to_self-create_opcode_CREATE2 [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'] 1 1 100000 CREATE2