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@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_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
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
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.