From 99bcb88302cb76f29c82ad72785164e041ff7ca2 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sun, 5 Jan 2025 14:25:04 +0900 Subject: [PATCH 1/3] Ensure that fanout_peers is always non-empty if it's Some --- protocols/gossipsub/src/behaviour.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 6172875e5b9..1e55e8b3da3 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -675,9 +675,15 @@ where // Gossipsub peers None => { tracing::debug!(topic=%topic_hash, "Topic not in the mesh"); + // `fanout_peers` is always non-empty if it's `Some`. + let fanout_peers = self + .fanout + .get(&topic_hash) + .map(|peers| if peers.is_empty() { None } else { Some(peers) }) + .unwrap_or(None); // If we have fanout peers add them to the map. - if self.fanout.contains_key(&topic_hash) { - for peer in self.fanout.get(&topic_hash).expect("Topic must exist") { + if let Some(peers) = fanout_peers { + for peer in peers { recipient_peers.insert(*peer); } } else { From 9b97d28b303ef6e67bd716d77f36530bbbe6d762 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sun, 5 Jan 2025 15:29:44 +0900 Subject: [PATCH 2/3] Add a CHANGELOG entry --- protocols/gossipsub/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 550bbad4d99..94b9b922973 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -35,6 +35,9 @@ - Fix `cargo clippy` warnings in `rustc 1.84.0-beta.1`. See [PR 5700](https://github.com/libp2p/rust-libp2p/pull/5700). +- Fixe an issue where an `InsufficientPeers` error could occur under certain conditions, despite having peers subscribed to a topic. + See [PR 5793](https://github.com/libp2p/rust-libp2p/pull/5793). + ## 0.47.0 From 21de066d4fce9038ef932621e80b731d2abbae74 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Mon, 6 Jan 2025 22:30:21 +0900 Subject: [PATCH 3/3] `filter` for simplicity --- protocols/gossipsub/src/behaviour.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 1e55e8b3da3..356f1d6cd77 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -679,8 +679,7 @@ where let fanout_peers = self .fanout .get(&topic_hash) - .map(|peers| if peers.is_empty() { None } else { Some(peers) }) - .unwrap_or(None); + .filter(|peers| !peers.is_empty()); // If we have fanout peers add them to the map. if let Some(peers) = fanout_peers { for peer in peers {