Fix free leaf index
@@ -164,17 +164,17 @@ impl<'a> TreeSyncDiff<'a> {
/// Find and return the index of either the left-most blank leaf, or, if
/// there are no blank leaves, the leaf count.
pub(crate) fn free_leaf_index(&self) -> LeafNodeIndex {
- // Find a free leaf and fill it with the new key package.
- let mut leaf_index_option = None;
+ let leaf_count = self.diff.leaves().count() as u32;
+
+ // Search for blank leaves in existing leaves
for (leaf_index, leaf_id) in self.diff.leaves() {
if leaf_id.node().is_none() {
- leaf_index_option = Some(leaf_index);
- break;
+ return leaf_index;
}
}
- // If we found a free leaf, replace it with the new one, otherwise
- // extend the tree.
- leaf_index_option.unwrap_or_else(|| LeafNodeIndex::new(self.leaf_count()))
+
+ // Return the next free virtual blank leaf
+ LeafNodeIndex::new(leaf_count)
}
/// Adds a new leaf to the tree either by filling a blank leaf or by
