1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317 | @pytest.mark.parametrize("access_warm", [True, False])
@pytest.mark.parametrize(
"initial_value,write_values",
[
pytest.param(
0xDEADBEEF,
[0, 0xDEADBEEF, 0, 0xDEADBEEF],
id="oscillation_4x",
),
pytest.param(
0xDEADBEEF,
[0, 0xDEADBEEF, 0, 0xDEADBEEF, 0, 0xDEADBEEF],
id="oscillation_6x",
),
pytest.param(
0xDEADBEEF,
[0xBEEFBEEF, 0xCAFECAFE, 0xDEADBEEF],
id="triple_write_restore",
),
pytest.param(
0xDEADBEEF,
[0],
id="mass_clear",
),
pytest.param(
0,
[1, 0, 1, 0],
id="oscillation_4x_from_zero",
),
pytest.param(
0,
[1],
id="mass_set_from_zero",
),
],
)
def test_sstore_dirty_transitions(
benchmark_test: BenchmarkTestFiller,
fork: Fork,
pre: Alloc,
tx_gas_limit: int,
gas_benchmark_value: int,
access_warm: bool,
initial_value: int,
write_values: List[int],
) -> None:
"""
Benchmark SSTORE dirty state transitions.
Exercise EIP-2200/EIP-3529 refund logic by writing the same slot
multiple times per iteration. Uses EIP-7702 delegation: authority
EOA delegates to initializer then to dirty-write executor.
Variants:
- oscillation: X→0→X→0, alternates clean (2900) and dirty (100)
- triple_write_restore: X→B→C→X, all SSTORE branches
- mass_clear: X→0, maximum per-slot refund generation
"""
# Initial Storage Construction
initializer_code = create_sstore_initializer(initial_value)
initializer_addr = pre.deploy_contract(code=initializer_code)
# Benchmark Executor — multi-write per slot
executor_code = create_sstore_dirty_executor(
write_values=write_values,
key_warm=access_warm,
initial_value=initial_value,
)
executor_addr = pre.deploy_contract(code=executor_code)
authority = pre.fund_eoa(amount=0)
authority_nonce = 0
delegation_sender = pre.fund_eoa()
calldata_gen = partial(executor_calldata_generator)
access_list_gen = partial(
access_list_generator,
access_warm=access_warm,
authority=authority,
)
# Number of slots processable in execution phase
num_target_slots = sum(
executor_code.tx_iterations_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
calldata=calldata_gen,
access_list=access_list_gen,
start_iteration=1,
)
)
# Setup phase: initialize all slots to initial_value
with TestPhaseManager.setup():
blocks = build_delegated_storage_setup(
pre=pre,
fork=fork,
tx_gas_limit=tx_gas_limit,
needs_init=initial_value != 0,
num_target_slots=num_target_slots,
initializer_code=initializer_code,
initializer_addr=initializer_addr,
executor_addr=executor_addr,
authority=authority,
authority_nonce=authority_nonce,
delegation_sender=delegation_sender,
initializer_calldata_generator=(initializer_calldata_generator),
)
# Execution phase — no expected_benchmark_gas_used because
# refund cap (gas_used/5) makes actual consumption non-trivial
with TestPhaseManager.execution():
exec_txs = list(
executor_code.transactions_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
sender=pre.fund_eoa(),
to=authority,
calldata=calldata_gen,
start_iteration=1,
access_list=access_list_gen,
)
)
blocks.append(Block(txs=exec_txs))
benchmark_test(
pre=pre,
blocks=blocks,
skip_gas_used_validation=True,
)
|