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
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 | @pytest.mark.parametrize("empty_authority", [True, False])
@pytest.mark.parametrize("zero_delegation", [True, False])
@pytest.mark.parametrize("empty_account", [True, False])
@pytest.mark.parametrize("transfer_amount", [True, False])
def test_auth_transaction(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
gas_benchmark_value: int,
fork: Fork,
empty_authority: bool,
empty_account: bool,
transfer_amount: int,
zero_delegation: bool,
tx_gas_limit: int,
) -> None:
"""Test an auth block."""
gas_costs = fork.gas_costs()
intrinsic_cost_calc = fork.transaction_intrinsic_cost_calculator()
code = Op.INVALID * fork.max_code_size()
auth_target = (
Address(0) if zero_delegation else pre.deploy_contract(code=code)
)
remaining_gas = gas_benchmark_value
authorizations_per_tx: List[int] = []
min_authorization_intrinsic_gas = intrinsic_cost_calc(
authorization_list_or_count=1
)
while remaining_gas >= min_authorization_intrinsic_gas:
tx_max_gas = min(remaining_gas, tx_gas_limit)
low = 1
high = 2
# Exponential search to find upper bound
while (
intrinsic_cost_calc(authorization_list_or_count=high) < tx_max_gas
):
low = high
high *= 2
# Binary search for exact fit
while low < high:
mid = (low + high) // 2
if (
intrinsic_cost_calc(authorization_list_or_count=mid)
> tx_max_gas
):
high = mid
else:
low = mid + 1
best_iterations = low - 1
authorizations_per_tx.append(best_iterations)
remaining_gas -= intrinsic_cost_calc(
authorization_list_or_count=best_iterations
)
total_gas_used = 0
total_refund = 0
txs = []
for auths_in_this_tx in authorizations_per_tx:
auth_tuples = []
for _ in range(auths_in_this_tx):
signer = (
pre.fund_eoa(amount=0, delegation=None)
if empty_authority
else pre.fund_eoa(amount=0, delegation=auth_target)
)
auth_tuple = AuthorizationTuple(
address=auth_target, nonce=signer.nonce, signer=signer
)
auth_tuples.append(auth_tuple)
tx_gas_used = intrinsic_cost_calc(
authorization_list_or_count=auth_tuples
)
total_gas_used += tx_gas_used
if not empty_authority:
total_refund += min(
tx_gas_used // 5,
(
gas_costs.AUTH_PER_EMPTY_ACCOUNT
- gas_costs.REFUND_AUTH_PER_EXISTING_ACCOUNT
)
* auths_in_this_tx,
)
receiver = pre.fund_eoa(0 if empty_account else 1)
txs.append(
Transaction(
to=receiver,
value=transfer_amount,
gas_limit=tx_gas_used,
sender=pre.fund_eoa(),
authorization_list=auth_tuples,
)
)
# EIP-7778: refunds no longer reduce block-level gas accounting
expected_gas_usage = (
total_gas_used
if fork.is_eip_enabled(7778)
else total_gas_used - total_refund
)
benchmark_test(
pre=pre,
post={},
blocks=[Block(txs=txs)],
expected_benchmark_gas_used=expected_gas_usage,
)
|