Skip to content

test_floor_cost_not_reduced_by_refunds()

Documentation for tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestRefundCapInteraction::test_floor_cost_not_reduced_by_refunds@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestRefundCapInteraction::test_floor_cost_not_reduced_by_refunds --fork Amsterdam

Test that the ⅕ refund cap interacts correctly with floor cost.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
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
931
932
933
934
935
936
class TestRefundCapInteraction:
    """Test that the 1/5 refund cap interacts correctly with floor cost."""

    @pytest.fixture
    def sender(self, pre: Alloc) -> Address:
        """Create sender account."""
        return pre.fund_eoa()

    def test_refund_cap_at_one_fifth(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        fork: Fork,
    ) -> None:
        """
        Test that refunds are capped at the fork max refund quotient.

        Even if the refund counter is high, the actual refund cannot exceed
        the fork cap. Use minimal calldata to avoid floor cost interference.
        """
        # Use minimal calldata so floor cost doesn't dominate
        calldata = Bytes(b"")

        # Deploy contract that clears multiple storage slots
        # This generates a large refund counter
        num_slots = 10
        code = Bytecode()
        storage = {i: 1 for i in range(num_slots)}  # noqa: C420

        for i in range(num_slots):
            code += Op.SSTORE(i, 0, original_value=1, new_value=0)

        code += Op.STOP
        contract = pre.deploy_contract(
            code,
            storage=storage,  # type: ignore[arg-type]
        )

        # Calculate costs
        intrinsic_cost_calculator = (
            fork.transaction_intrinsic_cost_calculator()
        )
        intrinsic_cost_before_execution = intrinsic_cost_calculator(
            calldata=calldata,
            contract_creation=False,
            access_list=None,
            authorization_list_or_count=None,
            return_cost_deducted_prior_execution=True,
        )

        execution_gas = code.gas_cost(fork)

        total_gas_before_refund = (
            intrinsic_cost_before_execution + execution_gas
        )

        refund_counter = code.refund(fork)

        # Actual refund is capped by the fork max refund quotient.
        refund_cap = total_gas_before_refund // fork.max_refund_quotient()
        actual_refund = min(refund_counter, refund_cap)

        # Verify that refund counter exceeds cap
        assert refund_counter > refund_cap, (
            "Test requires refund_counter > refund cap"
        )

        # Gas after refund (floor cost is minimal with empty calldata)
        expected_gas = total_gas_before_refund - actual_refund

        tx = Transaction(
            sender=sender,
            to=contract,
            data=calldata,
            gas_limit=total_gas_before_refund,
        )

        tx.expected_receipt = TransactionReceipt(
            cumulative_gas_used=expected_gas
        )

        # Verify all storage slots are cleared
        expected_storage = dict.fromkeys(range(num_slots), 0)

        state_test(
            pre=pre,
            post={
                contract: {
                    "storage": expected_storage,
                }
            },
            tx=tx,
        )

    def test_floor_cost_not_reduced_by_refunds(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        fork: Fork,
    ) -> None:
        """
        Verify floor cost acts as a true floor and is never reduced.

        Even with large refunds, the transaction must pay at least the
        floor cost.
        """
        # Use calldata that strongly triggers floor cost
        calldata = Bytes(b"\x01" * 1000)

        # Deploy contract that clears storage
        code = Op.SSTORE(0, 0, original_value=1, new_value=0) + Op.STOP
        contract = pre.deploy_contract(
            code,
            storage={0: 1},
        )

        # Calculate costs
        floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
        floor_cost = floor_cost_calculator(data=calldata)

        intrinsic_cost_calculator = (
            fork.transaction_intrinsic_cost_calculator()
        )
        intrinsic_cost_before_execution = intrinsic_cost_calculator(
            calldata=calldata,
            contract_creation=False,
            access_list=None,
            authorization_list_or_count=None,
            return_cost_deducted_prior_execution=True,
        )

        execution_gas = code.gas_cost(fork)

        total_gas_before_refund = (
            intrinsic_cost_before_execution + execution_gas
        )
        refund = min(
            code.refund(fork),
            total_gas_before_refund // fork.max_refund_quotient(),
        )
        gas_after_refund = total_gas_before_refund - refund

        # Even after refund, we should pay at least floor cost
        expected_gas = max(gas_after_refund, floor_cost)

        # Verify floor cost is the dominant factor
        assert expected_gas == floor_cost, (
            "Floor cost should dominate for data-heavy transaction"
        )

        # Gas limit must satisfy both execution needs and floor cost
        gas_limit = max(total_gas_before_refund, floor_cost)

        tx = Transaction(
            sender=sender,
            to=contract,
            data=calldata,
            gas_limit=gas_limit,
        )

        tx.expected_receipt = TransactionReceipt(
            cumulative_gas_used=expected_gas
        )

        state_test(
            pre=pre,
            post={
                contract: {
                    "storage": {0: 0},
                }
            },
            tx=tx,
        )

test_refund_cap_at_one_fifth(state_test, pre, sender, fork)

Test that refunds are capped at the fork max refund quotient.

Even if the refund counter is high, the actual refund cannot exceed the fork cap. Use minimal calldata to avoid floor cost interference.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
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
def test_refund_cap_at_one_fifth(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    fork: Fork,
) -> None:
    """
    Test that refunds are capped at the fork max refund quotient.

    Even if the refund counter is high, the actual refund cannot exceed
    the fork cap. Use minimal calldata to avoid floor cost interference.
    """
    # Use minimal calldata so floor cost doesn't dominate
    calldata = Bytes(b"")

    # Deploy contract that clears multiple storage slots
    # This generates a large refund counter
    num_slots = 10
    code = Bytecode()
    storage = {i: 1 for i in range(num_slots)}  # noqa: C420

    for i in range(num_slots):
        code += Op.SSTORE(i, 0, original_value=1, new_value=0)

    code += Op.STOP
    contract = pre.deploy_contract(
        code,
        storage=storage,  # type: ignore[arg-type]
    )

    # Calculate costs
    intrinsic_cost_calculator = (
        fork.transaction_intrinsic_cost_calculator()
    )
    intrinsic_cost_before_execution = intrinsic_cost_calculator(
        calldata=calldata,
        contract_creation=False,
        access_list=None,
        authorization_list_or_count=None,
        return_cost_deducted_prior_execution=True,
    )

    execution_gas = code.gas_cost(fork)

    total_gas_before_refund = (
        intrinsic_cost_before_execution + execution_gas
    )

    refund_counter = code.refund(fork)

    # Actual refund is capped by the fork max refund quotient.
    refund_cap = total_gas_before_refund // fork.max_refund_quotient()
    actual_refund = min(refund_counter, refund_cap)

    # Verify that refund counter exceeds cap
    assert refund_counter > refund_cap, (
        "Test requires refund_counter > refund cap"
    )

    # Gas after refund (floor cost is minimal with empty calldata)
    expected_gas = total_gas_before_refund - actual_refund

    tx = Transaction(
        sender=sender,
        to=contract,
        data=calldata,
        gas_limit=total_gas_before_refund,
    )

    tx.expected_receipt = TransactionReceipt(
        cumulative_gas_used=expected_gas
    )

    # Verify all storage slots are cleared
    expected_storage = dict.fromkeys(range(num_slots), 0)

    state_test(
        pre=pre,
        post={
            contract: {
                "storage": expected_storage,
            }
        },
        tx=tx,
    )

test_floor_cost_not_reduced_by_refunds(state_test, pre, sender, fork)

Verify floor cost acts as a true floor and is never reduced.

Even with large refunds, the transaction must pay at least the floor cost.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
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
931
932
933
934
935
936
def test_floor_cost_not_reduced_by_refunds(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    fork: Fork,
) -> None:
    """
    Verify floor cost acts as a true floor and is never reduced.

    Even with large refunds, the transaction must pay at least the
    floor cost.
    """
    # Use calldata that strongly triggers floor cost
    calldata = Bytes(b"\x01" * 1000)

    # Deploy contract that clears storage
    code = Op.SSTORE(0, 0, original_value=1, new_value=0) + Op.STOP
    contract = pre.deploy_contract(
        code,
        storage={0: 1},
    )

    # Calculate costs
    floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
    floor_cost = floor_cost_calculator(data=calldata)

    intrinsic_cost_calculator = (
        fork.transaction_intrinsic_cost_calculator()
    )
    intrinsic_cost_before_execution = intrinsic_cost_calculator(
        calldata=calldata,
        contract_creation=False,
        access_list=None,
        authorization_list_or_count=None,
        return_cost_deducted_prior_execution=True,
    )

    execution_gas = code.gas_cost(fork)

    total_gas_before_refund = (
        intrinsic_cost_before_execution + execution_gas
    )
    refund = min(
        code.refund(fork),
        total_gas_before_refund // fork.max_refund_quotient(),
    )
    gas_after_refund = total_gas_before_refund - refund

    # Even after refund, we should pay at least floor cost
    expected_gas = max(gas_after_refund, floor_cost)

    # Verify floor cost is the dominant factor
    assert expected_gas == floor_cost, (
        "Floor cost should dominate for data-heavy transaction"
    )

    # Gas limit must satisfy both execution needs and floor cost
    gas_limit = max(total_gas_before_refund, floor_cost)

    tx = Transaction(
        sender=sender,
        to=contract,
        data=calldata,
        gas_limit=gas_limit,
    )

    tx.expected_receipt = TransactionReceipt(
        cumulative_gas_used=expected_gas
    )

    state_test(
        pre=pre,
        post={
            contract: {
                "storage": {0: 0},
            }
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.