From 37e2ec38a7e001d8449d507897564c19f0c8ff32 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 14 Nov 2018 22:03:00 +0100 Subject: [PATCH] Update connected peers' topics on NodeEvent (#638) * Update connected peers' topics on NodeEvent After a peer connects they send us the list of the topics they're subscribed to. This causes a NodeEvent to be emitted. This PR makes sure we update the subscription info we have on the newly connected peer. * Formatting * Update protocols/floodsub/src/layer.rs Co-Authored-By: dvdplm * whiespace --- protocols/floodsub/src/layer.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index dffabe6f..523b1a94 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -218,6 +218,25 @@ where propagation_source: PeerId, event: FloodsubRpc, ) { + // Update connected peers topics + for subscription in event.subscriptions { + let mut remote_peer_topics = self.connected_peers + .get_mut(&propagation_source) + .expect("connected_peers is kept in sync with the peers we are connected to; we are guaranteed to only receive events from connected peers ; qed"); + match subscription.action { + FloodsubSubscriptionAction::Subscribe => { + if !remote_peer_topics.contains(&subscription.topic) { + remote_peer_topics.push(subscription.topic); + } + } + FloodsubSubscriptionAction::Unsubscribe => { + if let Some(pos) = remote_peer_topics.iter().position(|t| t == &subscription.topic ) { + remote_peer_topics.remove(pos); + } + } + } + } + // List of messages we're going to propagate on the network. let mut rpcs_to_dispatch: Vec<(PeerId, FloodsubRpc)> = Vec::new();