Skip to content

Commit a753ab0

Browse files
authored
fix(gossipsub): Fix underflow when shuffling peers after prunning.
I noticed two issues during testing. 1. We allow our mesh to grow greater than `mesh_n_high`, intentionally 2. There is a potential underflow in the heartbeat that can cause a panic For the first 1. This looks like its intentional but I can't recall why we would have added it. I think its counter-intuitive to allow our mesh to grow larger than the specified parameter. I suspect we added it to prevent our mesh from being filled with inbound peers and potentially being eclipsed. I suspect the best approach here is to remove inbound peers in the mesh maintenance rather than exceeding the mesh_n_high configuration. For 2. There is an underflow which this PR prevents. It can be triggered for low mesh_n_high values, i.e 0. This shouldn't be a concern for regular users, but we shouldn't have code that can panic based on user configuration. Pull-Request: #6183.
1 parent 0e52451 commit a753ab0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## 0.50.0
2+
- Fix underflow when shuffling peers after prunning.
3+
See [PR 6183](https://github.com/libp2p/rust-libp2p/pull/6183)
4+
25
- Remove peer penalty for duplicate messages.
36
See [PR 6112](https://github.com/libp2p/rust-libp2p/pull/6112)
47

protocols/gossipsub/src/behaviour.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,9 @@ where
22282228
score_p1.partial_cmp(&score_p2).unwrap_or(Ordering::Equal)
22292229
});
22302230
// shuffle everything except the last retain_scores many peers (the best ones)
2231-
shuffled[..peers.len() - self.config.retain_scores()].shuffle(&mut rng);
2231+
if peers.len() > self.config.retain_scores() {
2232+
shuffled[..peers.len() - self.config.retain_scores()].shuffle(&mut rng);
2233+
}
22322234

22332235
// count total number of outbound peers
22342236
let mut outbound = shuffled

0 commit comments

Comments
 (0)