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 <dvdplm@gmail.com>

* whiespace
This commit is contained in:
David 2018-11-14 22:03:00 +01:00 committed by Pierre Krieger
parent 24ccefbbc6
commit 37e2ec38a7

View File

@ -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();