From 708dc7e424f13a8881c683f82b608bc562b441fa Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sat, 22 Jul 2023 14:16:26 +0900 Subject: [PATCH 1/9] Add get_topic_params --- protocols/gossipsub/src/behaviour.rs | 9 +++++++++ protocols/gossipsub/src/peer_score.rs | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index db4ee58864f..adc35c27fb2 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -930,6 +930,15 @@ where } } + /// Returns a scoring parameters for a topic if existent. + pub fn get_topic_params(&self, topic: &Topic) -> Option { + 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 { diff --git a/protocols/gossipsub/src/peer_score.rs b/protocols/gossipsub/src/peer_score.rs index 83f5a68eca1..c3fb1ac848a 100644 --- a/protocols/gossipsub/src/peer_score.rs +++ b/protocols/gossipsub/src/peer_score.rs @@ -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 { + self.params.topics.get(topic_hash).map(|params| params.clone()) + } + /// 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) { From 45d6a3266edca943fca3a058bd760d580dd5fc72 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sat, 22 Jul 2023 14:41:26 +0900 Subject: [PATCH 2/9] Add changelog entry --- protocols/gossipsub/CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index cdcb2ea4105..13d4541b56d 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -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]. From 7a8c2ad4383b9a8ecbd0eb344827529a2ec8da98 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Sat, 22 Jul 2023 14:54:49 +0900 Subject: [PATCH 3/9] Make clippy happy --- protocols/gossipsub/src/peer_score.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/gossipsub/src/peer_score.rs b/protocols/gossipsub/src/peer_score.rs index c3fb1ac848a..6b629a1c69a 100644 --- a/protocols/gossipsub/src/peer_score.rs +++ b/protocols/gossipsub/src/peer_score.rs @@ -782,7 +782,7 @@ impl PeerScore { /// Returns a scoring parameters for a topic if existent. pub(crate) fn get_topic_params(&self, topic_hash: &TopicHash) -> Option { - self.params.topics.get(topic_hash).map(|params| params.clone()) + self.params.topics.get(topic_hash).cloned() } /// Increments the "invalid message deliveries" counter for all scored topics the message From d51dff812b0dd10d3909507d5b0d57980ff4f292 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Wed, 26 Jul 2023 07:29:40 +0900 Subject: [PATCH 4/9] Return a reference to avoid needless clones --- protocols/gossipsub/src/behaviour.rs | 2 +- protocols/gossipsub/src/peer_score.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index adc35c27fb2..878a44d51b1 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -931,7 +931,7 @@ where } /// Returns a scoring parameters for a topic if existent. - pub fn get_topic_params(&self, topic: &Topic) -> Option { + pub fn get_topic_params(&self, topic: &Topic) -> Option<&TopicScoreParams> { if let Some((peer_score, ..)) = &self.peer_score { peer_score.get_topic_params(&topic.hash()) } else { diff --git a/protocols/gossipsub/src/peer_score.rs b/protocols/gossipsub/src/peer_score.rs index 6b629a1c69a..ab92d536aba 100644 --- a/protocols/gossipsub/src/peer_score.rs +++ b/protocols/gossipsub/src/peer_score.rs @@ -781,8 +781,8 @@ impl PeerScore { } /// Returns a scoring parameters for a topic if existent. - pub(crate) fn get_topic_params(&self, topic_hash: &TopicHash) -> Option { - self.params.topics.get(topic_hash).cloned() + pub(crate) fn get_topic_params(&self, topic_hash: &TopicHash) -> Option<&TopicScoreParams> { + self.params.topics.get(topic_hash) } /// Increments the "invalid message deliveries" counter for all scored topics the message From c0478e668df21a0f02d1397a04f0e5dc91bc55cd Mon Sep 17 00:00:00 2001 From: ackintosh Date: Wed, 26 Jul 2023 07:37:37 +0900 Subject: [PATCH 5/9] Update Cargo.toml --- Cargo.toml | 2 +- protocols/gossipsub/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 956816074e2..2a5bbe1eb8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ libp2p-dcutr = { version = "0.10.0", path = "protocols/dcutr" } libp2p-deflate = { version = "0.40.0", path = "transports/deflate" } libp2p-dns = { version = "0.40.0", path = "transports/dns" } libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" } -libp2p-gossipsub = { version = "0.45.0", path = "protocols/gossipsub" } +libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" } libp2p-identify = { version = "0.43.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.1" } libp2p-kad = { version = "0.44.2", path = "protocols/kad" } diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 25232ff5bfd..9e06c142e8e 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-gossipsub" edition = "2021" rust-version = { workspace = true } description = "Gossipsub protocol for libp2p" -version = "0.45.0" +version = "0.45.1" authors = ["Age Manning "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 4ac800f2f3694ee7edff01d9390e96cd4d2b019a Mon Sep 17 00:00:00 2001 From: Akihito Nakano Date: Wed, 2 Aug 2023 06:35:49 +0900 Subject: [PATCH 6/9] Update protocols/gossipsub/CHANGELOG.md Co-authored-by: Thomas Eizinger --- protocols/gossipsub/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index 13d4541b56d..ab483bdd8a1 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.45.1 - unreleased - Add getter function to obtain `TopicScoreParams`. + See [PR 4231]. [PR 4231]: https://github.com/libp2p/rust-libp2p/pull/4231 From dddaae3a578b39a818a21a5f13677956da63b39c Mon Sep 17 00:00:00 2001 From: ackintosh Date: Wed, 2 Aug 2023 06:47:24 +0900 Subject: [PATCH 7/9] Use `?` to bubble up the `None` case --- protocols/gossipsub/src/behaviour.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 878a44d51b1..b41ed40b00f 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -932,11 +932,7 @@ where /// Returns a scoring parameters for a topic if existent. pub fn get_topic_params(&self, topic: &Topic) -> Option<&TopicScoreParams> { - if let Some((peer_score, ..)) = &self.peer_score { - peer_score.get_topic_params(&topic.hash()) - } else { - None - } + self.peer_score?.0.get_topic_params(&topic.hash()) } /// Sets the application specific score for a peer. Returns true if scoring is active and From c1245ee2d950e09dc05ed37a0cc124ff009d8cbb Mon Sep 17 00:00:00 2001 From: ackintosh Date: Wed, 2 Aug 2023 06:47:54 +0900 Subject: [PATCH 8/9] Update Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 861b13b00af..ef57f330d6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2718,7 +2718,7 @@ dependencies = [ [[package]] name = "libp2p-gossipsub" -version = "0.45.0" +version = "0.45.1" dependencies = [ "async-std", "asynchronous-codec", From 8563b933a06cc6c430de957126831dbdd0df6cfd Mon Sep 17 00:00:00 2001 From: ackintosh Date: Wed, 2 Aug 2023 06:58:49 +0900 Subject: [PATCH 9/9] Fix compile error error[E0507]: cannot move out of `self.peer_score` which is behind a shared reference --- protocols/gossipsub/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index b41ed40b00f..7ca9f6df9da 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -932,7 +932,7 @@ where /// Returns a scoring parameters for a topic if existent. pub fn get_topic_params(&self, topic: &Topic) -> Option<&TopicScoreParams> { - self.peer_score?.0.get_topic_params(&topic.hash()) + self.peer_score.as_ref()?.0.get_topic_params(&topic.hash()) } /// Sets the application specific score for a peer. Returns true if scoring is active and