Skip to content

Commit

Permalink
Use number of downloaded blocks for test (paritytech#6234)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar authored Jun 4, 2020
1 parent e0e8501 commit 6745240
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
6 changes: 3 additions & 3 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,9 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
self.sync.status().queued_blocks
}

/// Number of processed blocks.
pub fn num_processed_blocks(&self) -> usize {
self.sync.num_processed_blocks()
/// Number of downloaded blocks.
pub fn num_downloaded_blocks(&self) -> usize {
self.sync.num_downloaded_blocks()
}

/// Number of active sync requests.
Expand Down
16 changes: 7 additions & 9 deletions client/network/src/protocol/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ pub struct ChainSync<B: BlockT> {
block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
/// Maximum number of peers to ask the same blocks in parallel.
max_parallel_downloads: u32,
/// Total number of processed blocks (imported or failed).
processed_blocks: usize,
/// Total number of downloaded blocks.
downloaded_blocks: usize,
}

/// All the data we have about a Peer that we are trying to sync with
Expand Down Expand Up @@ -375,7 +375,7 @@ impl<B: BlockT> ChainSync<B> {
pending_requests: Default::default(),
block_announce_validator,
max_parallel_downloads,
processed_blocks: 0,
downloaded_blocks: 0,
}
}

Expand Down Expand Up @@ -415,9 +415,9 @@ impl<B: BlockT> ChainSync<B> {
self.fork_targets.len()
}

/// Number of processed blocks.
pub fn num_processed_blocks(&self) -> usize {
self.processed_blocks
/// Number of downloaded blocks.
pub fn num_downloaded_blocks(&self) -> usize {
self.downloaded_blocks
}

/// Handle a new connected peer.
Expand Down Expand Up @@ -719,6 +719,7 @@ impl<B: BlockT> ChainSync<B> {
request: Option<BlockRequest<B>>,
response: BlockResponse<B>
) -> Result<OnBlockData<B>, BadPeer> {
self.downloaded_blocks += response.blocks.len();
let mut new_blocks: Vec<IncomingBlock<B>> =
if let Some(peer) = self.peers.get_mut(who) {
let mut blocks = response.blocks;
Expand Down Expand Up @@ -1004,8 +1005,6 @@ impl<B: BlockT> ChainSync<B> {
for (_, hash) in &results {
self.queue_blocks.remove(&hash);
}
self.processed_blocks += results.len();

for (result, hash) in results {
if has_error {
continue;
Expand Down Expand Up @@ -1274,7 +1273,6 @@ impl<B: BlockT> ChainSync<B> {

/// Restart the sync process.
fn restart<'a>(&'a mut self) -> impl Iterator<Item = Result<(PeerId, BlockRequest<B>), BadPeer>> + 'a {
self.processed_blocks = 0;
self.blocks.clear();
let info = self.client.info();
self.best_queued_hash = info.best_hash;
Expand Down
6 changes: 3 additions & 3 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
self.network_service.user_protocol().num_queued_blocks()
}

/// Returns the number of processed blocks.
pub fn num_processed_blocks(&self) -> usize {
self.network_service.user_protocol().num_processed_blocks()
/// Returns the number of downloaded blocks.
pub fn num_downloaded_blocks(&self) -> usize {
self.network_service.user_protocol().num_downloaded_blocks()
}

/// Number of active sync requests.
Expand Down
6 changes: 3 additions & 3 deletions client/network/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ impl<D> Peer<D> {
self.network.num_connected_peers()
}

/// Returns the number of processed blocks.
pub fn num_processed_blocks(&self) -> usize {
self.network.num_processed_blocks()
/// Returns the number of downloaded blocks.
pub fn num_downloaded_blocks(&self) -> usize {
self.network.num_downloaded_blocks()
}

/// Returns true if we have no peer.
Expand Down
4 changes: 2 additions & 2 deletions client/network/test/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,12 @@ fn imports_stale_once() {
// check that NEW block is imported from announce message
let new_hash = net.peer(0).push_blocks(1, false);
import_with_announce(&mut net, new_hash);
assert_eq!(net.peer(1).num_processed_blocks(), 1);
assert_eq!(net.peer(1).num_downloaded_blocks(), 1);

// check that KNOWN STALE block is imported from announce message
let known_stale_hash = net.peer(0).push_blocks_at(BlockId::Number(0), 1, true);
import_with_announce(&mut net, known_stale_hash);
assert_eq!(net.peer(1).num_processed_blocks(), 2);
assert_eq!(net.peer(1).num_downloaded_blocks(), 2);
}

#[test]
Expand Down

0 comments on commit 6745240

Please sign in to comment.