diff --git a/sync/src/chain.rs b/sync/src/chain.rs index b9d0ccddf..5b4e97dc5 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1084,13 +1084,16 @@ impl ChainSync { let chain_info = chain.chain_info(); let latest_hash = chain_info.best_block_hash; let latest_number = chain_info.best_block_number; - self.peers.iter().filter(|&(peer_id, peer_info)| + self.peers.iter().filter(|&(_, peer_info)| match io.chain().block_status(&peer_info.latest) { - BlockStatus::InChain => peer_info.latest != latest_hash && latest_number - peer_info.latest_number < MAX_PEER_LAG_PROPAGATION, + BlockStatus::InChain => { + let peer_number = HeaderView::new(&io.chain().block_header(&peer_info.latest).unwrap()).number(); + peer_info.latest != latest_hash && latest_number > peer_number && latest_number - peer_number < MAX_PEER_LAG_PROPAGATION + }, _ => false }) - .map(|(peer_id, peer_info)| peer_id) + .map(|(peer_id, _)| peer_id) .cloned().collect::>() } @@ -1100,7 +1103,7 @@ impl ChainSync { // sqrt(x)/x scaled to max u32 let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32; - let mut lucky_peers = match lagging_peers.len() { + let lucky_peers = match lagging_peers.len() { 0 ... MIN_PEERS_PROPAGATION => lagging_peers, _ => lagging_peers.iter().filter(|_| ::rand::random::() < fraction).cloned().collect::>() }; @@ -1131,16 +1134,6 @@ impl ChainSync { trace!(target: "sync", "Sent new blocks to peers: {:?}", blocks_propagaded); } } - - #[cfg(test)] - pub fn get_peer_latet(&self, peer_id: usize) -> H256 { - self.peers[&peer_id].latest.clone() - } - - #[cfg(test)] - pub fn get_peer_latest_number(&self, peer_id: usize) -> BlockNumber { - self.peers[&peer_id].latest_number - } } #[cfg(test)] diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index 22c677aa0..a78af0ca6 100644 --- a/sync/src/tests/chain.rs +++ b/sync/src/tests/chain.rs @@ -136,8 +136,6 @@ fn propagade() { assert_eq!(1000, net.peer(1).chain.chain_info().best_block_number); assert_eq!(1000, net.peer(2).chain.chain_info().best_block_number); - assert_eq!(net.peer(0).sync.get_peer_latest_number(1), 1000); - net.sync_step_peer(0); // 2 peers to sync diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 2be501ebb..8fc9d8cf0 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -84,7 +84,6 @@ impl BlockChainClient for TestBlockChainClient { fn block_header(&self, h: &H256) -> Option { self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec()) - } fn block_body(&self, h: &H256) -> Option {