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
This commit is contained in:
AntiochP 2017-12-07 21:11:44 -05:00 committed by GitHub
parent 10030a224a
commit a4e0b5c56a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 15 deletions

View file

@ -287,28 +287,43 @@ impl NetToChainAdapter {
/// just receiving blocks through gossip. /// just receiving blocks through gossip.
pub fn is_syncing(&self) -> bool { pub fn is_syncing(&self) -> bool {
let local_diff = self.total_difficulty(); 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 // if we're already syncing, we're caught up if no peer has a higher
// difficulty than us // difficulty than us
if self.syncing.load(Ordering::Relaxed) { if self.syncing.load(Ordering::Relaxed) {
let higher_diff = peers.iter().any(|p| { if let Some(peer) = peer {
let p = p.read().unwrap(); if let Ok(peer) = peer.try_read() {
p.info.total_difficulty > local_diff if peer.info.total_difficulty <= local_diff {
}); info!(LOGGER, "sync: caught up on most worked chain, disabling sync");
if !higher_diff { self.syncing.store(false, Ordering::Relaxed);
info!(LOGGER, "sync: caught up on the most worked chain, disabling sync"); }
}
} else {
info!(LOGGER, "sync: no peers available, disabling sync");
self.syncing.store(false, Ordering::Relaxed); self.syncing.store(false, Ordering::Relaxed);
} }
} else { } else {
// if we're not syncing, we need to if our difficulty is much too low if let Some(peer) = peer {
let higher_diff_padded = peers.iter().any(|p| { if let Ok(peer) = peer.try_read() {
let p = p.read().unwrap(); // sum the last 5 difficulties to give us the threshold
p.info.total_difficulty > local_diff.clone() + Difficulty::from_num(1000) let threshold = self.chain
}); .difficulty_iter()
if higher_diff_padded { .filter_map(|x| x.map(|(_, x)| x).ok())
info!(LOGGER, "sync: late on the most worked chain, enabling sync"); .take(5)
self.syncing.store(true, Ordering::Relaxed); .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) self.syncing.load(Ordering::Relaxed)

View file

@ -39,6 +39,9 @@ pub fn run_sync(
let mut prev_body_sync = time::now_utc(); let mut prev_body_sync = time::now_utc();
let mut prev_header_sync = prev_body_sync.clone(); 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 { loop {
if a_inner.is_syncing() { if a_inner.is_syncing() {
let current_time = time::now_utc(); let current_time = time::now_utc();