Skip to content

test_invalid_history_contract_calls()

Documentation for tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_invalid_history_contract_calls@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

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

Test calling the history contract with invalid block numbers, such as blocks from the future or overflowing block numbers.

Also test the BLOCKHASH opcode with the same block numbers, which should not affect the behavior of the opcode, even after verkle.

Source code in tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py
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
456
457
458
459
460
461
462
463
464
465
466
@pytest.mark.parametrize(
    "block_number,reverts",
    [
        pytest.param(1, True, id="current_block"),
        pytest.param(2, True, id="future_block"),
        pytest.param(2**64 - 1, True, id="2**64-1"),
        pytest.param(2**64, True, id="2**64"),
    ],
)
@pytest.mark.valid_from("Prague")
def test_invalid_history_contract_calls(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    block_number: int,
    reverts: bool,
) -> None:
    """
    Test calling the history contract with invalid block numbers, such as
    blocks from the future or overflowing block numbers.

    Also test the BLOCKHASH opcode with the same block numbers, which should
    not affect the behavior of the opcode, even after verkle.
    """
    storage = Storage()

    return_code_slot = storage.store_next(not reverts)
    returned_block_hash_slot = storage.store_next(0)
    block_hash_opcode_slot = storage.store_next(0)

    return_offset = 64
    return_size = 32
    args_size = 32

    # Check the first block outside of the window if any
    code = (
        Op.MSTORE(0, block_number)
        + Op.SSTORE(
            return_code_slot,
            Op.CALL(
                address=Spec.HISTORY_STORAGE_ADDRESS,
                args_offset=0,
                args_size=args_size,
                ret_offset=return_offset,
                ret_size=return_size,
            ),
        )
        + Op.SSTORE(returned_block_hash_slot, Op.MLOAD(return_offset))
        + Op.SSTORE(block_hash_opcode_slot, Op.BLOCKHASH(block_number))
    )
    check_contract_address = pre.deploy_contract(
        code, storage=storage.canary()
    )

    txs = [
        Transaction(
            to=check_contract_address,
            gas_limit=10_000_000,
            sender=pre.fund_eoa(),
        )
    ]
    post = {check_contract_address: Account(storage=storage)}

    blocks = [Block(txs=txs)]
    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 3 forks.