(cherry picked from commit dc7030ffaa
)
Co-authored-by: carllin <carl@solana.com>
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
use solana_sdk::clock::Slot;
|
||||
use std::collections::HashSet;
|
||||
use std::{collections::HashSet, hash::Hash};
|
||||
|
||||
pub trait TreeDiff {
|
||||
fn children(&self, slot: Slot) -> Option<&[Slot]>;
|
||||
type TreeKey: Hash + PartialEq + Eq + Copy;
|
||||
fn children(&self, key: &Self::TreeKey) -> Option<&[Self::TreeKey]>;
|
||||
|
||||
fn contains_slot(&self, slot: Slot) -> bool;
|
||||
fn contains_slot(&self, slot: &Self::TreeKey) -> 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) {
|
||||
fn subtree_diff(&self, root1: Self::TreeKey, root2: Self::TreeKey) -> HashSet<Self::TreeKey> {
|
||||
if !self.contains_slot(&root1) {
|
||||
return HashSet::new();
|
||||
}
|
||||
let mut pending_slots = vec![root1];
|
||||
let mut pending_keys = vec![root1];
|
||||
let mut reachable_set = HashSet::new();
|
||||
while !pending_slots.is_empty() {
|
||||
let current_slot = pending_slots.pop().unwrap();
|
||||
if current_slot == root2 {
|
||||
while !pending_keys.is_empty() {
|
||||
let current_key = pending_keys.pop().unwrap();
|
||||
if current_key == root2 {
|
||||
continue;
|
||||
}
|
||||
reachable_set.insert(current_slot);
|
||||
for child in self
|
||||
.children(current_slot)
|
||||
.children(¤t_key)
|
||||
.expect("slot was discovered earlier, must exist")
|
||||
{
|
||||
pending_slots.push(*child);
|
||||
pending_keys.push(*child);
|
||||
}
|
||||
reachable_set.insert(current_key);
|
||||
}
|
||||
|
||||
reachable_set
|
||||
|
Reference in New Issue
Block a user