feat: add support for MMR to the MerkleStore

This commit is contained in:
Augusto F. Hack
2023-03-31 04:47:12 +02:00
parent f19fe6e739
commit 429d3bab6f
3 changed files with 163 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
use super::mmr::{Mmr, MmrPeaks};
use super::{
BTreeMap, BTreeSet, EmptySubtreeRoots, MerkleError, MerklePath, MerklePathSet, MerkleTree,
NodeIndex, RootPath, Rpo256, RpoDigest, SimpleSmt, ValuePath, Vec, Word,
@@ -151,6 +152,15 @@ impl MerkleStore {
Ok(self)
}
/// Appends the provided [Mmr] represented by its `leaves` to the set.
pub fn with_mmr<I>(mut self, leaves: I) -> Result<Self, MerkleError>
where
I: IntoIterator<Item = Word>,
{
self.add_mmr(leaves)?;
Ok(self)
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
@@ -375,6 +385,25 @@ impl MerkleStore {
Ok(root)
}
/// Appends the provided [Mmr] into the store.
pub fn add_mmr<I>(&mut self, leaves: I) -> Result<MmrPeaks, MerkleError>
where
I: IntoIterator<Item = Word>,
{
let mmr = Mmr::from(leaves);
for node in mmr.inner_nodes() {
self.nodes.insert(
node.value.into(),
Node {
left: node.left.into(),
right: node.right.into(),
},
);
}
Ok(mmr.accumulator())
}
/// Sets a node to `value`.
///
/// # Errors