Skip to content

Commit

Permalink
shardtree: Don't insert empty subtrees in ShardTree::insert_tree
Browse files Browse the repository at this point in the history
`ShardTree::max_leaf_position` relies on the invariant that the last
shard in the subtrees vector is never created without a leaf then being
added to it. `LocatedTree::decompose_to_level` can return a trailing
empty subtree for some inputs, and given that it is always correct to
not insert an empty subtree into `self`, we maintain the invariant by
skipping empty subtrees.
  • Loading branch information
str4d committed Jul 24, 2023
1 parent 1eda8b2 commit 4666e9e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions shardtree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,15 @@ impl<
) -> Result<Vec<IncompleteAt>, ShardTreeError<S::Error>> {
let mut all_incomplete = vec![];
for subtree in tree.decompose_to_level(Self::subtree_level()).into_iter() {
// `ShardTree::max_leaf_position` relies on the invariant that the last shard
// in the subtrees vector is never created without a leaf then being added to
// it. `LocatedTree::decompose_to_level` can return a trailing empty subtree
// for some inputs, and given that it is always correct to not insert an empty
// subtree into `self`, we maintain the invariant by skipping empty subtrees.
if subtree.root().is_empty() {
continue;
}

// `LocatedTree::decompose_to_level` will return the tree as-is if it is
// smaller than a shard, so we can't assume that the address of `subtree` is a
// valid shard address.
Expand Down

0 comments on commit 4666e9e

Please sign in to comment.