1use alloc::boxed::Box;
2use alloc::vec::Vec;
3
4use crate::Word;
5use crate::crypto::merkle::MerkleError;
6#[cfg(feature = "std")]
7use crate::crypto::merkle::smt::{LargeSmt, LargeSmtError, SmtStorage, SmtStorageReader};
8use crate::crypto::merkle::smt::{LeafIndex, MutationSet, SMT_DEPTH, Smt, SmtLeaf, SmtProof};
9
10pub trait SmtBackendReader: Sized {
26 type Error: core::error::Error + Send + 'static;
27
28 fn num_leaves(&self) -> usize;
30
31 fn num_entries(&self) -> usize;
35
36 fn leaves(&self) -> Box<dyn Iterator<Item = (LeafIndex<SMT_DEPTH>, SmtLeaf)> + '_>;
38
39 fn entries(&self) -> Box<dyn Iterator<Item = (Word, Word)> + '_>;
41
42 fn open(&self, key: &Word) -> SmtProof;
44
45 fn get_value(&self, key: &Word) -> Word;
47
48 fn get_leaf(&self, key: &Word) -> SmtLeaf;
50
51 fn root(&self) -> Word;
53}
54
55pub trait SmtBackend: SmtBackendReader {
60 fn compute_mutations(
62 &self,
63 updates: Vec<(Word, Word)>,
64 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error>;
65
66 fn apply_mutations(
68 &mut self,
69 set: MutationSet<SMT_DEPTH, Word, Word>,
70 ) -> Result<(), Self::Error>;
71
72 fn apply_mutations_with_reversion(
76 &mut self,
77 set: MutationSet<SMT_DEPTH, Word, Word>,
78 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error>;
79
80 fn insert(&mut self, key: Word, value: Word) -> Result<Word, Self::Error>;
82}
83
84impl SmtBackendReader for Smt {
88 type Error = MerkleError;
89
90 fn num_leaves(&self) -> usize {
91 Smt::num_leaves(self)
92 }
93
94 fn num_entries(&self) -> usize {
95 Smt::num_entries(self)
96 }
97
98 fn leaves(&self) -> Box<dyn Iterator<Item = (LeafIndex<SMT_DEPTH>, SmtLeaf)> + '_> {
99 Box::new(Smt::leaves(self).map(|(idx, leaf)| (idx, leaf.clone())))
100 }
101
102 fn entries(&self) -> Box<dyn Iterator<Item = (Word, Word)> + '_> {
103 Box::new(Smt::entries(self).map(|(k, v)| (*k, *v)))
104 }
105
106 fn open(&self, key: &Word) -> SmtProof {
107 Smt::open(self, key)
108 }
109
110 fn get_value(&self, key: &Word) -> Word {
111 Smt::get_value(self, key)
112 }
113
114 fn get_leaf(&self, key: &Word) -> SmtLeaf {
115 Smt::get_leaf(self, key)
116 }
117
118 fn root(&self) -> Word {
119 Smt::root(self)
120 }
121}
122
123impl SmtBackend for Smt {
127 fn compute_mutations(
128 &self,
129 updates: Vec<(Word, Word)>,
130 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error> {
131 Smt::compute_mutations(self, updates)
132 }
133
134 fn apply_mutations(
135 &mut self,
136 set: MutationSet<SMT_DEPTH, Word, Word>,
137 ) -> Result<(), Self::Error> {
138 Smt::apply_mutations(self, set)
139 }
140
141 fn apply_mutations_with_reversion(
142 &mut self,
143 set: MutationSet<SMT_DEPTH, Word, Word>,
144 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error> {
145 Smt::apply_mutations_with_reversion(self, set)
146 }
147
148 fn insert(&mut self, key: Word, value: Word) -> Result<Word, Self::Error> {
149 Smt::insert(self, key, value)
150 }
151}
152
153#[cfg(feature = "std")]
157impl<Backend> SmtBackendReader for LargeSmt<Backend>
158where
159 Backend: SmtStorageReader,
160{
161 type Error = MerkleError;
162
163 fn num_leaves(&self) -> usize {
164 LargeSmt::num_leaves(self)
165 }
166
167 fn num_entries(&self) -> usize {
168 LargeSmt::num_entries(self)
169 }
170
171 fn leaves(&self) -> Box<dyn Iterator<Item = (LeafIndex<SMT_DEPTH>, SmtLeaf)> + '_> {
172 Box::new(
173 LargeSmt::leaves(self)
174 .expect("Only IO can error out here")
175 .map(|leaf| leaf.expect("Only IO can error out here")),
176 )
177 }
178
179 fn entries(&self) -> Box<dyn Iterator<Item = (Word, Word)> + '_> {
180 Box::new(
183 LargeSmt::entries(self)
184 .expect("Storage I/O error accessing entries")
185 .map(|entry| entry.expect("Storage I/O error accessing entries")),
186 )
187 }
188
189 fn open(&self, key: &Word) -> SmtProof {
190 LargeSmt::open(self, key)
191 }
192
193 fn get_value(&self, key: &Word) -> Word {
194 LargeSmt::get_value(self, key)
195 }
196
197 fn get_leaf(&self, key: &Word) -> SmtLeaf {
198 LargeSmt::get_leaf(self, key)
199 }
200
201 fn root(&self) -> Word {
202 LargeSmt::root(self)
203 }
204}
205
206#[cfg(feature = "std")]
210impl<Backend> SmtBackend for LargeSmt<Backend>
211where
212 Backend: SmtStorage,
213{
214 fn compute_mutations(
215 &self,
216 updates: Vec<(Word, Word)>,
217 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error> {
218 LargeSmt::compute_mutations(self, updates).map_err(large_smt_error_to_merkle_error)
219 }
220
221 fn apply_mutations(
222 &mut self,
223 set: MutationSet<SMT_DEPTH, Word, Word>,
224 ) -> Result<(), Self::Error> {
225 LargeSmt::apply_mutations(self, set).map_err(large_smt_error_to_merkle_error)
226 }
227
228 fn apply_mutations_with_reversion(
229 &mut self,
230 set: MutationSet<SMT_DEPTH, Word, Word>,
231 ) -> Result<MutationSet<SMT_DEPTH, Word, Word>, Self::Error> {
232 LargeSmt::apply_mutations_with_reversion(self, set).map_err(large_smt_error_to_merkle_error)
233 }
234
235 fn insert(&mut self, key: Word, value: Word) -> Result<Word, Self::Error> {
236 LargeSmt::insert(self, key, value)
237 }
238}
239
240#[cfg(feature = "std")]
248pub(crate) fn large_smt_error_to_merkle_error(err: LargeSmtError) -> MerkleError {
249 match err {
250 LargeSmtError::Storage(storage_err) => {
251 panic!("Storage error encountered: {:?}", storage_err)
252 },
253 LargeSmtError::StorageNotEmpty => {
254 panic!("StorageNotEmpty error encountered: {:?}", err)
255 },
256 LargeSmtError::Merkle(merkle_err) => merkle_err,
257 LargeSmtError::RootMismatch { expected, actual } => MerkleError::ConflictingRoots {
258 expected_root: expected,
259 actual_root: actual,
260 },
261 }
262}