proto/bloom: add BloomFilter::fpr() and use it for the false-positive-rate

The false-positive-rate primitive (fill ratio raised to the hash count) was
inlined at three sites. Hoist it into a BloomFilter::fpr() method and call it
from all three; the threshold/policy logic stays at the call sites. Behavior
and result bytes are unchanged.
This commit is contained in:
Johnathan Corgan
2026-07-08 19:00:24 +00:00
parent 4ad5940114
commit b53db662c3
2 changed files with 8 additions and 3 deletions
+2 -2
View File
@@ -87,7 +87,7 @@ impl Node {
// operator to see one clear message, not spam. // operator to see one clear message, not spam.
let max_fpr = self.config().node.bloom.max_inbound_fpr; let max_fpr = self.config().node.bloom.max_inbound_fpr;
let out_fill = sent_filter.fill_ratio(); let out_fill = sent_filter.fill_ratio();
let out_fpr = out_fill.powi(sent_filter.hash_count() as i32); let out_fpr = sent_filter.fpr();
if out_fpr > max_fpr { if out_fpr > max_fpr {
let now = std::time::Instant::now(); let now = std::time::Instant::now();
let should_warn = self let should_warn = self
@@ -213,7 +213,7 @@ impl Node {
// to wipe a victim's contribution to aggregation. // to wipe a victim's contribution to aggregation.
let max_fpr = self.config().node.bloom.max_inbound_fpr; let max_fpr = self.config().node.bloom.max_inbound_fpr;
let fill = announce.filter.fill_ratio(); let fill = announce.filter.fill_ratio();
let fpr = fill.powi(announce.filter.hash_count() as i32); let fpr = announce.filter.fpr();
if fpr > max_fpr { if fpr > max_fpr {
self.metrics() self.metrics()
.bloom .bloom
+6 -1
View File
@@ -139,6 +139,11 @@ impl BloomFilter {
self.count_ones() as f64 / self.num_bits as f64 self.count_ones() as f64 / self.num_bits as f64
} }
/// Current false-positive rate: fill ratio raised to the hash count.
pub fn fpr(&self) -> f64 {
self.fill_ratio().powi(self.hash_count as i32)
}
/// Estimate the number of elements in the filter. /// Estimate the number of elements in the filter.
/// ///
/// Uses the formula: n = -(m/k) * ln(1 - X/m) /// Uses the formula: n = -(m/k) * ln(1 - X/m)
@@ -160,7 +165,7 @@ impl BloomFilter {
} }
let fill = x / m; let fill = x / m;
let fpr = fill.powi(self.hash_count as i32); let fpr = self.fpr();
if fpr > max_fpr { if fpr > max_fpr {
return None; return None;
} }