Skip to content

test_self_destructing_initcode_create_tx()

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

Generate fixtures for these test cases for Amsterdam with:

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

Use a Create Transaction to execute a self-destructing initcode.

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

Test using
  • Different initial balances for the self-destructing contract
  • Different transaction value amounts
Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
@pytest.mark.parametrize("tx_value", [0, 100_000])
@pytest.mark.parametrize(
    "selfdestruct_contract_initial_balance",
    [0, 100_000],
)
@pytest.mark.valid_from("Shanghai")
def test_self_destructing_initcode_create_tx(
    state_test: StateTestFiller,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    tx_value: int,
    selfdestruct_code: Bytecode,
    sendall_recipient_addresses: List[Address],
    selfdestruct_contract_initial_balance: int,
) -> None:
    """
    Use a Create Transaction to execute a self-destructing initcode.

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

    Test using:
      - Different initial balances for the self-destructing contract
      - Different transaction value amounts
    """
    tx = Transaction(
        sender=sender,
        value=tx_value,
        data=selfdestruct_code,
        to=None,
    )
    selfdestruct_contract_address = tx.created_contract
    if selfdestruct_contract_initial_balance > 0:
        pre.fund_address(
            selfdestruct_contract_address,
            selfdestruct_contract_initial_balance,
        )

    # Our entry point is an initcode that in turn creates a self-destructing
    # contract
    sendall_amount = selfdestruct_contract_initial_balance + tx_value

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

    if fork.is_eip_enabled(7708):
        expected_logs = []
        if tx_value > 0:
            expected_logs.append(
                transfer_log(sender, selfdestruct_contract_address, tx_value)
            )
        if sendall_amount > 0:
            expected_logs.append(
                transfer_log(
                    selfdestruct_contract_address,
                    sendall_recipient_addresses[0],
                    sendall_amount,
                )
            )
        tx.expected_receipt = TransactionReceipt(logs=expected_logs)

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 5 forks.