Skip to content

test_memory_expansion_with_calldata()

Documentation for tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestMemoryExpansion::test_memory_expansion_with_calldata@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestMemoryExpansion::test_memory_expansion_with_calldata --fork Amsterdam

Test memory expansion interaction with floor cost.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
class TestMemoryExpansion:
    """Test memory expansion interaction with floor 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 contract that causes memory expansion.

        The contract performs CALLDATACOPY to expand memory, then stops.
        This ensures memory expansion gas is counted in execution gas.
        """
        # CALLDATACOPY(destOffset=0, offset=0, length=CALLDATASIZE)
        # This will copy all calldata to memory starting at offset 0
        code = (
            Op.CALLDATASIZE  # Push calldata size
            + Op.PUSH1(0)  # Push offset (0)
            + Op.PUSH1(0)  # Push destOffset (0)
            + Op.CALLDATACOPY  # Copy calldata to memory
            + Op.STOP
        )
        return pre.deploy_contract(code)

    @pytest.mark.parametrize(
        "calldata_size",
        [
            pytest.param(1024, id="1kb"),
            pytest.param(10240, id="10kb"),
            pytest.param(32768, id="32kb"),
        ],
    )
    def test_memory_expansion_with_calldata(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        to: Address,
        calldata_size: int,
        fork: Fork,
    ) -> None:
        """
        Test memory expansion gas is counted in execution_gas not floor.

        The transaction pays max(standard_cost + execution_gas, floor_cost)
        where execution_gas includes memory expansion costs.
        """
        # Create calldata with non-zero bytes to trigger floor cost
        calldata = Bytes(b"\x01" * calldata_size)

        # 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,
        )

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

        code = (
            Op.CALLDATASIZE
            + Op.PUSH1(0)
            + Op.PUSH1(0)
            + Op.CALLDATACOPY(
                data_size=calldata_size,
                new_memory_size=calldata_size,
            )
            + Op.STOP
        )
        execution_gas = code.gas_cost(fork)

        # Total gas is intrinsic + execution
        total_with_execution = intrinsic_cost_before_execution + execution_gas

        # The actual gas used is max(total_with_execution, floor_cost)
        expected_gas = max(total_with_execution, floor_cost)

        tx = Transaction(
            sender=sender,
            to=to,
            data=calldata,
            gas_limit=expected_gas + 100000,  # Add buffer for safety
        )

        # Verify the transaction executes successfully
        state_test(
            pre=pre,
            post={},
            tx=tx,
        )

test_memory_expansion_with_calldata(state_test, pre, sender, to, calldata_size, fork)

Test memory expansion gas is counted in execution_gas not floor.

The transaction pays max(standard_cost + execution_gas, floor_cost) where execution_gas includes memory expansion costs.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
@pytest.mark.parametrize(
    "calldata_size",
    [
        pytest.param(1024, id="1kb"),
        pytest.param(10240, id="10kb"),
        pytest.param(32768, id="32kb"),
    ],
)
def test_memory_expansion_with_calldata(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    to: Address,
    calldata_size: int,
    fork: Fork,
) -> None:
    """
    Test memory expansion gas is counted in execution_gas not floor.

    The transaction pays max(standard_cost + execution_gas, floor_cost)
    where execution_gas includes memory expansion costs.
    """
    # Create calldata with non-zero bytes to trigger floor cost
    calldata = Bytes(b"\x01" * calldata_size)

    # 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,
    )

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

    code = (
        Op.CALLDATASIZE
        + Op.PUSH1(0)
        + Op.PUSH1(0)
        + Op.CALLDATACOPY(
            data_size=calldata_size,
            new_memory_size=calldata_size,
        )
        + Op.STOP
    )
    execution_gas = code.gas_cost(fork)

    # Total gas is intrinsic + execution
    total_with_execution = intrinsic_cost_before_execution + execution_gas

    # The actual gas used is max(total_with_execution, floor_cost)
    expected_gas = max(total_with_execution, floor_cost)

    tx = Transaction(
        sender=sender,
        to=to,
        data=calldata,
        gas_limit=expected_gas + 100000,  # Add buffer for safety
    )

    # Verify the transaction executes successfully
    state_test(
        pre=pre,
        post={},
        tx=tx,
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.