From 0bb1a5878e10c037f01d67d205085f0e9e0154da Mon Sep 17 00:00:00 2001 From: keorn Date: Sun, 8 Jan 2017 17:35:09 +0100 Subject: [PATCH 1/5] fix --- sync/src/chain.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index df69dbb084d..7d37c6a3118 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -2082,16 +2082,12 @@ impl ChainSync { if !enacted.is_empty() { // Select random peers to re-broadcast transactions to. - let mut random = random::new(); - let len = self.peers.len(); - let peers = random.gen_range(0, min(len, 3)); + let peers: Vec = ChainSync::select_random_peers(self.peers.keys().collect()).iter().take(3).collect(); trace!(target: "sync", "Re-broadcasting transactions to {} random peers.", peers); - - for _ in 0..peers { - let peer = random.gen_range(0, len); - self.peers.values_mut().nth(peer).map(|mut peer_info| { + for peer in peers { + self.peers.get_mut(peer).map(|mut peer_info| peer_info.last_sent_transactions.clear() - }); + ); } } } From 289424baa3f2cd689cd9314f65016706d2fbb1f8 Mon Sep 17 00:00:00 2001 From: keorn Date: Sun, 8 Jan 2017 17:54:01 +0100 Subject: [PATCH 2/5] fix compile --- sync/src/chain.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 7d37c6a3118..40850cfffcc 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -2082,10 +2082,13 @@ impl ChainSync { if !enacted.is_empty() { // Select random peers to re-broadcast transactions to. - let peers: Vec = ChainSync::select_random_peers(self.peers.keys().collect()).iter().take(3).collect(); - trace!(target: "sync", "Re-broadcasting transactions to {} random peers.", peers); + let peers: Vec = ChainSync::select_random_peers(&self.peers.keys().cloned().collect::>()) + .into_iter() + .take(3) + .collect(); + trace!(target: "sync", "Re-broadcasting transactions to random peers: {:?}", peers); for peer in peers { - self.peers.get_mut(peer).map(|mut peer_info| + self.peers.get_mut(&peer).map(|mut peer_info| peer_info.last_sent_transactions.clear() ); } From 65bfba4ce917ece91ee5c46b55dca7d896bcefb9 Mon Sep 17 00:00:00 2001 From: keorn Date: Sun, 8 Jan 2017 18:34:36 +0100 Subject: [PATCH 3/5] remove peers trace --- sync/src/chain.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 40850cfffcc..e42f4bb57a7 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -2082,13 +2082,10 @@ impl ChainSync { if !enacted.is_empty() { // Select random peers to re-broadcast transactions to. - let peers: Vec = ChainSync::select_random_peers(&self.peers.keys().cloned().collect::>()) - .into_iter() - .take(3) - .collect(); - trace!(target: "sync", "Re-broadcasting transactions to random peers: {:?}", peers); - for peer in peers { - self.peers.get_mut(&peer).map(|mut peer_info| + let peers: Vec = ChainSync::select_random_peers(&self.peers.keys().cloned().collect::>()); + trace!(target: "sync", "Re-broadcasting transactions to random peers."); + for peer in peers.iter().take(3) { + self.peers.get_mut(peer).map(|mut peer_info| peer_info.last_sent_transactions.clear() ); } From 463f50efc07152e30003fe76a93f0ef930c2bfa1 Mon Sep 17 00:00:00 2001 From: keorn Date: Mon, 9 Jan 2017 10:33:46 +0100 Subject: [PATCH 4/5] simplify --- sync/src/chain.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index e42f4bb57a7..279fcf158ec 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -2070,7 +2070,8 @@ impl ChainSync { /// called when block is imported to chain - propagates the blocks and updates transactions sent to peers pub fn chain_new_blocks(&mut self, io: &mut SyncIo, _imported: &[H256], invalid: &[H256], enacted: &[H256], _retracted: &[H256], sealed: &[H256], proposed: &[Bytes]) { let queue_info = io.chain().queue_info(); - if !self.status().is_syncing(queue_info) || !sealed.is_empty() { + let is_syncing = self.status().is_syncing(queue_info); + if !is_syncing || !sealed.is_empty() { trace!(target: "sync", "Propagating blocks, state={:?}", self.state); self.propagate_latest_blocks(io, sealed); self.propagate_proposed_blocks(io, proposed); @@ -2080,15 +2081,13 @@ impl ChainSync { self.restart(io); } - if !enacted.is_empty() { - // Select random peers to re-broadcast transactions to. - let peers: Vec = ChainSync::select_random_peers(&self.peers.keys().cloned().collect::>()); - trace!(target: "sync", "Re-broadcasting transactions to random peers."); - for peer in peers.iter().take(3) { - self.peers.get_mut(peer).map(|mut peer_info| - peer_info.last_sent_transactions.clear() - ); - } + if !is_syncing && !enacted.is_empty() && !self.peers.is_empty() { + // Select random peer to re-broadcast transactions to. + let peer = random::new().get_rng(0, self.peers.len()); + trace!(target: "sync", "Re-broadcasting transactions to random peer."); + self.peers.get_mut(peer).map(|mut peer_info| + peer_info.last_sent_transactions.clear() + ); } } From 1a3c30e0e52c1132ee80dd13b8f1045003be86aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 9 Jan 2017 10:52:15 +0100 Subject: [PATCH 5/5] Fixing 'simplify' --- sync/src/chain.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index b0358b76236..0271952c51c 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -2085,9 +2085,9 @@ impl ChainSync { if !is_syncing && !enacted.is_empty() && !self.peers.is_empty() { // Select random peer to re-broadcast transactions to. - let peer = random::new().get_rng(0, self.peers.len()); - trace!(target: "sync", "Re-broadcasting transactions to random peer."); - self.peers.get_mut(peer).map(|mut peer_info| + let peer = random::new().gen_range(0, self.peers.len()); + trace!(target: "sync", "Re-broadcasting transactions to a random peer."); + self.peers.values_mut().nth(peer).map(|mut peer_info| peer_info.last_sent_transactions.clear() ); } @@ -2550,6 +2550,26 @@ mod tests { assert_eq!(0x02, io.packets[0].packet_id); } + #[test] + fn does_not_fail_for_no_peers() { + let mut client = TestBlockChainClient::new(); + client.add_blocks(100, EachBlockWith::Uncle); + client.insert_transaction_to_queue(); + // Sync with no peers + let mut sync = ChainSync::new(SyncConfig::default(), &client); + let queue = RwLock::new(VecDeque::new()); + let ss = TestSnapshotService::new(); + let mut io = TestIo::new(&mut client, &ss, &queue, None); + let peer_count = sync.propagate_new_transactions(&mut io); + sync.chain_new_blocks(&mut io, &[], &[], &[], &[], &[], &[]); + // Try to propagate same transactions for the second time + let peer_count2 = sync.propagate_new_transactions(&mut io); + + assert_eq!(0, io.packets.len()); + assert_eq!(0, peer_count); + assert_eq!(0, peer_count2); + } + #[test] fn propagates_transactions_without_alternating() { let mut client = TestBlockChainClient::new();