Skip to content

Commit a1f3547

Browse files
protocols/gossipsub: Allow score buckets to be set (#2595)
Moves the score buckets to the metrics configuration, setting the same defaults as what we had but also allowing the user to pass a reference to the score thresholds to create them from that. Having them in the config also allows users to set them directly.
1 parent 102509a commit a1f3547

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
- Fix gossipsub metric (see [PR 2558]).
1212

13+
- Allow the user to set the buckets for the score histogram, and to adjust them from the score thresholds. See [PR 2595].
14+
1315
[PR 2558]: https://github.com/libp2p/rust-libp2p/pull/2558
16+
[PR 2595]: https://github.com/libp2p/rust-libp2p/pull/2595
1417

1518
# 0.36.0 [2022-02-22]
1619

protocols/gossipsub/src/metrics.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,48 @@ pub struct Config {
4949
/// determined by users on the network. This limit permits a fixed amount of topics to allow,
5050
/// in-addition to the mesh topics.
5151
pub max_never_subscribed_topics: usize,
52+
/// Buckets used for the score histograms.
53+
pub score_buckets: Vec<f64>,
54+
}
55+
56+
impl Config {
57+
/// Create buckets for the score histograms based on score thresholds.
58+
pub fn buckets_using_scoring_thresholds(&mut self, params: &crate::PeerScoreThresholds) {
59+
self.score_buckets = vec![
60+
params.graylist_threshold,
61+
params.publish_threshold,
62+
params.gossip_threshold,
63+
params.gossip_threshold / 2.0,
64+
params.gossip_threshold / 4.0,
65+
0.0,
66+
1.0,
67+
10.0,
68+
100.0,
69+
];
70+
}
5271
}
5372

5473
impl Default for Config {
5574
fn default() -> Self {
75+
// Some sensible defaults
76+
let gossip_threshold = -4000.0;
77+
let publish_threshold = -8000.0;
78+
let graylist_threshold = -16000.0;
79+
let score_buckets: Vec<f64> = vec![
80+
graylist_threshold,
81+
publish_threshold,
82+
gossip_threshold,
83+
gossip_threshold / 2.0,
84+
gossip_threshold / 4.0,
85+
0.0,
86+
1.0,
87+
10.0,
88+
100.0,
89+
];
5690
Config {
5791
max_topics: DEFAULT_MAX_TOPICS,
5892
max_never_subscribed_topics: DEFAULT_MAX_NEVER_SUBSCRIBED_TOPICS,
93+
score_buckets,
5994
}
6095
}
6196
}
@@ -147,6 +182,7 @@ impl Metrics {
147182
let Config {
148183
max_topics,
149184
max_never_subscribed_topics,
185+
score_buckets,
150186
} = config;
151187

152188
macro_rules! register_family {
@@ -224,24 +260,9 @@ impl Metrics {
224260
"topic_msg_recv_bytes",
225261
"Bytes received from gossip messages for each topic"
226262
);
227-
// TODO: Update default variables once a builder pattern is used.
228-
let gossip_threshold = -4000.0;
229-
let publish_threshold = -8000.0;
230-
let greylist_threshold = -16000.0;
231-
let histogram_buckets: Vec<f64> = vec![
232-
greylist_threshold,
233-
publish_threshold,
234-
gossip_threshold,
235-
gossip_threshold / 2.0,
236-
gossip_threshold / 4.0,
237-
0.0,
238-
1.0,
239-
10.0,
240-
100.0,
241-
];
242263

243264
let hist_builder = HistBuilder {
244-
buckets: histogram_buckets,
265+
buckets: score_buckets,
245266
};
246267

247268
let score_per_mesh: Family<_, _, HistBuilder> = Family::new_with_constructor(hist_builder);

0 commit comments

Comments
 (0)