Skip to content

test_beacon_root_contract_deploy()

Documentation for tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py::test_beacon_root_contract_deploy@21fb11c8.

Generate fixtures for these test cases for Prague with:

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

Tests the fork transition to cancun deploying the contract during Shanghai and verifying the code deployed and its functionality after Cancun.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
@pytest.mark.parametrize(
    "timestamp",
    [
        pytest.param(15_000, id="deploy_on_shanghai"),
        pytest.param(30_000, id="deploy_on_cancun"),
    ],
)
@pytest.mark.valid_at_transition_to("Cancun")
def test_beacon_root_contract_deploy(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    beacon_root: bytes,
    tx: Transaction,
    timestamp: int,
    post: Dict,
    fork: Fork,
):
    """
    Tests the fork transition to cancun deploying the contract during Shanghai and verifying the
    code deployed and its functionality after Cancun.
    """
    assert fork.header_beacon_root_required(1, timestamp)
    tx_gas_limit = 0x3D090
    tx_gas_price = 0xE8D4A51000
    deployer_required_balance = tx_gas_limit * tx_gas_price
    deploy_tx = Transaction(
        ty=0,
        nonce=0,
        to=None,
        gas_limit=tx_gas_limit,
        gas_price=tx_gas_price,
        value=0,
        data=bytes.fromhex(
            "60618060095f395ff33373fffffffffffffffffffffffffffffffffffffffe14604d576020361460"
            "24575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f526020"
            "5ff35b5f5ffd5b62001fff42064281555f359062001fff015500"
        ),
        v=0x1B,
        r=0x539,
        s=0x1B9B6EB1F0,
        protected=False,
    ).with_signature_and_sender()
    deployer_address = deploy_tx.sender
    assert deployer_address is not None
    assert Address(deployer_address) == Spec.BEACON_ROOTS_DEPLOYER_ADDRESS
    blocks: List[Block] = []

    beacon_root_contract_storage: Dict = {}
    for i, current_timestamp in enumerate(range(timestamp // 2, timestamp + 1, timestamp // 2)):
        if i == 0:
            blocks.append(
                Block(  # Deployment block
                    txs=[deploy_tx],
                    parent_beacon_block_root=(
                        beacon_root
                        if fork.header_beacon_root_required(1, current_timestamp)
                        else None
                    ),
                    timestamp=timestamp // 2,
                    withdrawals=[
                        # Also withdraw to the beacon root contract and the system address
                        Withdrawal(
                            address=Spec.BEACON_ROOTS_ADDRESS,
                            amount=1,
                            index=0,
                            validator_index=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=1,
                            validator_index=1,
                        ),
                    ],
                )
            )
            beacon_root_contract_storage[current_timestamp % Spec.HISTORY_BUFFER_LENGTH] = 0
            beacon_root_contract_storage[
                (current_timestamp % Spec.HISTORY_BUFFER_LENGTH) + Spec.HISTORY_BUFFER_LENGTH
            ] = 0
        elif i == 1:
            blocks.append(
                Block(  # Contract already deployed
                    txs=[tx],
                    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=2,
                            validator_index=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=3,
                            validator_index=1,
                        ),
                    ],
                ),
            )
            beacon_root_contract_storage[current_timestamp % Spec.HISTORY_BUFFER_LENGTH] = (
                current_timestamp
            )
            beacon_root_contract_storage[
                (current_timestamp % Spec.HISTORY_BUFFER_LENGTH) + Spec.HISTORY_BUFFER_LENGTH
            ] = beacon_root
        else:
            raise AssertionError("This test should only have two blocks")

    expected_code = fork.pre_allocation_blockchain()[Spec.BEACON_ROOTS_ADDRESS]["code"]
    pre[Spec.BEACON_ROOTS_ADDRESS] = Account(
        code=b"",  # Remove the code that is automatically allocated on Cancun fork
        nonce=0,
        balance=0,
    )
    pre[deployer_address] = Account(
        balance=deployer_required_balance,
    )

    post[Spec.BEACON_ROOTS_ADDRESS] = Account(
        storage=beacon_root_contract_storage,
        code=expected_code,
        nonce=1,
        balance=int(2e9),
    )
    post[Spec.SYSTEM_ADDRESS] = Account(
        storage={},
        code=b"",
        nonce=0,
        balance=int(2e9),
    )
    post[deployer_address] = Account(
        balance=175916000000000000,  # It doesn't consume all the balance :(
        nonce=1,
    )
    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) timestamp
...fork_ShanghaiToCancunAtTime15k-blockchain_test-deploy_on_shanghai 15000
...fork_ShanghaiToCancunAtTime15k-blockchain_test-deploy_on_cancun 30000