Skip to content

test_bal_create_nonce_overflow()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_create_nonce_overflow@8acae1b0.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_create_nonce_overflow --fork Amsterdam

Test BAL with the factory's nonce at the EIP-2681 boundary.

At the maximum nonce the creation fails before the computed address is accessed, so the address MUST NOT appear in the BAL; one below the maximum the creation proceeds and the address appears with its deployed nonce and code.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
@pytest.mark.with_all_create_opcodes
@pytest.mark.parametrize(
    "factory_nonce",
    [
        pytest.param(Spec2681.max_nonce, id="nonce_at_max"),
        pytest.param(Spec2681.max_nonce - 1, id="nonce_below_max"),
    ],
)
def test_bal_create_nonce_overflow(
    pre: Alloc,
    state_test: StateTestFiller,
    create_opcode: Op,
    factory_nonce: int,
) -> None:
    """
    Test BAL with the factory's nonce at the EIP-2681 boundary.

    At the maximum nonce the creation fails before the computed address
    is accessed, so the address MUST NOT appear in the BAL; one below
    the maximum the creation proceeds and the address appears with its
    deployed nonce and code.
    """
    alice = pre.fund_eoa()

    init_code = Initcode(deploy_code=Op.STOP)
    init_code_bytes = bytes(init_code)

    factory_code = (
        Op.MSTORE(0, Op.PUSH32(init_code_bytes))
        + Op.SSTORE(
            0x00,
            Op.GT(
                create_opcode(
                    value=0,
                    offset=32 - len(init_code_bytes),
                    size=len(init_code_bytes),
                ),
                0,
            ),
        )
        + Op.STOP
    )

    factory = pre.deploy_contract(
        code=factory_code,
        nonce=factory_nonce,
        storage={0x00: 0xDEAD},
    )

    target = compute_create_address(
        address=factory,
        nonce=factory_nonce,
        salt=0,
        initcode=init_code_bytes,
        opcode=create_opcode,
    )

    tx = Transaction(sender=alice, to=factory)

    factory_nonce_changes: list[BalNonceChange]
    target_expectation: BalAccountExpectation | None
    target_post: Account | None

    if factory_nonce == Spec2681.max_nonce:
        create_result = 0
        factory_nonce_changes = []
        target_expectation = None
        target_post = Account.NONEXISTENT
    elif factory_nonce == Spec2681.max_nonce - 1:
        create_result = 1
        factory_nonce_changes = [
            BalNonceChange(block_access_index=1, post_nonce=Spec2681.max_nonce)
        ]
        target_expectation = BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
            code_changes=[
                BalCodeChange(block_access_index=1, new_code=bytes(Op.STOP))
            ],
        )
        target_post = Account(nonce=1, code=Op.STOP)
    else:
        raise ValueError(f"Invariant: unhandled factory_nonce {factory_nonce}")

    state_test(
        pre=pre,
        post={
            alice: Account(nonce=1),
            # At the boundary the nonce is unchanged; one below, it is
            # incremented into it. Both arms end at the maximum.
            factory: Account(
                nonce=Spec2681.max_nonce, storage={0x00: create_result}
            ),
            target: target_post,
        },
        tx=tx,
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                ),
                factory: BalAccountExpectation(
                    nonce_changes=factory_nonce_changes,
                    storage_changes=[
                        BalStorageSlot(
                            slot=0x00,
                            slot_changes=[
                                BalStorageChange(
                                    block_access_index=1,
                                    post_value=create_result,
                                )
                            ],
                        )
                    ],
                ),
                target: target_expectation,
            }
        ),
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.