Skip to content

test_exact_threshold_boundary()

Documentation for tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestExactThresholdBoundary::test_exact_threshold_boundary@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestExactThresholdBoundary::test_exact_threshold_boundary --fork Amsterdam

Test exact threshold where floor_cost == intrinsic_cost.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
class TestExactThresholdBoundary:
    """Test exact threshold where floor_cost == intrinsic_cost."""

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

    @pytest.fixture
    def to(self, pre: Alloc) -> Address:
        """Deploy a simple contract."""
        return pre.deploy_contract(Op.STOP)

    @pytest.mark.parametrize(
        "access_list,authorization_list",
        [
            pytest.param(None, None, id="no_extras"),
            pytest.param(
                [AccessList(address=Address(1), storage_keys=[])],
                None,
                id="with_access_list",
            ),
        ],
        indirect=["authorization_list"],
    )
    @pytest.mark.parametrize(
        "ty",
        [
            # Type 1 (EIP-2930) introduced access lists
            pytest.param(1, id="type_1"),
            pytest.param(2, id="type_2"),
        ],
    )
    @pytest.mark.parametrize(
        "threshold_offset",
        [
            pytest.param(0, id="below_threshold"),
            pytest.param(1, id="above_threshold"),
            pytest.param(2, id="above_threshold_plus_2"),
        ],
    )
    def test_exact_threshold_boundary(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        to: Address,
        fork: Fork,
        access_list: List[AccessList] | None,
        authorization_list: List[AuthorizationTuple] | None,
        ty: int,
        threshold_offset: int,
    ) -> None:
        """
        Find exact calldata byte count N where floor_cost == intrinsic_cost.

        Test with N, N+1, and N+2 bytes to verify max() function
        switches correctly.
        """
        from .helpers import find_floor_cost_threshold

        def bytes_to_data(byte_count: int) -> Bytes:
            """Convert byte count to calldata bytes."""
            return Bytes(b"\x01" * byte_count)

        intrinsic_cost_calculator = (
            fork.transaction_intrinsic_cost_calculator()
        )

        def intrinsic_cost(byte_count: int) -> int:
            return intrinsic_cost_calculator(
                calldata=bytes_to_data(byte_count),
                contract_creation=False,
                access_list=access_list,
                authorization_list_or_count=authorization_list,
                return_cost_deducted_prior_execution=True,
            )

        floor_cost_calculator = fork.transaction_data_floor_cost_calculator()

        def floor_cost(byte_count: int) -> int:
            return floor_cost_calculator(data=bytes_to_data(byte_count))

        # Find the threshold
        threshold_bytes = find_floor_cost_threshold(
            floor_data_gas_cost_calculator=floor_cost,
            intrinsic_gas_cost_calculator=intrinsic_cost,
        )

        byte_count = threshold_bytes + threshold_offset
        calldata = bytes_to_data(byte_count)
        intrinsic_raw = intrinsic_cost(byte_count)
        floor_raw = floor_cost(byte_count)

        if threshold_offset == 0:
            assert intrinsic_raw >= floor_raw, (
                "At threshold: intrinsic should dominate"
            )
        else:
            assert floor_raw > intrinsic_raw, (
                f"Above threshold: floor should dominate. "
                f"floor={floor_raw}, intrinsic={intrinsic_raw}"
            )

        intrinsic_total = intrinsic_cost_calculator(
            calldata=calldata,
            contract_creation=False,
            access_list=access_list,
            authorization_list_or_count=authorization_list,
        )

        tx = Transaction(
            ty=ty,
            sender=sender,
            to=to,
            nonce=0,
            data=calldata,
            gas_limit=intrinsic_total,
            access_list=access_list,
            authorization_list=authorization_list,
        )
        tx.expected_receipt = TransactionReceipt(
            cumulative_gas_used=intrinsic_total
        )

        state_test(
            pre=pre,
            post={},
            tx=tx,
        )

test_exact_threshold_boundary(state_test, pre, sender, to, fork, access_list, authorization_list, ty, threshold_offset)

Find exact calldata byte count N where floor_cost == intrinsic_cost.

Test with N, N+1, and N+2 bytes to verify max() function switches correctly.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
550
551
552
553
554
555
556
557
558
559
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(
    "access_list,authorization_list",
    [
        pytest.param(None, None, id="no_extras"),
        pytest.param(
            [AccessList(address=Address(1), storage_keys=[])],
            None,
            id="with_access_list",
        ),
    ],
    indirect=["authorization_list"],
)
@pytest.mark.parametrize(
    "ty",
    [
        # Type 1 (EIP-2930) introduced access lists
        pytest.param(1, id="type_1"),
        pytest.param(2, id="type_2"),
    ],
)
@pytest.mark.parametrize(
    "threshold_offset",
    [
        pytest.param(0, id="below_threshold"),
        pytest.param(1, id="above_threshold"),
        pytest.param(2, id="above_threshold_plus_2"),
    ],
)
def test_exact_threshold_boundary(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    to: Address,
    fork: Fork,
    access_list: List[AccessList] | None,
    authorization_list: List[AuthorizationTuple] | None,
    ty: int,
    threshold_offset: int,
) -> None:
    """
    Find exact calldata byte count N where floor_cost == intrinsic_cost.

    Test with N, N+1, and N+2 bytes to verify max() function
    switches correctly.
    """
    from .helpers import find_floor_cost_threshold

    def bytes_to_data(byte_count: int) -> Bytes:
        """Convert byte count to calldata bytes."""
        return Bytes(b"\x01" * byte_count)

    intrinsic_cost_calculator = (
        fork.transaction_intrinsic_cost_calculator()
    )

    def intrinsic_cost(byte_count: int) -> int:
        return intrinsic_cost_calculator(
            calldata=bytes_to_data(byte_count),
            contract_creation=False,
            access_list=access_list,
            authorization_list_or_count=authorization_list,
            return_cost_deducted_prior_execution=True,
        )

    floor_cost_calculator = fork.transaction_data_floor_cost_calculator()

    def floor_cost(byte_count: int) -> int:
        return floor_cost_calculator(data=bytes_to_data(byte_count))

    # Find the threshold
    threshold_bytes = find_floor_cost_threshold(
        floor_data_gas_cost_calculator=floor_cost,
        intrinsic_gas_cost_calculator=intrinsic_cost,
    )

    byte_count = threshold_bytes + threshold_offset
    calldata = bytes_to_data(byte_count)
    intrinsic_raw = intrinsic_cost(byte_count)
    floor_raw = floor_cost(byte_count)

    if threshold_offset == 0:
        assert intrinsic_raw >= floor_raw, (
            "At threshold: intrinsic should dominate"
        )
    else:
        assert floor_raw > intrinsic_raw, (
            f"Above threshold: floor should dominate. "
            f"floor={floor_raw}, intrinsic={intrinsic_raw}"
        )

    intrinsic_total = intrinsic_cost_calculator(
        calldata=calldata,
        contract_creation=False,
        access_list=access_list,
        authorization_list_or_count=authorization_list,
    )

    tx = Transaction(
        ty=ty,
        sender=sender,
        to=to,
        nonce=0,
        data=calldata,
        gas_limit=intrinsic_total,
        access_list=access_list,
        authorization_list=authorization_list,
    )
    tx.expected_receipt = TransactionReceipt(
        cumulative_gas_used=intrinsic_total
    )

    state_test(
        pre=pre,
        post={},
        tx=tx,
    )

Parametrized Test Cases

This test generates 12 parametrized test cases across 1 fork.