Skip to main content

reth_storage_api/
history.rs

1use alloy_primitives::{Address, BlockNumber, B256};
2use auto_impl::auto_impl;
3use core::ops::{RangeBounds, RangeInclusive};
4use reth_db_api::models::BlockNumberAddress;
5use reth_db_models::AccountBeforeTx;
6use reth_primitives_traits::StorageEntry;
7use reth_storage_errors::provider::ProviderResult;
8
9/// Location of a historical account or storage value.
10#[derive(Debug, Eq, PartialEq)]
11pub enum HistoryInfo {
12    /// The key had not been written at the requested block.
13    NotYetWritten,
14    /// The value is in this block's changeset.
15    InChangeset(BlockNumber),
16    /// The value is in plain state.
17    InPlainState,
18    /// Pruning requires a plain-state fallback.
19    MaybeInPlainState,
20}
21
22/// Reads account and storage history indices.
23#[auto_impl(&, Arc, Box)]
24pub trait HistoryReader: Send {
25    /// Looks up an account's historical storage location.
26    fn account_history_info(
27        &self,
28        address: Address,
29        block_number: BlockNumber,
30        lowest_available_block_number: Option<BlockNumber>,
31    ) -> ProviderResult<HistoryInfo>;
32
33    /// Looks up a storage slot's historical storage location.
34    fn storage_history_info(
35        &self,
36        address: Address,
37        storage_key: B256,
38        block_number: BlockNumber,
39        lowest_available_block_number: Option<BlockNumber>,
40    ) -> ProviderResult<HistoryInfo>;
41}
42
43/// History Writer
44#[auto_impl(&, Arc, Box)]
45pub trait HistoryWriter: Send {
46    /// Unwind and clear account history indices.
47    ///
48    /// Returns number of changesets walked.
49    fn unwind_account_history_indices<'a>(
50        &self,
51        changesets: impl Iterator<Item = &'a (BlockNumber, AccountBeforeTx)>,
52    ) -> ProviderResult<usize>;
53
54    /// Unwind and clear account history indices in a given block range.
55    ///
56    /// Returns number of changesets walked.
57    fn unwind_account_history_indices_range(
58        &self,
59        range: impl RangeBounds<BlockNumber>,
60    ) -> ProviderResult<usize>;
61
62    /// Insert account change index to database. Used inside `AccountHistoryIndex` stage
63    fn insert_account_history_index(
64        &self,
65        index_updates: impl IntoIterator<Item = (Address, impl IntoIterator<Item = u64>)>,
66    ) -> ProviderResult<()>;
67
68    /// Unwind and clear storage history indices.
69    ///
70    /// Returns number of changesets walked.
71    fn unwind_storage_history_indices(
72        &self,
73        changesets: impl Iterator<Item = (BlockNumberAddress, StorageEntry)>,
74    ) -> ProviderResult<usize>;
75
76    /// Unwind and clear storage history indices in a given block range.
77    ///
78    /// Returns number of changesets walked.
79    fn unwind_storage_history_indices_range(
80        &self,
81        range: impl RangeBounds<BlockNumber>,
82    ) -> ProviderResult<usize>;
83
84    /// Insert storage change index to database. Used inside `StorageHistoryIndex` stage
85    fn insert_storage_history_index(
86        &self,
87        storage_transitions: impl IntoIterator<Item = ((Address, B256), impl IntoIterator<Item = u64>)>,
88    ) -> ProviderResult<()>;
89
90    /// Read account/storage changesets and update account/storage history indices.
91    fn update_history_indices(&self, range: RangeInclusive<BlockNumber>) -> ProviderResult<()>;
92}