Skip to content

test_bal_create_early_failure()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_create_early_failure@21507778.

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_early_failure --fork Amsterdam

Test BAL with CREATE failure due to insufficient endowment.

Factory (balance=50) attempts CREATE(value=100). Fails before nonce increment (before track_address). Distinct from collision where address IS accessed.

Expected BAL: - Alice: nonce_changes - Factory: storage_changes slot 0 (0xDEAD→0), NO nonce_changes - Contract address: MUST NOT appear (never accessed)

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
def test_bal_create_early_failure(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Test BAL with CREATE failure due to insufficient endowment.

    Factory (balance=50) attempts CREATE(value=100).
    Fails before nonce increment (before track_address).
    Distinct from collision where address IS accessed.

    Expected BAL:
    - Alice: nonce_changes
    - Factory: storage_changes slot 0 (0xDEAD→0), NO nonce_changes
    - Contract address: MUST NOT appear (never accessed)
    """
    alice = pre.fund_eoa()

    factory_balance = 50
    endowment = 100  # More than factory has

    # Simple init code that deploys STOP
    init_code = Initcode(deploy_code=Op.STOP)
    init_code_bytes = bytes(init_code)

    # Factory code: CREATE(value=endowment) and store result in slot 0
    factory_code = (
        # Push init code to memory
        Op.MSTORE(0, Op.PUSH32(init_code_bytes))
        # SSTORE(0, CREATE(value, offset, size))
        + Op.SSTORE(
            0x00,
            Op.CREATE(
                value=endowment,  # 100 > 50, will fail
                offset=32 - len(init_code_bytes),
                size=len(init_code_bytes),
            ),
        )
        + Op.STOP
    )

    # Deploy factory with insufficient balance for the CREATE endowment
    factory = pre.deploy_contract(
        code=factory_code,
        balance=factory_balance,
        storage={0x00: 0xDEAD},  # Initial value to prove SSTORE works
    )

    # Calculate what the contract address WOULD be (but it won't be created)
    would_be_contract_address = compute_create_address(
        address=factory, nonce=1
    )

    tx = Transaction(
        sender=alice,
        to=factory,
        gas_limit=1_000_000,
    )

    block = Block(
        txs=[tx],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                ),
                factory: BalAccountExpectation(
                    # NO nonce_changes - CREATE failed before increment_nonce
                    nonce_changes=[],
                    # Storage changes: slot 0 = 0xDEAD → 0 (CREATE returned 0)
                    storage_changes=[
                        BalStorageSlot(
                            slot=0x00,
                            slot_changes=[
                                BalStorageChange(
                                    block_access_index=1, post_value=0
                                )
                            ],
                        )
                    ],
                ),
                # Contract address MUST NOT appear in BAL - never accessed
                # (CREATE failed before track_address was called)
                would_be_contract_address: None,
            }
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[block],
        post={
            alice: Account(nonce=1),
            # Factory nonce unchanged (still 1), balance unchanged
            factory: Account(
                nonce=1, balance=factory_balance, storage={0x00: 0}
            ),
            # Contract was never created
            would_be_contract_address: Account.NONEXISTENT,
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.