Skip to content

test_bal_cross_tx_balance_dependency()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_tx_balance_dependency@622620d8.

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

Verify clients apply Tx1's balance change before executing Tx2 in the same block. Tx1 routes value into a contract; Tx2 invokes the contract which records its SELFBALANCE to storage. A client that parallelizes Tx2 without applying Tx1's balance_changes would record the pre-block balance, yielding a different state root. The selfdestruct variant routes the funds via SELFDESTRUCT from a pre-funded killer contract so the recipient's bytecode never runs in Tx1 — catching any client optimization that ties balance tracking to code execution.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
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
@pytest.mark.parametrize(
    "funding_method",
    ["direct_call", "selfdestruct"],
)
def test_bal_cross_tx_balance_dependency(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    funding_method: str,
) -> None:
    """
    Verify clients apply Tx1's balance change before executing Tx2 in
    the same block. Tx1 routes value into a contract; Tx2 invokes the
    contract which records its `SELFBALANCE` to storage. A client that
    parallelizes Tx2 without applying Tx1's `balance_changes` would
    record the pre-block balance, yielding a different state root. The
    `selfdestruct` variant routes the funds via SELFDESTRUCT from a
    pre-funded killer contract so the recipient's bytecode never runs
    in Tx1 — catching any client optimization that ties balance
    tracking to code execution.
    """
    transferred = 1
    alice = pre.fund_eoa()
    bob = pre.fund_eoa()

    # Any non-empty calldata triggers the SELFBALANCE record path;
    # empty calldata is the value-receiver path.
    contract = pre.deploy_contract(
        code=Conditional(
            condition=Op.ISZERO(Op.CALLDATASIZE),
            if_true=Op.STOP,
            if_false=Op.SSTORE(0, Op.SELFBALANCE),
        ),
    )

    if funding_method == "direct_call":
        tx_send = Transaction(sender=alice, to=contract, value=transferred)
        send_expectations: dict = {}
    elif funding_method == "selfdestruct":
        killer = pre.deploy_contract(
            code=Op.SELFDESTRUCT(contract),
            balance=transferred,
        )
        tx_send = Transaction(sender=alice, to=killer)
        send_expectations = {
            killer: BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=0),
                ],
            ),
        }
    else:
        raise ValueError(f"unknown funding_method: {funding_method}")

    tx_read = Transaction(sender=bob, to=contract, data=b"\x01")

    account_expectations = {
        contract: BalAccountExpectation(
            balance_changes=[
                BalBalanceChange(
                    block_access_index=1, post_balance=transferred
                ),
            ],
            storage_changes=[
                BalStorageSlot(
                    slot=0,
                    slot_changes=[
                        BalStorageChange(
                            block_access_index=2, post_value=transferred
                        ),
                    ],
                ),
            ],
        ),
        **send_expectations,
    }

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx_send, tx_read],
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations=account_expectations
                ),
            )
        ],
        post={
            contract: Account(balance=transferred, storage={0: transferred}),
        },
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.