You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🧐 Motivation
Before Pectra you can only query the last 256 block hashes. With EIP-2539 the EL will hold in a system contract the last 8192 block hashes(~27 hs) of blocks, this is very useful for rollups and other applications that need to query or proof something against older ethereum state.
📝 Details
Calling the new contract will be more expensive, so you will probably want to still use the precompile for newer blocks - a simple library that abstracts this away seems pretty useful.
Here's a very simple way to implement this. If we use this in our minimal-rollup I'm happy to contribute upstream
/// @dev Reads a block hash up to 8 191 blocks ago after the Pectra fork./// Falls back to the BLOCKHASH opcode for <256 blocks.libraryBlockhashHistory {
addressconstant _HISTORY_ADDR =0x0000F90827F1C53a10Cb7A02335B175320002935;
uint256constant _WINDOW =8191;
/// @notice Returns the hash of block `n` or reverts if too old / future.function hashOf(uint256n) internalviewreturns (bytes32h) {
uint256 current =block.number;
if (n >= current) revert("future block");
unchecked {
if (current - n <=256) {
returnblockhash(n); // cheap path
}
if (current - n > _WINDOW) revert("too old");
}
bytesmemory input =abi.encodePacked(bytes32(n));
// solhint-disable-next-line no-inline-assemblyassembly {
let success :=staticcall(gas(), _HISTORY_ADDR, add(input, 32), 32, 0, 32)
ifiszero(success) { revert(0, 0) }
h :=mload(0)
}
}
}
The text was updated successfully, but these errors were encountered:
🧐 Motivation
Before Pectra you can only query the last 256 block hashes. With EIP-2539 the EL will hold in a system contract the last 8192 block hashes(~27 hs) of blocks, this is very useful for rollups and other applications that need to query or proof something against older ethereum state.
📝 Details
Calling the new contract will be more expensive, so you will probably want to still use the precompile for newer blocks - a simple library that abstracts this away seems pretty useful.
Here's a very simple way to implement this. If we use this in our minimal-rollup I'm happy to contribute upstream
The text was updated successfully, but these errors were encountered: