Skip to content

test_bal_selfdestruct_to_coinbase()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_selfdestruct_to_coinbase@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_selfdestruct_to_coinbase --fork Amsterdam

Ensure BAL records SELFDESTRUCT when the beneficiary is the coinbase.

Post-Cancun (EIP-6780) the contract is only actually destroyed when created in the same tx; the pre-deployed path only transfers balance and preserves the contract. Both shapes must appear in BAL.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
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
641
642
643
644
645
646
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
@pytest.mark.parametrize(
    "same_tx", [False, True], ids=["pre_deploy", "same_tx"]
)
def test_bal_selfdestruct_to_coinbase(
    pre: Alloc,
    state_test: StateTestFiller,
    same_tx: bool,
) -> None:
    """
    Ensure BAL records SELFDESTRUCT when the beneficiary is the coinbase.

    Post-Cancun (EIP-6780) the contract is only actually destroyed when
    created in the same tx; the pre-deployed path only transfers balance
    and preserves the contract. Both shapes must appear in BAL.
    """
    alice = pre.fund_eoa()
    coinbase = pre.fund_eoa(amount=0)
    victim_balance = 100
    victim_code = Op.SELFDESTRUCT(Op.COINBASE)

    # Match gas_price to base_fee so the priority-fee tip is zero;
    # coinbase's BAL entry then carries only the SELFDESTRUCT transfer.
    base_fee_per_gas = 7
    env = Environment(
        base_fee_per_gas=base_fee_per_gas, fee_recipient=coinbase
    )

    account_expectations: dict[Address, BalAccountExpectation]

    if same_tx:
        initcode = Initcode(deploy_code=victim_code)
        factory_code = Om.MSTORE(initcode, 0) + Op.CALL(
            gas=Op.GAS,
            address=Op.CREATE(
                value=victim_balance, offset=0, size=len(initcode)
            ),
        )
        factory = pre.deploy_contract(
            code=factory_code, balance=victim_balance
        )
        victim = compute_create_address(address=factory, nonce=1)
        tx_target = factory
        post = {
            factory: Account(balance=0),
            victim: Account.NONEXISTENT,
            coinbase: Account(balance=victim_balance),
        }
        account_expectations = {
            factory: BalAccountExpectation(
                nonce_changes=[
                    BalNonceChange(block_access_index=1, post_nonce=2)
                ],
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=0)
                ],
            ),
            # Created and destroyed in the same tx — empty changes.
            victim: BalAccountExpectation.empty(),
            coinbase: BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(
                        block_access_index=1, post_balance=victim_balance
                    )
                ],
            ),
        }
    else:
        victim = pre.deploy_contract(code=victim_code, balance=victim_balance)
        tx_target = victim
        # Pre-deployed and not same-tx: post-Cancun preserves the contract.
        post = {
            victim: Account(balance=0, code=victim_code),
            coinbase: Account(balance=victim_balance),
        }
        account_expectations = {
            victim: BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=0)
                ],
            ),
            coinbase: BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(
                        block_access_index=1, post_balance=victim_balance
                    )
                ],
            ),
        }

    tx = Transaction(
        sender=alice,
        to=tx_target,
        gas_price=base_fee_per_gas,
    )

    state_test(
        env=env,
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(
            base_fee_per_gas=base_fee_per_gas
        ),
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations=account_expectations,
        ),
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.