Skip to content

test_bal_cross_tx_storage_chain()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_cross_tx_storage_chain@2119b382.

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

Verify clients apply BAL state changes from prior transactions before executing later transactions in the same block.

Each Tx i seeds slots 0 and 1 with 1, then computes a Fibonacci-style sum into slot i: slot[i] = SLOAD(i-1) + SLOAD(i-2). Every Tx i>=2 depends on the two immediately preceding writes, so any parallelization that fails to apply a prior Tx's BAL storage change cascades into a wrong slot value and a different state root.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
def test_bal_cross_tx_storage_chain(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Verify clients apply BAL state changes from prior transactions before
    executing later transactions in the same block.

    Each Tx i seeds slots 0 and 1 with `1`, then computes a
    Fibonacci-style sum into slot i: `slot[i] = SLOAD(i-1) + SLOAD(i-2)`.
    Every Tx i>=2 depends on the two immediately preceding writes, so
    any parallelization that fails to apply a prior Tx's BAL storage
    change cascades into a wrong slot value and a different state root.
    """
    chain_length = 8
    # i<2 seeds slot i with 1; i>=2 computes the Fibonacci sum.
    contract = pre.deploy_contract(
        code=Conditional(
            condition=Op.LT(Op.CALLDATALOAD(0), 2),
            if_true=Op.SSTORE(Op.CALLDATALOAD(0), 1),
            if_false=Op.SSTORE(
                Op.CALLDATALOAD(0),
                Op.ADD(
                    Op.SLOAD(Op.SUB(Op.CALLDATALOAD(0), 1)),
                    Op.SLOAD(Op.SUB(Op.CALLDATALOAD(0), 2)),
                ),
            ),
        ),
    )

    fib = [1, 1]
    for i in range(2, chain_length):
        fib.append(fib[i - 1] + fib[i - 2])

    txs = []
    senders = []
    for i in range(chain_length):
        sender = pre.fund_eoa()
        senders.append(sender)
        txs.append(
            Transaction(
                sender=sender,
                to=contract,
                data=Hash(i),
            )
        )

    account_expectations: dict = {
        sender: BalAccountExpectation(
            nonce_changes=[
                BalNonceChange(block_access_index=i + 1, post_nonce=1)
            ],
        )
        for i, sender in enumerate(senders)
    }
    account_expectations[contract] = BalAccountExpectation(
        storage_changes=[
            BalStorageSlot(
                slot=i,
                slot_changes=[
                    BalStorageChange(
                        block_access_index=i + 1, post_value=fib[i]
                    ),
                ],
            )
            for i in range(chain_length)
        ],
        nonce_changes=[],
        balance_changes=[],
        code_changes=[],
        storage_reads=[],
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=txs,
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations=account_expectations
                ),
            )
        ],
        post={
            contract: Account(
                storage={i: fib[i] for i in range(chain_length)}
            ),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.