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

fix: don't panic if a bloom counter underflows #3614

Merged
merged 2 commits into from
Mar 15, 2023
Merged

fix: don't panic if a bloom counter underflows #3614

merged 2 commits into from
Mar 15, 2023

Conversation

jcnelson
Copy link
Member

This fixes #3599.

The Stacks node uses a persistent bloom counter to sketch the recent contents of its mempool in a space-efficient way, which it uses to derive a bloom filter in order to query other nodes' mempools for transactions it does not have. The bloom counter's remove_raw() method had been panicking in #3599 because it seemed that an item was simultaneously reported as present in the bloom counter and then turned out to be absent.

While this seems like a logic error (hence the original implementation's panic!() in this condition), it turns out this can happen naturally because an item can hash to the same bin multiple times. If the bin has value B and the item hashes to the same bin C times, where B < C, then this panic condition arises.

The solution is to simply remove the panic.

@@ -365,15 +365,15 @@ impl<H: BloomHash + Clone + StacksMessageCodec> BloomCounter<H> {
let sql = format!("CREATE TABLE IF NOT EXISTS {}(counts BLOB NOT NULL, num_bins INTEGER NOT NULL, num_hashes INTEGER NOT NULL, hasher BLOB NOT NULL);", table_name);
tx.execute(&sql, NO_PARAMS).map_err(db_error::SqliteError)?;

let (num_bits, num_hashes) = bloom_hash_count(error_rate, max_items);
let counts_vec = vec![0u8; (num_bits * 4) as usize];
let (num_bins, num_hashes) = bloom_hash_count(error_rate, max_items);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to the reviewer: this is just fixing a typo.

@codecov
Copy link

codecov bot commented Mar 14, 2023

Codecov Report

Merging #3614 (15c28b4) into master (6ccadda) will decrease coverage by 5.57%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #3614      +/-   ##
==========================================
- Coverage   30.33%   24.77%   -5.57%     
==========================================
  Files         298      298              
  Lines      275069   275068       -1     
==========================================
- Hits        83435    68136   -15299     
- Misses     191634   206932   +15298     
Impacted Files Coverage Δ
src/util_lib/bloom.rs 25.31% <100.00%> (+0.03%) ⬆️

... and 126 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@jio-gl jio-gl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing panic if bin is empty, is not an abnormal condition.

@kantai kantai merged commit d69012e into master Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

BUG: item is present in the bloom counter, but has a zero count
3 participants