Refactor and make is_syncing.

This commit is contained in:
Gav Wood 2016-12-16 10:31:10 +01:00
parent 35b18485d4
commit 396ab5b361
No known key found for this signature in database
GPG Key ID: C49C1ACA1CC9B252
3 changed files with 34 additions and 5 deletions

View File

@ -312,7 +312,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R
}
// the updater service
let updater = Updater::new(Arc::downgrade(&(service.client() as Arc<BlockChainClient>)), update_policy);
let updater = Updater::new(Arc::downgrade(&(service.client() as Arc<BlockChainClient>)), Arc::downgrade(&sync_provider), update_policy);
service.add_notify(updater.clone());
// set up dependencies for rpc servers

View File

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

View File

@ -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<BlockChainClient>, sync: Arc<SyncProvider>, update_policy: UpdatePolicy) -> Arc<Self> {
pub fn new(client: Weak<BlockChainClient>, sync: Weak<SyncProvider>, update_policy: UpdatePolicy) -> Arc<Self> {
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<H256>, _invalid: Vec<H256>, _enacted: Vec<H256>, _retracted: Vec<H256>, _sealed: Vec<H256>, _proposed: Vec<Bytes>, _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(),
_ => {},
}
}