From 93eccea84c241e605dbfbd924bf163fde6414efa Mon Sep 17 00:00:00 2001 From: keroro520 Date: Wed, 8 Apr 2020 16:39:04 +0800 Subject: [PATCH] feat: avoid explosion of disordering blocks based on BLOCK_DOWNLOAD_WINDOW --- sync/src/lib.rs | 5 +++++ sync/src/synchronizer/block_fetcher.rs | 18 +++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 0a2243d3ed..07de21b279 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -65,6 +65,11 @@ pub const MAX_LOCATOR_SIZE: usize = 101; pub const BLOCK_DOWNLOAD_TIMEOUT: u64 = 30 * 1000; // 30s +// Size of the "block download window": how far ahead of our current height do we fetch? +// Larger windows tolerate larger download speed differences between peers, but increase the +// potential degree of disordering of blocks. +pub const BLOCK_DOWNLOAD_WINDOW: u64 = 1024 * 8; // 1024 * default_outbound_peers + pub const RETRY_ASK_TX_TIMEOUT_INCREASE: Duration = Duration::from_secs(30); // ban time diff --git a/sync/src/synchronizer/block_fetcher.rs b/sync/src/synchronizer/block_fetcher.rs index ea797bfab5..ff06032802 100644 --- a/sync/src/synchronizer/block_fetcher.rs +++ b/sync/src/synchronizer/block_fetcher.rs @@ -1,7 +1,7 @@ use crate::block_status::BlockStatus; use crate::synchronizer::Synchronizer; use crate::types::{ActiveChain, HeaderView, IBDState}; -use crate::MAX_BLOCKS_IN_TRANSIT_PER_PEER; +use crate::{BLOCK_DOWNLOAD_WINDOW, MAX_BLOCKS_IN_TRANSIT_PER_PEER}; use ckb_logger::{debug, trace}; use ckb_network::PeerIndex; use ckb_types::{core, packed}; @@ -126,16 +126,16 @@ impl BlockFetcher { debug_assert!(best_known_header.number() > fixed_last_common_header.number()); let mut inflight = self.synchronizer.shared().state().write_inflight_blocks(); - let count = - MAX_BLOCKS_IN_TRANSIT_PER_PEER.saturating_sub(inflight.peer_inflight_count(self.peer)); - let mut fetch = Vec::with_capacity(count); let mut start = fixed_last_common_header.number() + 1; + let end = min(best_known_header.number(), start + BLOCK_DOWNLOAD_WINDOW); + let n_fetch = min( + end.saturating_sub(start) as usize + 1, + MAX_BLOCKS_IN_TRANSIT_PER_PEER.saturating_sub(inflight.peer_inflight_count(self.peer)), + ); + let mut fetch = Vec::with_capacity(n_fetch); - while fetch.len() < count && start <= best_known_header.number() { - let span = min( - best_known_header.number() - start + 1, - (count - fetch.len()) as u64, - ); + while fetch.len() < n_fetch && start <= end { + let span = min(end - start + 1, (n_fetch - fetch.len()) as u64); // Iterate in range `[start, start+span)` and consider as the next to-fetch candidates. let mut header = self