mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
Prunable and easy to store MMR impl
This commit is contained in:
parent
c994c7cba2
commit
95fe570187
2 changed files with 45 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue