1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use solana_sdk::clock::Slot;
use std::collections::HashSet;

pub trait TreeDiff {
    fn children(&self, slot: Slot) -> Option<&[Slot]>;

    fn contains_slot(&self, slot: Slot) -> bool;

    // Find all nodes reachable from `root1`, excluding subtree at `root2`
    fn subtree_diff(&self, root1: Slot, root2: Slot) -> HashSet<Slot> {
        if !self.contains_slot(root1) {
            return HashSet::new();
        }
        let mut pending_slots = vec![root1];
        let mut reachable_set = HashSet::new();
        while !pending_slots.is_empty() {
            let current_slot = pending_slots.pop().unwrap();
            if current_slot == root2 {
                continue;
            }
            reachable_set.insert(current_slot);
            for child in self
                .children(current_slot)
                .expect("slot was discovered earlier, must exist")
            {
                pending_slots.push(*child);
            }
        }

        reachable_set
    }
}