|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import "./utils/SoladyTest.sol"; |
| 5 | +import {LibBlockHash} from "../src/utils/LibBlockHash.sol"; |
| 6 | + |
| 7 | +contract LibBlockHashTest is SoladyTest { |
| 8 | + uint256 internal startingBlock; |
| 9 | + |
| 10 | + address internal constant SYSTEM_ADDRESS = 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE; |
| 11 | + |
| 12 | + bytes private constant _HISTORY_STORAGE_BYTECODE = |
| 13 | + hex"3373fffffffffffffffffffffffffffffffffffffffe14604657602036036042575f35600143038111604257611fff81430311604257611fff9006545f5260205ff35b5f5ffd5b5f35611fff60014303065500"; |
| 14 | + |
| 15 | + function setUp() public { |
| 16 | + vm.roll(block.number + 100); |
| 17 | + startingBlock = block.number; |
| 18 | + vm.etch(LibBlockHash.HISTORY_STORAGE_ADDRESS, _HISTORY_STORAGE_BYTECODE); |
| 19 | + } |
| 20 | + |
| 21 | + function __blockHash(uint256 blockNumber, bytes32 expectedHash, bytes32 sysExpectedHash) |
| 22 | + internal |
| 23 | + view |
| 24 | + returns (bool) |
| 25 | + { |
| 26 | + if (expectedHash != sysExpectedHash) return false; |
| 27 | + return sysExpectedHash == LibBlockHash.blockHash(blockNumber); |
| 28 | + } |
| 29 | + |
| 30 | + function testFuzzRecentBlocks(uint8 offset, uint64 currentBlock, bytes32 expectedHash) public { |
| 31 | + // Recent blocks (1-256 blocks old) |
| 32 | + uint256 boundedOffset = uint256(offset) + 1; |
| 33 | + vm.assume(currentBlock > boundedOffset); |
| 34 | + vm.roll(currentBlock); |
| 35 | + |
| 36 | + uint256 targetBlock = currentBlock - boundedOffset; |
| 37 | + vm.setBlockhash(targetBlock, expectedHash); |
| 38 | + |
| 39 | + assertTrue(__blockHash(targetBlock, expectedHash, blockhash(targetBlock))); |
| 40 | + } |
| 41 | + |
| 42 | + function testFuzzVeryOldBlocks(uint256 offset, uint256 currentBlock) public { |
| 43 | + // Very old blocks (>8191 blocks old) |
| 44 | + offset = _bound(offset, 8192, type(uint256).max); |
| 45 | + vm.assume(currentBlock > offset); |
| 46 | + vm.roll(currentBlock); |
| 47 | + |
| 48 | + uint256 targetBlock = currentBlock - offset; |
| 49 | + assertTrue(__blockHash(targetBlock, bytes32(0), bytes32(0))); |
| 50 | + } |
| 51 | + |
| 52 | + function testFuzzFutureBlocks(uint256 offset, uint256 currentBlock) public { |
| 53 | + // Future blocks |
| 54 | + offset = _bound(offset, 1, type(uint256).max); |
| 55 | + currentBlock = _bound(currentBlock, 0, type(uint256).max - offset); |
| 56 | + vm.roll(currentBlock); |
| 57 | + |
| 58 | + unchecked { |
| 59 | + uint256 targetBlock = currentBlock + offset; |
| 60 | + assertTrue(__blockHash(targetBlock, blockhash(targetBlock), blockhash(targetBlock))); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function testUnsupportedChainsReturnZeroWhenOutOfRange() public { |
| 65 | + vm.etch(LibBlockHash.HISTORY_STORAGE_ADDRESS, hex""); |
| 66 | + |
| 67 | + vm.roll(block.number + 1000); |
| 68 | + assertEq(LibBlockHash.blockHash(block.number - 1000), bytes32(0)); |
| 69 | + } |
| 70 | +} |
0 commit comments