Skip to main content

miden_protocol/block/account_tree/
partial.rs

1use miden_crypto::merkle::smt::{PartialSmt, SmtLeaf};
2
3use super::{AccountWitness, account_id_to_smt_key};
4use crate::Word;
5use crate::account::AccountId;
6use crate::errors::AccountTreeError;
7
8/// The partial sparse merkle tree containing the state commitments of accounts in the chain.
9///
10/// This is the partial version of [`AccountTree`](crate::block::account_tree::AccountTree).
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct PartialAccountTree {
13    smt: PartialSmt,
14}
15
16impl PartialAccountTree {
17    // CONSTRUCTORS
18    // --------------------------------------------------------------------------------------------
19
20    /// Creates a new partial account tree with the provided root that does not track any account
21    /// IDs.
22    pub fn new(root: Word) -> Self {
23        PartialAccountTree { smt: PartialSmt::new(root) }
24    }
25
26    /// Returns a new [`PartialAccountTree`] instantiated with the provided entries.
27    ///
28    /// # Errors
29    ///
30    /// Returns an error if:
31    /// - the merkle paths of the witnesses do not result in the same tree root.
32    /// - there are multiple witnesses for the same ID _prefix_.
33    pub fn with_witnesses(
34        witnesses: impl IntoIterator<Item = AccountWitness>,
35    ) -> Result<Self, AccountTreeError> {
36        let mut witnesses = witnesses.into_iter();
37
38        let Some(first_witness) = witnesses.next() else {
39            return Ok(Self::default());
40        };
41
42        // Construct a partial account tree with the root of the first witness.
43        // SAFETY: This is guaranteed to _not_ result in a tree with more than one entry because
44        // the account witness type guarantees that it tracks zero or one entries.
45        let partial_smt = PartialSmt::from_proofs([first_witness.into_proof()])
46            .map_err(AccountTreeError::TreeRootConflict)?;
47        let mut tree = PartialAccountTree { smt: partial_smt };
48
49        // Add all remaining witnesses to the tree, which validates the invariants of the account
50        // tree.
51        for witness in witnesses {
52            tree.track_account(witness)?;
53        }
54
55        Ok(tree)
56    }
57
58    // PUBLIC ACCESSORS
59    // --------------------------------------------------------------------------------------------
60
61    /// Returns an opening of the leaf associated with the `account_id`. This is a proof of the
62    /// current state commitment of the given account ID.
63    ///
64    /// Conceptually, an opening is a Merkle path to the leaf, as well as the leaf itself.
65    ///
66    /// # Errors
67    ///
68    /// Returns an error if:
69    /// - the account ID is not tracked by this account tree.
70    pub fn open(&self, account_id: AccountId) -> Result<AccountWitness, AccountTreeError> {
71        let key = account_id_to_smt_key(account_id);
72
73        self.smt
74            .open(&key)
75            .map(|proof| AccountWitness::from_smt_proof(account_id, proof))
76            .map_err(|source| AccountTreeError::UntrackedAccountId { id: account_id, source })
77    }
78
79    /// Returns the current state commitment of the given account ID.
80    ///
81    /// # Errors
82    ///
83    /// Returns an error if:
84    /// - the account ID is not tracked by this account tree.
85    pub fn get(&self, account_id: AccountId) -> Result<Word, AccountTreeError> {
86        let key = account_id_to_smt_key(account_id);
87        self.smt
88            .get_value(&key)
89            .map_err(|source| AccountTreeError::UntrackedAccountId { id: account_id, source })
90    }
91
92    /// Returns the root of the tree.
93    pub fn root(&self) -> Word {
94        self.smt.root()
95    }
96
97    // PUBLIC MUTATORS
98    // --------------------------------------------------------------------------------------------
99
100    /// Adds the given account witness to the partial tree and tracks it. Once an account has
101    /// been added to the tree, it can be updated using [`Self::upsert_state_commitments`].
102    ///
103    /// # Errors
104    ///
105    /// Returns an error if:
106    /// - after the witness' merkle path was added, the partial account tree has a different root
107    ///   than before it was added (except when the first witness is added).
108    /// - there exists a leaf in the tree whose account ID prefix matches the one in the provided
109    ///   witness.
110    pub fn track_account(&mut self, witness: AccountWitness) -> Result<(), AccountTreeError> {
111        let id_prefix = witness.id().prefix();
112        let id_key = account_id_to_smt_key(witness.id());
113
114        // If a leaf with the same prefix is already tracked by this partial tree, consider it an
115        // error.
116        //
117        // We return an error even for empty leaves, because tracking the same ID prefix twice
118        // indicates that different IDs are attempted to be tracked. It would technically not
119        // violate the invariant of the tree that it only tracks zero or one entries per leaf, but
120        // since tracking the same ID twice should practically never happen, we return an error, out
121        // of an abundance of caution.
122        if self.smt.get_leaf(&id_key).is_ok() {
123            return Err(AccountTreeError::DuplicateIdPrefix { duplicate_prefix: id_prefix });
124        }
125
126        self.smt
127            .add_proof(witness.into_proof())
128            .map_err(AccountTreeError::TreeRootConflict)?;
129
130        Ok(())
131    }
132
133    /// Inserts or updates the provided account ID -> state commitment updates into the partial tree
134    /// which results in a new tree root.
135    ///
136    /// # Errors
137    ///
138    /// Returns an error if:
139    /// - the prefix of the account ID already exists in the tree.
140    /// - the account_id is not tracked by this partial account tree.
141    pub fn upsert_state_commitments(
142        &mut self,
143        updates: impl IntoIterator<Item = (AccountId, Word)>,
144    ) -> Result<(), AccountTreeError> {
145        for (account_id, state_commitment) in updates {
146            self.insert(account_id, state_commitment)?;
147        }
148
149        Ok(())
150    }
151
152    /// Inserts the state commitment for the given account ID, returning the previous state
153    /// commitment associated with that ID.
154    ///
155    /// This also recomputes all hashes between the leaf (associated with the key) and the root,
156    /// updating the root itself.
157    ///
158    /// # Errors
159    ///
160    /// Returns an error if:
161    /// - the prefix of the account ID already exists in the tree.
162    /// - the account_id is not tracked by this partial account tree.
163    fn insert(
164        &mut self,
165        account_id: AccountId,
166        state_commitment: Word,
167    ) -> Result<Word, AccountTreeError> {
168        let key = account_id_to_smt_key(account_id);
169
170        // If there exists a tracked leaf whose key is _not_ the one we're about to overwrite, then
171        // we would insert the new commitment next to an existing account ID with the same prefix,
172        // which is an error.
173        // Note that if the leaf is empty, that's fine. It means it is tracked by the partial SMT,
174        // but no account ID is inserted yet.
175        // Also note that the multiple variant cannot occur by construction of the tree.
176        if let Ok(SmtLeaf::Single((existing_key, _))) = self.smt.get_leaf(&key)
177            && key != existing_key
178        {
179            return Err(AccountTreeError::DuplicateIdPrefix {
180                duplicate_prefix: account_id.prefix(),
181            });
182        }
183
184        self.smt
185            .insert(key, state_commitment)
186            .map_err(|source| AccountTreeError::UntrackedAccountId { id: account_id, source })
187    }
188}
189
190#[cfg(test)]
191mod tests {
192    use assert_matches::assert_matches;
193    use miden_crypto::merkle::smt::Smt;
194
195    use super::*;
196    use crate::block::account_tree::AccountTree;
197    use crate::block::account_tree::tests::setup_duplicate_prefix_ids;
198
199    #[test]
200    fn insert_fails_on_duplicate_prefix() -> anyhow::Result<()> {
201        let mut full_tree = AccountTree::<Smt>::default();
202
203        let [(id0, commitment0), (id1, commitment1)] = setup_duplicate_prefix_ids();
204
205        full_tree.insert(id0, commitment0).unwrap();
206        let witness = full_tree.open(id0);
207
208        let mut partial_tree = PartialAccountTree::with_witnesses([witness])?;
209
210        partial_tree.insert(id0, commitment0).unwrap();
211        assert_eq!(partial_tree.get(id0).unwrap(), commitment0);
212
213        let err = partial_tree.insert(id1, commitment1).unwrap_err();
214
215        assert_matches!(err, AccountTreeError::DuplicateIdPrefix {
216          duplicate_prefix
217        } if duplicate_prefix == id0.prefix());
218
219        partial_tree.upsert_state_commitments([(id1, commitment1)]).unwrap_err();
220
221        assert_matches!(err, AccountTreeError::DuplicateIdPrefix {
222          duplicate_prefix
223        } if duplicate_prefix == id0.prefix());
224
225        Ok(())
226    }
227
228    #[test]
229    fn insert_succeeds_on_multiple_updates() {
230        let mut full_tree = AccountTree::<Smt>::default();
231        let [(id0, commitment0), (_, commitment1)] = setup_duplicate_prefix_ids();
232
233        full_tree.insert(id0, commitment0).unwrap();
234        let witness = full_tree.open(id0);
235
236        let mut partial_tree = PartialAccountTree::new(full_tree.root());
237
238        partial_tree.track_account(witness.clone()).unwrap();
239        assert_eq!(
240            partial_tree.open(id0).unwrap(),
241            witness,
242            "full tree witness and partial tree witness should be the same"
243        );
244        assert_eq!(
245            partial_tree.root(),
246            full_tree.root(),
247            "full tree root and partial tree root should be the same"
248        );
249
250        partial_tree.insert(id0, commitment0).unwrap();
251        partial_tree.insert(id0, commitment1).unwrap();
252        assert_eq!(partial_tree.get(id0).unwrap(), commitment1);
253    }
254
255    #[test]
256    fn upsert_state_commitments_fails_on_untracked_key() {
257        let mut partial_tree = PartialAccountTree::default();
258        let [update, _] = setup_duplicate_prefix_ids();
259
260        let err = partial_tree.upsert_state_commitments([update]).unwrap_err();
261        assert_matches!(err, AccountTreeError::UntrackedAccountId { id, .. }
262          if id == update.0
263        )
264    }
265
266    #[test]
267    fn track_fails_on_duplicate_prefix() {
268        // Use a raw Smt since an account tree would not allow us to get the witnesses for two
269        // account IDs with the same prefix.
270        let full_tree = Smt::with_entries(
271            setup_duplicate_prefix_ids()
272                .map(|(id, commitment)| (account_id_to_smt_key(id), commitment)),
273        )
274        .unwrap();
275
276        let [(id0, _), (id1, _)] = setup_duplicate_prefix_ids();
277
278        let key0 = account_id_to_smt_key(id0);
279        let key1 = account_id_to_smt_key(id1);
280        let proof0 = full_tree.open(&key0);
281        let proof1 = full_tree.open(&key1);
282        assert_eq!(proof0.leaf(), proof1.leaf());
283
284        let witness0 =
285            AccountWitness::new(id0, proof0.get(&key0).unwrap(), proof0.into_parts().0).unwrap();
286        let witness1 =
287            AccountWitness::new(id1, proof1.get(&key1).unwrap(), proof1.into_parts().0).unwrap();
288
289        let mut partial_tree = PartialAccountTree::with_witnesses([witness0]).unwrap();
290        let err = partial_tree.track_account(witness1).unwrap_err();
291
292        assert_matches!(err, AccountTreeError::DuplicateIdPrefix { duplicate_prefix, .. }
293          if duplicate_prefix == id1.prefix()
294        )
295    }
296}