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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742 | @pytest.mark.parametrize(
"pair_independence",
[
pytest.param(True, id="independent_pairs"),
pytest.param(False, id="single_contract"),
],
)
@pytest.mark.parametrize("tx_density", TX_DENSITY_PARAMS)
def test_deploy_then_interact(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
tx_gas_limit: int,
tx_density: TxDensity,
pair_independence: bool,
) -> None:
"""
Benchmark structural cross-tx code dependencies.
Transactions include deploy and call operations within a single
block. Without a BAL, clients must discover that calls depend on
deploys through speculative execution or re-execution. With a BAL
the dependency is explicit.
With ``pair_independence=True`` each pair deploys and calls its own
contract — pairs are independent and parallelizable. With
``pair_independence=False`` a single contract is deployed first and
all subsequent txs call it, creating a fully serial dependency
chain (deploy + shared slot 0).
"""
intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
intrinsic_gas = intrinsic_gas_calculator()
runtime_code, setup_gas, _, reserve_gas = _build_keccak_chain(fork)
creation_code = Initcode(
deploy_code=runtime_code,
initcode_prefix=Op.SSTORE(0, 1),
)
intrinsic_gas_create = intrinsic_gas_calculator(
calldata=bytes(creation_code),
contract_creation=True,
)
initcode_sstore = Op.SSTORE(
0,
1,
key_warm=False,
original_value=0,
current_value=0,
new_value=1,
)
initcode_exec_gas = initcode_sstore.gas_cost(fork)
code_deposit_gas = 200 * len(runtime_code)
# Buffer for Initcode wrapper overhead (CODECOPY + RETURN + memory).
deploy_gas_limit = (
intrinsic_gas_create + initcode_exec_gas + code_deposit_gas + 10000
)
min_call_gas = intrinsic_gas + setup_gas + reserve_gas
fraction = {
TxDensity.GREEDY: None,
TxDensity.HALF: 0.5,
TxDensity.MAX: 1.0,
}[tx_density]
if pair_independence:
# N pairs: [deploy_0, call_0, deploy_1, call_1, ...]
if fraction is None:
per_pair_gas = deploy_gas_limit + tx_gas_limit
num_pairs = max(1, gas_benchmark_value // per_pair_gas)
remainder = gas_benchmark_value - num_pairs * per_pair_gas
if remainder >= deploy_gas_limit + min_call_gas:
num_pairs += 1
else:
min_per_pair = deploy_gas_limit + min_call_gas
max_pairs = gas_benchmark_value // min_per_pair
num_pairs = max(1, int(max_pairs * fraction))
per_pair_gas = gas_benchmark_value // num_pairs
call_gas_limit = min(tx_gas_limit, per_pair_gas - deploy_gas_limit)
num_call_txs = num_pairs
else:
# 1 deploy + N calls: [deploy_0, call_0, call_1, ...]
call_budget = gas_benchmark_value - deploy_gas_limit
if fraction is None:
num_call_txs = call_budget // tx_gas_limit
remainder = call_budget - num_call_txs * tx_gas_limit
if remainder >= min_call_gas:
num_call_txs += 1
else:
max_calls = call_budget // min_call_gas
num_call_txs = max(1, int(max_calls * fraction))
call_gas_limit = min(tx_gas_limit, call_budget // num_call_txs)
num_pairs = 1
blocks: list[Block] = []
with TestPhaseManager.execution():
exec_txs: list[Transaction] = []
if pair_independence:
for _ in range(num_pairs):
deployer = pre.fund_eoa()
exec_txs.append(
Transaction(
to=None,
gas_limit=deploy_gas_limit,
data=creation_code,
sender=deployer,
)
)
contract = compute_create_address(address=deployer, nonce=0)
exec_txs.append(
Transaction(
to=contract,
gas_limit=call_gas_limit,
sender=pre.fund_eoa(),
)
)
else:
# Single deploy followed by serial calls.
deployer = pre.fund_eoa()
exec_txs.append(
Transaction(
to=None,
gas_limit=deploy_gas_limit,
data=creation_code,
sender=deployer,
)
)
contract = compute_create_address(address=deployer, nonce=0)
for _ in range(num_call_txs):
exec_txs.append(
Transaction(
to=contract,
gas_limit=call_gas_limit,
sender=pre.fund_eoa(),
)
)
blocks.append(Block(txs=exec_txs))
benchmark_test(blocks=blocks, skip_gas_used_validation=True)
|