Prunable and easy to store MMR impl

This commit is contained in:
Ignotus Peverell 2017-08-04 19:50:32 +00:00
parent c994c7cba2
commit 95fe570187
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
2 changed files with 45 additions and 1 deletions

View file

@ -17,6 +17,7 @@
pub mod block;
pub mod build;
pub mod hash;
pub mod pmmr;
pub mod sumtree;
pub mod target;
pub mod transaction;

View file

@ -403,7 +403,50 @@ where
}
}
// TODO push_many to allow bulk updates
fn clone_pruned_recurse(node: &NodeData<T>) -> NodeData<T> {
if node.full {
// replaces full internal nodes, leaves and already pruned nodes are full
// as well
NodeData {
full: true,
node: Node::Pruned(node.sum()),
hash: node.hash,
depth: node.depth,
}
} else {
if let Node::Internal { ref lchild, ref rchild, ref sum } = node.node {
// just recurse on each side to get the pruned version
NodeData {
full: false,
node: Node::Internal {
lchild: Box::new(SumTree::clone_pruned_recurse(lchild)),
rchild: Box::new(SumTree::clone_pruned_recurse(rchild)),
sum: sum.clone(),
},
hash: node.hash,
depth: node.depth,
}
} else {
unreachable!()
}
}
}
/// Minimal clone of this tree, replacing all full nodes with a pruned node,
/// therefore only copying non-full subtrees.
pub fn clone_pruned(&self) -> SumTree<T> {
match self.root {
Some(ref node) => {
SumTree {
index: HashMap::new(),
root: Some(SumTree::clone_pruned_recurse(node)),
}
},
None => SumTree::new(),
}
}
// TODO push_many, truncate to allow bulk updates
}
// A SumTree is encoded as follows: an empty tree is the single byte 0x00.