miden_objects/asset/vault/
partial.rs1use alloc::vec::Vec;
2
3use miden_crypto::merkle::{InnerNodeInfo, SmtLeaf, SmtProof};
4use vm_core::utils::{Deserializable, Serializable};
5use vm_processor::Digest;
6
7use super::AssetVault;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct PartialVault {
16 root: Digest,
18 vault_proofs: Vec<SmtProof>,
20}
21
22impl PartialVault {
23 pub fn new(root: Digest, vault_proofs: Vec<SmtProof>) -> Self {
25 PartialVault { root, vault_proofs }
26 }
27
28 pub fn root(&self) -> Digest {
30 self.root
31 }
32
33 pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
38 self.vault_proofs.iter().flat_map(|proof| {
39 let leaf = proof.leaf();
40 proof.path().inner_nodes(leaf.index().value(), leaf.hash()).unwrap()
41 })
42 }
43
44 pub fn leaves(&self) -> impl Iterator<Item = &SmtLeaf> {
48 self.vault_proofs.iter().map(SmtProof::leaf)
49 }
50}
51
52impl From<&AssetVault> for PartialVault {
53 fn from(value: &AssetVault) -> Self {
54 let root = value.root();
55 let vault_proofs: Vec<SmtProof> = value
56 .asset_tree()
57 .entries()
58 .map(|(key, _)| value.asset_tree().open(key))
59 .collect();
60
61 PartialVault { root, vault_proofs }
62 }
63}
64
65impl Serializable for PartialVault {
66 fn write_into<W: vm_core::utils::ByteWriter>(&self, target: &mut W) {
67 target.write(self.root);
68 target.write(&self.vault_proofs);
69 }
70}
71
72impl Deserializable for PartialVault {
73 fn read_from<R: vm_core::utils::ByteReader>(
74 source: &mut R,
75 ) -> Result<Self, vm_processor::DeserializationError> {
76 let root = source.read()?;
77 let vault_proofs = source.read()?;
78
79 Ok(PartialVault { root, vault_proofs })
80 }
81}