Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gossipsub): add getter function to obtain TopicScoreParams #4231

Merged
merged 10 commits into from
Aug 3, 2023
8 changes: 7 additions & 1 deletion protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 0.45.0
## 0.45.1 - unreleased

- Add getter function to obtain `TopicScoreParams`.

[PR 4231]: https://github.com/libp2p/rust-libp2p/pull/4231

## 0.45.0

- Raise MSRV to 1.65.
See [PR 3715].
Expand Down
9 changes: 9 additions & 0 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,15 @@ where
}
}

/// Returns a scoring parameters for a topic if existent.
pub fn get_topic_params<H: Hasher>(&self, topic: &Topic<H>) -> Option<TopicScoreParams> {
if let Some((peer_score, ..)) = &self.peer_score {
peer_score.get_topic_params(&topic.hash())
} else {
None
}
}

/// Sets the application specific score for a peer. Returns true if scoring is active and
/// the peer is connected or if the score of the peer is not yet expired, false otherwise.
pub fn set_application_score(&mut self, peer_id: &PeerId, new_score: f64) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions protocols/gossipsub/src/peer_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ impl PeerScore {
}
}

/// Returns a scoring parameters for a topic if existent.
pub(crate) fn get_topic_params(&self, topic_hash: &TopicHash) -> Option<TopicScoreParams> {
self.params.topics.get(topic_hash).cloned()
}

/// Increments the "invalid message deliveries" counter for all scored topics the message
/// is published in.
fn mark_invalid_message_delivery(&mut self, peer_id: &PeerId, topic_hash: &TopicHash) {
Expand Down