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
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 | @pytest.mark.parametrize(
"is_success", [True, False], ids=["exact_gas", "exact_gas_minus_1"]
)
@pytest.mark.with_all_precompiles
@pytest.mark.parametrize(
"same_tx", [False, True], ids=["pre_deploy", "same_tx"]
)
@pytest.mark.parametrize(
"originator_balance",
[0, 1],
ids=["no_balance", "has_balance"],
)
@pytest.mark.parametrize(
"beneficiary_initial_balance",
[
pytest.param(0, id="dead_beneficiary"),
pytest.param(1, id="alive_beneficiary"),
],
)
@pytest.mark.valid_from("TangerineWhistle")
def test_selfdestruct_to_precompile(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
is_success: bool,
precompile: Address,
same_tx: bool,
originator_balance: int,
beneficiary_initial_balance: int,
) -> None:
"""
Test SELFDESTRUCT success boundary for precompile beneficiaries.
Precompiles are always warm (no cold access charge).
- exact_gas: succeeds, balance transferred, contract destroyed
- exact_gas_minus_1: OOG, operation fails
"""
# Fund precompile if needed
if beneficiary_initial_balance > 0:
pre.fund_address(precompile, beneficiary_initial_balance)
# Precompiles are dead when they have no balance
beneficiary_dead = beneficiary_initial_balance == 0
# Calculate exact gas for success (includes NEW_ACCOUNT if applicable)
# Precompiles are always warm
inner_call_gas = calculate_selfdestruct_gas(
fork,
beneficiary_warm=True, # Precompiles are always warm
beneficiary_dead=beneficiary_dead,
originator_balance=originator_balance,
)
if not is_success:
inner_call_gas -= 1
# In BAL if: success OR NEW_ACCOUNT charged (OOG after access)
needs_new_account = False
if beneficiary_dead:
if fork >= SpuriousDragon:
needs_new_account = originator_balance > 0
else:
needs_new_account = True
beneficiary_in_bal = is_success or needs_new_account
alice, caller, victim, tx = setup_selfdestruct_test(
pre,
fork,
precompile,
originator_balance,
same_tx,
beneficiary_warm=True, # Precompiles are always warm
inner_call_gas=inner_call_gas,
)
expected_bal = build_bal_expectations(
fork,
alice,
caller,
victim,
precompile,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=is_success,
beneficiary_in_bal=beneficiary_in_bal,
)
post = build_post_state(
fork,
alice,
caller,
victim,
precompile,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=is_success,
beneficiary_has_code=False, # Precompiles don't have stored code
)
blockchain_test(
pre=pre,
blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
post=post,
)
|