reth_storage_api/
trie.rs

1use alloc::vec::Vec;
2use alloy_primitives::{map::B256Map, Address, Bytes, B256};
3use reth_storage_errors::provider::ProviderResult;
4use reth_trie_common::{
5    updates::{StorageTrieUpdates, TrieUpdates},
6    AccountProof, HashedPostState, HashedStorage, MultiProof, MultiProofTargets, StorageMultiProof,
7    StorageProof, TrieInput,
8};
9
10/// A type that can compute the state root of a given post state.
11#[auto_impl::auto_impl(&, Box, Arc)]
12pub trait StateRootProvider: Send + Sync {
13    /// Returns the state root of the `BundleState` on top of the current state.
14    ///
15    /// # Note
16    ///
17    /// It is recommended to provide a different implementation from
18    /// `state_root_with_updates` since it affects the memory usage during state root
19    /// computation.
20    fn state_root(&self, hashed_state: HashedPostState) -> ProviderResult<B256>;
21
22    /// Returns the state root of the `HashedPostState` on top of the current state but reuses the
23    /// intermediate nodes to speed up the computation. It's up to the caller to construct the
24    /// prefix sets and inform the provider of the trie paths that have changes.
25    fn state_root_from_nodes(&self, input: TrieInput) -> ProviderResult<B256>;
26
27    /// Returns the state root of the `HashedPostState` on top of the current state with trie
28    /// updates to be committed to the database.
29    fn state_root_with_updates(
30        &self,
31        hashed_state: HashedPostState,
32    ) -> ProviderResult<(B256, TrieUpdates)>;
33
34    /// Returns state root and trie updates.
35    /// See [`StateRootProvider::state_root_from_nodes`] for more info.
36    fn state_root_from_nodes_with_updates(
37        &self,
38        input: TrieInput,
39    ) -> ProviderResult<(B256, TrieUpdates)>;
40}
41
42/// A type that can compute the storage root for a given account.
43#[auto_impl::auto_impl(&, Box, Arc)]
44pub trait StorageRootProvider: Send + Sync {
45    /// Returns the storage root of the `HashedStorage` for target address on top of the current
46    /// state.
47    fn storage_root(&self, address: Address, hashed_storage: HashedStorage)
48        -> ProviderResult<B256>;
49
50    /// Returns the storage proof of the `HashedStorage` for target slot on top of the current
51    /// state.
52    fn storage_proof(
53        &self,
54        address: Address,
55        slot: B256,
56        hashed_storage: HashedStorage,
57    ) -> ProviderResult<StorageProof>;
58
59    /// Returns the storage multiproof for target slots.
60    fn storage_multiproof(
61        &self,
62        address: Address,
63        slots: &[B256],
64        hashed_storage: HashedStorage,
65    ) -> ProviderResult<StorageMultiProof>;
66}
67
68/// A type that can generate state proof on top of a given post state.
69#[auto_impl::auto_impl(&, Box, Arc)]
70pub trait StateProofProvider: Send + Sync {
71    /// Get account and storage proofs of target keys in the `HashedPostState`
72    /// on top of the current state.
73    fn proof(
74        &self,
75        input: TrieInput,
76        address: Address,
77        slots: &[B256],
78    ) -> ProviderResult<AccountProof>;
79
80    /// Generate [`MultiProof`] for target hashed account and corresponding
81    /// hashed storage slot keys.
82    fn multiproof(
83        &self,
84        input: TrieInput,
85        targets: MultiProofTargets,
86    ) -> ProviderResult<MultiProof>;
87
88    /// Get trie witness for provided state.
89    fn witness(&self, input: TrieInput, target: HashedPostState) -> ProviderResult<Vec<Bytes>>;
90}
91
92/// Trie Writer
93#[auto_impl::auto_impl(&, Arc, Box)]
94pub trait TrieWriter: Send + Sync {
95    /// Writes trie updates to the database.
96    ///
97    /// Returns the number of entries modified.
98    fn write_trie_updates(&self, trie_updates: &TrieUpdates) -> ProviderResult<usize>;
99}
100
101/// Storage Trie Writer
102#[auto_impl::auto_impl(&, Arc, Box)]
103pub trait StorageTrieWriter: Send + Sync {
104    /// Writes storage trie updates from the given storage trie map.
105    ///
106    /// First sorts the storage trie updates by the hashed address key, writing in sorted order.
107    ///
108    /// Returns the number of entries modified.
109    fn write_storage_trie_updates(
110        &self,
111        storage_tries: &B256Map<StorageTrieUpdates>,
112    ) -> ProviderResult<usize>;
113
114    /// Writes storage trie updates for the given hashed address.
115    fn write_individual_storage_trie_updates(
116        &self,
117        hashed_address: B256,
118        updates: &StorageTrieUpdates,
119    ) -> ProviderResult<usize>;
120}