Skip to content

test_bal_net_zero_balance_transfer()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py::test_bal_net_zero_balance_transfer@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_net_zero_balance_transfer --fork Amsterdam

Test that BAL does not record balance changes when net change is zero.

A contract starts with initial_balance, receives transfer_amount (increasing its balance), then sends transfer_amount to a recipient (decreasing its balance back to initial_balance). The net change is zero, so BAL should not record any balance changes for this contract.

The contract verifies this by reading its own balance with SELFBALANCE, storing it in slot 0, then sending that amount to the recipient.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists.py
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
@pytest.mark.parametrize(
    "initial_balance,transfer_amount,transfer_mechanism",
    [
        pytest.param(0, 0, "call", id="zero_balance_zero_transfer_call"),
        pytest.param(
            0, 0, "selfdestruct", id="zero_balance_zero_transfer_selfdestruct"
        ),
        pytest.param(1, 1, "call", id="nonzero_balance_net_zero"),
        pytest.param(100, 50, "call", id="larger_balance_net_zero"),
    ],
)
def test_bal_net_zero_balance_transfer(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    initial_balance: int,
    transfer_amount: int,
    transfer_mechanism: str,
) -> None:
    """
    Test that BAL does not record balance changes when net change is zero.

    A contract starts with `initial_balance`, receives `transfer_amount`
    (increasing its balance), then sends `transfer_amount` to a recipient
    (decreasing its balance back to `initial_balance`). The net change is zero,
    so BAL should not record any balance changes for this contract.

    The contract verifies this by reading its own balance with SELFBALANCE,
    storing it in slot 0, then sending that amount to the recipient.
    """
    alice = pre.fund_eoa()
    recipient = pre.fund_eoa(amount=0)

    net_zero_bal_contract_code = (
        Op.SSTORE(0, Op.SELFBALANCE) + Op.SELFDESTRUCT(recipient)
        if transfer_mechanism == "selfdestruct"
        # store current balance in slot 0
        else (
            Op.SSTORE(0, Op.SELFBALANCE)
            # send only the `transfer_amount` received to recipient (net zero)
            + Op.CALL(0, recipient, Op.CALLVALUE, 0, 0, 0, 0)
            + Op.STOP
        )
    )
    net_zero_bal_contract = pre.deploy_contract(
        code=net_zero_bal_contract_code, balance=initial_balance
    )

    tx = Transaction(
        sender=alice,
        to=net_zero_bal_contract,
        value=transfer_amount,
        gas_limit=1_000_000,
        gas_price=0xA,
    )

    expected_balance_in_slot = initial_balance + transfer_amount

    block = Block(
        txs=[tx],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                ),
                net_zero_bal_contract: BalAccountExpectation(
                    # receives transfer_amount and sends transfer_amount away
                    # (net-zero change)
                    balance_changes=[],
                    storage_reads=[0x00]
                    if expected_balance_in_slot == 0
                    else [],
                    storage_changes=[
                        BalStorageSlot(
                            slot=0x00,
                            slot_changes=[
                                BalStorageChange(
                                    block_access_index=1,
                                    post_value=expected_balance_in_slot,
                                )
                            ],
                        )
                    ]
                    if expected_balance_in_slot > 0
                    else [],
                ),
                # recipient receives transfer_amount
                recipient: BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(
                            block_access_index=1, post_balance=transfer_amount
                        )
                    ]
                    if transfer_amount > 0
                    else [],
                ),
            }
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[block],
        post={
            net_zero_bal_contract: Account(
                balance=initial_balance,
                storage={0x00: expected_balance_in_slot}
                if expected_balance_in_slot > 0
                else {},
            ),
            recipient: Account(balance=transfer_amount)
            if transfer_amount > 0
            else Account.NONEXISTENT,
        },
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.