Skip to content

Commit 3e75ff1

Browse files
arr00ernestognw
andauthored
Add blockhash to docs (#5653)
Co-authored-by: ernestognw <ernestognw@gmail.com>
1 parent 6dd191a commit 3e75ff1

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

contracts/utils/Blockhash.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pragma solidity ^0.8.20;
1414
* Before that, only block hashes since the fork block will be available.
1515
*/
1616
library Blockhash {
17+
/// @dev Address of the EIP-2935 history storage contract.
1718
address internal constant HISTORY_STORAGE_ADDRESS = 0x0000F90827F1C53a10cb7A02335B175320002935;
1819

1920
/**

contracts/utils/README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
4545
* {Panic}: A library to revert with https://docs.soliditylang.org/en/v0.8.20/control-structures.html#panic-via-assert-and-error-via-require[Solidity panic codes].
4646
* {Comparators}: A library that contains comparator functions to use with the {Heap} library.
4747
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
48+
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
4849

4950
[NOTE]
5051
====
@@ -156,3 +157,5 @@ Ethereum contracts have no native concept of an interface, so applications must
156157
{{CAIP2}}
157158

158159
{{CAIP10}}
160+
161+
{{Blockhash}}

docs/modules/ROOT/pages/utilities.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,22 @@ await instance.multicall([
386386
instance.interface.encodeFunctionData("bar")
387387
]);
388388
----
389+
390+
=== Historical Block Hashes
391+
392+
xref:api:utils.adoc#Blockhash[`Blockhash`] provides L2 protocol developers with extended access to historical block hashes beyond Ethereum's native 256-block limit. By leveraging https://eips.ethereum.org/EIPS/eip-2935[EIP-2935]'s history storage contract, the library enables access to block hashes up to 8,191 blocks in the past, making it invaluable for L2 fraud proofs and state verification systems.
393+
394+
The library seamlessly combines native `BLOCKHASH` opcode access for recent blocks (≤256) with EIP-2935 history storage queries for older blocks (257-8,191). It handles edge cases gracefully by returning zero for future blocks or those beyond the history window, matching the EVM's behavior. The implementation uses gas-efficient assembly for static calls to the history storage contract.
395+
396+
[source,solidity]
397+
----
398+
contract L1Inbox {
399+
using Blockhash for uint256;
400+
401+
function verifyBlockHash(uint256 blockNumber, bytes32 expectedHash) public view returns (bool) {
402+
return blockNumber.blockHash() == expectedHash;
403+
}
404+
}
405+
----
406+
407+
IMPORTANT: After EIP-2935 activation, it takes 8,191 blocks to completely fill the history storage. Before that, only block hashes since the fork block will be available.

0 commit comments

Comments
 (0)