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
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
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 | @pytest.mark.valid_from("Prague")
@pytest.mark.parametrize("num_contracts", [1, 5, 10, 20, 100])
@pytest.mark.parametrize(
"sload_percent,sstore_percent",
[
pytest.param(50, 50, id="50-50"),
pytest.param(70, 30, id="70-30"),
pytest.param(90, 10, id="90-10"),
],
)
def test_mixed_sload_sstore(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
address_stubs: AddressStubs,
num_contracts: int,
sload_percent: int,
sstore_percent: int,
request: pytest.FixtureRequest,
) -> None:
"""
BloatNet mixed SLOAD/SSTORE benchmark with configurable operation ratios.
This test:
1. Filters stubs matching test name prefix
(e.g., test_mixed_sload_sstore_*)
2. Uses first N contracts based on num_contracts parameter
3. Divides gas budget evenly across all selected contracts
4. For each contract, divides gas into SLOAD and SSTORE portions by
percentage
5. Executes balanceOf (SLOAD) and approve (SSTORE) calls per the ratio
6. Stresses clients with combined read/write operations on large
contracts
"""
# Extract test function name for stub filtering
test_name = request.node.name.split("[")[0] # Remove parametrization suffix
# Filter stubs that match the test name prefix
matching_stubs = [
stub_name for stub_name in address_stubs.root.keys() if stub_name.startswith(test_name)
]
# Validate we have enough stubs
if len(matching_stubs) < num_contracts:
pytest.fail(
f"Not enough matching stubs for test '{test_name}'. "
f"Required: {num_contracts}, Found: {len(matching_stubs)}. "
f"Matching stubs: {matching_stubs}"
)
# Select first N stubs
selected_stubs = matching_stubs[:num_contracts]
gas_costs = fork.gas_costs()
# Calculate gas costs
intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(calldata=b"")
# Fixed overhead for SLOAD loop
sload_loop_overhead = (
# Attack contract loop overhead
gas_costs.G_VERY_LOW * 2 # MLOAD counter (3*2)
+ gas_costs.G_VERY_LOW * 2 # MSTORE selector (3*2)
+ gas_costs.G_VERY_LOW * 3 # MLOAD + MSTORE address (3*3)
+ gas_costs.G_BASE # POP (2)
+ gas_costs.G_BASE * 3 # SUB + MLOAD + MSTORE for counter decrement (2*3)
+ gas_costs.G_BASE * 2 # ISZERO * 2 for loop condition (2*2)
+ gas_costs.G_MID # JUMPI (8)
)
# ERC20 balanceOf internal gas
sload_erc20_internal = (
gas_costs.G_VERY_LOW # PUSH4 selector (3)
+ gas_costs.G_BASE # EQ selector match (2)
+ gas_costs.G_MID # JUMPI to function (8)
+ gas_costs.G_JUMPDEST # JUMPDEST at function start (1)
+ gas_costs.G_VERY_LOW * 2 # CALLDATALOAD arg (3*2)
+ gas_costs.G_KECCAK_256 # keccak256 static (30)
+ gas_costs.G_KECCAK_256_WORD * 2 # keccak256 dynamic for 64 bytes (2*6)
+ gas_costs.G_COLD_SLOAD # Cold SLOAD - always cold for random addresses (2100)
+ gas_costs.G_VERY_LOW * 3 # MSTORE result + RETURN setup (3*3)
)
# Fixed overhead for SSTORE loop
sstore_loop_overhead = (
# Attack contract loop body operations
gas_costs.G_VERY_LOW # MSTORE selector at memory[32] (3)
+ gas_costs.G_LOW # MLOAD counter (5)
+ gas_costs.G_VERY_LOW # MSTORE spender at memory[64] (3)
+ gas_costs.G_BASE # POP call result (2)
# Counter decrement
+ gas_costs.G_LOW # MLOAD counter (5)
+ gas_costs.G_VERY_LOW # PUSH1 1 (3)
+ gas_costs.G_VERY_LOW # SUB (3)
+ gas_costs.G_VERY_LOW # MSTORE counter back (3)
# While loop condition check
+ gas_costs.G_LOW # MLOAD counter (5)
+ gas_costs.G_BASE # ISZERO (2)
+ gas_costs.G_BASE # ISZERO (2)
+ gas_costs.G_MID # JUMPI back to loop start (8)
)
# ERC20 approve internal gas
# Cold SSTORE: 22100 = 20000 base + 2100 cold access
sstore_erc20_internal = (
gas_costs.G_VERY_LOW # PUSH4 selector (3)
+ gas_costs.G_BASE # EQ selector match (2)
+ gas_costs.G_MID # JUMPI to function (8)
+ gas_costs.G_JUMPDEST # JUMPDEST at function start (1)
+ gas_costs.G_VERY_LOW # CALLDATALOAD spender (3)
+ gas_costs.G_VERY_LOW # CALLDATALOAD amount (3)
+ gas_costs.G_KECCAK_256 # keccak256 static (30)
+ gas_costs.G_KECCAK_256_WORD * 2 # keccak256 dynamic for 64 bytes (12)
+ gas_costs.G_COLD_SLOAD # Cold SLOAD for allowance check (2100)
+ gas_costs.G_STORAGE_SET # SSTORE base cost (20000)
+ gas_costs.G_COLD_SLOAD # Additional cold storage access (2100)
+ gas_costs.G_VERY_LOW # PUSH1 1 for return value (3)
+ gas_costs.G_VERY_LOW # MSTORE return value (3)
+ gas_costs.G_VERY_LOW # PUSH1 32 for return size (3)
+ gas_costs.G_VERY_LOW # PUSH1 0 for return offset (3)
)
# Calculate gas budget per contract
available_gas = gas_benchmark_value - intrinsic_gas
gas_per_contract = available_gas // num_contracts
# For each contract, split gas by percentage
sload_gas_per_contract = (gas_per_contract * sload_percent) // 100
sstore_gas_per_contract = (gas_per_contract * sstore_percent) // 100
# Account for cold/warm transitions in CALL costs
# First SLOAD call is COLD (2600), rest are WARM (100)
sload_warm_cost = sload_loop_overhead + gas_costs.G_WARM_ACCOUNT_ACCESS + sload_erc20_internal
cold_warm_diff = gas_costs.G_COLD_ACCOUNT_ACCESS - gas_costs.G_WARM_ACCOUNT_ACCESS
sload_calls_per_contract = int((sload_gas_per_contract - cold_warm_diff) // sload_warm_cost)
# First SSTORE call is COLD (2600), rest are WARM (100)
sstore_warm_cost = (
sstore_loop_overhead + gas_costs.G_WARM_ACCOUNT_ACCESS + sstore_erc20_internal
)
sstore_calls_per_contract = int((sstore_gas_per_contract - cold_warm_diff) // sstore_warm_cost)
# Deploy selected ERC20 contracts using stubs
erc20_addresses = []
for stub_name in selected_stubs:
addr = pre.deploy_contract(
code=Bytecode(),
stub=stub_name,
)
erc20_addresses.append(addr)
# Log test requirements
print(
f"Total gas budget: {gas_benchmark_value / 1_000_000:.1f}M gas. "
f"~{gas_per_contract / 1_000_000:.1f}M gas per contract "
f"({sload_percent}% SLOAD, {sstore_percent}% SSTORE). "
f"Per contract: {sload_calls_per_contract} balanceOf calls, "
f"{sstore_calls_per_contract} approve calls."
)
# Build attack code that loops through each contract
attack_code: Bytecode = (
Op.JUMPDEST # Entry point
+ Op.MSTORE(offset=0, value=BALANCEOF_SELECTOR) # Store selector once for all contracts
)
for erc20_address in erc20_addresses:
# For each contract, execute SLOAD operations (balanceOf)
attack_code += (
# Initialize counter in memory[32] = number of balanceOf calls
Op.MSTORE(offset=32, value=sload_calls_per_contract)
# Loop for balanceOf calls
+ While(
condition=Op.MLOAD(32) + Op.ISZERO + Op.ISZERO,
body=(
# Call balanceOf(address) on ERC20 contract
# args_offset=28 reads: selector from MEM[28:32] + address
# from MEM[32:64]
Op.CALL(
address=erc20_address,
value=0,
args_offset=28,
args_size=36,
ret_offset=0,
ret_size=0,
)
+ Op.POP # Discard CALL success status
# Decrement counter
+ Op.MSTORE(offset=32, value=Op.SUB(Op.MLOAD(32), 1))
),
)
)
# For each contract, execute SSTORE operations (approve)
# Reuse the same memory layout as balanceOf
attack_code += (
# Store approve selector at memory[0] (reusing same slot)
Op.MSTORE(offset=0, value=APPROVE_SELECTOR)
# Initialize counter in memory[32] = number of approve calls
# (reusing same slot)
+ Op.MSTORE(offset=32, value=sstore_calls_per_contract)
# Loop for approve calls
+ While(
condition=Op.MLOAD(32) + Op.ISZERO + Op.ISZERO,
body=(
# Store spender at memory[64] (counter as spender/amount)
Op.MSTORE(offset=64, value=Op.MLOAD(32))
# Call approve(spender, amount) on ERC20 contract
# args_offset=28 reads: selector from MEM[28:32] +
# spender from MEM[32:64] + amount from MEM[64:96]
# Note: counter at MEM[32:64] is reused as spender,
# and value at MEM[64:96] serves as the amount
+ Op.CALL(
address=erc20_address,
value=0,
args_offset=28,
args_size=68,
ret_offset=0,
ret_size=0,
)
+ Op.POP # Discard CALL success status
# Decrement counter
+ Op.MSTORE(offset=32, value=Op.SUB(Op.MLOAD(32), 1))
),
)
)
# Deploy attack contract
attack_address = pre.deploy_contract(code=attack_code)
# Run the attack
attack_tx = Transaction(
to=attack_address,
gas_limit=gas_benchmark_value,
sender=pre.fund_eoa(),
)
# Post-state
post = {
attack_address: Account(storage={}),
}
blockchain_test(
pre=pre,
blocks=[Block(txs=[attack_tx])],
post=post,
)
|