protocols/floodsub: Propagate messages only to target peers (#2360)

Propagate messages only to the target peers and not all connected peers.

Co-authored-by: Victor Ermolaev <victorermolaev@gmail.com>
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Victor Ermolaev
2021-12-06 17:08:29 +01:00
committed by GitHub
parent 4401ffa8b3
commit 75ae7b0461
2 changed files with 16 additions and 0 deletions

View File

@ -4,8 +4,12 @@
- Migrate to Rust edition 2021 (see [PR 2339]). - Migrate to Rust edition 2021 (see [PR 2339]).
- Propagate messages only to the target peers and not all connected peers (see [PR2360]).
[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339 [PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
[PR 2360]: https://github.com/libp2p/rust-libp2p/pull/2360/
# 0.32.0 [2021-11-16] # 0.32.0 [2021-11-16]
- Update dependencies. - Update dependencies.

View File

@ -253,6 +253,12 @@ impl Floodsub {
// Send to peers we know are subscribed to the topic. // Send to peers we know are subscribed to the topic.
for (peer_id, sub_topic) in self.connected_peers.iter() { for (peer_id, sub_topic) in self.connected_peers.iter() {
// Peer must be in a communication list.
if !self.target_peers.contains(peer_id) {
continue;
}
// Peer must be subscribed for the topic.
if !sub_topic if !sub_topic
.iter() .iter()
.any(|t| message.topics.iter().any(|u| t == u)) .any(|t| message.topics.iter().any(|u| t == u))
@ -402,6 +408,12 @@ impl NetworkBehaviour for Floodsub {
continue; continue;
} }
// Peer must be in a communication list.
if !self.target_peers.contains(peer_id) {
continue;
}
// Peer must be subscribed for the topic.
if !subscr_topics if !subscr_topics
.iter() .iter()
.any(|t| message.topics.iter().any(|u| t == u)) .any(|t| message.topics.iter().any(|u| t == u))