Skip to content

Commit

Permalink
properly account for peers' changing authority IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Sep 8, 2023
1 parent 91fbc13 commit 6319b92
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions polkadot/node/network/gossip-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub struct GossipSupport<AD> {
/// By `PeerId`.
///
/// Needed for efficient handling of disconnect events.
connected_authorities_by_peer_id: HashMap<PeerId, HashSet<AuthorityDiscoveryId>>,
connected_peers: HashMap<PeerId, HashSet<AuthorityDiscoveryId>>,
/// Authority discovery service.
authority_discovery: AD,

Expand All @@ -130,7 +130,7 @@ where
failure_start: None,
resolved_authorities: HashMap::new(),
connected_authorities: HashMap::new(),
connected_authorities_by_peer_id: HashMap::new(),
connected_peers: HashMap::new(),
authority_discovery,
metrics,
}
Expand Down Expand Up @@ -407,20 +407,43 @@ where
}
}

for (peer_id, auths) in authority_ids {

// peer was authority and now isn't
for (peer_id, current) in self.connected_peers.iter_mut() {
// empty -> nonempty is handled in the next loop
if !current.is_empty() && !authority_ids.contains_key(peer_id) {
sender
.send_message(NetworkBridgeRxMessage::UpdatedAuthorityIds {
peer_id: peer_id.clone(),
authority_ids: HashSet::new(),
})
.await;

for a in current.drain() {
self.connected_authorities.remove(&a);
}
}
}

// peer has new authority set.
for (peer_id, new) in authority_ids {
// If the peer is connected _and_ the authority IDs have changed.
if self.connected_authorities_by_peer_id.get(&peer_id).map_or(false, |x| x != &auths) {
if let Some(prev) = self.connected_peers.get(&peer_id).filter(|x| x != &&new) {
sender
.send_message(NetworkBridgeRxMessage::UpdatedAuthorityIds {
peer_id,
authority_ids: auths.clone(),
authority_ids: new.clone(),
})
.await;

auths.iter().for_each(|a| {
prev.iter().for_each(|a| {
self.connected_authorities.remove(a);
});
new.iter().for_each(|a| {
self.connected_authorities.insert(a.clone(), peer_id);
});
self.connected_authorities_by_peer_id.insert(peer_id, auths);

self.connected_peers.insert(peer_id, new);
}
}
}
Expand All @@ -432,11 +455,13 @@ where
authority_ids.iter().for_each(|a| {
self.connected_authorities.insert(a.clone(), peer_id);
});
self.connected_authorities_by_peer_id.insert(peer_id, authority_ids);
self.connected_peers.insert(peer_id, authority_ids);
} else {
self.connected_peers.insert(peer_id, HashSet::new());
}
},
NetworkBridgeEvent::PeerDisconnected(peer_id) => {
if let Some(authority_ids) = self.connected_authorities_by_peer_id.remove(&peer_id)
if let Some(authority_ids) = self.connected_peers.remove(&peer_id)
{
authority_ids.into_iter().for_each(|a| {
self.connected_authorities.remove(&a);
Expand Down

0 comments on commit 6319b92

Please sign in to comment.