Skip to content

Commit

Permalink
fix: fix calculation of headers sync timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Dec 19, 2018
1 parent 1095fd1 commit 06a5e29
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ impl<CI: ChainIndex> Synchronizer<CI> {
now_ms().saturating_sub(self.shared.tip_header().read().inner().timestamp()) > MAX_TIP_AGE
}

pub fn get_headers_sync_timeout(&self, header: &Header) -> u64 {
HEADERS_DOWNLOAD_TIMEOUT_BASE
pub fn predict_headers_sync_time(&self, header: &Header) -> u64 {
let now = now_ms();
now + HEADERS_DOWNLOAD_TIMEOUT_BASE
+ HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
* (now_ms().saturating_sub(header.timestamp()) / POW_SPACE)
* (now.saturating_sub(header.timestamp()) / POW_SPACE)
}

pub fn mark_block_stored(&self, hash: H256) {
Expand Down Expand Up @@ -480,7 +481,7 @@ impl<CI: ChainIndex> Synchronizer<CI> {

fn on_connected(&self, nc: &CKBProtocolContext, peer: PeerIndex) {
let tip = self.tip_header();
let timeout = self.get_headers_sync_timeout(&tip);
let predicted_headers_sync_time = self.predict_headers_sync_time(&tip);

let protect_outbound = is_outbound(nc, peer).unwrap_or_else(|| false)
&& self.outbound_peers_with_protect.load(Ordering::Acquire)
Expand All @@ -491,7 +492,8 @@ impl<CI: ChainIndex> Synchronizer<CI> {
.fetch_add(1, Ordering::Release);
}

self.peers.on_connected(peer, timeout, protect_outbound);
self.peers
.on_connected(peer, predicted_headers_sync_time, protect_outbound);
}

pub fn send_getheaders_to_peer(
Expand Down
6 changes: 3 additions & 3 deletions sync/src/synchronizer/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ impl Peers {
.or_insert_with(|| score);
}

pub fn on_connected(&self, peer: PeerIndex, headers_sync_timeout: u64, protect: bool) {
pub fn on_connected(&self, peer: PeerIndex, predicted_headers_sync_time: u64, protect: bool) {
self.state
.write()
.entry(peer)
.and_modify(|state| {
state.headers_sync_timeout = Some(headers_sync_timeout);
state.headers_sync_timeout = Some(predicted_headers_sync_time);
state.chain_sync.protect = protect;
})
.or_insert_with(|| {
Expand All @@ -138,7 +138,7 @@ impl Peers {
negotiate: Negotiate::default(),
sync_started: false,
last_block_announcement: None,
headers_sync_timeout: Some(headers_sync_timeout),
headers_sync_timeout: Some(predicted_headers_sync_time),
disconnect: false,
chain_sync,
}
Expand Down

0 comments on commit 06a5e29

Please sign in to comment.