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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
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 | @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("EIP8037")
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
"""
if refund_type == RefundTypes.AUTHORIZATION_EXISTING_AUTHORITY:
if calldata_test_type == (
CallDataTestType.DATA_FLOOR_BETWEEN_TX_GAS_BEFORE_AND_AFTER
):
pytest.skip(
"EIP-7702 auth refund routes through state_gas_reservoir "
"and state_refund (deducted from tx_state_gas); it does "
"not feed refund_counter, so receipt gas_used_pre_refund "
"== gas_used_post_refund and no calldata floor can land "
"strictly between them"
)
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,
tx_state_gas,
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."
)
# EIP-8037: block gas_used = max(block_regular_gas, block_state_gas),
# with the calldata floor binding the regular dimension.
block_regular = max(gas_used_pre_refund, call_data_floor_cost)
refund_tx_block_gas_used = max(block_regular, tx_state_gas)
blockchain_test(
pre=pre,
blocks=[
Block(
txs=[refund_tx],
expected_gas_used=refund_tx_block_gas_used,
)
],
post=post,
)
|