Skip to content

test_bal_2935_query()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip2935.py::test_bal_2935_query@20373115.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip2935.py::test_bal_2935_query --fork Amsterdam

Ensure BAL captures storage reads when querying historical block hashes.

Test scenarios: 1. Valid query (block_number=0, genesis hash): History storage contract reads the genesis hash slot, query contract writes returned value 2. Invalid query (block_number=1042, out of range): History storage contract reverts before any storage access, query contract has implicit read recorded 3. With value transfer: BAL captures balance changes in addition to storage operations (only when query is valid)

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_eip2935.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
@pytest.mark.parametrize(
    "query_block_number,is_valid",
    [
        pytest.param(0, True, id="valid_block_number"),
        pytest.param(1042, False, id="block_number_out_of_range"),
    ],
)
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="no_value"),
        pytest.param(100, id="with_value"),
    ],
)
def test_bal_2935_query(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    query_block_number: int,
    is_valid: bool,
    value: int,
) -> None:
    """
    Ensure BAL captures storage reads when querying historical block hashes.

    Test scenarios:
    1. Valid query (block_number=0, genesis hash): History storage contract
       reads the genesis hash slot, query contract writes returned value
    2. Invalid query (block_number=1042, out of range): History storage
       contract reverts before any storage access, query contract has
       implicit read recorded
    3. With value transfer: BAL captures balance changes in addition
       to storage operations (only when query is valid)
    """
    alice = pre.fund_eoa()

    # Contract that calls history storage contract with block number from
    # calldata and stores returned block hash in slot 0,
    # forwarding any value sent
    query_code = (
        Op.CALLDATACOPY(0, 0, 32)
        + Op.CALL(
            Op.GAS,
            HISTORY_STORAGE_ADDRESS,
            Op.CALLVALUE,
            0,
            32,
            32,
            32,
        )
        + Op.SSTORE(0, Op.MLOAD(32))
    )
    oracle = pre.deploy_contract(query_code)

    tx = Transaction(
        sender=alice,
        to=oracle,
        data=Hash(query_block_number),
        value=value,
        gas_limit=fork.transaction_gas_limit_cap(),
    )

    # A setup up block that writes genesis block-hash
    # to history storage contract so that it can be
    # queried later.
    block_1 = Block(
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations=block_hash_system_call_expectations(0)
        )
    )

    block_hash_slot = query_block_number % Spec.HISTORY_SERVE_WINDOW
    # Storage reads for the query:
    # - Valid query (block 0): reads `block_hash_slot`
    # - Invalid query (out-of-range block): reverts before SLOAD
    account_expectations = block_hash_system_call_expectations(1)
    account_expectations[HISTORY_STORAGE_ADDRESS].storage_reads = (
        # Read only occurs for valid query
        [block_hash_slot] if is_valid else []
    )

    # Balance changes for callee: credited if valid, must be empty if invalid
    if value > 0:
        account_expectations[HISTORY_STORAGE_ADDRESS].balance_changes = (
            [BalBalanceChange(block_access_index=1, post_balance=value)]
            if is_valid
            else []
        )

    account_expectations[alice] = BalAccountExpectation(
        nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
    )

    account_expectations[oracle] = BalAccountExpectation(
        # Valid: write the returned hash (value is framework-computed)
        # Invalid: no-op SSTORE(0, 0) becomes implicit read
        storage_reads=[] if is_valid else [0],
        storage_changes=[
            BalStorageSlot(
                slot=0,
                validate_any_change=True,
            ),
        ]
        if is_valid
        else [],
        # if value > 0 and invalid, value stays in query contract
        balance_changes=[
            BalBalanceChange(
                block_access_index=1,
                post_balance=value,
            )
        ]
        if not is_valid and value > 0
        else [],
    )

    block_2 = Block(
        txs=[tx],
        expected_block_access_list=BlockAccessListExpectation(
            account_expectations=account_expectations
        ),
    )

    post_state: dict[Address, Account] = {
        alice: Account(nonce=1),
    }

    if is_valid:
        post_state[oracle] = Account()
    else:
        # Invalid query: zero stored
        post_state[oracle] = Account(storage={0: 0})

    if value > 0 and is_valid:
        post_state[HISTORY_STORAGE_ADDRESS] = Account(balance=value)

    blockchain_test(
        pre=pre,
        blocks=[block_1, block_2],
        post=post_state,
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.