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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
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 | @pytest.mark.parametrize(
"timestamps_factory",
[
pytest.param(
count_factory(
start=Spec.HISTORY_BUFFER_LENGTH - 5,
step=1,
),
id="buffer_wraparound",
),
pytest.param(
count_factory(
start=12,
step=Spec.HISTORY_BUFFER_LENGTH,
),
id="buffer_wraparound_overwrite",
),
pytest.param(
count_factory(
start=2**32,
step=Spec.HISTORY_BUFFER_LENGTH,
),
id="buffer_wraparound_overwrite_high_timestamp",
),
pytest.param(
count_factory(
start=5,
step=Spec.HISTORY_BUFFER_LENGTH - 1,
),
id="buffer_wraparound_no_overwrite",
),
pytest.param(
count_factory(
start=Spec.HISTORY_BUFFER_LENGTH - 3,
step=Spec.HISTORY_BUFFER_LENGTH + 1,
),
id="buffer_wraparound_no_overwrite_2",
),
],
)
@pytest.mark.parametrize("block_count", [10]) # All tests use 10 blocks
@pytest.mark.valid_from("Cancun")
def test_multi_block_beacon_root_timestamp_calls(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
timestamps_factory: Callable[[], Iterator[int]],
beacon_roots: Iterator[bytes],
block_count: int,
call_gas: int,
call_value: int,
) -> None:
"""
Tests multiple blocks where each block writes a timestamp to storage and
contains one transaction that calls the beacon root contract multiple
times.
The blocks might overwrite the historical roots buffer, or not, depending
on the `timestamps`, and whether they increment in multiples of
`Spec.HISTORY_BUFFER_LENGTH` or not.
By default, the beacon roots are the keccak of the block number.
Each transaction checks the current timestamp and also all previous
timestamps, and verifies that the beacon root is correct for all of them if
the timestamp is supposed to be in the buffer, which might have been
overwritten by a later block.
"""
# Create fresh iterator to avoid state persistence between test phases
timestamps = timestamps_factory()
blocks: List[Block] = []
post = {}
timestamps_storage: Dict[int, int] = {}
roots_storage: Dict[int, bytes] = {}
all_timestamps: List[int] = []
sender = pre.fund_eoa()
for timestamp, beacon_root, _i in zip(
timestamps,
beacon_roots,
range(block_count),
strict=False,
):
timestamp_index = timestamp % Spec.HISTORY_BUFFER_LENGTH
timestamps_storage[timestamp_index] = timestamp
roots_storage[timestamp_index] = beacon_root
all_timestamps.append(timestamp)
withdraw_index = count(0)
current_call_account_code = Bytecode()
current_call_account_expected_storage = Storage()
# We are going to call the beacon roots contract once for every
# timestamp of the current and all previous blocks, and check that the
# returned beacon root is still correct only if it was not overwritten.
for t in all_timestamps:
current_call_account_code += Op.MSTORE(0, t)
call_valid = (
timestamp_index in timestamps_storage
and timestamps_storage[t % Spec.HISTORY_BUFFER_LENGTH] == t
)
current_call_account_code += Op.SSTORE(
current_call_account_expected_storage.store_next(
0x01 if call_valid else 0x00
),
Op.CALL(
call_gas,
Spec.BEACON_ROOTS_ADDRESS,
call_value,
0x00,
0x20,
0x20,
0x20,
),
)
current_call_account_code += Op.SSTORE(
current_call_account_expected_storage.store_next(
roots_storage[t % Spec.HISTORY_BUFFER_LENGTH]
if call_valid
else 0x00
),
Op.MLOAD(0x20),
)
current_call_account_address = pre.deploy_contract(
current_call_account_code
)
post[current_call_account_address] = Account(
storage=current_call_account_expected_storage,
)
blocks.append(
Block(
txs=[
Transaction(
sender=sender,
to=current_call_account_address,
data=Hash(timestamp),
gas_limit=1_000_000,
)
],
parent_beacon_block_root=beacon_root,
timestamp=timestamp,
withdrawals=[
# Also withdraw to the beacon root contract and the system
# address
Withdrawal(
address=Spec.BEACON_ROOTS_ADDRESS,
amount=1,
index=next(withdraw_index),
validator_index=0,
),
Withdrawal(
address=Spec.SYSTEM_ADDRESS,
amount=1,
index=next(withdraw_index),
validator_index=1,
),
],
)
)
blockchain_test(
pre=pre,
blocks=blocks,
post=post,
)
|