Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Resilient warp sync (#5018)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar authored and gavofyork committed Mar 24, 2017
1 parent 9efab78 commit 3b54b49
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
33 changes: 19 additions & 14 deletions sync/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ const HEADERS_TIMEOUT_SEC: u64 = 15;
const BODIES_TIMEOUT_SEC: u64 = 10;
const RECEIPTS_TIMEOUT_SEC: u64 = 10;
const FORK_HEADER_TIMEOUT_SEC: u64 = 3;
const SNAPSHOT_MANIFEST_TIMEOUT_SEC: u64 = 3;
const SNAPSHOT_DATA_TIMEOUT_SEC: u64 = 60;
const SNAPSHOT_MANIFEST_TIMEOUT_SEC: u64 = 5;
const SNAPSHOT_DATA_TIMEOUT_SEC: u64 = 120;

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
/// Sync state
Expand Down Expand Up @@ -463,12 +463,7 @@ impl ChainSync {
/// Reset sync. Clear all downloaded data but keep the queue
fn reset(&mut self, io: &mut SyncIo) {
self.new_blocks.reset();
self.snapshot.clear();
let chain_info = io.chain().chain_info();
if self.state == SyncState::SnapshotData {
debug!(target:"sync", "Aborting snapshot restore");
io.snapshot_service().abort_restore();
}
for (_, ref mut p) in &mut self.peers {
if p.block_set != Some(BlockSet::OldBlocks) {
p.reset_asking();
Expand All @@ -487,6 +482,11 @@ impl ChainSync {
/// Restart sync
pub fn reset_and_continue(&mut self, io: &mut SyncIo) {
trace!(target: "sync", "Restarting");
if self.state == SyncState::SnapshotData {
debug!(target:"sync", "Aborting snapshot restore");
io.snapshot_service().abort_restore();
}
self.snapshot.clear();
self.reset(io);
self.continue_sync(io);
}
Expand All @@ -499,7 +499,7 @@ impl ChainSync {
}

fn maybe_start_snapshot_sync(&mut self, io: &mut SyncIo) {
if self.state != SyncState::WaitingPeers {
if self.state != SyncState::WaitingPeers && self.state != SyncState::Blocks && self.state != SyncState::Waiting {
return;
}
// Make sure the snapshot block is not too far away from best block and network best block and
Expand Down Expand Up @@ -531,7 +531,7 @@ impl ChainSync {
(best_hash, max_peers, snapshot_peers)
};

let timeout = self.sync_start_time.map_or(false, |t| ((time::precise_time_ns() - t) / 1_000_000_000) > WAIT_PEERS_TIMEOUT_SEC);
let timeout = (self.state == SyncState::WaitingPeers) && self.sync_start_time.map_or(false, |t| ((time::precise_time_ns() - t) / 1_000_000_000) > WAIT_PEERS_TIMEOUT_SEC);

if let (Some(hash), Some(peers)) = (best_hash, best_hash.map_or(None, |h| snapshot_peers.get(&h))) {
if max_peers >= SNAPSHOT_MIN_PEERS {
Expand All @@ -549,13 +549,18 @@ impl ChainSync {
}

fn start_snapshot_sync(&mut self, io: &mut SyncIo, peers: &[PeerId]) {
self.snapshot.clear();
for p in peers {
if self.peers.get(p).map_or(false, |p| p.asking == PeerAsking::Nothing) {
self.request_snapshot_manifest(io, *p);
if !self.snapshot.have_manifest() {
for p in peers {
if self.peers.get(p).map_or(false, |p| p.asking == PeerAsking::Nothing) {
self.request_snapshot_manifest(io, *p);
}
}
self.state = SyncState::SnapshotManifest;
trace!(target: "sync", "New snapshot sync with {:?}", peers);
} else {
self.state = SyncState::SnapshotData;
trace!(target: "sync", "Resumed snapshot sync with {:?}", peers);
}
self.state = SyncState::SnapshotManifest;
}

/// Restart sync disregarding the block queue status. May end up re-downloading up to QUEUE_SIZE blocks
Expand Down
5 changes: 5 additions & 0 deletions sync/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl Snapshot {
self.snapshot_hash = None;
}

/// Check if currently downloading a snapshot.
pub fn have_manifest(&self) -> bool {
self.snapshot_hash.is_some()
}

/// Reset collection for a manifest RLP
pub fn reset_to(&mut self, manifest: &ManifestData, hash: &H256) {
self.clear();
Expand Down
4 changes: 2 additions & 2 deletions util/network/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use stats::NetworkStats;
use time;

// Timeout must be less than (interval - 1).
const PING_TIMEOUT_SEC: u64 = 15;
const PING_INTERVAL_SEC: u64 = 30;
const PING_TIMEOUT_SEC: u64 = 60;
const PING_INTERVAL_SEC: u64 = 120;

#[derive(Debug, Clone)]
enum ProtocolState {
Expand Down

0 comments on commit 3b54b49

Please sign in to comment.