Check queue to determine major importing (#2763)
* simplify major sync detection * fix typos * fix merge * more realistic EthTester * add new synced state * remove Blocks synced state * move is_major_importing to rpc crate and check queue * add tests
This commit is contained in:
@@ -56,6 +56,7 @@ use self::jsonrpc_core::{IoHandler, IoDelegate};
|
||||
pub use jsonrpc_http_server::{ServerBuilder, Server, RpcServerError};
|
||||
pub mod v1;
|
||||
pub use v1::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings};
|
||||
pub use v1::block_import::is_major_importing;
|
||||
|
||||
/// An object that can be extended with `IoDelegates`
|
||||
pub trait Extendable {
|
||||
|
||||
60
rpc/src/v1/helpers/block_import.rs
Normal file
60
rpc/src/v1/helpers/block_import.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Block import analysis functions.
|
||||
|
||||
use ethcore::client::BlockQueueInfo;
|
||||
use ethsync::SyncState;
|
||||
|
||||
/// Check if client is during major sync or during block import.
|
||||
pub fn is_major_importing(sync_state: Option<SyncState>, queue_info: BlockQueueInfo) -> bool {
|
||||
let is_syncing_state = sync_state.map_or(false, |s|
|
||||
s != SyncState::Idle && s != SyncState::NewBlocks
|
||||
);
|
||||
let is_verifying = queue_info.unverified_queue_size + queue_info.verified_queue_size > 3;
|
||||
is_verifying || is_syncing_state
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ethcore::client::BlockQueueInfo;
|
||||
use ethsync::SyncState;
|
||||
use super::is_major_importing;
|
||||
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ pub mod errors;
|
||||
|
||||
pub mod dispatch;
|
||||
pub mod params;
|
||||
pub mod block_import;
|
||||
|
||||
mod poll_manager;
|
||||
mod poll_filter;
|
||||
|
||||
@@ -49,6 +49,7 @@ use v1::types::{
|
||||
};
|
||||
use v1::helpers::{CallRequest as CRequest, errors, limit_logs};
|
||||
use v1::helpers::dispatch::{default_gas_price, dispatch_transaction};
|
||||
use v1::helpers::block_import::is_major_importing;
|
||||
use v1::helpers::auto_args::Trailing;
|
||||
|
||||
/// Eth RPC options
|
||||
@@ -254,8 +255,9 @@ impl<C, S: ?Sized, M, EM> Eth for EthClient<C, S, M, EM> where
|
||||
fn syncing(&self) -> Result<SyncStatus, Error> {
|
||||
try!(self.active());
|
||||
let status = take_weak!(self.sync).status();
|
||||
if status.is_major_syncing() {
|
||||
let current_block = U256::from(take_weak!(self.client).chain_info().best_block_number);
|
||||
let client = take_weak!(self.client);
|
||||
if is_major_importing(Some(status.state), client.queue_info()) {
|
||||
let current_block = U256::from(client.chain_info().best_block_number);
|
||||
let highest_block = U256::from(status.highest_block_number.unwrap_or(status.start_block_number));
|
||||
let info = SyncInfo {
|
||||
starting_block: status.start_block_number.into(),
|
||||
|
||||
@@ -28,4 +28,4 @@ pub mod types;
|
||||
|
||||
pub use self::traits::{Web3, Eth, EthFilter, EthSigning, Personal, PersonalSigner, Net, Ethcore, EthcoreSet, Traces, Rpc};
|
||||
pub use self::impls::*;
|
||||
pub use self::helpers::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings};
|
||||
pub use self::helpers::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, block_import};
|
||||
|
||||
Reference in New Issue
Block a user