feat: add parent node iterator for SimpleSMT

This commit is contained in:
Augusto F. Hack
2023-04-03 19:08:42 +02:00
parent f19fe6e739
commit 9275dd00ad
3 changed files with 71 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
use super::{
BTreeMap, EmptySubtreeRoots, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, Vec, Word,
BTreeMap, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Rpo256,
RpoDigest, Vec, Word,
};
#[cfg(test)]
@@ -15,14 +16,20 @@ pub struct SimpleSmt {
depth: u8,
root: Word,
leaves: BTreeMap<u64, Word>,
pub(crate) branches: BTreeMap<NodeIndex, BranchNode>,
branches: BTreeMap<NodeIndex, BranchNode>,
empty_hashes: Vec<RpoDigest>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub(crate) struct BranchNode {
pub(crate) left: RpoDigest,
pub(crate) right: RpoDigest,
struct BranchNode {
left: RpoDigest,
right: RpoDigest,
}
impl BranchNode {
fn parent(&self) -> RpoDigest {
Rpo256::merge(&[self.left, self.right])
}
}
impl SimpleSmt {
@@ -171,6 +178,15 @@ impl SimpleSmt {
self.get_path(NodeIndex::new(self.depth(), key))
}
/// Iterator over the inner nodes of the [SimpleSmt].
pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
self.branches.values().map(|e| InnerNodeInfo {
value: e.parent().into(),
left: e.left.into(),
right: e.right.into(),
})
}
// STATE MUTATORS
// --------------------------------------------------------------------------------------------