diff --git a/src/sync/chain.rs b/src/sync/chain.rs index d1d7df03b..41979293d 100644 --- a/src/sync/chain.rs +++ b/src/sync/chain.rs @@ -261,7 +261,8 @@ impl ChainSync { _ => { if self.have_common_block { //validate chain - if number == self.last_imported_block + 1 && info.parent_hash != self.last_imported_hash { + if self.have_common_block && number == self.last_imported_block + 1 && info.parent_hash != self.last_imported_hash { + // TODO: lower peer rating debug!(target: "sync", "Mismatched block header {} {}", number, info.hash()); continue; } @@ -423,10 +424,11 @@ impl ChainSync { self.send_status(io, peer); } - /// Resume downloading after witing state + /// Resume downloading fn continue_sync(&mut self, io: &mut SyncIo) { - let peers: Vec = self.peers.keys().map(|k| *k).collect(); - for p in peers { + let mut peers: Vec<(PeerId, U256)> = self.peers.iter().map(|(k, p)| (*k, p.difficulty)).collect(); + peers.sort_by(|&(_, d1), &(_, d2)| d1.cmp(&d2).reverse()); //TODO: sort by rating + for (p, _) in peers { self.sync_peer(io, &p, false); } } @@ -511,12 +513,14 @@ impl ChainSync { let mut start = 0usize; if !self.have_common_block { // download backwards until common block is found 1 header at a time - start = io.chain().info().last_block_number as usize; + let chain_info = io.chain().info(); + start = chain_info.last_block_number as usize; if !self.headers.is_empty() { start = min(start, self.headers.range_iter().next().unwrap().0 as usize - 1); } if start == 0 { self.have_common_block = true; //reached genesis + self.last_imported_hash = chain_info.genesis_hash; } } if self.have_common_block { diff --git a/src/sync/tests.rs b/src/sync/tests.rs index 6b2db9c9b..21ba006a6 100644 --- a/src/sync/tests.rs +++ b/src/sync/tests.rs @@ -298,6 +298,7 @@ impl TestNet { #[test] fn full_sync_two_peers() { + ::env_logger::init().ok(); let mut net = TestNet::new(3); net.peer_mut(1).chain.add_blocks(1000, false); net.peer_mut(2).chain.add_blocks(1000, false); @@ -308,6 +309,7 @@ fn full_sync_two_peers() { #[test] fn full_sync_empty_blocks() { + ::env_logger::init().ok(); let mut net = TestNet::new(3); for n in 0..200 { net.peer_mut(1).chain.add_blocks(5, n % 2 == 0);