From 396ab5b361d80f5e8af39d1908652207886ce5af Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 16 Dec 2016 10:31:10 +0100 Subject: [PATCH] Refactor and make is_syncing. --- parity/run.rs | 2 +- sync/src/chain.rs | 32 +++++++++++++++++++++++++++++++- updater/src/updater.rs | 5 ++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/parity/run.rs b/parity/run.rs index 77d1bf1e7..23eebe183 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -312,7 +312,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R } // the updater service - let updater = Updater::new(Arc::downgrade(&(service.client() as Arc)), update_policy); + let updater = Updater::new(Arc::downgrade(&(service.client() as Arc)), Arc::downgrade(&sync_provider), update_policy); service.add_notify(updater.clone()); // set up dependencies for rpc servers diff --git a/sync/src/chain.rs b/sync/src/chain.rs index d3638f35d..5f969af35 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -94,7 +94,7 @@ use rlp::*; use network::*; use ethcore::views::{HeaderView}; use ethcore::header::{BlockNumber, Header as BlockHeader}; -use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo, BlockImportError}; +use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo, BlockImportError, BlockQueueInfo}; use ethcore::error::*; use ethcore::snapshot::{ManifestData, RestorationStatus}; use sync_io::SyncIo; @@ -235,6 +235,13 @@ impl SyncStatus { min_peers } } + + /// Is it doing a major sync? + pub fn is_syncing(&self, queue_info: BlockQueueInfo) -> bool { + let is_syncing_state = match self.state { SyncState::Idle | SyncState::NewBlocks => false, _ => true }; + let is_verifying = queue_info.unverified_queue_size + queue_info.verified_queue_size > 3; + is_verifying || is_syncing_state + } } #[derive(PartialEq, Eq, Debug, Clone)] @@ -2112,6 +2119,29 @@ mod tests { rlp.out() } + fn queue_info(unverified: usize, verified: usize) -> BlockQueueInfo { + BlockQueueInfo { + unverified_queue_size: unverified, + verified_queue_size: verified, + verifying_queue_size: 0, + max_queue_size: 1000, + max_mem_use: 1000, + mem_used: 500 + } + } + + #[test] + fn is_still_verifying() { + assert!(!is_major_importing(None, queue_info(2, 1))); + assert!(is_major_importing(None, queue_info(2, 2))); + } + + #[test] + fn is_synced_state() { + assert!(is_major_importing(Some(SyncState::Blocks), queue_info(0, 0))); + assert!(!is_major_importing(Some(SyncState::Idle), queue_info(0, 0))); + } + #[test] fn return_receipts_empty() { let mut client = TestBlockChainClient::new(); diff --git a/updater/src/updater.rs b/updater/src/updater.rs index a82748c84..9f781ed5a 100644 --- a/updater/src/updater.rs +++ b/updater/src/updater.rs @@ -27,7 +27,6 @@ use hash_fetch::{self as fetch, HashFetch}; use operations::Operations; use service::{Service}; use types::all::{ReleaseInfo, OperationsInfo, CapState}; -use ethcore_rpc::is_major_importing; /// Filter for releases. #[derive(Debug, Eq, PartialEq, Clone)] @@ -99,7 +98,7 @@ pub struct Updater { const CLIENT_ID: &'static str = "parity"; impl Updater { - pub fn new(client: Weak, sync: Arc, update_policy: UpdatePolicy) -> Arc { + pub fn new(client: Weak, sync: Weak, update_policy: UpdatePolicy) -> Arc { let r = Arc::new(Updater { update_policy: update_policy, weak_self: Mutex::new(Default::default()), @@ -295,7 +294,7 @@ impl Updater { impl ChainNotify for Updater { fn new_blocks(&self, _imported: Vec, _invalid: Vec, _enacted: Vec, _retracted: Vec, _sealed: Vec, _proposed: Vec, _duration: u64) { match (self.client.upgrade(), self.sync.upgrade()) { - (Some(c), Some(s)) if is_major_importing(s.status().state, c.queue_info()) => self.poll(), + (Some(ref c), Some(ref s)) if s.status().is_syncing(c.queue_info()) => self.poll(), _ => {}, } }