|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.26; |
| 4 | + |
| 5 | +import {SafeCast} from "./math/SafeCast.sol"; |
| 6 | +import {Blockhash} from "./Blockhash.sol"; |
| 7 | +import {Memory} from "./Memory.sol"; |
| 8 | +import {RLP} from "./RLP.sol"; |
| 9 | + |
| 10 | +/// @dev Library for parsing and verifying RLP-encoded block headers. |
| 11 | +library BlockHeader { |
| 12 | + using SafeCast for *; |
| 13 | + using RLP for *; |
| 14 | + |
| 15 | + /// @dev List of evm versions. |
| 16 | + enum Hardforks { |
| 17 | + Homestead, |
| 18 | + DAO, |
| 19 | + TangerineWhistle, |
| 20 | + SpuriousDragon, |
| 21 | + Byzantium, |
| 22 | + Constantinople, |
| 23 | + Petersburg, |
| 24 | + Istanbul, |
| 25 | + MuirGlacier, |
| 26 | + Berlin, |
| 27 | + London, |
| 28 | + ArrowGlacier, |
| 29 | + GreyGlacier, |
| 30 | + Paris, |
| 31 | + Shanghai, |
| 32 | + Cancun, |
| 33 | + Prague, |
| 34 | + Osaka, |
| 35 | + Amsterdam |
| 36 | + } |
| 37 | + |
| 38 | + /// @dev List of block header fields, in the order they are encoded in the block header RLP. |
| 39 | + enum HeaderField { |
| 40 | + ParentHash, // Since Homestead |
| 41 | + OmmersHash, // Since Homestead |
| 42 | + Coinbase, // Since Homestead |
| 43 | + StateRoot, // Since Homestead |
| 44 | + TransactionsRoot, // Since Homestead |
| 45 | + ReceiptsRoot, // Since Homestead |
| 46 | + LogsBloom, // Since Homestead |
| 47 | + Difficulty, // Since Homestead |
| 48 | + Number, // Since Homestead |
| 49 | + GasLimit, // Since Homestead |
| 50 | + GasUsed, // Since Homestead |
| 51 | + Timestamp, // Since Homestead |
| 52 | + ExtraData, // Since Homestead |
| 53 | + PrevRandao, // Since Homestead (called MixHash before Paris) |
| 54 | + Nonce, // Since Homestead |
| 55 | + BaseFeePerGas, // Since London |
| 56 | + WithdrawalsRoot, // Since Shanghai |
| 57 | + BlobGasUsed, // Since Cancun |
| 58 | + ExcessBlobGas, // Since Cancun |
| 59 | + ParentBeaconBlockRoot, // Since Cancun |
| 60 | + RequestsHash, // Since Prague |
| 61 | + BlockAccessListHash // Since Amsterdam |
| 62 | + } |
| 63 | + |
| 64 | + /// @dev Thrown when the provided block header RLP does not have the expected number of fields for the given hardfork version. |
| 65 | + error InvalidBlockHeader(Hardforks expectedVersion); |
| 66 | + |
| 67 | + /// @dev Verifies that the given block header RLP corresponds to a valid block header for the current chain. |
| 68 | + function verifyBlockHeader(bytes memory headerRLP) internal view returns (bool) { |
| 69 | + return Blockhash.blockHash(getNumber(headerRLP)) == keccak256(headerRLP); |
| 70 | + } |
| 71 | + |
| 72 | + /// @dev Extract the parent hash from the block header RLP. |
| 73 | + function getParentHash(bytes memory headerRLP) internal pure returns (bytes32) { |
| 74 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.ParentHash)].readBytes32(); |
| 75 | + } |
| 76 | + |
| 77 | + /// @dev Extract the ommers hash from the block header RLP. This is constant to keccak256(rlp([])) since EIP-3675 (Paris) |
| 78 | + function getOmmersHash(bytes memory headerRLP) internal pure returns (bytes32) { |
| 79 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.OmmersHash)].readBytes32(); |
| 80 | + } |
| 81 | + |
| 82 | + /// @dev Extract the coinbase (a.k.a. beneficiary or miner) address from the block header RLP. |
| 83 | + function getCoinbase(bytes memory headerRLP) internal pure returns (address) { |
| 84 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.Coinbase)].readAddress(); |
| 85 | + } |
| 86 | + |
| 87 | + /// @dev Extract the state root from the block header RLP. |
| 88 | + function getStateRoot(bytes memory headerRLP) internal pure returns (bytes32) { |
| 89 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.StateRoot)].readBytes32(); |
| 90 | + } |
| 91 | + |
| 92 | + /// @dev Extract the transactions root from the block header RLP. |
| 93 | + function getTransactionsRoot(bytes memory headerRLP) internal pure returns (bytes32) { |
| 94 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.TransactionsRoot)].readBytes32(); |
| 95 | + } |
| 96 | + |
| 97 | + /// @dev Extract the receipts root from the block header RLP. |
| 98 | + function getReceiptsRoot(bytes memory headerRLP) internal pure returns (bytes32) { |
| 99 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.ReceiptsRoot)].readBytes32(); |
| 100 | + } |
| 101 | + |
| 102 | + /// @dev Extract the logs bloom from the block header RLP. |
| 103 | + function getLogsBloom(bytes memory headerRLP) internal pure returns (bytes memory) { |
| 104 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.LogsBloom)].readBytes(); |
| 105 | + } |
| 106 | + |
| 107 | + /// @dev Extract the difficulty from the block header RLP. This is constant to 0 since EIP-3675 (Paris) |
| 108 | + function getDifficulty(bytes memory headerRLP) internal pure returns (uint256) { |
| 109 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.Difficulty)].readUint256(); |
| 110 | + } |
| 111 | + |
| 112 | + /// @dev Extract the block number from the block header RLP. |
| 113 | + function getNumber(bytes memory headerRLP) internal pure returns (uint256) { |
| 114 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.Number)].readUint256(); |
| 115 | + } |
| 116 | + |
| 117 | + /// @dev Extract the gas used from the block header RLP. |
| 118 | + function getGasUsed(bytes memory headerRLP) internal pure returns (uint256) { |
| 119 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.GasUsed)].readUint256(); |
| 120 | + } |
| 121 | + |
| 122 | + /// @dev Extract the gas limit from the block header RLP. |
| 123 | + function getGasLimit(bytes memory headerRLP) internal pure returns (uint256) { |
| 124 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.GasLimit)].readUint256(); |
| 125 | + } |
| 126 | + |
| 127 | + /// @dev Extract the timestamp from the block header RLP. |
| 128 | + function getTimestamp(bytes memory headerRLP) internal pure returns (uint256) { |
| 129 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.Timestamp)].readUint256(); |
| 130 | + } |
| 131 | + |
| 132 | + /// @dev Extract the extra data from the block header RLP. |
| 133 | + function getExtraData(bytes memory headerRLP) internal pure returns (bytes memory) { |
| 134 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.ExtraData)].readBytes(); |
| 135 | + } |
| 136 | + |
| 137 | + /// @dev Extract the prevRandao (a.k.a. mixHash before Paris) from the block header RLP. |
| 138 | + function getPrevRandao(bytes memory headerRLP) internal pure returns (bytes32) { |
| 139 | + return _parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.PrevRandao)].readBytes32(); |
| 140 | + } |
| 141 | + |
| 142 | + /// @dev Extract the nonce from the block header RLP. This is constant to 0 since EIP-3675 (Paris) |
| 143 | + function getNonce(bytes memory headerRLP) internal pure returns (bytes8) { |
| 144 | + return bytes8(_parseHeader(headerRLP, Hardforks.Homestead)[uint8(HeaderField.Nonce)].readBytes32()); |
| 145 | + } |
| 146 | + |
| 147 | + /// @dev Extract the base fee per gas from the block header RLP. This was introduced in London. |
| 148 | + function getBaseFeePerGas(bytes memory headerRLP) internal pure returns (uint256) { |
| 149 | + return _parseHeader(headerRLP, Hardforks.London)[uint8(HeaderField.BaseFeePerGas)].readUint256(); |
| 150 | + } |
| 151 | + |
| 152 | + /// @dev Extract the withdrawals root from the block header RLP. This was introduced in Shanghai. |
| 153 | + function getWithdrawalsRoot(bytes memory headerRLP) internal pure returns (bytes32) { |
| 154 | + return _parseHeader(headerRLP, Hardforks.Shanghai)[uint8(HeaderField.WithdrawalsRoot)].readBytes32(); |
| 155 | + } |
| 156 | + |
| 157 | + /// @dev Extract the blob gas used from the block header RLP. This was introduced in Cancun. |
| 158 | + function getBlobGasUsed(bytes memory headerRLP) internal pure returns (uint64) { |
| 159 | + return _parseHeader(headerRLP, Hardforks.Cancun)[uint8(HeaderField.BlobGasUsed)].readUint256().toUint64(); |
| 160 | + } |
| 161 | + |
| 162 | + /// @dev Extract the excess blob gas from the block header RLP. This was introduced in Cancun. |
| 163 | + function getExcessBlobGas(bytes memory headerRLP) internal pure returns (uint64) { |
| 164 | + return _parseHeader(headerRLP, Hardforks.Cancun)[uint8(HeaderField.ExcessBlobGas)].readUint256().toUint64(); |
| 165 | + } |
| 166 | + |
| 167 | + /// @dev Extract the parent beacon block root from the block header RLP. This was introduced in Cancun. |
| 168 | + function getParentBeaconBlockRoot(bytes memory headerRLP) internal pure returns (bytes32) { |
| 169 | + return _parseHeader(headerRLP, Hardforks.Cancun)[uint8(HeaderField.ParentBeaconBlockRoot)].readBytes32(); |
| 170 | + } |
| 171 | + |
| 172 | + /// @dev Extract the requests hash from the block header RLP. This was introduced in Prague. |
| 173 | + function getRequestsHash(bytes memory headerRLP) internal pure returns (bytes32) { |
| 174 | + return _parseHeader(headerRLP, Hardforks.Prague)[uint8(HeaderField.RequestsHash)].readBytes32(); |
| 175 | + } |
| 176 | + |
| 177 | + /// @dev Extract the block access list hash from the block header RLP. This will be introduced in Amsterdam. |
| 178 | + function getBlockAccessListHash(bytes memory headerRLP) internal pure returns (bytes32) { |
| 179 | + return _parseHeader(headerRLP, Hardforks.Amsterdam)[uint8(HeaderField.BlockAccessListHash)].readBytes32(); |
| 180 | + } |
| 181 | + |
| 182 | + /// @dev Internal function to parse the block header RLP and return the list of fields as memory slices. |
| 183 | + /// It also checks that the number of fields is at least the expected number for the given hardfork version. |
| 184 | + function _parseHeader(bytes memory headerRLP, Hardforks version) private pure returns (Memory.Slice[] memory) { |
| 185 | + Memory.Slice[] memory fields = RLP.decodeList(headerRLP); |
| 186 | + require(fields.length >= _expectedHeadersLength(version), InvalidBlockHeader(version)); |
| 187 | + return fields; |
| 188 | + } |
| 189 | + |
| 190 | + /// @dev Internal function to return the expected number of fields in the block header RLP for a given hardfork version. |
| 191 | + function _expectedHeadersLength(Hardforks version) private pure returns (uint8 count) { |
| 192 | + assembly ("memory-safe") { |
| 193 | + count := byte(version, 0x0F0F0F0F0F0F0F0F0F0F10101010111415151600000000000000000000000000) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments