|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +// This file is auto-generated. |
| 5 | + |
| 6 | +/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 7 | +/* STRUCTS */ |
| 8 | +/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 9 | + |
| 10 | +/// @dev Ethereum block header fields relevant to historical MPT proofs. |
| 11 | +struct ShortHeader { |
| 12 | + bytes32 parentHash; |
| 13 | + bytes32 stateRoot; |
| 14 | + bytes32 transactionsRoot; |
| 15 | + bytes32 receiptsRoot; |
| 16 | + bytes32[8] logsBloom; |
| 17 | +} |
| 18 | + |
| 19 | +using BlockHashLib for ShortHeader global; |
| 20 | + |
| 21 | +/// @notice Library for accessing block hashes way beyond the 256-block limit. |
| 22 | +/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/g/BlockHashLib.sol) |
| 23 | +/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Blockhash.sol) |
| 24 | +library BlockHashLib { |
| 25 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 26 | + /* CUSTOM ERRORS */ |
| 27 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 28 | + |
| 29 | + /// @dev The keccak256 of the RLP-encoded block header does not equal to the block hash. |
| 30 | + error BlockHashMismatch(); |
| 31 | + |
| 32 | + /// @dev The block header is not properly RLP-encoded. |
| 33 | + error InvalidBlockHeaderEncoding(); |
| 34 | + |
| 35 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 36 | + /* CONSTANTS */ |
| 37 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 38 | + |
| 39 | + /// @dev Address of the EIP-2935 history storage contract. |
| 40 | + /// See: https://eips.ethereum.org/EIPS/eip-2935 |
| 41 | + address internal constant HISTORY_STORAGE_ADDRESS = 0x0000F90827F1C53a10cb7A02335B175320002935; |
| 42 | + |
| 43 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 44 | + /* OPERATIONS */ |
| 45 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 46 | + |
| 47 | + /// @dev Retrieves the block hash for any historical block within the supported range. |
| 48 | + /// The function gracefully handles future blocks and blocks beyond the history window by returning zero, |
| 49 | + /// consistent with the EVM's native `BLOCKHASH` behavior. |
| 50 | + function blockHash(uint256 blockNumber) internal view returns (bytes32 result) { |
| 51 | + unchecked { |
| 52 | + // If `blockNumber + 256` overflows: |
| 53 | + // - Typical chain height (`block.number > 255`) -> `staticcall` -> 0. |
| 54 | + // - Very early chain (`block.number <= 255`) -> `blockhash` -> 0. |
| 55 | + if (block.number <= blockNumber + 256) return blockhash(blockNumber); |
| 56 | + } |
| 57 | + /// @solidity memory-safe-assembly |
| 58 | + assembly { |
| 59 | + mstore(0x20, blockNumber) |
| 60 | + mstore(0x00, 0) |
| 61 | + pop(staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0x20, 0x20, 0x00, 0x20)) |
| 62 | + result := mload(0x00) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// @dev Reverts if `keccak256(encodedHeader) != blockHash(blockNumber)`, |
| 67 | + /// where `encodedHeader` is a RLP-encoded block header. |
| 68 | + /// Else, returns `blockHash(blockNumber)`. |
| 69 | + function verifyBlock(bytes calldata encodedHeader, uint256 blockNumber) |
| 70 | + internal |
| 71 | + view |
| 72 | + returns (bytes32 result) |
| 73 | + { |
| 74 | + result = blockHash(blockNumber); |
| 75 | + /// @solidity memory-safe-assembly |
| 76 | + assembly { |
| 77 | + calldatacopy(mload(0x40), encodedHeader.offset, encodedHeader.length) |
| 78 | + if iszero(eq(result, keccak256(mload(0x40), encodedHeader.length))) { |
| 79 | + mstore(0x00, 0xe42b5e7e) // `BlockHashMismatch()`. |
| 80 | + revert(0x1c, 0x04) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// @dev Retrieves the most relevant fields for MPT proofs from an RLP-encoded block header. |
| 86 | + /// Leading fields are always present and have fixed offsets and lengths. |
| 87 | + /// This function efficiently extracts the fields without full RLP decoding. |
| 88 | + /// For the specification of field order and lengths, please refer to |
| 89 | + /// prefix. 6 of the Ethereum Yellow Paper: |
| 90 | + /// (https://ethereum.github.io/yellowpaper/paper.pdf) |
| 91 | + /// and the Ethereum Wiki (https://epf.wiki/#/wiki/EL/RLP). |
| 92 | + function toShortHeader(bytes calldata encodedHeader) |
| 93 | + internal |
| 94 | + pure |
| 95 | + returns (ShortHeader memory result) |
| 96 | + { |
| 97 | + /// @solidity memory-safe-assembly |
| 98 | + assembly { |
| 99 | + mstore(result, calldataload(add(4, encodedHeader.offset))) // `parentHash`. |
| 100 | + mstore(add(0x20, result), calldataload(add(91, encodedHeader.offset))) // `stateRoot`. |
| 101 | + mstore(add(0x40, result), calldataload(add(124, encodedHeader.offset))) // `transactionsRoot`. |
| 102 | + mstore(add(0x60, result), calldataload(add(157, encodedHeader.offset))) // `receiptsRoot`. |
| 103 | + calldatacopy(mload(add(0x80, result)), add(192, encodedHeader.offset), 0x100) // `logsBloom`. |
| 104 | + if iszero( // Just perform some minimal light bounds checking. |
| 105 | + and( |
| 106 | + gt(encodedHeader.length, 447), // `0x100 + 192 - 1`. |
| 107 | + eq(byte(0, calldataload(encodedHeader.offset)), 0xf9) // `0xff < len < 0x10000`. |
| 108 | + ) |
| 109 | + ) { |
| 110 | + mstore(0x00, 0x1a27c4e4) // `InvalidBlockHeaderEncoding()`. |
| 111 | + revert(0x1c, 0x04) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments