Skip to content

test_beacon_root_transition()

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

Generate fixtures for these test cases for Cancun with:

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

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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
@pytest.mark.parametrize(
    "timestamps_factory",
    [pytest.param(count_factory(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_factory: Callable[[], Iterator[int]],
    beacon_roots: Iterator[bytes],
    block_count: int,
    call_gas: int,
    call_value: int,
    fork: TransitionFork,
) -> None:
    """
    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.
    """
    # 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] = []
    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.fork_at(
            block_number=i, timestamp=timestamp
        ).header_beacon_root_required()
        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 generates 1 parametrized test case across 1 fork.