Skip to main content

miden_node_store/accounts/
mod.rs

1//! Historical tracking for `AccountTree` via mutation overlays
2
3use std::collections::{BTreeMap, HashMap};
4
5#[cfg(feature = "rocksdb")]
6use miden_large_smt_backend_rocksdb::RocksDbStorage;
7use miden_protocol::account::{AccountId, AccountIdPrefix};
8use miden_protocol::block::BlockNumber;
9use miden_protocol::block::account_tree::{AccountMutationSet, AccountTree, AccountWitness};
10use miden_protocol::crypto::merkle::smt::{
11    LargeSmt,
12    LeafIndex,
13    MemoryStorage,
14    NodeMutation,
15    SMT_DEPTH,
16    SmtLeaf,
17    SmtStorage,
18};
19use miden_protocol::crypto::merkle::{
20    EmptySubtreeRoots,
21    MerkleError,
22    MerklePath,
23    NodeIndex,
24    SparseMerklePath,
25};
26use miden_protocol::errors::AccountTreeError;
27use miden_protocol::{EMPTY_WORD, Word};
28use tracing::instrument;
29
30use crate::COMPONENT;
31
32#[cfg(test)]
33mod tests;
34
35/// Convenience for an in-memory-only account tree.
36pub type InMemoryAccountTree = AccountTree<LargeSmt<MemoryStorage>>;
37
38#[cfg(feature = "rocksdb")]
39/// Convenience for a persistent account tree.
40pub type PersistentAccountTree = AccountTree<LargeSmt<RocksDbStorage>>;
41
42// HISTORICAL ERROR TYPES
43// ================================================================================================
44
45#[expect(missing_docs)]
46#[derive(thiserror::Error, Debug)]
47pub enum HistoricalError {
48    #[error(transparent)]
49    MerkleError(#[from] MerkleError),
50    #[error(transparent)]
51    AccountTreeError(#[from] AccountTreeError),
52}
53
54// HISTORICAL SELECTOR ENUM
55// ================================================================================================
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58enum HistoricalSelector {
59    /// The requested block is in the future (later than current block).
60    Future,
61    /// The requested block is available in history.
62    At(BlockNumber),
63    /// The requested block is the current/latest block.
64    Latest,
65    /// The requested block is too old and has been pruned from history.
66    TooAncient,
67}
68
69// HISTORICAL OVERLAY
70// ================================================================================================
71
72/// Captures reversion state for historical queries at a specific block.
73#[derive(Debug, Clone)]
74struct HistoricalOverlay {
75    block_number: BlockNumber,
76    root: Word,
77    node_mutations: HashMap<NodeIndex, Word>,
78    account_updates: HashMap<LeafIndex<SMT_DEPTH>, (Word, Word)>,
79}
80
81impl HistoricalOverlay {
82    fn new(block_number: BlockNumber, rev_set: AccountMutationSet) -> Self {
83        let root = rev_set.as_mutation_set().root();
84        let mut_set = rev_set.into_mutation_set();
85
86        let node_mutations =
87            HashMap::from_iter(mut_set.node_mutations().iter().map(|(node_index, mutation)| {
88                match mutation {
89                    NodeMutation::Addition(inner_node) => (*node_index, inner_node.hash()),
90                    NodeMutation::Removal => {
91                        // Store the actual empty subtree root for this depth depth() is 1-indexed
92                        // from leaf, so we use it directly for EmptySubtreeRoots
93                        let empty_root = *EmptySubtreeRoots::entry(SMT_DEPTH, node_index.depth());
94                        (*node_index, empty_root)
95                    },
96                }
97            }));
98
99        let account_updates = HashMap::from_iter(
100            mut_set.new_pairs().iter().map(|(&k, &v)| (LeafIndex::from(k), (k, v))),
101        );
102
103        Self {
104            block_number,
105            root,
106            node_mutations,
107            account_updates,
108        }
109    }
110}
111
112// ACCOUNT TREE WITH HISTORY
113// ================================================================================================
114
115/// Wraps `AccountTree` with historical query support via reversion overlays.
116///
117/// This structure maintains a sliding window of historical account states by storing
118/// reversion data (mutations that undo changes). Historical witnesses are reconstructed
119/// by starting from the latest state and applying reversion overlays backwards in time.
120#[derive(Debug)]
121pub struct AccountTreeWithHistory<S: SmtStorage> {
122    /// The current block number (latest state).
123    block_number: BlockNumber,
124    /// The latest account tree state.
125    latest: AccountTree<LargeSmt<S>>,
126    /// Historical overlays indexed by block number, storing reversion data.
127    overlays: BTreeMap<BlockNumber, HistoricalOverlay>,
128}
129
130impl<S: SmtStorage> AccountTreeWithHistory<S> {
131    /// Maximum number of historical blocks to maintain.
132    pub const MAX_HISTORY: usize = 50;
133
134    // CONSTRUCTORS
135    // --------------------------------------------------------------------------------------------
136
137    /// Creates a new historical tree starting at the given block number.
138    pub fn new(account_tree: AccountTree<LargeSmt<S>>, block_number: BlockNumber) -> Self {
139        Self {
140            block_number,
141            latest: account_tree,
142            overlays: BTreeMap::new(),
143        }
144    }
145
146    /// Removes oldest overlays when exceeding the maximum history depth.
147    fn drain_excess(overlays: &mut BTreeMap<BlockNumber, HistoricalOverlay>) {
148        while overlays.len() > Self::MAX_HISTORY {
149            overlays.pop_first();
150        }
151    }
152
153    // PUBLIC ACCESSORS
154    // --------------------------------------------------------------------------------------------
155
156    /// Returns the latest block number.
157    pub fn block_number_latest(&self) -> BlockNumber {
158        self.block_number
159    }
160
161    /// Returns the root hash of the latest state.
162    pub fn root_latest(&self) -> Word {
163        self.latest.root()
164    }
165
166    /// Returns the root hash at a specific historical block.
167    ///
168    /// Returns `None` if the block is in the future or too old (pruned).
169    pub fn root_at(&self, block_number: BlockNumber) -> Option<Word> {
170        match self.historical_selector(block_number) {
171            HistoricalSelector::Latest => Some(self.latest.root()),
172            HistoricalSelector::At(block_number) => {
173                let overlay = self.overlays.get(&block_number)?;
174                debug_assert_eq!(overlay.block_number, block_number);
175                Some(overlay.root)
176            },
177            HistoricalSelector::Future | HistoricalSelector::TooAncient => None,
178        }
179    }
180
181    /// Returns the number of accounts in the latest state.
182    pub fn num_accounts_latest(&self) -> usize {
183        self.latest.num_accounts()
184    }
185
186    /// Returns the number of historical blocks currently stored.
187    pub fn history_len(&self) -> usize {
188        self.overlays.len()
189    }
190
191    /// Opens an account at the latest block, returning its witness.
192    #[instrument(target = COMPONENT, skip_all)]
193    pub fn open_latest(&self, account_id: AccountId) -> AccountWitness {
194        self.latest.open(account_id)
195    }
196
197    /// Opens an account at a historical block, returning its witness.
198    ///
199    /// This method reconstructs the account witness at the given historical block by:
200    /// 1. Starting with the latest account state
201    /// 2. Applying reversion mutations from the overlays to walk back in time
202    /// 3. Reconstructing the Merkle path with the historical node values
203    ///
204    /// Returns `None` if the block is in the future or too old (pruned).
205    #[instrument(target = COMPONENT, skip_all)]
206    pub fn open_at(
207        &self,
208        account_id: AccountId,
209        block_number: BlockNumber,
210    ) -> Option<AccountWitness> {
211        match self.historical_selector(block_number) {
212            HistoricalSelector::Latest => Some(self.latest.open(account_id)),
213            HistoricalSelector::At(block_number) => {
214                // Ensure overlay exists before reconstruction
215                self.overlays.get(&block_number)?;
216                Self::reconstruct_historical_witness(self, account_id, block_number)
217            },
218            HistoricalSelector::Future | HistoricalSelector::TooAncient => None,
219        }
220    }
221
222    /// Gets the account state commitment at the latest block.
223    pub fn get_latest_commitment(&self, account_id: AccountId) -> Word {
224        self.latest.get(account_id)
225    }
226
227    /// Checks if the tree contains an account with the given prefix.
228    pub fn contains_account_id_prefix_in_latest(&self, prefix: AccountIdPrefix) -> bool {
229        self.latest.contains_account_id_prefix(prefix)
230    }
231
232    // PRIVATE HELPERS - HISTORICAL RECONSTRUCTION
233    // --------------------------------------------------------------------------------------------
234
235    /// Determines the historical state selector of a requested block number.
236    fn historical_selector(&self, desired_block_number: BlockNumber) -> HistoricalSelector {
237        if desired_block_number == self.block_number {
238            return HistoricalSelector::Latest;
239        }
240
241        // Check if block is in the future
242        if self.block_number.checked_sub(desired_block_number.as_u32()).is_none() {
243            return HistoricalSelector::Future;
244        }
245
246        // Check if block exists in overlays
247        if !self.overlays.contains_key(&desired_block_number) {
248            return HistoricalSelector::TooAncient;
249        }
250
251        HistoricalSelector::At(desired_block_number)
252    }
253
254    /// Reconstructs a historical account witness by applying reversion overlays.
255    #[instrument(target = COMPONENT, skip_all)]
256    fn reconstruct_historical_witness(
257        &self,
258        account_id: AccountId,
259        block_target: BlockNumber,
260    ) -> Option<AccountWitness> {
261        // Start with the latest witness
262        let latest_witness = self.latest.open(account_id);
263        let (latest_path, leaf) = latest_witness.into_proof().into_parts();
264        let path_nodes = Self::initialize_path_nodes(&latest_path);
265
266        let leaf_index = NodeIndex::from(leaf.index());
267
268        // Apply reversion overlays to reconstruct historical state. We reverse the overlay
269        // iteration (newest to oldest) to walk backwards in time from the latest state to the
270        // target block.
271        let (path, leaf) = Self::apply_reversion_overlays(
272            self.overlays.range(block_target..).rev().map(|(_, overlay)| overlay),
273            path_nodes,
274            leaf_index,
275            leaf,
276        )?;
277
278        // Extract commitment from leaf
279        let commitment = match leaf {
280            SmtLeaf::Empty(_) => EMPTY_WORD,
281            SmtLeaf::Single((_, value)) => value,
282            SmtLeaf::Multiple(_) => unreachable!("AccountTree uses prefix-free IDs"),
283        };
284
285        AccountWitness::new(account_id, commitment, path).ok()
286    }
287
288    /// Initializes the path nodes array from the latest state.
289    ///
290    /// Converts the sparse path to a dense path and reverses it for indexing by depth from leaf.
291    fn initialize_path_nodes(path: &SparseMerklePath) -> [Word; SMT_DEPTH as usize] {
292        let mut path_nodes: [Word; SMT_DEPTH as usize] = MerklePath::from(path.clone())
293            .to_vec()
294            .try_into()
295            .expect("MerklePath should have exactly SMT_DEPTH nodes");
296        path_nodes.reverse();
297        path_nodes
298    }
299
300    /// Applies reversion overlays to reconstruct the historical state.
301    ///
302    /// Iterates through overlays from newest to oldest (walking backwards in time),
303    /// updating both the path nodes and the leaf value based on reversion mutations.
304    #[instrument(target = COMPONENT, skip_all)]
305    fn apply_reversion_overlays<'a>(
306        overlays: impl IntoIterator<Item = &'a HistoricalOverlay>,
307        mut path_nodes: [Word; SMT_DEPTH as usize],
308        leaf_index: NodeIndex,
309        mut leaf: SmtLeaf,
310    ) -> Option<(SparseMerklePath, SmtLeaf)> {
311        // Iterate through overlays
312        for overlay in overlays {
313            // Update path sibling nodes that changed in this overlay
314            for sibling in leaf_index.proof_indices() {
315                let height = sibling
316                    .depth()
317                    .checked_sub(1) // -1: Convert from 1-indexed to 0-indexed
318                    .expect("proof_indices should not include root")
319                    as usize;
320
321                // Apply reversion mutation if this node was modified. It's sound since
322                // `proof_indices()`` returns siblings on the path from leaf to root, hence the
323                // height is always less than `SMT_DEPTH`, the leaf and root are not included.
324                if let Some(hash) = overlay.node_mutations.get(&sibling) {
325                    path_nodes[height] = *hash;
326                }
327            }
328
329            // Update leaf if it was modified in this overlay
330            if let Some(&(key, value)) = overlay.account_updates.get(&leaf.index()) {
331                leaf = if value == EMPTY_WORD {
332                    SmtLeaf::new_empty(leaf.index())
333                } else {
334                    SmtLeaf::new_single(key, value)
335                };
336            }
337        }
338
339        // Build the Merkle path directly from the reconstructed nodes No need for build_dense_path
340        // since all nodes have actual values (not sentinels)
341        let dense: Vec<Word> = path_nodes.iter().rev().copied().collect();
342        let path = MerklePath::new(dense);
343        let path = SparseMerklePath::try_from(path).ok()?;
344        Some((path, leaf))
345    }
346
347    // PUBLIC MUTATORS
348    // --------------------------------------------------------------------------------------------
349
350    /// Computes and applies mutations in one operation.
351    ///
352    /// This is a convenience method primarily for testing.
353    pub fn compute_and_apply_mutations(
354        &mut self,
355        account_commitments: impl IntoIterator<Item = (AccountId, Word)>,
356    ) -> Result<(), HistoricalError> {
357        let mutations = self.compute_mutations(account_commitments)?;
358        self.apply_mutations(mutations)
359    }
360
361    /// Computes mutations relative to the latest state.
362    pub fn compute_mutations(
363        &self,
364        account_commitments: impl IntoIterator<Item = (AccountId, Word)>,
365    ) -> Result<AccountMutationSet, HistoricalError> {
366        Ok(self.latest.compute_mutations(account_commitments)?)
367    }
368
369    /// Applies mutations and advances to the next block.
370    ///
371    /// This method:
372    /// 1. Applies the mutations to the latest tree, getting back reversion data
373    /// 2. Stores the reversion data as a historical overlay
374    /// 3. Advances the block number
375    /// 4. Prunes old overlays if exceeding `MAX_HISTORY`
376    #[instrument(target = COMPONENT, skip_all)]
377    pub fn apply_mutations(
378        &mut self,
379        mutations: AccountMutationSet,
380    ) -> Result<(), HistoricalError> {
381        // Apply mutations and get reversion data
382        let rev = self.latest.apply_mutations_with_reversion(mutations)?;
383
384        // Store reversion data for current block before advancing
385        let block_num = self.block_number;
386        let overlay = HistoricalOverlay::new(block_num, rev);
387        self.overlays.insert(block_num, overlay);
388
389        // Advance to next block
390        self.block_number = block_num.child();
391
392        // Prune old history if needed
393        Self::drain_excess(&mut self.overlays);
394
395        Ok(())
396    }
397}