From ab32d13da12a889ffa202720fe16a06b4fb4574c Mon Sep 17 00:00:00 2001 From: carllin Date: Fri, 26 Jun 2020 01:09:04 -0700 Subject: [PATCH] Add debugging (#10820) Co-authored-by: Carl --- core/src/heaviest_subtree_fork_choice.rs | 29 +++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/core/src/heaviest_subtree_fork_choice.rs b/core/src/heaviest_subtree_fork_choice.rs index 8856903e4e..dd06871640 100644 --- a/core/src/heaviest_subtree_fork_choice.rs +++ b/core/src/heaviest_subtree_fork_choice.rs @@ -11,13 +11,15 @@ use solana_sdk::{ pubkey::Pubkey, }; use std::{ - collections::{BTreeMap, HashMap, HashSet}, + collections::{BTreeMap, HashMap, HashSet, VecDeque}, sync::{Arc, RwLock}, + time::Instant, }; #[cfg(test)] use trees::{Tree, TreeWalk}; pub type ForkWeight = u64; +const MAX_ROOT_PRINT_SECONDS: u64 = 30; #[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord)] enum UpdateLabel { @@ -56,18 +58,22 @@ struct ForkInfo { children: Vec, } -#[derive(Default)] pub struct HeaviestSubtreeForkChoice { fork_infos: HashMap, latest_votes: HashMap, root: Slot, + last_root_time: Instant, } impl HeaviestSubtreeForkChoice { pub(crate) fn new(root: Slot) -> Self { let mut heaviest_subtree_fork_choice = Self { root, - ..HeaviestSubtreeForkChoice::default() + // Doesn't implement default because `root` must + // exist in all the fields + fork_infos: HashMap::new(), + latest_votes: HashMap::new(), + last_root_time: Instant::now(), }; heaviest_subtree_fork_choice.add_new_leaf_slot(root, None); heaviest_subtree_fork_choice @@ -154,6 +160,7 @@ impl HeaviestSubtreeForkChoice { } pub fn set_root(&mut self, root: Slot) { + self.last_root_time = Instant::now(); self.root = root; let mut pending_slots = vec![root]; let mut new_fork_infos = HashMap::new(); @@ -177,6 +184,11 @@ impl HeaviestSubtreeForkChoice { } pub fn add_new_leaf_slot(&mut self, slot: Slot, parent: Option) { + if self.last_root_time.elapsed().as_secs() > MAX_ROOT_PRINT_SECONDS { + self.print_state(); + self.last_root_time = Instant::now(); + } + self.fork_infos .entry(slot) .and_modify(|slot_info| slot_info.parent = parent) @@ -402,6 +414,17 @@ impl HeaviestSubtreeForkChoice { .unwrap_or(None) } + fn print_state(&self) { + let best_slot = self.best_overall_slot(); + let mut best_path: VecDeque<_> = self.ancestor_iterator(best_slot).collect(); + best_path.push_front(best_slot); + info!( + "Latest known votes by vote pubkey: {:#?}, best path: {:?}", + self.latest_votes, + best_path.iter().rev() + ); + } + #[cfg(test)] fn stake_voted_at(&self, slot: Slot) -> Option { self.fork_infos