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@c74f1a67.

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
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
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=[],
        recipient_type=RecipientType.EMPTY_ACCOUNT,
        sends_value=True,
    )
    top_frame_state_gas = fork.transaction_top_frame_state_gas(
        sends_value=True,
        recipient_type=RecipientType.EMPTY_ACCOUNT,
    )

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

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

    tx1 = Transaction(
        sender=bob,
        to=charlie,
        value=spend_amount,
        gas_limit=expected_gas_used,
        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.