Fix bug with num_nodes/num_chunks

This commit is contained in:
Paul Hauner 2019-04-24 10:17:05 +10:00
parent a84a063c25
commit e19abee7f9
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 14 additions and 8 deletions

View File

@ -316,7 +316,7 @@ impl TreeHashCache {
old_flags, old_flags,
old_overlay.height(), old_overlay.height(),
new_overlay.height(), new_overlay.height(),
new_overlay.total_chunks(), new_overlay.num_chunks(),
) )
.ok_or_else(|| Error::UnableToShrinkMerkleTree)? .ok_or_else(|| Error::UnableToShrinkMerkleTree)?
}; };

View File

@ -42,6 +42,10 @@ impl BTreeOverlay {
self.num_leaf_nodes() - self.lengths.len() self.num_leaf_nodes() - self.lengths.len()
} }
/// Returns the number of nodes in the tree.
///
/// Note: this is distinct from `num_chunks`, which returns the total number of chunks in
/// this tree.
pub fn num_nodes(&self) -> usize { pub fn num_nodes(&self) -> usize {
2 * self.num_leaf_nodes() - 1 2 * self.num_leaf_nodes() - 1
} }
@ -71,7 +75,11 @@ impl BTreeOverlay {
self.first_node()..self.next_node() self.first_node()..self.next_node()
} }
pub fn total_chunks(&self) -> usize { /// Returns the number of chunks inside this tree (including subtrees).
///
/// Note: this is distinct from `num_nodes` which returns the number of nodes in the binary
/// tree.
pub fn num_chunks(&self) -> usize {
self.next_node() - self.first_node() self.next_node() - self.first_node()
} }

View File

@ -48,7 +48,7 @@ where
let mut lengths = vec![]; let mut lengths = vec![];
for item in self { for item in self {
lengths.push(BTreeOverlay::new(item, 0, depth)?.num_nodes()) lengths.push(BTreeOverlay::new(item, 0, depth)?.num_chunks())
} }
// Disallow zero-length as an empty list still has one all-padding node. // Disallow zero-length as an empty list still has one all-padding node.
@ -177,8 +177,6 @@ where
cache.update_internal_nodes(&new_overlay)?; cache.update_internal_nodes(&new_overlay)?;
dbg!(&new_overlay);
// Mix in length. // Mix in length.
let root_node = new_overlay.root(); let root_node = new_overlay.root();
if cache.changed(root_node)? { if cache.changed(root_node)? {

View File

@ -215,9 +215,9 @@ fn test_list_of_struct_with_vec() {
let modified = vec![ let modified = vec![
vec![a.clone(), c.clone()], vec![a.clone(), c.clone()],
// vec![a.clone(), b.clone(), c.clone(), d.clone()], vec![a.clone(), b.clone(), c.clone(), d.clone()],
// vec![b.clone(), a.clone(), c.clone(), d.clone()], vec![b.clone(), a.clone(), c.clone(), d.clone()],
// vec![], vec![],
]; ];
test_routine(original, modified); test_routine(original, modified);