Skip to content

Commit

Permalink
Track pruned subtrees in repair weight
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinSekar committed Jan 26, 2023
1 parent bda0c60 commit 6a1d676
Show file tree
Hide file tree
Showing 2 changed files with 539 additions and 215 deletions.
38 changes: 37 additions & 1 deletion core/src/heaviest_subtree_fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use {
},
std::{
borrow::Borrow,
cmp::Reverse,
collections::{
btree_set::Iter, hash_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet, VecDeque,
btree_set::Iter, hash_map::Entry, BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet,
VecDeque,
},
sync::{Arc, RwLock},
time::Instant,
Expand Down Expand Up @@ -309,6 +311,40 @@ impl HeaviestSubtreeForkChoice {
self.last_root_time = Instant::now();
}

/// Prunes all slots not descending from `new_root`.
/// Returns slots that were removed (`< new_root`) and the newly pruned subtrees with only the slots > `new_root`
pub fn prune(&mut self, new_root: SlotHashKey) -> (Vec<SlotHashKey>, Vec<Self>) {
let mut pruned = vec![];
let mut removed = vec![];
// Find the pruned trees
let mut to_visit = BinaryHeap::from([Reverse(self.tree_root)]);
while !to_visit.is_empty() {
let cur_slot = to_visit.pop().unwrap().0;
if cur_slot == new_root {
continue;
}
if cur_slot < new_root {
for child in (&*self)
.children(&cur_slot)
.expect("slot was discovered earlier, must exist")
{
to_visit.push(Reverse(*child))
}
removed.push(cur_slot);
} else {
// The start of a pruned subtree. Split it out and stop traversing this subtree.
pruned.push(self.split_off(&cur_slot));
}
}

for slot in removed.iter() {
self.fork_infos
.remove(slot)
.expect("Slots reachable from old root must exist in tree");
}
(removed, pruned)
}

pub fn add_root_parent(&mut self, root_parent: SlotHashKey) {
assert!(root_parent.0 < self.tree_root.0);
assert!(self.fork_infos.get(&root_parent).is_none());
Expand Down
Loading

0 comments on commit 6a1d676

Please sign in to comment.