Unify major syncing detection (#2699)
* simplify major sync detection * fix typos * fix merge * more realistic EthTester * add new synced state
This commit is contained in:
@@ -24,7 +24,7 @@ use std::thread;
|
||||
use std::time::{Instant, Duration};
|
||||
use std::sync::{Arc, Weak};
|
||||
use time::get_time;
|
||||
use ethsync::{SyncProvider, SyncState};
|
||||
use ethsync::{SyncProvider};
|
||||
use ethcore::miner::{MinerService, ExternalMinerService};
|
||||
use jsonrpc_core::*;
|
||||
use util::{H256, Address, FixedHash, U256, H64, Uint};
|
||||
@@ -253,26 +253,18 @@ 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();
|
||||
match status.state {
|
||||
SyncState::Idle => Ok(SyncStatus::None),
|
||||
SyncState::Waiting | SyncState::Blocks | SyncState::NewBlocks
|
||||
| SyncState::SnapshotManifest | SyncState::SnapshotData | SyncState::SnapshotWaiting => {
|
||||
let current_block = U256::from(take_weak!(self.client).chain_info().best_block_number);
|
||||
let highest_block = U256::from(status.highest_block_number.unwrap_or(status.start_block_number));
|
||||
|
||||
if highest_block > current_block + U256::from(6) {
|
||||
let info = SyncInfo {
|
||||
starting_block: status.start_block_number.into(),
|
||||
current_block: current_block.into(),
|
||||
highest_block: highest_block.into(),
|
||||
};
|
||||
Ok(SyncStatus::Info(info))
|
||||
} else {
|
||||
Ok(SyncStatus::None)
|
||||
}
|
||||
}
|
||||
if status.is_major_syncing() {
|
||||
let current_block = U256::from(take_weak!(self.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(),
|
||||
current_block: current_block.into(),
|
||||
highest_block: highest_block.into(),
|
||||
};
|
||||
Ok(SyncStatus::Info(info))
|
||||
} else {
|
||||
Ok(SyncStatus::None)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,13 @@ impl TestSyncProvider {
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Simulate importing blocks.
|
||||
pub fn increase_imported_block_number(&self, count: u64) {
|
||||
let mut status = self.status.write();
|
||||
let current_number = status.last_imported_block_number.unwrap_or(0);
|
||||
status.last_imported_block_number = Some(current_number + count);
|
||||
}
|
||||
}
|
||||
|
||||
impl SyncProvider for TestSyncProvider {
|
||||
|
||||
@@ -92,6 +92,11 @@ impl EthTester {
|
||||
hashrates: hashrates,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_blocks(&self, count: usize, with: EachBlockWith) {
|
||||
self.client.add_blocks(count, with);
|
||||
self.sync.increase_imported_block_number(count as u64);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -115,24 +120,21 @@ fn rpc_eth_syncing() {
|
||||
let mut status = tester.sync.status.write();
|
||||
status.state = SyncState::Blocks;
|
||||
status.highest_block_number = Some(2500);
|
||||
|
||||
// "sync" to 1000 blocks.
|
||||
// causes TestBlockChainClient to return 1000 for its best block number.
|
||||
let mut blocks = tester.client.blocks.write();
|
||||
for i in 0..1000 {
|
||||
blocks.insert(H256::from(i), Vec::new());
|
||||
}
|
||||
}
|
||||
|
||||
// "sync" to 1000 blocks.
|
||||
// causes TestBlockChainClient to return 1000 for its best block number.
|
||||
tester.add_blocks(1000, EachBlockWith::Nothing);
|
||||
|
||||
let true_res = r#"{"jsonrpc":"2.0","result":{"currentBlock":"0x3e8","highestBlock":"0x9c4","startingBlock":"0x0"},"id":1}"#;
|
||||
assert_eq!(tester.io.handle_request_sync(request), Some(true_res.to_owned()));
|
||||
|
||||
// finish "syncing"
|
||||
tester.add_blocks(1500, EachBlockWith::Nothing);
|
||||
|
||||
{
|
||||
// finish "syncing"
|
||||
let mut blocks = tester.client.blocks.write();
|
||||
for i in 0..1500 {
|
||||
blocks.insert(H256::from(i + 1000), Vec::new());
|
||||
}
|
||||
let mut status = tester.sync.status.write();
|
||||
status.state = SyncState::Idle;
|
||||
}
|
||||
|
||||
assert_eq!(tester.io.handle_request_sync(request), Some(false_res.to_owned()));
|
||||
|
||||
Reference in New Issue
Block a user