From f8a35817fb6bda962a7114061a9800943facbfef Mon Sep 17 00:00:00 2001 From: driftluo Date: Mon, 24 Jun 2019 20:04:05 +0800 Subject: [PATCH] fix: fix sync logic 1. fix protect node count 2. fix header sync eviction condition --- sync/src/synchronizer/mod.rs | 25 ++++++++++++++++++++----- sync/src/types.rs | 4 ++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 1c0ea9fbe0..9618525163 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -285,17 +285,21 @@ impl Synchronizer { // reached, disconnect. pub fn eviction(&self, nc: &CKBProtocolContext) { let mut peer_states = self.peers().state.write(); - let is_initial_block_download = self.shared.is_initial_block_download(); + let is_initial_header_sync = self.shared.is_initial_header_sync(); let mut eviction = Vec::new(); for (peer, state) in peer_states.iter_mut() { let now = unix_time_as_millis(); // headers_sync_timeout if let Some(timeout) = state.headers_sync_timeout { - if now > timeout && is_initial_block_download && !state.disconnect { - eviction.push(*peer); - state.disconnect = true; - continue; + if is_initial_header_sync { + if now > timeout && !state.disconnect { + eviction.push(*peer); + state.disconnect = true; + continue; + } + } else { + state.headers_sync_timeout = None } } @@ -382,6 +386,10 @@ impl Synchronizer { .cloned() .collect(); + if peers.is_empty() { + return; + } + let tip = { let (header, total_difficulty) = { let chain_state = self.shared.lock_chain_state(); @@ -529,6 +537,13 @@ impl CKBProtocolHandler for Synchronizer { { panic!("Synchronizer n_sync overflow"); } + + // Protection node disconnected + if peer_state.chain_sync.protect { + self.shared() + .n_protected_outbound_peers() + .fetch_sub(1, Ordering::Release); + } } } diff --git a/sync/src/types.rs b/sync/src/types.rs index 44ef24a329..27c821fd37 100644 --- a/sync/src/types.rs +++ b/sync/src/types.rs @@ -751,6 +751,10 @@ impl SyncSharedState { } } + pub fn is_initial_header_sync(&self) -> bool { + unix_time_as_millis().saturating_sub(self.shared_best_header().timestamp()) > MAX_TIP_AGE + } + pub fn shared_best_header(&self) -> HeaderView { self.shared_best_header.read().to_owned() }