Skip to content

test_block_hashes_history()

Documentation for tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history@20373115.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history --fork Amsterdam

Tests that block hashes are stored correctly at the system contract address after the fork transition. Block hashes are stored incrementally at the transition until the HISTORY_SERVE_WINDOW ring buffer is full. Afterwards the oldest block hash is replaced by the new one.

Source code in tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
@pytest.mark.parametrize(
    "block_count,check_contract_first",
    [
        pytest.param(1, False, id="single_block_check_blockhash_first"),
        pytest.param(1, True, id="single_block_check_contract_first"),
        pytest.param(2, False, id="two_blocks_check_blockhash_first"),
        pytest.param(2, True, id="two_blocks_check_contract_first"),
        pytest.param(
            Spec.HISTORY_SERVE_WINDOW + 1,
            False,
            marks=[
                pytest.mark.skip("Slow test not relevant anymore"),
                pytest.mark.slow,
            ],
            id="full_history_plus_one_check_blockhash_first",
        ),
    ],
)
@pytest.mark.valid_from("Prague")
@pytest.mark.eels_base_coverage
def test_block_hashes_history(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    block_count: int,
    check_contract_first: bool,
) -> None:
    """
    Tests that block hashes are stored correctly at the system contract address
    after the fork transition. Block hashes are stored incrementally at the
    transition until the `HISTORY_SERVE_WINDOW` ring buffer is full. Afterwards
    the oldest block hash is replaced by the new one.
    """
    blocks: List[Block] = []

    sender = pre.fund_eoa(10_000_000_000)
    post: Dict[Address, Account] = {}
    current_block_number = 1
    fork_block_number = 0  # We fork at genesis

    for _ in range(block_count - 1):
        # Generate empty blocks after the fork.
        blocks.append(Block())
        current_block_number += 1

    txs = []
    # On these blocks, `BLOCKHASH` will still return values for the last 256
    # blocks, and `HISTORY_STORAGE_ADDRESS` should now serve values for the
    # previous blocks in the new fork.
    code = Bytecode()
    storage = Storage()

    # Check the first block outside of the window if any
    code += generate_block_check_code(
        check_block_number=current_block_number
        - Spec.HISTORY_SERVE_WINDOW
        - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block inside the window
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.HISTORY_SERVE_WINDOW,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block outside the BLOCKHASH window
    code += generate_block_check_code(
        check_block_number=current_block_number
        - Spec.BLOCKHASH_OLD_WINDOW
        - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block inside the BLOCKHASH window
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.BLOCKHASH_OLD_WINDOW,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the previous block
    code += generate_block_check_code(
        check_block_number=current_block_number - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    check_blocks_after_fork_address = pre.deploy_contract(code)
    txs.append(
        Transaction(
            to=check_blocks_after_fork_address,
            gas_limit=10_000_000,
            sender=sender,
        )
    )
    post[check_blocks_after_fork_address] = Account(storage=storage)

    blocks.append(Block(txs=txs))
    current_block_number += 1

    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

Parametrized Test Cases

This test generates 5 parametrized test cases across 3 forks.