Skip to content

test_varying_calldata_costs()

Documentation for tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_varying_calldata_costs@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_varying_calldata_costs --fork Amsterdam

Test by varying the calldata_floor_cost.

Performs tests for the following 3 scenarios.

  1. calldata_floor < tx_gas_after_refund
  2. tx_gas_after_refund < calldata_floor < tx_gas_before_refund
  3. calldata_floor > tx_gas_before_refund
Source code in tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
@pytest.mark.parametrize(
    "refund_tx_reverts",
    [
        pytest.param(True, id="refund_tx_reverts"),
        pytest.param(False, id=""),
    ],
)
@pytest.mark.parametrize(
    "calldata_test_type",
    [
        CallDataTestType.DATA_FLOOR_LT_TX_GAS_AFTER_REFUND,
        CallDataTestType.DATA_FLOOR_BETWEEN_TX_GAS_BEFORE_AND_AFTER,
        CallDataTestType.DATA_FLOOR_GT_TX_GAS_BEFORE_REFUND,
    ],
)
@pytest.mark.with_all_refund_types()
@pytest.mark.filter_combinations(
    lambda refund_type, refund_tx_reverts, calldata_test_type, **_: not (
        refund_type == RefundTypes.STORAGE_CLEAR
        and refund_tx_reverts
        and calldata_test_type
        == CallDataTestType.DATA_FLOOR_BETWEEN_TX_GAS_BEFORE_AND_AFTER
    ),
    reason=(
        "STORAGE_CLEAR refund is zero on revert, so the (post, pre) "
        "interval that DATA_FLOOR_BETWEEN needs is empty"
    ),
)
@pytest.mark.valid_from("EIP7778")
def test_varying_calldata_costs(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    refund_type: RefundTypes,
    refund_tx_reverts: bool,
    calldata_test_type: CallDataTestType,
) -> None:
    """
    Test by varying the calldata_floor_cost.

    Performs tests for the following 3 scenarios.

    1. calldata_floor < tx_gas_after_refund
    2. tx_gas_after_refund < calldata_floor < tx_gas_before_refund
    3. calldata_floor > tx_gas_before_refund
    """
    match refund_type:
        case RefundTypes.STORAGE_CLEAR:
            bytes_to_add_per_iteration = b"00" * 2
        case RefundTypes.AUTHORIZATION_EXISTING_AUTHORITY:
            bytes_to_add_per_iteration = b"00" * 10
        case _:
            raise ValueError(
                f"Unknown refund type: {refund_type} (Test needs update)"
            )

    data = b""

    # Time to start searching for appropriate call data for each scenario
    num_iterations = 200
    # Currently in EIP-7778, the optimal call data is found in about
    # 30 iterations for CallDataTestType.DATA_FLOOR_GT_TX_GAS_BEFORE_REFUND.
    # Setting this higher just to make it
    # a bit more future proof if the gas calc logic changes
    found_call_data = False
    for _ in range(num_iterations):
        post = Alloc()

        (
            gas_used_post_refund,
            gas_used_pre_refund,
            call_data_floor_cost,
            refund_tx,
        ) = build_refund_tx(
            fork=fork,
            pre=pre,
            post=post,
            refund_types={refund_type},
            refund_tx_reverts=refund_tx_reverts,
            call_data=data,
        )

        if (
            calldata_test_type
            == CallDataTestType.DATA_FLOOR_LT_TX_GAS_AFTER_REFUND
        ):
            if call_data_floor_cost < gas_used_post_refund:
                found_call_data = True
                break
        elif (
            calldata_test_type
            == CallDataTestType.DATA_FLOOR_BETWEEN_TX_GAS_BEFORE_AND_AFTER
        ):
            if (
                gas_used_post_refund
                < call_data_floor_cost
                < gas_used_pre_refund
            ):
                found_call_data = True
                break
        elif (
            calldata_test_type
            == CallDataTestType.DATA_FLOOR_GT_TX_GAS_BEFORE_REFUND
        ):
            if gas_used_pre_refund < call_data_floor_cost:
                found_call_data = True
                break
        else:
            raise ValueError("Invalid calldata test type")

        data += bytes_to_add_per_iteration

    if not found_call_data:
        raise ValueError(
            f"Could not find the call_data with {num_iterations} iterations."
        )

    refund_tx_block_gas_used = max(call_data_floor_cost, gas_used_pre_refund)

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[refund_tx],
                expected_gas_used=refund_tx_block_gas_used,
            )
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 11 parametrized test cases across 1 fork.