Skip to content

Commit 3f1ee1e

Browse files
authored
fix(gossipsub): don't panic on mcache while adding fanout peers
Fanout peers may not be been added correctly because the scoring function was not negated. This PR changes it to be more consistent with other parts of the code base. Also, users can set the memcache size to be 0. When this happens, a few panics occur. I've added statements to prevent this. Pull-Request: #6095.
1 parent 6e729d9 commit 3f1ee1e

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
## 0.49.0
99

10+
- Fix fanout logic to include correctly scored peers and prevent panics when memcache is set to 0.
11+
See [PR 6095](https://github.com/libp2p/rust-libp2p/pull/6095)
12+
1013
- Feature gate metrics related code. This changes some `Behaviour` constructor methods.
1114
See [PR 6020](https://github.com/libp2p/rust-libp2p/pull/6020)
1215
- Send IDONTWANT before Publishing a new message.

protocols/gossipsub/src/behaviour.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,8 +2473,10 @@ where
24732473
get_random_peers(&self.connected_peers, topic_hash, needed_peers, |peer_id| {
24742474
!peers.contains(peer_id)
24752475
&& !explicit_peers.contains(peer_id)
2476-
&& scores.get(peer_id).map(|r| r.score).unwrap_or_default()
2477-
< publish_threshold
2476+
&& !self
2477+
.peer_score
2478+
.below_threshold(peer_id, |ts| ts.publish_threshold)
2479+
.0
24782480
});
24792481
peers.extend(new_peers);
24802482
}

protocols/gossipsub/src/mcache.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ impl MessageCache {
7676
///
7777
/// Returns true if the message didn't already exist in the cache.
7878
pub(crate) fn put(&mut self, message_id: &MessageId, msg: RawMessage) -> bool {
79+
if self.history.is_empty() {
80+
return true;
81+
}
7982
match self.msgs.entry(message_id.clone()) {
8083
Entry::Occupied(_) => {
8184
// Don't add duplicate entries to the cache.
@@ -187,6 +190,10 @@ impl MessageCache {
187190
/// Shift the history array down one and delete messages associated with the
188191
/// last entry.
189192
pub(crate) fn shift(&mut self) {
193+
if self.history.is_empty() {
194+
return;
195+
}
196+
190197
for entry in self.history.pop().expect("history is always > 1") {
191198
if let Some((msg, _)) = self.msgs.remove(&entry.mid) {
192199
if !msg.validated {

0 commit comments

Comments
 (0)