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