|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {Blockhash} from "../../contracts/utils/Blockhash.sol"; |
| 6 | + |
| 7 | +contract BlockhashTest is Test { |
| 8 | + uint256 internal startingBlock; |
| 9 | + |
| 10 | + address internal constant SYSTEM_ADDRESS = 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE; |
| 11 | + |
| 12 | + // See https://eips.ethereum.org/EIPS/eip-2935#bytecode |
| 13 | + // Generated using https://www.evm.codes/playground |
| 14 | + bytes private constant HISTORY_STORAGE_BYTECODE = |
| 15 | + hex"3373fffffffffffffffffffffffffffffffffffffffe14604657602036036042575f35600143038111604257611fff81430311604257611fff9006545f5260205ff35b5f5ffd5b5f35611fff60014303065500"; |
| 16 | + |
| 17 | + function setUp() public { |
| 18 | + vm.roll(block.number + 100); |
| 19 | + |
| 20 | + startingBlock = block.number; |
| 21 | + vm.etch(Blockhash.HISTORY_STORAGE_ADDRESS, HISTORY_STORAGE_BYTECODE); |
| 22 | + } |
| 23 | + |
| 24 | + function testFuzzRecentBlocks(uint8 offset, uint64 currentBlock, bytes32 expectedHash) public { |
| 25 | + // Recent blocks (1-256 blocks old) |
| 26 | + uint256 boundedOffset = uint256(offset) + 1; |
| 27 | + vm.assume(currentBlock > boundedOffset); |
| 28 | + vm.roll(currentBlock); |
| 29 | + |
| 30 | + uint256 targetBlock = currentBlock - boundedOffset; |
| 31 | + vm.setBlockhash(targetBlock, expectedHash); |
| 32 | + |
| 33 | + bytes32 result = Blockhash.blockHash(targetBlock); |
| 34 | + assertEq(result, blockhash(targetBlock)); |
| 35 | + assertEq(result, expectedHash); |
| 36 | + } |
| 37 | + |
| 38 | + function testFuzzHistoryBlocks(uint16 offset, uint256 currentBlock, bytes32 expectedHash) public { |
| 39 | + // History blocks (257-8191 blocks old) |
| 40 | + offset = uint16(bound(offset, 257, 8191)); |
| 41 | + vm.assume(currentBlock > offset); |
| 42 | + vm.roll(currentBlock); |
| 43 | + |
| 44 | + uint256 targetBlock = currentBlock - offset; |
| 45 | + _setHistoryBlockhash(targetBlock, expectedHash); |
| 46 | + |
| 47 | + bytes32 result = Blockhash.blockHash(targetBlock); |
| 48 | + (bool success, bytes memory returndata) = Blockhash.HISTORY_STORAGE_ADDRESS.staticcall( |
| 49 | + abi.encodePacked(bytes32(targetBlock)) |
| 50 | + ); |
| 51 | + assertTrue(success); |
| 52 | + assertEq(result, abi.decode(returndata, (bytes32))); |
| 53 | + assertEq(result, expectedHash); |
| 54 | + } |
| 55 | + |
| 56 | + function testFuzzVeryOldBlocks(uint256 offset, uint256 currentBlock) public { |
| 57 | + // Very old blocks (>8191 blocks old) |
| 58 | + offset = bound(offset, 8192, type(uint256).max); |
| 59 | + vm.assume(currentBlock > offset); |
| 60 | + vm.roll(currentBlock); |
| 61 | + |
| 62 | + uint256 targetBlock = currentBlock - offset; |
| 63 | + bytes32 result = Blockhash.blockHash(targetBlock); |
| 64 | + assertEq(result, bytes32(0)); |
| 65 | + } |
| 66 | + |
| 67 | + function testFuzzFutureBlocks(uint256 offset, uint256 currentBlock) public { |
| 68 | + // Future blocks |
| 69 | + offset = bound(offset, 1, type(uint256).max); |
| 70 | + vm.roll(currentBlock); |
| 71 | + |
| 72 | + unchecked { |
| 73 | + uint256 targetBlock = currentBlock + offset; |
| 74 | + bytes32 result = Blockhash.blockHash(targetBlock); |
| 75 | + assertEq(result, blockhash(targetBlock)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + function testUnsupportedChainsReturnZeroWhenOutOfRange() public { |
| 80 | + vm.etch(Blockhash.HISTORY_STORAGE_ADDRESS, hex""); |
| 81 | + |
| 82 | + vm.roll(block.number + 1000); |
| 83 | + assertEq(Blockhash.blockHash(block.number - 1000), bytes32(0)); |
| 84 | + } |
| 85 | + |
| 86 | + function _setHistoryBlockhash(bytes32 blockHash) internal { |
| 87 | + _setHistoryBlockhash(block.number, blockHash); |
| 88 | + } |
| 89 | + |
| 90 | + function _setHistoryBlockhash(uint256 blockNumber, bytes32 blockHash) internal { |
| 91 | + // Subtracting 1 due to bug encountered during coverage |
| 92 | + uint256 currentBlock = block.number - 1; |
| 93 | + vm.assume(blockNumber < type(uint256).max); |
| 94 | + vm.roll(blockNumber + 1); // roll to the next block so the storage contract sets the parent's blockhash |
| 95 | + vm.prank(SYSTEM_ADDRESS); |
| 96 | + (bool success, ) = Blockhash.HISTORY_STORAGE_ADDRESS.call(abi.encode(blockHash)); // set parent's blockhash |
| 97 | + assertTrue(success); |
| 98 | + vm.roll(currentBlock + 1); |
| 99 | + } |
| 100 | +} |
0 commit comments