212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 | @pytest.mark.parametrize(
"storage_action,tx_result",
[
pytest.param(
StorageAction.READ, TransactionResult.SUCCESS, id="SSLOAD"
),
pytest.param(
StorageAction.WRITE_SAME_VALUE,
TransactionResult.SUCCESS,
id="SSTORE same value",
),
pytest.param(
StorageAction.WRITE_SAME_VALUE,
TransactionResult.REVERT,
id="SSTORE same value, revert",
),
pytest.param(
StorageAction.WRITE_SAME_VALUE,
TransactionResult.OUT_OF_GAS,
id="SSTORE same value, out of gas",
),
pytest.param(
StorageAction.WRITE_NEW_VALUE,
TransactionResult.SUCCESS,
id="SSTORE new value",
),
pytest.param(
StorageAction.WRITE_NEW_VALUE,
TransactionResult.REVERT,
id="SSTORE new value, revert",
),
pytest.param(
StorageAction.WRITE_NEW_VALUE,
TransactionResult.OUT_OF_GAS,
id="SSTORE new value, out of gas",
),
],
)
@pytest.mark.parametrize("absent_slots", [True, False])
def test_storage_access_cold(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
storage_action: StorageAction,
absent_slots: bool,
gas_benchmark_value: int,
tx_gas_limit: int,
tx_result: TransactionResult,
) -> None:
"""
Benchmark cold storage slot accesses using EIP-7702 delegation.
The authority EOA delegates to:
- StorageInitializer: storage[i] = i for each slot (absent_slots=False)
- BenchmarkExecutor: performs the benchmark operation (SLOAD/SSTORE)
"""
executor_code = create_benchmark_executor(
storage_action, absent_slots, tx_result
)
initializer_code = create_storage_initializer()
authority = pre.fund_eoa(amount=0)
initializer_addr = pre.deploy_contract(code=initializer_code)
executor_addr = pre.deploy_contract(code=executor_code)
# Calldata generator for both the executor and initializer.
def calldata_generator(
iteration_count: int, start_iteration: int
) -> bytes:
return Hash(start_iteration) + Hash(iteration_count)
# Number of slots that can be processed in the execution phase
num_target_slots = sum(
executor_code.tx_iterations_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
calldata=calldata_generator,
)
)
blocks = []
delegation_sender = pre.fund_eoa()
# Setup phase: initialize storage slots (only if absent_slots=False)
with TestPhaseManager.setup():
setup_txs = []
authority_nonce = 0
if not absent_slots:
setup_txs.append(
Transaction(
to=delegation_sender,
gas_limit=tx_gas_limit,
sender=delegation_sender,
authorization_list=[
AuthorizationTuple(
address=initializer_addr,
nonce=authority_nonce,
signer=authority,
),
],
)
)
authority_nonce += 1
setup_txs += list(
initializer_code.transactions_by_total_iteration_count(
fork=fork,
total_iterations=num_target_slots,
sender=pre.fund_eoa(),
to=authority,
start_iteration=1,
calldata=calldata_generator,
)
)
setup_txs.append(
Transaction(
to=delegation_sender,
gas_limit=tx_gas_limit,
sender=delegation_sender,
authorization_list=[
AuthorizationTuple(
address=executor_addr,
nonce=authority_nonce,
signer=authority,
),
],
)
)
blocks.append(Block(txs=setup_txs))
# Execution phase: run benchmark
# For absent_slots=False, authority has storage, triggering refund
expected_gas_used = 0
with TestPhaseManager.execution():
tx_gas_limit_delta = (
-1 if tx_result == TransactionResult.OUT_OF_GAS else 0
)
exec_txs = list(
executor_code.transactions_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
sender=pre.fund_eoa(),
to=authority,
calldata=calldata_generator,
start_iteration=1,
tx_gas_limit_delta=tx_gas_limit_delta,
)
)
for exec_tx in exec_txs:
if tx_result == TransactionResult.OUT_OF_GAS:
expected_gas_used += exec_tx.gas_limit
else:
expected_gas_used += exec_tx.gas_cost
blocks.append(Block(txs=exec_txs))
benchmark_test(
blocks=blocks,
expected_benchmark_gas_used=expected_gas_used,
)
|