diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 6e19eaf1be9..940030a632b 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,4 +1,7 @@ ## 0.50.0 +- Fix underflow when shuffling peers after prunning. + See [PR 6183](https://github.com/libp2p/rust-libp2p/pull/6183) + - Remove peer penalty for duplicate messages. See [PR 6112](https://github.com/libp2p/rust-libp2p/pull/6112) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index a0a3a16f0e7..1b780ffba5e 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -2228,7 +2228,9 @@ where score_p1.partial_cmp(&score_p2).unwrap_or(Ordering::Equal) }); // shuffle everything except the last retain_scores many peers (the best ones) - shuffled[..peers.len() - self.config.retain_scores()].shuffle(&mut rng); + if peers.len() > self.config.retain_scores() { + shuffled[..peers.len() - self.config.retain_scores()].shuffle(&mut rng); + } // count total number of outbound peers let mut outbound = shuffled