From a4e0b5c56a19988ee5a6b83ca0a206ad55be301a Mon Sep 17 00:00:00 2001 From: AntiochP <30642645+antiochp@users.noreply.github.com> Date: Thu, 7 Dec 2017 21:11:44 -0500 Subject: [PATCH] base threshold for sync on difficulty of past 5 blocks (#433) * base threshold for sync on difficulty of past 5 blocks * cleanup threshold calc filter_map + fold --- grin/src/adapters.rs | 45 +++++++++++++++++++++++++++++--------------- grin/src/sync.rs | 3 +++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/grin/src/adapters.rs b/grin/src/adapters.rs index e6818be1..9e0cd328 100644 --- a/grin/src/adapters.rs +++ b/grin/src/adapters.rs @@ -287,28 +287,43 @@ impl NetToChainAdapter { /// just receiving blocks through gossip. pub fn is_syncing(&self) -> bool { let local_diff = self.total_difficulty(); - let peers = self.p2p_server.borrow().connected_peers(); + let peer = self.p2p_server.borrow().most_work_peer(); // if we're already syncing, we're caught up if no peer has a higher // difficulty than us if self.syncing.load(Ordering::Relaxed) { - let higher_diff = peers.iter().any(|p| { - let p = p.read().unwrap(); - p.info.total_difficulty > local_diff - }); - if !higher_diff { - info!(LOGGER, "sync: caught up on the most worked chain, disabling sync"); + if let Some(peer) = peer { + if let Ok(peer) = peer.try_read() { + if peer.info.total_difficulty <= local_diff { + info!(LOGGER, "sync: caught up on most worked chain, disabling sync"); + self.syncing.store(false, Ordering::Relaxed); + } + } + } else { + info!(LOGGER, "sync: no peers available, disabling sync"); self.syncing.store(false, Ordering::Relaxed); } } else { - // if we're not syncing, we need to if our difficulty is much too low - let higher_diff_padded = peers.iter().any(|p| { - let p = p.read().unwrap(); - p.info.total_difficulty > local_diff.clone() + Difficulty::from_num(1000) - }); - if higher_diff_padded { - info!(LOGGER, "sync: late on the most worked chain, enabling sync"); - self.syncing.store(true, Ordering::Relaxed); + if let Some(peer) = peer { + if let Ok(peer) = peer.try_read() { + // sum the last 5 difficulties to give us the threshold + let threshold = self.chain + .difficulty_iter() + .filter_map(|x| x.map(|(_, x)| x).ok()) + .take(5) + .fold(Difficulty::zero(), |sum, val| sum + val); + + if peer.info.total_difficulty > local_diff.clone() + threshold.clone() { + info!( + LOGGER, + "sync: total_difficulty {}, peer_difficulty {}, threshold {} (last 5 blocks), enabling sync", + local_diff, + peer.info.total_difficulty, + threshold, + ); + self.syncing.store(true, Ordering::Relaxed); + } + } } } self.syncing.load(Ordering::Relaxed) diff --git a/grin/src/sync.rs b/grin/src/sync.rs index 4e0963ee..4dac6ad8 100644 --- a/grin/src/sync.rs +++ b/grin/src/sync.rs @@ -39,6 +39,9 @@ pub fn run_sync( let mut prev_body_sync = time::now_utc(); let mut prev_header_sync = prev_body_sync.clone(); + // initial sleep to give us time to peer with some nodes + thread::sleep(Duration::from_secs(30)); + loop { if a_inner.is_syncing() { let current_time = time::now_utc();