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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388 | @pytest.mark.parametrize(
"timestamp,beacon_root,query_timestamp,expected_result,is_valid",
[
pytest.param(
12, Hash(0xABCDEF), 12, Hash(0xABCDEF), True, id="valid_timestamp"
),
pytest.param(12, Hash(0xABCDEF), 42, 0, False, id="invalid_timestamp"),
pytest.param(12, Hash(0xABCDEF), 0, 0, False, id="zero_timestamp"),
],
)
@pytest.mark.parametrize(
"value",
[
pytest.param(0, id="no_value"),
pytest.param(100, id="with_value"),
],
)
def test_bal_4788_query(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
timestamp: int,
beacon_root: Hash,
query_timestamp: int,
expected_result: int | Hash,
is_valid: bool,
value: int,
) -> None:
"""
Ensure BAL captures storage reads when querying beacon root.
Test scenarios:
1. Valid query (timestamp=12, matches stored timestamp): Beacon root
contract reads both timestamp and root slots, query contract writes
returned value
2. Invalid query with non-zero timestamp (timestamp=42, no match):
Beacon root contract reads only timestamp slot then reverts, query
contract has implicit read recorded
3. Invalid query with zero timestamp (timestamp=0): Beacon root
contract reverts immediately before any storage access, query
contract has implicit read recorded
4. With value transfer: BAL captures balance changes in addition
to storage operations (only when query is valid)
"""
# Block 1: Store beacon root
block1 = build_beacon_root_setup_block(timestamp, beacon_root)
# Block 2: Alice queries the beacon root
alice = pre.fund_eoa()
# Contract that calls beacon root contract with timestamp from calldata
# and stores returned beacon root in slot 0, forwarding any value sent
query_code = (
Op.CALLDATACOPY(0, 0, 32)
+ Op.CALL(
Spec.BEACON_ROOTS_CALL_GAS,
BEACON_ROOTS_ADDRESS,
Op.CALLVALUE, # forward value to beacon root contract
0, # args offset
32, # args size (timestamp)
32, # return offset
32, # return size (beacon root)
)
+ Op.SSTORE(0, Op.MLOAD(32))
)
query_contract = pre.deploy_contract(query_code)
tx = Transaction(
sender=alice,
to=query_contract,
data=Hash(query_timestamp),
value=value,
gas_limit=fork.transaction_gas_limit_cap(),
)
# Build BAL expectations for block 2
block2_timestamp = timestamp + 1
block2_beacon_root = Hash(0xDEADBEEF)
account_expectations = beacon_root_system_call_expectations(
block2_timestamp, block2_beacon_root
)
# Add storage reads for the query
timestamp_slot, root_slot = get_beacon_root_slots(query_timestamp)
# Storage access depends on query validity:
# - Zero timestamp: reverts immediately (no storage access)
# - Valid timestamp: reads both timestamp and root slots
# - Invalid non-zero timestamp: reads only timestamp slot before reverting
account_expectations[BEACON_ROOTS_ADDRESS].storage_reads = (
[]
if query_timestamp == 0 # Reverts early if timestamp is zero
else [timestamp_slot, root_slot]
if is_valid
else [timestamp_slot]
)
# Balance changes for callee: credited if valid, must be empty if invalid
if value > 0:
account_expectations[BEACON_ROOTS_ADDRESS].balance_changes = (
[BalBalanceChange(block_access_index=1, post_balance=value)]
if is_valid
else []
)
# Add transaction-specific expectations
account_expectations[alice] = BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
)
account_expectations[query_contract] = BalAccountExpectation(
# If the call to beacon root contract reverts
# a no-op write happens and an implicit read is
# recorded.
storage_reads=[] if is_valid else [0],
# Write reverts if invalid
storage_changes=[
BalStorageSlot(
slot=0,
slot_changes=[
BalStorageChange(
block_access_index=1, post_value=expected_result
)
],
),
]
if is_valid
else [],
# if value > 0 and invalid, no balance is sent to beacon root so
# is kept in the query contract
balance_changes=[
BalBalanceChange(
block_access_index=1,
post_balance=value,
)
]
if not is_valid and value > 0
else [],
)
block2 = Block(
txs=[tx],
parent_beacon_block_root=block2_beacon_root,
timestamp=block2_timestamp,
expected_block_access_list=BlockAccessListExpectation(
account_expectations=account_expectations
),
)
post_state = {
alice: Account(nonce=1),
query_contract: Account(storage={0: expected_result}),
}
if value > 0 and is_valid:
post_state[BEACON_ROOTS_ADDRESS] = Account(balance=value)
blockchain_test(
pre=pre,
blocks=[block1, block2],
post=post_state,
)
|