miden_tx/host/
script_mast_forest_store.rs

1use alloc::collections::BTreeMap;
2use alloc::sync::Arc;
3
4use miden_objects::Word;
5use miden_objects::assembly::mast::MastForest;
6use miden_objects::note::NoteScript;
7use miden_objects::transaction::TransactionScript;
8use miden_objects::vm::AdviceMap;
9use miden_processor::MastForestStore;
10
11/// Stores the MAST forests for a set of scripts (both note scripts and transaction scripts).
12///
13/// A [ScriptMastForestStore] is meant to exclusively store MAST forests related to both
14/// transaction and input note scripts.
15pub struct ScriptMastForestStore {
16    mast_forests: BTreeMap<Word, Arc<MastForest>>,
17    advice_map: AdviceMap,
18}
19
20impl ScriptMastForestStore {
21    /// Creates a new [ScriptMastForestStore].
22    pub fn new(
23        tx_script: Option<&TransactionScript>,
24        note_scripts: impl Iterator<Item = impl AsRef<NoteScript>>,
25    ) -> Self {
26        let mut mast_store = ScriptMastForestStore {
27            mast_forests: BTreeMap::new(),
28            advice_map: AdviceMap::default(),
29        };
30
31        for note_script in note_scripts {
32            mast_store.insert(note_script.as_ref().mast());
33        }
34
35        if let Some(tx_script) = tx_script {
36            mast_store.insert(tx_script.mast());
37        }
38        mast_store
39    }
40
41    /// Registers all procedures of the provided [MastForest] with this store.
42    fn insert(&mut self, mast_forest: Arc<MastForest>) {
43        // only register procedures that are local to this forest
44        for proc_digest in mast_forest.local_procedure_digests() {
45            self.mast_forests.insert(proc_digest, mast_forest.clone());
46        }
47
48        // collect advice data from the forest
49        for (key, values) in mast_forest.advice_map().clone() {
50            self.advice_map.insert((*key).into(), values);
51        }
52    }
53
54    /// Returns a reference to the advice data collected from all forests.
55    pub fn advice_map(&self) -> &AdviceMap {
56        &self.advice_map
57    }
58}
59
60// MAST FOREST STORE IMPLEMENTATION
61// ================================================================================================
62
63impl MastForestStore for ScriptMastForestStore {
64    fn get(&self, procedure_root: &Word) -> Option<Arc<MastForest>> {
65        self.mast_forests.get(procedure_root).cloned()
66    }
67}