diff --git a/consensus/src/sync/history/sync.rs b/consensus/src/sync/history/sync.rs index f6b15d7a00..a97e3ccee1 100644 --- a/consensus/src/sync/history/sync.rs +++ b/consensus/src/sync/history/sync.rs @@ -100,6 +100,11 @@ impl HistoryMacroSync { impl MacroSync for HistoryMacroSync { fn add_peer(&self, peer_id: TNetwork::PeerId) { + // Ignore peer if we already know it. + if self.peers.contains_key(&peer_id) { + return; + } + debug!("Requesting epoch ids for peer: {:?}", peer_id); let future = Self::request_epoch_ids( Arc::clone(&self.blockchain), diff --git a/consensus/src/sync/history/sync_clustering.rs b/consensus/src/sync/history/sync_clustering.rs index 2510d15f02..963584c608 100644 --- a/consensus/src/sync/history/sync_clustering.rs +++ b/consensus/src/sync/history/sync_clustering.rs @@ -115,6 +115,12 @@ impl HistoryMacroSync { &mut self, mut epoch_ids: EpochIds, ) -> Option { + // Ignore epoch_ids if we are already tracking the sending peer. + // We are going to request more ids from the sender once all already known ids are processed. + if self.peers.contains_key(&epoch_ids.sender) { + return None; + } + // Read our current blockchain state. let (our_epoch_id, our_epoch_number, our_block_number) = { let blockchain = self.blockchain.read(); @@ -219,8 +225,8 @@ impl HistoryMacroSync { // more epoch ids from it when the job is processed. if let Some(cluster) = cluster { let sender_peer_id = epoch_ids.sender; - cluster.add_peer(sender_peer_id); - self.peers.insert(sender_peer_id, 1); + assert!(cluster.add_peer(sender_peer_id)); + assert!(self.peers.insert(sender_peer_id, 1).is_none()); return None; } @@ -315,8 +321,9 @@ impl HistoryMacroSync { // The peer's epoch ids matched at least a part of this (now potentially truncated) cluster, // so we add the peer to this cluster. We also increment the peer's number of clusters. - cluster.add_peer(sender_peer_id); - num_clusters += 1; + if cluster.add_peer(sender_peer_id) { + num_clusters += 1; + } // Advance the id_index by the number of matched ids. // If there are no more ids to cluster, we can stop iterating. @@ -362,8 +369,10 @@ impl HistoryMacroSync { { // The peer's checkpoint id matched this cluster, // so we add the peer to this cluster. We also increment the peer's number of clusters. - cluster.add_peer(sender_peer_id); - num_clusters += 1; + if cluster.add_peer(sender_peer_id) { + num_clusters += 1; + } + found_cluster = true; break; }