reth_storage_api/
storage.rs

1use alloy_primitives::{Address, BlockNumber, B256};
2use reth_db_api::models::BlockNumberAddress;
3use reth_primitives::StorageEntry;
4use reth_storage_errors::provider::ProviderResult;
5use std::{
6    collections::{BTreeMap, BTreeSet},
7    ops::RangeInclusive,
8};
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#[auto_impl::auto_impl(&, Arc, Box)]
37pub trait StorageChangeSetReader: Send + Sync {
38    /// Iterate over storage changesets and return the storage state from before this block.
39    fn storage_changeset(
40        &self,
41        block_number: BlockNumber,
42    ) -> ProviderResult<Vec<(BlockNumberAddress, StorageEntry)>>;
43}
44
45/// An enum that represents the storage location for a piece of data.
46#[derive(Debug, Copy, Clone, PartialEq, Eq)]
47pub enum StorageLocation {
48    /// Write only to static files.
49    StaticFiles,
50    /// Write only to the database.
51    Database,
52    /// Write to both the database and static files.
53    Both,
54}
55
56impl StorageLocation {
57    /// Returns true if the storage location includes static files.
58    pub const fn static_files(&self) -> bool {
59        matches!(self, Self::StaticFiles | Self::Both)
60    }
61
62    /// Returns true if the storage location includes the database.
63    pub const fn database(&self) -> bool {
64        matches!(self, Self::Database | Self::Both)
65    }
66}