From b604c801a88b06c90b0f641f74907069cf4efd39 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 6 Jul 2023 03:28:34 +0000 Subject: [PATCH] Add more TRACE-level logging to `shardtree` internals --- shardtree/src/lib.rs | 1 + shardtree/src/prunable.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/shardtree/src/lib.rs b/shardtree/src/lib.rs index 46937b6..11808ba 100644 --- a/shardtree/src/lib.rs +++ b/shardtree/src/lib.rs @@ -649,6 +649,7 @@ impl< mut start: Position, values: I, ) -> Result)>, ShardTreeError> { + trace!("Batch inserting from {:?}", start); let mut values = values.peekable(); let mut subtree_root_addr = Self::subtree_addr(start); let mut max_insert_position = None; diff --git a/shardtree/src/prunable.rs b/shardtree/src/prunable.rs index c3bccbd..0738fc4 100644 --- a/shardtree/src/prunable.rs +++ b/shardtree/src/prunable.rs @@ -224,18 +224,17 @@ impl PrunableTree { value: (vl.0, vl.1 | vr.1), })) } else { + trace!(left = ?vl.0, right = ?vr.0, "Merge conflict for leaves"); Err(addr) } } (Tree(Node::Leaf { value }), parent @ Tree(Node::Parent { .. })) | (parent @ Tree(Node::Parent { .. }), Tree(Node::Leaf { value })) => { - if parent - .root_hash(addr, no_default_fill) - .iter() - .all(|r| r == &value.0) - { + let parent_hash = parent.root_hash(addr, no_default_fill); + if parent_hash.iter().all(|r| r == &value.0) { Ok(parent.reannotate_root(Some(Rc::new(value.0)))) } else { + trace!(leaf = ?value, node = ?parent_hash, "Merge conflict for leaf into node"); Err(addr) } } @@ -244,7 +243,7 @@ impl PrunableTree { let rroot = rparent.root_hash(addr, no_default_fill).ok(); // If both parents share the same root hash (or if one of them is absent), // they can be merged - if lroot.zip(rroot).iter().all(|(l, r)| l == r) { + if lroot.iter().zip(&rroot).all(|(l, r)| l == r) { // using `if let` here to bind variables; we need to borrow the trees for // root hash calculation but binding the children of the parent node // interferes with binding a reference to the parent. @@ -272,12 +271,14 @@ impl PrunableTree { unreachable!() } } else { + trace!(left = ?lroot, right = ?rroot, "Merge conflict for nodes"); Err(addr) } } } } + trace!(this = ?self, other = ?other, "Merging subtrees"); go(root_addr, self, other) } @@ -692,6 +693,11 @@ impl LocatedPrunableTree { (node.root.reannotate_root(ann), incomplete) }; + trace!( + "Node at {:?} contains subtree at {:?}", + root_addr, + subtree.root_addr(), + ); match into { Tree(Node::Nil) => Ok(replacement(None, subtree)), Tree(Node::Leaf { value: (value, _) }) => { @@ -707,6 +713,11 @@ impl LocatedPrunableTree { vec![], )) } else { + trace!( + cur_root = ?value, + new_root = ?subtree.root.node_value(), + "Insertion conflict", + ); Err(InsertionError::Conflict(root_addr)) } } else { @@ -863,6 +874,12 @@ impl LocatedPrunableTree { prune_below: Level, mut values: I, ) -> Option> { + trace!( + position_range = ?position_range, + prune_below = ?prune_below, + "Creating minimal tree for insertion" + ); + // Unite two subtrees by either adding a parent node, or a leaf containing the Merkle root // of such a parent if both nodes are ephemeral leaves. // @@ -1031,6 +1048,7 @@ impl LocatedPrunableTree { break; } } + trace!("Initial fragments: {:?}", fragments); build_minimal_tree(fragments, prune_below).map( |(to_insert, contains_marked, incomplete)| BatchInsertionResult { @@ -1059,6 +1077,7 @@ impl LocatedPrunableTree { start: Position, values: I, ) -> Result>, InsertionError> { + trace!("Batch inserting into {:?} from {:?}", self.root_addr, start); let subtree_range = self.root_addr.position_range(); let contains_start = subtree_range.contains(&start); if contains_start {