Skip to main content

foundry_evm_core/
eip2935.rs

1//! EIP-2935 history storage helpers.
2
3use alloy_primitives::{Address, B256, U256};
4
5pub use alloy_eips::eip2935::{
6    HISTORY_SERVE_WINDOW, HISTORY_STORAGE_ADDRESS, HISTORY_STORAGE_CODE,
7};
8
9/// Returns whether `address` is the EIP-2935 history storage contract.
10#[inline]
11pub fn is_history_storage_address(address: &Address) -> bool {
12    *address == HISTORY_STORAGE_ADDRESS
13}
14
15/// Returns the history storage ring slot for `block_number`.
16#[inline]
17pub fn history_storage_slot(block_number: U256) -> U256 {
18    block_number % U256::from(HISTORY_SERVE_WINDOW)
19}
20
21/// Encodes a block hash as the history contract storage value.
22#[inline]
23pub const fn history_storage_value(block_hash: B256) -> U256 {
24    U256::from_be_bytes(block_hash.0)
25}
26
27/// Returns the first block in the valid EIP-2935 window for `current_block`.
28#[inline]
29pub fn history_window_start(current_block: U256) -> U256 {
30    current_block.saturating_sub(U256::from(HISTORY_SERVE_WINDOW))
31}
32
33/// Returns the first block to backfill when rolling forward from `old_block` to `new_block`.
34#[inline]
35pub fn forward_fill_start(old_block: U256, new_block: U256) -> U256 {
36    old_block.max(history_window_start(new_block))
37}