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(discv5): fix bug flip byte lookup tgt #7764

Merged
merged 10 commits into from Apr 20, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions crates/net/discv5/src/lib.rs
Expand Up @@ -541,18 +541,14 @@ pub fn get_lookup_target(
let mut target = local_node_id.raw();

// make sure target has a 'log2distance'-long suffix that differs from local node id
if kbucket_index != 0 {
let suffix_bit_offset = MAX_KBUCKET_INDEX.saturating_sub(kbucket_index);
let suffix_byte_offset = suffix_bit_offset / 8;
// todo: flip the precise bit
// let rel_suffix_bit_offset = suffix_bit_offset % 8;
target[suffix_byte_offset] = !target[suffix_byte_offset];

if suffix_byte_offset != 31 {
for b in target.iter_mut().take(31).skip(suffix_byte_offset + 1) {
*b = rand::random::<u8>();
}
}
let suffix_bit_offset = MAX_KBUCKET_INDEX.saturating_sub(kbucket_index);
let suffix_byte_offset = suffix_bit_offset / 8;
// todo: flip the precise bit
// let rel_suffix_bit_offset = suffix_bit_offset % 8;
target[suffix_byte_offset] = !target[suffix_byte_offset];
emhane marked this conversation as resolved.
Show resolved Hide resolved

for b in target.iter_mut().skip(suffix_byte_offset + 1) {
*b = rand::random::<u8>();
}
emhane marked this conversation as resolved.
Show resolved Hide resolved

target.into()
Expand Down Expand Up @@ -807,9 +803,12 @@ mod tests {

#[test]
fn select_lookup_target() {
// bucket index ceiled to the next multiple of 4
// bucket index ceiled to the next multiple of 8
emhane marked this conversation as resolved.
Show resolved Hide resolved
const fn expected_bucket_index(kbucket_index: usize) -> u64 {
let log2distance = kbucket_index + 1;
if log2distance % 8 == 0 {
return log2distance as u64;
}
let log2distance = log2distance / 8;
((log2distance + 1) * 8) as u64
}
Expand Down