Skip to content

test_bal_cross_tx_funding_chain()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_tx_funding_chain@9c2813ee.

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

Funding chain: alice → bob → charlie → dan → eunice → target. Each intermediate starts empty and must receive the prior tx's forwarded value to afford its own upfront gas + outgoing transfer. A client that parallelizes any later tx against pre-block state would see a zero balance on its sender and wrongly reject the block. The oog_minus_1 variant funds eunice with exactly gas_limit - 1 worth of gas so her SSTORE OOGs at the boundary (target's BAL flips from storage_changes to storage_reads). The insufficient_funds variant has dan forward one wei short of eunice's gas_limit * gas_price, so eunice's tx is rejected pre-execution and the entire block MUST be rejected with INSUFFICIENT_ACCOUNT_FUNDS — a sanity check on the off-by-one boundary of the upfront balance check.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
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
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
@pytest.mark.parametrize(
    "eunice_outcome",
    [
        pytest.param("success", id="success"),
        pytest.param("oog_minus_1", id="oog_minus_1"),
        pytest.param(
            "insufficient_funds",
            id="insufficient_funds",
            marks=pytest.mark.exception_test,
        ),
    ],
)
def test_bal_cross_tx_funding_chain(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    eunice_outcome: str,
) -> None:
    """
    Funding chain: alice → bob → charlie → dan → eunice → target. Each
    intermediate starts empty and must receive the prior tx's forwarded
    value to afford its own upfront gas + outgoing transfer. A client
    that parallelizes any later tx against pre-block state would see a
    zero balance on its sender and wrongly reject the block. The
    `oog_minus_1` variant funds eunice with exactly `gas_limit - 1`
    worth of gas so her SSTORE OOGs at the boundary (target's BAL flips
    from `storage_changes` to `storage_reads`). The `insufficient_funds`
    variant has dan forward one wei short of eunice's `gas_limit *
    gas_price`, so eunice's tx is rejected pre-execution and the entire
    block MUST be rejected with `INSUFFICIENT_ACCOUNT_FUNDS` — a sanity
    check on the off-by-one boundary of the upfront balance check.
    """
    gas_price = 0xA

    target_code = Op.SSTORE(
        0, 0xC0FFEE, key_warm=False, original_value=0, new_value=0xC0FFEE
    )
    target = pre.deploy_contract(code=target_code)

    intrinsic_calc = fork.transaction_intrinsic_cost_calculator()
    intrinsic_gas = intrinsic_calc()
    eunice_exact_gas = intrinsic_gas + target_code.gas_cost(fork)
    eunice_gas_limit = (
        eunice_exact_gas - 1
        if eunice_outcome == "oog_minus_1"
        else eunice_exact_gas
    )
    eunice_upfront = eunice_gas_limit * gas_price
    transfer_cost = intrinsic_gas * gas_price

    # Each sender (including alice) starts with or receives exactly what
    # the next forward + its own gas demands; everyone ends at zero in
    # the success/oog variants. `insufficient_funds` shorts eunice by
    # one wei via dan, leaving her unable to cover upfront gas.
    dan_value = (
        eunice_upfront - 1
        if eunice_outcome == "insufficient_funds"
        else eunice_upfront
    )
    charlie_value = transfer_cost + dan_value
    bob_value = transfer_cost + charlie_value
    alice_value = transfer_cost + bob_value
    alice_pre_balance = transfer_cost + alice_value

    alice = pre.fund_eoa(amount=alice_pre_balance)
    bob = pre.fund_eoa(amount=0)
    charlie = pre.fund_eoa(amount=0)
    dan = pre.fund_eoa(amount=0)
    eunice = pre.fund_eoa(amount=0)

    eunice_error = (
        TransactionException.INSUFFICIENT_ACCOUNT_FUNDS
        if eunice_outcome == "insufficient_funds"
        else None
    )

    txs = [
        Transaction(
            sender=alice,
            to=bob,
            value=alice_value,
            gas_limit=intrinsic_gas,
            gas_price=gas_price,
        ),
        Transaction(
            sender=bob,
            to=charlie,
            value=bob_value,
            gas_limit=intrinsic_gas,
            gas_price=gas_price,
        ),
        Transaction(
            sender=charlie,
            to=dan,
            value=charlie_value,
            gas_limit=intrinsic_gas,
            gas_price=gas_price,
        ),
        Transaction(
            sender=dan,
            to=eunice,
            value=dan_value,
            gas_limit=intrinsic_gas,
            gas_price=gas_price,
        ),
        Transaction(
            sender=eunice,
            to=target,
            gas_limit=eunice_gas_limit,
            gas_price=gas_price,
            error=eunice_error,
        ),
    ]

    if eunice_outcome == "insufficient_funds":
        blockchain_test(
            pre=pre,
            blocks=[
                Block(
                    txs=txs,
                    exception=(
                        TransactionException.INSUFFICIENT_ACCOUNT_FUNDS
                    ),
                )
            ],
            post={},
        )
        return

    if eunice_outcome == "oog_minus_1":
        target_bal = BalAccountExpectation(
            storage_reads=[0],
            nonce_changes=[],
            balance_changes=[],
            code_changes=[],
            storage_changes=[],
        )
        target_post = Account(storage={})
    elif eunice_outcome == "success":
        target_bal = BalAccountExpectation(
            storage_changes=[
                BalStorageSlot(
                    slot=0,
                    slot_changes=[
                        BalStorageChange(
                            block_access_index=5, post_value=0xC0FFEE
                        ),
                    ],
                ),
            ],
            nonce_changes=[],
            balance_changes=[],
            code_changes=[],
            storage_reads=[],
        )
        target_post = Account(storage={0: 0xC0FFEE})
    else:
        raise ValueError(f"unknown eunice_outcome: {eunice_outcome}")

    account_expectations = {
        alice: BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(block_access_index=1, post_balance=0),
            ],
        ),
        bob: BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(
                    block_access_index=1, post_balance=alice_value
                ),
                BalBalanceChange(block_access_index=2, post_balance=0),
            ],
        ),
        charlie: BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=3, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(block_access_index=2, post_balance=bob_value),
                BalBalanceChange(block_access_index=3, post_balance=0),
            ],
        ),
        dan: BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=4, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(
                    block_access_index=3, post_balance=charlie_value
                ),
                BalBalanceChange(block_access_index=4, post_balance=0),
            ],
        ),
        eunice: BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=5, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(block_access_index=4, post_balance=dan_value),
                BalBalanceChange(block_access_index=5, post_balance=0),
            ],
        ),
        target: target_bal,
    }

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=txs,
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations=account_expectations,
                ),
            )
        ],
        post={
            alice: Account(nonce=1, balance=0),
            bob: Account(nonce=1, balance=0),
            charlie: Account(nonce=1, balance=0),
            dan: Account(nonce=1, balance=0),
            eunice: Account(nonce=1, balance=0),
            target: target_post,
        },
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.