Skip to content

test_token_calculation_verification()

Documentation for tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestTokenCalculation::test_token_calculation_verification@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestTokenCalculation::test_token_calculation_verification --fork Amsterdam

Test token calculation with different byte compositions.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class TestTokenCalculation:
    """Test token calculation with different byte compositions."""

    @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 that does nothing."""
        return pre.deploy_contract(Op.STOP)

    @pytest.mark.parametrize(
        "calldata,expected_standard_tokens,description",
        [
            pytest.param(
                Bytes(b"\x00" * 100),
                100,
                "all_zero_bytes",
                id="all_zero_bytes",
            ),
            pytest.param(
                Bytes(b"\x01" * 100),
                400,
                "all_nonzero_bytes",
                id="all_nonzero_bytes",
            ),
            pytest.param(
                Bytes(b"\x01" * 75 + b"\x00" * 25),
                325,  # 75*4 + 25*1
                "75_percent_nonzero",
                id="75_percent_nonzero",
            ),
            pytest.param(
                Bytes(b"\x01" * 25 + b"\x00" * 75),
                175,  # 25*4 + 75*1
                "25_percent_nonzero",
                id="25_percent_nonzero",
            ),
            pytest.param(
                Bytes(b"\x01\x02\x03\x00"),
                13,  # 3*4 + 1*1
                "three_nonzero_one_zero",
                id="three_nonzero_one_zero",
            ),
            pytest.param(
                Bytes(b"\xff" * 50 + b"\x00" * 50),
                250,  # 50*4 + 50*1
                "half_and_half",
                id="half_and_half",
            ),
        ],
    )
    def test_token_calculation_verification(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        to: Address,
        calldata: Bytes,
        expected_standard_tokens: int,
        description: str,
        fork: Fork,
    ) -> None:
        """
        Verify token calculation is correct for different byte compositions.

        Standard calldata token formula:
        tokens = zero_bytes + (nonzero_bytes * 4)

        Floor token formula introduced in EIP-7976:
        floor_tokens = 4 * calldata_bytes
        """
        # Calculate expected 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,
        )

        floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
        floor_cost = floor_cost_calculator(data=calldata)

        # Verify floor token calculation:
        # floor_cost = TX_BASE + (floor_tokens * floor_token_cost)
        # where floor_tokens = 4 * calldata_bytes
        gas_costs = fork.gas_costs()
        floor_token_cost = gas_costs.TX_DATA_TOKEN_FLOOR
        expected_floor_tokens = len(calldata) * 4
        expected_floor_cost = gas_costs.TX_BASE + (
            expected_floor_tokens * floor_token_cost
        )
        # EIP-2780 anchors the floor on the decomposed intrinsic base,
        # which includes the recipient-access charge for a non-self,
        # non-create transaction (the ``to`` fixture is a contract).
        expected_floor_cost += gas_costs.COLD_ACCOUNT_ACCESS
        assert floor_cost == expected_floor_cost, (
            f"Floor cost mismatch for {description}: "
            f"{floor_cost} != {expected_floor_cost} "
            f"(floor_tokens={expected_floor_tokens}, "
            f"floor_cost_per_token={floor_token_cost})"
        )

        expected_intrinsic_cost = gas_costs.TX_BASE + (
            expected_standard_tokens * gas_costs.TX_DATA_TOKEN_STANDARD
        )
        if fork.is_eip_enabled(2780):
            # EIP-2780 surfaces an explicit recipient-access charge for
            # non-self, non-create transactions; the ``to`` fixture
            # defaults to a deployed contract, so the charge applies.
            expected_intrinsic_cost += gas_costs.COLD_ACCOUNT_ACCESS
        assert intrinsic_cost_before_execution == expected_intrinsic_cost, (
            f"Intrinsic cost mismatch for {description}: "
            f"{intrinsic_cost_before_execution} != {expected_intrinsic_cost} "
            f"(standard_tokens={expected_standard_tokens})"
        )

        # Create transaction with exact gas needed
        total_intrinsic_cost = max(intrinsic_cost_before_execution, floor_cost)
        tx = Transaction(
            sender=sender,
            to=to,
            data=calldata,
            gas_limit=total_intrinsic_cost,
        )

        # Expected gas used should be the floor cost if it's greater
        expected_gas_used = total_intrinsic_cost
        tx.expected_receipt = TransactionReceipt(
            cumulative_gas_used=expected_gas_used
        )

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

test_token_calculation_verification(state_test, pre, sender, to, calldata, expected_standard_tokens, description, fork)

Verify token calculation is correct for different byte compositions.

Standard calldata token formula: tokens = zero_bytes + (nonzero_bytes * 4)

Floor token formula introduced in EIP-7976: floor_tokens = 4 * calldata_bytes

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@pytest.mark.parametrize(
    "calldata,expected_standard_tokens,description",
    [
        pytest.param(
            Bytes(b"\x00" * 100),
            100,
            "all_zero_bytes",
            id="all_zero_bytes",
        ),
        pytest.param(
            Bytes(b"\x01" * 100),
            400,
            "all_nonzero_bytes",
            id="all_nonzero_bytes",
        ),
        pytest.param(
            Bytes(b"\x01" * 75 + b"\x00" * 25),
            325,  # 75*4 + 25*1
            "75_percent_nonzero",
            id="75_percent_nonzero",
        ),
        pytest.param(
            Bytes(b"\x01" * 25 + b"\x00" * 75),
            175,  # 25*4 + 75*1
            "25_percent_nonzero",
            id="25_percent_nonzero",
        ),
        pytest.param(
            Bytes(b"\x01\x02\x03\x00"),
            13,  # 3*4 + 1*1
            "three_nonzero_one_zero",
            id="three_nonzero_one_zero",
        ),
        pytest.param(
            Bytes(b"\xff" * 50 + b"\x00" * 50),
            250,  # 50*4 + 50*1
            "half_and_half",
            id="half_and_half",
        ),
    ],
)
def test_token_calculation_verification(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    to: Address,
    calldata: Bytes,
    expected_standard_tokens: int,
    description: str,
    fork: Fork,
) -> None:
    """
    Verify token calculation is correct for different byte compositions.

    Standard calldata token formula:
    tokens = zero_bytes + (nonzero_bytes * 4)

    Floor token formula introduced in EIP-7976:
    floor_tokens = 4 * calldata_bytes
    """
    # Calculate expected 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,
    )

    floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
    floor_cost = floor_cost_calculator(data=calldata)

    # Verify floor token calculation:
    # floor_cost = TX_BASE + (floor_tokens * floor_token_cost)
    # where floor_tokens = 4 * calldata_bytes
    gas_costs = fork.gas_costs()
    floor_token_cost = gas_costs.TX_DATA_TOKEN_FLOOR
    expected_floor_tokens = len(calldata) * 4
    expected_floor_cost = gas_costs.TX_BASE + (
        expected_floor_tokens * floor_token_cost
    )
    # EIP-2780 anchors the floor on the decomposed intrinsic base,
    # which includes the recipient-access charge for a non-self,
    # non-create transaction (the ``to`` fixture is a contract).
    expected_floor_cost += gas_costs.COLD_ACCOUNT_ACCESS
    assert floor_cost == expected_floor_cost, (
        f"Floor cost mismatch for {description}: "
        f"{floor_cost} != {expected_floor_cost} "
        f"(floor_tokens={expected_floor_tokens}, "
        f"floor_cost_per_token={floor_token_cost})"
    )

    expected_intrinsic_cost = gas_costs.TX_BASE + (
        expected_standard_tokens * gas_costs.TX_DATA_TOKEN_STANDARD
    )
    if fork.is_eip_enabled(2780):
        # EIP-2780 surfaces an explicit recipient-access charge for
        # non-self, non-create transactions; the ``to`` fixture
        # defaults to a deployed contract, so the charge applies.
        expected_intrinsic_cost += gas_costs.COLD_ACCOUNT_ACCESS
    assert intrinsic_cost_before_execution == expected_intrinsic_cost, (
        f"Intrinsic cost mismatch for {description}: "
        f"{intrinsic_cost_before_execution} != {expected_intrinsic_cost} "
        f"(standard_tokens={expected_standard_tokens})"
    )

    # Create transaction with exact gas needed
    total_intrinsic_cost = max(intrinsic_cost_before_execution, floor_cost)
    tx = Transaction(
        sender=sender,
        to=to,
        data=calldata,
        gas_limit=total_intrinsic_cost,
    )

    # Expected gas used should be the floor cost if it's greater
    expected_gas_used = total_intrinsic_cost
    tx.expected_receipt = TransactionReceipt(
        cumulative_gas_used=expected_gas_used
    )

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

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.