Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid explosion of disordering blocks based on BLOCK_DOWNLOAD_WINDOW #2002

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions sync/src/synchronizer/block_fetcher.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down