miden_objects/account/storage/map/
partial.rs1use miden_core::utils::{Deserializable, Serializable};
2use miden_crypto::Word;
3use miden_crypto::merkle::{
4    InnerNodeInfo,
5    LeafIndex,
6    MerkleError,
7    PartialSmt,
8    SMT_DEPTH,
9    SmtLeaf,
10    SmtProof,
11};
12
13use crate::account::StorageMap;
14
15#[derive(Clone, Debug, PartialEq, Eq, Default)]
22pub struct PartialStorageMap {
23    partial_smt: PartialSmt,
24}
25
26impl PartialStorageMap {
27    pub fn new(partial_smt: PartialSmt) -> Self {
32        PartialStorageMap { partial_smt }
33    }
34
35    pub fn partial_smt(&self) -> &PartialSmt {
36        &self.partial_smt
37    }
38
39    pub fn root(&self) -> Word {
40        self.partial_smt.root()
41    }
42
43    pub fn open(&self, key: &Word) -> Result<SmtProof, MerkleError> {
54        let key = StorageMap::hash_key(*key);
55        self.partial_smt.open(&key)
56    }
57
58    pub fn leaves(&self) -> impl Iterator<Item = (LeafIndex<SMT_DEPTH>, &SmtLeaf)> {
63        self.partial_smt.leaves()
64    }
65
66    pub fn entries(&self) -> impl Iterator<Item = (Word, Word)> {
68        self.partial_smt.entries().copied()
69    }
70
71    pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
73        self.partial_smt.inner_nodes()
74    }
75
76    pub fn add(&mut self, proof: SmtProof) -> Result<(), MerkleError> {
81        self.partial_smt.add_proof(proof)
82    }
83}
84
85impl From<StorageMap> for PartialStorageMap {
86    fn from(value: StorageMap) -> Self {
87        let v = value.smt;
88
89        PartialStorageMap { partial_smt: v.into() }
90    }
91}
92
93impl From<PartialSmt> for PartialStorageMap {
94    fn from(partial_smt: PartialSmt) -> Self {
95        PartialStorageMap { partial_smt }
96    }
97}
98
99impl Serializable for PartialStorageMap {
100    fn write_into<W: miden_core::utils::ByteWriter>(&self, target: &mut W) {
101        target.write(&self.partial_smt);
102    }
103}
104
105impl Deserializable for PartialStorageMap {
106    fn read_from<R: miden_core::utils::ByteReader>(
107        source: &mut R,
108    ) -> Result<Self, miden_processor::DeserializationError> {
109        let storage: PartialSmt = source.read()?;
110        Ok(PartialStorageMap { partial_smt: storage })
111    }
112}