Skip to content

test_beacon_root_transition()

Documentation for tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py::test_beacon_root_transition@83970623.

Generate fixtures for these test cases for Prague with:

fill -v tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py::test_beacon_root_transition --fork Prague

Tests the fork transition to cancun and verifies that blocks with timestamp lower than the transition timestamp do not contain beacon roots in the pre-deployed contract.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
@pytest.mark.parametrize(
    "timestamps",
    [pytest.param(count(start=1000, step=1000), id="fork_transition")],
)
@pytest.mark.parametrize("block_count", [20])
@pytest.mark.valid_at_transition_to("Cancun")
def test_beacon_root_transition(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    timestamps: Iterator[int],
    beacon_roots: Iterator[bytes],
    block_count: int,
    call_gas: int,
    call_value: int,
    fork: Fork,
):
    """
    Tests the fork transition to cancun and verifies that blocks with timestamp lower than the
    transition timestamp do not contain beacon roots in the pre-deployed contract.
    """
    blocks: List[Block] = []
    post = {}

    timestamps_storage: Dict[int, int] = {}
    roots_storage: Dict[int, bytes] = {}

    all_timestamps: List[int] = []
    timestamps_in_beacon_root_contract: 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

        transitioned = fork.header_beacon_root_required(i, timestamp)
        if transitioned:
            # We've transitioned, the current timestamp must contain a value in the contract
            timestamps_in_beacon_root_contract.append(timestamp)
            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 correct only
        # if it was after the transition timestamp.
        for t in all_timestamps:
            current_call_account_code += Op.MSTORE(0, t)
            call_valid = (
                t in timestamps_in_beacon_root_contract
                and 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 if transitioned else None,
                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,
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) block_count timestamps
...fork_ShanghaiToCancunAtTime15k-blockchain_test-block_count_20-fork_transition 20 count(1000, 1000)