Skip to content

test_selfdestruct_finalization_after_priority_fee()

Documentation for tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py::test_selfdestruct_finalization_after_priority_fee@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py::test_selfdestruct_finalization_after_priority_fee --fork Amsterdam

Verify finalization burn logs are emitted after priority fee payment.

Sets coinbase to a contract that self-destructs in the same tx. The finalization burn log includes the priority fee, proving finalization happens after fee payment per EIP-7708.

funded_after_selfdestruct: - if True: payer sends ETH, finalization = funding + priority_fee - if False: no payer, finalization = priority_fee only

Source code in tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
@pytest.mark.parametrize(
    "funded_after_selfdestruct",
    [
        pytest.param(True, id="funded_after_selfdestruct"),
        pytest.param(False, id="miner_fee_only"),
    ],
)
def test_selfdestruct_finalization_after_priority_fee(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    funded_after_selfdestruct: bool,
) -> None:
    """
    Verify finalization burn logs are emitted after priority fee payment.

    Sets coinbase to a contract that self-destructs in the same tx. The
    finalization burn log includes the priority fee, proving finalization
    happens after fee payment per EIP-7708.

    funded_after_selfdestruct:
    - if True: payer sends ETH, finalization = funding + priority_fee
    - if False: no payer, finalization = priority_fee only
    """
    genesis_base_fee = 7
    env = Environment(base_fee_per_gas=genesis_base_fee)
    contract_balance = 1000
    funding_amount = 10_000 if funded_after_selfdestruct else 0

    sender = pre.fund_eoa()

    factory_address = compute_create_address(address=sender, nonce=0)
    created_address = compute_create_address(address=factory_address, nonce=1)
    coinbase = created_address  # coinbase == self-destructed contract

    # inner contract: simple SELFDESTRUCT to self
    runtime_code = (
        Op.SELFDESTRUCT(
            Op.ADDRESS,
            # Gas accounting
            address_warm=True,
            account_new=False,
            self_destructed_account=True,
            self_destructed_account_code_deposit=len(
                Op.SELFDESTRUCT(address=Op.ADDRESS)
            ),
        )
        if fork.is_eip_enabled(8037)
        else Op.SELFDESTRUCT(
            Op.ADDRESS,
            # Gas accounting
            address_warm=True,
            account_new=False,
        )
    )
    initcode = Initcode(deploy_code=runtime_code)
    initcode_len = len(initcode)

    gas_costs = fork.gas_costs()
    mem_after_mstore = ((initcode_len + 31) // 32) * 32

    # The base factory code: CREATE + CALL to trigger selfdestruct
    call_gas = 100_000
    if fork.is_eip_enabled(8037):
        call_gas = 500_000
    factory_code = Om.MSTORE(
        initcode, 0, new_memory_size=mem_after_mstore
    ) + Op.CALL(
        gas=call_gas,
        address=Op.CREATE(
            value=contract_balance,
            offset=0,
            size=initcode_len,
            init_code_size=initcode_len,
        ),
        address_warm=True,
    )

    # optionally add payer call to fund coinbase after selfdestruct
    payer = None
    payer_runtime_gas = 0
    if funded_after_selfdestruct:
        payer_code = Op.SELFDESTRUCT(Op.CALLDATALOAD(0))
        payer = pre.deploy_contract(payer_code, balance=funding_amount)
        factory_code += Op.MSTORE(0, created_address)
        factory_code += Op.CALL(
            gas=call_gas, address=payer, args_offset=0, args_size=32
        )
        payer_runtime_gas = Op.SELFDESTRUCT(
            Op.CALLDATALOAD(0), address_warm=True, account_new=False
        ).gas_cost(fork)

    pre.fund_address(factory_address, contract_balance)

    # prio fee calc
    gas_price = 10
    base_fee = fork.base_fee_per_gas_calculator()(
        parent_base_fee_per_gas=genesis_base_fee,
        parent_gas_used=0,
        parent_gas_limit=env.gas_limit,
    )
    priority_fee_per_gas = gas_price - base_fee

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=bytes(factory_code),
        contract_creation=True,
    )
    factory_gas = factory_code.gas_cost(fork)
    initcode_exec_gas = initcode.execution_gas(fork)
    code_deposit_gas = len(runtime_code) * gas_costs.CODE_DEPOSIT_PER_BYTE
    inner_runtime_gas = runtime_code.gas_cost(fork)

    gas_used = (
        intrinsic_gas
        + factory_gas
        + initcode_exec_gas
        + code_deposit_gas
        + inner_runtime_gas
        + payer_runtime_gas
    )

    inner_runtime_refund = runtime_code.refund(fork)
    gas_refunds = inner_runtime_refund
    discount = min(
        gas_refunds,
        gas_used // 5,  # max discount EIP-3529
    )
    priority_fee = priority_fee_per_gas * (gas_used - discount)

    # Finalization burn log proves coinbase received priority fee before log
    finalization_balance: int | None = funding_amount + priority_fee

    expected_logs = [
        transfer_log(factory_address, created_address, contract_balance),
        burn_log(created_address, contract_balance),
    ]

    # if funded after selfdestruct, expect transfer log from payer
    if funded_after_selfdestruct:
        assert payer is not None
        expected_logs.append(
            transfer_log(payer, created_address, funding_amount)
        )

    # finalization burn log
    if fork.is_eip_enabled(8037):
        raise Exception(
            "Test needs update: recompute exact gas usage with 8037"
        )

    expected_logs.append(burn_log(created_address, finalization_balance))
    gas_limit = 500_000
    if fork.is_eip_enabled(8037):
        gas_limit = 2_000_000

    tx = Transaction(
        sender=sender,
        to=None,
        value=0,
        data=factory_code,
        gas_limit=gas_limit,
        gas_price=gas_price,
        expected_receipt=TransactionReceipt(logs=expected_logs),
    )

    post: dict[Address, Account | None] = {
        created_address: Account.NONEXISTENT,
    }
    if payer is not None:
        post[payer] = Account(balance=0)

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                fee_recipient=coinbase,
                header_verify=Header(base_fee_per_gas=base_fee),
            )
        ],
        post=post,
        genesis_environment=env,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.