Skip to content

test_bal_multiple_balance_changes_same_account()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_multiple_balance_changes_same_account@892e6d1e.

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

Ensure BAL correctly tracks multiple balance changes to same account across multiple transactions.

An account that receives funds in TX0 and spends them in TX1 should have TWO balance change entries in the BAL, one for each transaction.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
def test_bal_multiple_balance_changes_same_account(
    pre: Alloc,
    fork: Fork,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Ensure BAL correctly tracks multiple balance changes to same account
    across multiple transactions.

    An account that receives funds in TX0 and spends them in TX1 should
    have TWO balance change entries in the BAL, one for each transaction.
    """
    alice = pre.fund_eoa()
    bob = pre.fund_eoa(amount=0)
    charlie = pre.fund_eoa(amount=0)

    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    tx_intrinsic_gas = intrinsic_gas_calculator(calldata=b"", access_list=[])

    # bob receives funds in tx0, then spends everything in tx1
    gas_price = 10
    tx1_gas_cost = tx_intrinsic_gas * gas_price
    spend_amount = 100
    funding_amount = tx1_gas_cost + spend_amount

    tx0 = Transaction(
        sender=alice,
        to=bob,
        value=funding_amount,
        gas_limit=tx_intrinsic_gas,
        gas_price=gas_price,
    )

    tx1 = Transaction(
        sender=bob,
        to=charlie,
        value=spend_amount,
        gas_limit=tx_intrinsic_gas,
        gas_price=gas_price,
    )

    bob_balance_after_tx0 = funding_amount
    bob_balance_after_tx1 = 0

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx0, tx1],
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations={
                        alice: BalAccountExpectation(
                            nonce_changes=[
                                BalNonceChange(
                                    block_access_index=1, post_nonce=1
                                )
                            ],
                        ),
                        bob: BalAccountExpectation(
                            nonce_changes=[
                                BalNonceChange(
                                    block_access_index=2, post_nonce=1
                                )
                            ],
                            balance_changes=[
                                BalBalanceChange(
                                    block_access_index=1,
                                    post_balance=bob_balance_after_tx0,
                                ),
                                BalBalanceChange(
                                    block_access_index=2,
                                    post_balance=bob_balance_after_tx1,
                                ),
                            ],
                        ),
                        charlie: BalAccountExpectation(
                            balance_changes=[
                                BalBalanceChange(
                                    block_access_index=2,
                                    post_balance=spend_amount,
                                )
                            ],
                        ),
                    }
                ),
            )
        ],
        post={
            bob: Account(nonce=1, balance=bob_balance_after_tx1),
            charlie: Account(balance=spend_amount),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.