Skip to content

test_bal_cross_tx_factory_nonce_create_chain()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_tx_factory_nonce_create_chain@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_tx_factory_nonce_create_chain --fork Amsterdam

Cross-tx CREATE chain: 8 senders share a factory whose CREATE address derives solely from factory.nonce. collision and oog test opposite parallelization hazards mid-chain — collision still bumps factory.nonce (later txs slide forward), OOG does not (later txs slide backward, reusing the OOG'd slot).

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
@pytest.mark.parametrize(
    "failure_mode",
    [
        pytest.param("none", id="no_failure"),
        pytest.param("collision", id="mid_chain_collision"),
        pytest.param("oog", id="mid_chain_oog"),
    ],
)
@pytest.mark.pre_alloc_mutable()
def test_bal_cross_tx_factory_nonce_create_chain(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    failure_mode: str,
) -> None:
    """
    Cross-tx CREATE chain: 8 senders share a factory whose CREATE
    address derives solely from `factory.nonce`. `collision` and `oog`
    test opposite parallelization hazards mid-chain — collision still
    bumps factory.nonce (later txs slide forward), OOG does not (later
    txs slide backward, reusing the OOG'd slot).
    """
    chain_length = 8
    failure_index = 3 if failure_mode in ("collision", "oog") else None

    factory_code = (
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.CREATE(0, 0, Op.CALLDATASIZE)
        + Op.STOP
    )
    factory = pre.deploy_contract(code=factory_code)
    factory_pre_nonce = 1

    deploy_code = Op.STOP
    initcode = Initcode(deploy_code=deploy_code)
    collision_code = Op.PUSH1(0x42) + Op.STOP

    targets = [
        compute_create_address(address=factory, nonce=factory_pre_nonce + k)
        for k in range(chain_length)
    ]

    if failure_mode == "collision":
        assert failure_index is not None
        pre[targets[failure_index]] = Account(code=collision_code)

    sequence: list[dict] = []
    factory_nonce = factory_pre_nonce
    for i in range(chain_length):
        block_idx = i + 1
        if failure_mode == "oog" and i == failure_index:
            sequence.append(
                {"block_idx": block_idx, "target_idx": None, "deployed": False}
            )
        else:
            target_idx = factory_nonce - factory_pre_nonce
            factory_nonce += 1
            deployed = not (failure_mode == "collision" and i == failure_index)
            sequence.append(
                {
                    "block_idx": block_idx,
                    "factory_post_nonce": factory_nonce,
                    "target_idx": target_idx,
                    "deployed": deployed,
                }
            )

    senders = [pre.fund_eoa() for _ in range(chain_length)]
    # OOG tx: intrinsic + 1 — valid to include but no gas to run CREATE.
    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        calldata=bytes(initcode), contract_creation=False, access_list=[]
    )
    txs = [
        Transaction(
            sender=senders[i],
            to=factory,
            data=initcode,
            gas_limit=(
                intrinsic + 1
                if failure_mode == "oog" and i == failure_index
                else fork.transaction_gas_limit_cap()
            ),
        )
        for i in range(chain_length)
    ]

    account_expectations: dict = {
        senders[i]: BalAccountExpectation(
            nonce_changes=[
                BalNonceChange(block_access_index=i + 1, post_nonce=1)
            ],
        )
        for i in range(chain_length)
    }
    # Factory: only txs that bumped its nonce contribute entries.
    account_expectations[factory] = BalAccountExpectation(
        nonce_changes=[
            BalNonceChange(
                block_access_index=s["block_idx"],
                post_nonce=s["factory_post_nonce"],
            )
            for s in sequence
            if s["target_idx"] is not None
        ],
    )
    for s in sequence:
        if s["target_idx"] is None:
            continue
        target = targets[s["target_idx"]]
        if s["deployed"]:
            account_expectations[target] = BalAccountExpectation(
                nonce_changes=[
                    BalNonceChange(
                        block_access_index=s["block_idx"], post_nonce=1
                    )
                ],
                code_changes=[
                    BalCodeChange(
                        block_access_index=s["block_idx"],
                        new_code=deploy_code,
                    )
                ],
            )
        else:
            # Collision: accessed during EIP-684 check, no state change.
            account_expectations[target] = BalAccountExpectation.empty()

    touched_target_idxs = {
        s["target_idx"] for s in sequence if s["target_idx"] is not None
    }
    final_factory_nonce = factory_pre_nonce + len(touched_target_idxs)
    post: dict = {
        factory: Account(nonce=final_factory_nonce),
        **{sender: Account(nonce=1) for sender in senders},
    }
    for s in sequence:
        if s["target_idx"] is None:
            continue
        target = targets[s["target_idx"]]
        post[target] = (
            Account(nonce=1, code=deploy_code)
            if s["deployed"]
            else Account(code=collision_code)
        )
    for k, target in enumerate(targets):
        if k not in touched_target_idxs:
            post[target] = Account.NONEXISTENT

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=txs,
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations=account_expectations
                ),
            )
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.