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 | @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_state_access_boundary(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
is_success: bool,
precompile: Address,
same_tx: bool,
originator_balance: int,
beneficiary_initial_balance: int,
) -> None:
"""
Test state access boundary for precompile beneficiaries.
Consensus check: precompile must be accessed at base cost boundary,
before NEW_ACCOUNT is evaluated. Precompiles are always warm.
- exact_gas: precompile IS accessed (in BAL)
- exact_gas_minus_1: precompile NOT accessed (not in BAL)
"""
# Fund precompile if needed
if beneficiary_initial_balance > 0:
pre.fund_address(precompile, beneficiary_initial_balance)
beneficiary_dead = beneficiary_initial_balance == 0
# State access boundary: base cost only (no NEW_ACCOUNT)
# Precompiles are always warm
inner_call_gas = Op.SELFDESTRUCT(
0, address_warm=True, account_new=False
).gas_cost(fork)
if not is_success:
inner_call_gas -= 1
# Success at base cost if no NEW_ACCOUNT needed
needs_new_account = False
if beneficiary_dead:
if fork >= SpuriousDragon:
needs_new_account = originator_balance > 0
else:
needs_new_account = True
operation_success = is_success and not 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,
)
# Key difference: beneficiary_in_bal depends on is_success
# exact_gas: state accessed, precompile in BAL
# exact_gas_minus_1: OOG before state access, precompile NOT in BAL
expected_bal = build_bal_expectations(
fork,
alice,
caller,
victim,
precompile,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=operation_success,
beneficiary_in_bal=is_success,
)
post = build_post_state(
fork,
alice,
caller,
victim,
precompile,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=operation_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,
)
|