Skip to content

test_transfer_logs_and_bal_balance_changes()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip7708.py::test_transfer_logs_and_bal_balance_changes@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip7708.py::test_transfer_logs_and_bal_balance_changes --fork Amsterdam

Ensure Transfer logs and BAL balance changes stay consistent within one block.

The first transaction is a plain value transfer paying a priority fee: both parties get BAL balance changes, the receipt carries one Transfer log, and the coinbase tip appears in the BAL only. The second transaction sweeps a contract balance via SELFDESTRUCT with a zero tip: the sweep shows up both as a Transfer log and as BAL balance changes, while the coinbase entry stays fee-only from the first transaction.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip7708.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def test_transfer_logs_and_bal_balance_changes(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
) -> None:
    """
    Ensure Transfer logs and BAL balance changes stay consistent within
    one block.

    The first transaction is a plain value transfer paying a priority
    fee: both parties get BAL balance changes, the receipt carries one
    Transfer log, and the coinbase tip appears in the BAL only. The
    second transaction sweeps a contract balance via SELFDESTRUCT with a
    zero tip: the sweep shows up both as a Transfer log and as BAL
    balance changes, while the coinbase entry stays fee-only from the
    first transaction.
    """
    coinbase = pre.fund_eoa(amount=0)

    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    intrinsic_gas = intrinsic_gas_calculator(
        calldata=b"",
        contract_creation=False,
        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,
    )
    expected_gas_used = intrinsic_gas + top_frame_state_gas
    tx_gas_limit = expected_gas_used + 1000  # add a small buffer
    gas_price = 0xA
    tx_value = 100
    extra_balance = 1000

    alice_initial_balance = (
        (tx_gas_limit * gas_price) + tx_value + extra_balance
    )
    alice = pre.fund_eoa(amount=alice_initial_balance)
    bob = pre.fund_eoa(amount=0)

    genesis_env = Environment(base_fee_per_gas=0x7)
    base_fee_per_gas = fork.base_fee_per_gas_calculator()(
        parent_base_fee_per_gas=int(genesis_env.base_fee_per_gas or 0),
        parent_gas_used=0,
        parent_gas_limit=genesis_env.gas_limit,
    )
    tip_to_coinbase = (gas_price - base_fee_per_gas) * expected_gas_used
    alice_final_balance = (
        alice_initial_balance - tx_value - expected_gas_used * gas_price
    )

    tx_transfer = Transaction(
        sender=alice,
        to=bob,
        value=tx_value,
        gas_limit=tx_gas_limit,
        gas_price=gas_price,
        expected_receipt=TransactionReceipt(
            logs=[transfer_log(alice, bob, tx_value)]
        ),
    )

    # SELFDESTRUCT sweep with a zero tip, so the coinbase BAL entry
    # stays fee-only from the first transaction.
    sweep_value = 500
    carol = pre.fund_eoa()
    dave = pre.fund_eoa(amount=0)
    sweeper = pre.deploy_contract(
        code=Op.SELFDESTRUCT(dave), balance=sweep_value
    )

    tx_sweep = Transaction(
        sender=carol,
        to=sweeper,
        max_fee_per_gas=base_fee_per_gas,
        max_priority_fee_per_gas=0,
        expected_receipt=TransactionReceipt(
            logs=[transfer_log(sweeper, dave, sweep_value)]
        ),
    )

    block = Block(
        txs=[tx_transfer, tx_sweep],
        fee_recipient=coinbase,
        header_verify=Header(base_fee_per_gas=base_fee_per_gas),
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                    balance_changes=[
                        BalBalanceChange(
                            block_access_index=1,
                            post_balance=alice_final_balance,
                        )
                    ],
                ),
                bob: BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(
                            block_access_index=1, post_balance=tx_value
                        )
                    ],
                ),
                carol: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=2, post_nonce=1)
                    ],
                ),
                sweeper: BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(block_access_index=2, post_balance=0)
                    ],
                ),
                dave: BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(
                            block_access_index=2, post_balance=sweep_value
                        )
                    ],
                ),
                # The tip is a BAL-only flow: it must never produce a
                # Transfer log, and the zero-tip second transaction must
                # not add a second balance change.
                coinbase: BalAccountExpectation(
                    balance_changes=[
                        BalBalanceChange(
                            block_access_index=1,
                            post_balance=tip_to_coinbase,
                        )
                    ],
                ),
            }
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[block],
        post={
            bob: Account(balance=tx_value),
            dave: Account(balance=sweep_value),
            sweeper: Account(balance=0),
        },
        genesis_environment=genesis_env,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.