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@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

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
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
@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")
@pytest.mark.pre_alloc_group(
    "beacon_root_deploy_contract",
    reason=(
        "This test is parametrized with a hard-coded address (the beacon root contract deployer "
        "address); they can't be in the same pre alloc group."
    ),
)
def test_beacon_root_contract_deploy(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    beacon_root: bytes,
    tx: Transaction,
    timestamp: int,
    post: Dict,
    fork: Fork,
) -> None:
    """
    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(block_number=1, timestamp=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(
                            block_number=1, timestamp=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