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

disk index: bucket_index_ix doesn't % by capacity #31096

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions bucket_map/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
key: &Pubkey,
random: u64,
) -> Result<(Option<IndexEntryPlaceInBucket<T>>, u64), BucketMapError> {
let ix = Self::bucket_index_ix(index, key, random);
let ix = Self::bucket_index_ix(key, random) % index.capacity();
let mut first_free = None;
let mut m = Measure::start("bucket_find_index_entry_mut");
let capacity = index.capacity();
Expand Down Expand Up @@ -222,7 +222,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
key: &Pubkey,
random: u64,
) -> Option<(IndexEntryPlaceInBucket<T>, u64)> {
let ix = Self::bucket_index_ix(index, key, random);
let ix = Self::bucket_index_ix(key, random) % index.capacity();
for i in ix..ix + index.max_search() {
let ii = i % index.capacity();
if index.is_free(ii) {
Expand All @@ -243,7 +243,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
is_resizing: bool,
) -> Result<u64, BucketMapError> {
let mut m = Measure::start("bucket_create_key");
let ix = Self::bucket_index_ix(index, key, random);
let ix = Self::bucket_index_ix(key, random) % index.capacity();
for i in ix..ix + index.max_search() {
let ii = i % index.capacity();
if !index.is_free(ii) {
Expand Down Expand Up @@ -568,15 +568,14 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
items.data = Some((data_index, new_bucket));
}

fn bucket_index_ix(index: &BucketStorage<IndexBucket<T>>, key: &Pubkey, random: u64) -> u64 {
fn bucket_index_ix(key: &Pubkey, random: u64) -> u64 {
let mut s = DefaultHasher::new();
key.hash(&mut s);
//the locally generated random will make it hard for an attacker
//to deterministically cause all the pubkeys to land in the same
//location in any bucket on all validators
random.hash(&mut s);
let ix = s.finish();
ix % index.capacity()
s.finish()
//debug!( "INDEX_IX: {:?} uid:{} loc: {} cap:{}", key, uid, location, index.capacity() );
}

Expand Down