Skip to content

test_multi_block_beacon_root_timestamp_calls()

Documentation for tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py::test_multi_block_beacon_root_timestamp_calls@21fb11c8.

Generate fixtures for these test cases for Prague with:

fill -v tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py::test_multi_block_beacon_root_timestamp_calls --fork Prague

Tests multiple blocks where each block writes a timestamp to storage and contains one transaction that calls the beacon root contract multiple times.

The blocks might overwrite the historical roots buffer, or not, depending on the timestamps, and whether they increment in multiples of Spec.HISTORY_BUFFER_LENGTH or not.

By default, the beacon roots are the keccak of the block number.

Each transaction checks the current timestamp and also all previous timestamps, and verifies that the beacon root is correct for all of them if the timestamp is supposed to be in the buffer, which might have been overwritten by a later block.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
@pytest.mark.parametrize(
    "timestamps",
    [
        pytest.param(
            count(
                start=Spec.HISTORY_BUFFER_LENGTH - 5,
                step=1,
            ),
            id="buffer_wraparound",
        ),
        pytest.param(
            count(
                start=12,
                step=Spec.HISTORY_BUFFER_LENGTH,
            ),
            id="buffer_wraparound_overwrite",
        ),
        pytest.param(
            count(
                start=2**32,
                step=Spec.HISTORY_BUFFER_LENGTH,
            ),
            id="buffer_wraparound_overwrite_high_timestamp",
        ),
        pytest.param(
            count(
                start=5,
                step=Spec.HISTORY_BUFFER_LENGTH - 1,
            ),
            id="buffer_wraparound_no_overwrite",
        ),
        pytest.param(
            count(
                start=Spec.HISTORY_BUFFER_LENGTH - 3,
                step=Spec.HISTORY_BUFFER_LENGTH + 1,
            ),
            id="buffer_wraparound_no_overwrite_2",
        ),
    ],
)
@pytest.mark.parametrize("block_count", [10])  # All tests use 10 blocks
@pytest.mark.valid_from("Cancun")
def test_multi_block_beacon_root_timestamp_calls(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    timestamps: Iterator[int],
    beacon_roots: Iterator[bytes],
    block_count: int,
    call_gas: int,
    call_value: int,
):
    """
    Tests multiple blocks where each block writes a timestamp to storage and contains one
    transaction that calls the beacon root contract multiple times.

    The blocks might overwrite the historical roots buffer, or not, depending on the `timestamps`,
    and whether they increment in multiples of `Spec.HISTORY_BUFFER_LENGTH` or not.

    By default, the beacon roots are the keccak of the block number.

    Each transaction checks the current timestamp and also all previous timestamps, and verifies
    that the beacon root is correct for all of them if the timestamp is supposed to be in the
    buffer, which might have been overwritten by a later block.
    """
    blocks: List[Block] = []
    post = {}

    timestamps_storage: Dict[int, int] = {}
    roots_storage: Dict[int, bytes] = {}

    all_timestamps: List[int] = []

    sender = pre.fund_eoa()

    for timestamp, beacon_root, _i in zip(
        timestamps,
        beacon_roots,
        range(block_count),
        strict=False,
    ):
        timestamp_index = timestamp % Spec.HISTORY_BUFFER_LENGTH
        timestamps_storage[timestamp_index] = timestamp
        roots_storage[timestamp_index] = beacon_root

        all_timestamps.append(timestamp)

        withdraw_index = count(0)

        current_call_account_code = Bytecode()
        current_call_account_expected_storage = Storage()

        # We are going to call the beacon roots contract once for every timestamp of the current
        # and all previous blocks, and check that the returned beacon root is still correct only
        # if it was not overwritten.
        for t in all_timestamps:
            current_call_account_code += Op.MSTORE(0, t)
            call_valid = (
                timestamp_index in timestamps_storage
                and timestamps_storage[t % Spec.HISTORY_BUFFER_LENGTH] == t
            )
            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(0x01 if call_valid else 0x00),
                Op.CALL(
                    call_gas,
                    Spec.BEACON_ROOTS_ADDRESS,
                    call_value,
                    0x00,
                    0x20,
                    0x20,
                    0x20,
                ),
            )

            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(
                    roots_storage[t % Spec.HISTORY_BUFFER_LENGTH] if call_valid else 0x00
                ),
                Op.MLOAD(0x20),
            )

        current_call_account_address = pre.deploy_contract(current_call_account_code)

        post[current_call_account_address] = Account(
            storage=current_call_account_expected_storage,
        )
        blocks.append(
            Block(
                txs=[
                    Transaction(
                        sender=sender,
                        to=current_call_account_address,
                        data=Hash(timestamp),
                        gas_limit=1_000_000,
                    )
                ],
                parent_beacon_block_root=beacon_root,
                timestamp=timestamp,
                withdrawals=[
                    # Also withdraw to the beacon root contract and the system address
                    Withdrawal(
                        address=Spec.BEACON_ROOTS_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=0,
                    ),
                    Withdrawal(
                        address=Spec.SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=1,
                    ),
                ],
            )
        )

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

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) block_count timestamps
...fork_Cancun-blockchain_test-block_count_10-buffer_wraparound 10 count(8186)
...fork_Cancun-blockchain_test-block_count_10-buffer_wraparound_overwrite 10 count(12, 8191)
...fork_Cancun-blockchain_test-block_count_10-buffer_wraparound_overwrite_high_timestamp 10 count(4294967296, 8191)
...fork_Cancun-blockchain_test-block_count_10-buffer_wraparound_no_overwrite 10 count(5, 8190)
...fork_Cancun-blockchain_test-block_count_10-buffer_wraparound_no_overwrite_2 10 count(8188, 8192)
...fork_Prague-blockchain_test-block_count_10-buffer_wraparound 10 count(8186)
...fork_Prague-blockchain_test-block_count_10-buffer_wraparound_overwrite 10 count(12, 8191)
...fork_Prague-blockchain_test-block_count_10-buffer_wraparound_overwrite_high_timestamp 10 count(4294967296, 8191)
...fork_Prague-blockchain_test-block_count_10-buffer_wraparound_no_overwrite 10 count(5, 8190)
...fork_Prague-blockchain_test-block_count_10-buffer_wraparound_no_overwrite_2 10 count(8188, 8192)
...fork_Osaka-blockchain_test-block_count_10-buffer_wraparound 10 count(8186)
...fork_Osaka-blockchain_test-block_count_10-buffer_wraparound_overwrite 10 count(12, 8191)
...fork_Osaka-blockchain_test-block_count_10-buffer_wraparound_overwrite_high_timestamp 10 count(4294967296, 8191)
...fork_Osaka-blockchain_test-block_count_10-buffer_wraparound_no_overwrite 10 count(5, 8190)
...fork_Osaka-blockchain_test-block_count_10-buffer_wraparound_no_overwrite_2 10 count(8188, 8192)