79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 | @pytest.mark.valid_from("Prague")
@pytest.mark.parametrize("num_contracts", [1, 5, 10, 20, 100])
def test_sload_empty_erc20_balanceof(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
address_stubs: AddressStubs,
num_contracts: int,
request: pytest.FixtureRequest,
) -> None:
"""
BloatNet SLOAD benchmark using ERC20 balanceOf queries on random
addresses.
This test:
1. Filters stubs matching test name prefix
(e.g., test_sload_empty_erc20_balanceof_*)
2. Uses first N contracts based on num_contracts parameter
3. Splits gas budget evenly across the selected contracts
4. Queries balanceOf() incrementally starting by 0 and increasing by 1
(thus forcing SLOADs to non-existing addresses)
"""
# 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 per iteration (loop mechanics, independent of warm/cold)
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 internal gas (same for all calls)
erc20_internal_gas = (
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)
# RETURN costs 0 gas
)
# Calculate gas budget per contract
available_gas = gas_benchmark_value - intrinsic_gas
gas_per_contract = available_gas // num_contracts
# For each contract: first call is COLD (2600), subsequent are WARM (100)
# Solve for calls_per_contract:
# gas_per_contract = cold_call + (calls-1) * warm_call
# Simplifies to: gas = cold_warm_diff + calls * warm_call_cost
warm_call_cost = loop_overhead + gas_costs.G_WARM_ACCOUNT_ACCESS + erc20_internal_gas
cold_warm_diff = gas_costs.G_COLD_ACCOUNT_ACCESS - gas_costs.G_WARM_ACCOUNT_ACCESS
calls_per_contract = int((gas_per_contract - cold_warm_diff) // warm_call_cost)
# Deploy selected ERC20 contracts using stubs
# In execute mode: stubs point to already-deployed contracts on chain
# In fill mode: empty bytecode is deployed as placeholder
erc20_addresses = []
for stub_name in selected_stubs:
addr = pre.deploy_contract(
code=Bytecode(), # Required parameter, ignored for stubs in execute mode
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"{calls_per_contract} balanceOf calls per contract."
)
# 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, initialize counter and loop
attack_code += (
# Initialize counter in memory[32] = number of calls
Op.MSTORE(offset=32, value=calls_per_contract)
# Loop for this specific contract
+ While(
condition=Op.MLOAD(32) + Op.ISZERO + Op.ISZERO, # Continue while counter > 0
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: counter - 1
+ 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,
)
|