reth_storage_api/
storage.rs

1use alloc::{
2    collections::{BTreeMap, BTreeSet},
3    vec::Vec,
4};
5use alloy_primitives::{Address, BlockNumber, B256};
6use core::ops::RangeInclusive;
7use reth_primitives_traits::StorageEntry;
8use reth_storage_errors::provider::ProviderResult;
9
10/// Storage reader
11#[auto_impl::auto_impl(&, Arc, Box)]
12pub trait StorageReader: Send + Sync {
13    /// Get plainstate storages for addresses and storage keys.
14    fn plain_state_storages(
15        &self,
16        addresses_with_keys: impl IntoIterator<Item = (Address, impl IntoIterator<Item = B256>)>,
17    ) -> ProviderResult<Vec<(Address, Vec<StorageEntry>)>>;
18
19    /// Iterate over storage changesets and return all storage slots that were changed.
20    fn changed_storages_with_range(
21        &self,
22        range: RangeInclusive<BlockNumber>,
23    ) -> ProviderResult<BTreeMap<Address, BTreeSet<B256>>>;
24
25    /// Iterate over storage changesets and return all storage slots that were changed alongside
26    /// each specific set of blocks.
27    ///
28    /// NOTE: Get inclusive range of blocks.
29    fn changed_storages_and_blocks_with_range(
30        &self,
31        range: RangeInclusive<BlockNumber>,
32    ) -> ProviderResult<BTreeMap<(Address, B256), Vec<u64>>>;
33}
34
35/// Storage ChangeSet reader
36#[cfg(feature = "db-api")]
37#[auto_impl::auto_impl(&, Arc, Box)]
38pub trait StorageChangeSetReader: Send + Sync {
39    /// Iterate over storage changesets and return the storage state from before this block.
40    fn storage_changeset(
41        &self,
42        block_number: BlockNumber,
43    ) -> ProviderResult<Vec<(reth_db_api::models::BlockNumberAddress, StorageEntry)>>;
44}
45
46/// An enum that represents the storage location for a piece of data.
47#[derive(Debug, Copy, Clone, PartialEq, Eq)]
48pub enum StorageLocation {
49    /// Write only to static files.
50    StaticFiles,
51    /// Write only to the database.
52    Database,
53    /// Write to both the database and static files.
54    Both,
55}
56
57impl StorageLocation {
58    /// Returns true if the storage location includes static files.
59    pub const fn static_files(&self) -> bool {
60        matches!(self, Self::StaticFiles | Self::Both)
61    }
62
63    /// Returns true if the storage location includes the database.
64    pub const fn database(&self) -> bool {
65        matches!(self, Self::Database | Self::Both)
66    }
67}