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.
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)

View file

@ -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();