From b4ec05a78ccfb33419ee1cd3098b34746ef7d71f Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 25 Aug 2020 15:49:34 +0200 Subject: [PATCH 1/5] protocols/kad: Make U256 within Distance public In order to make use of the distances returned by `KBucketRef::range` in a human readable format, one needs to be able to tranlate the 256 bit in a `Distance` to a smaller space, e.g. the currently used 8 bit bucket scheme. To be able to do so one needs to be able to extract the 256 bit from a `Distance`. This commit makes the `U256` within a `Distance` public allowing callers of `KBucketRef::range` to work with the raw bits themselves. --- protocols/kad/src/kbucket/key.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 302d93a08cd..2faa673f042 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -28,7 +28,7 @@ use std::hash::{Hash, Hasher}; construct_uint! { /// 256-bit unsigned integer. - pub(super) struct U256(4); + pub struct U256(4); } /// A `Key` in the DHT keyspace with preserved preimage. @@ -167,7 +167,7 @@ impl AsRef for KeyBytes { /// A distance between two keys in the DHT keyspace. #[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Debug)] -pub struct Distance(pub(super) U256); +pub struct Distance(pub U256); #[cfg(test)] mod tests { From 55a01455dc3daa75733ca855a698745b9f82a400 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 26 Aug 2020 17:09:26 +0200 Subject: [PATCH 2/5] protocols/kad: Implement log2 for Distance --- protocols/kad/src/kbucket/key.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 2faa673f042..694dc78604a 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -169,6 +169,13 @@ impl AsRef for KeyBytes { #[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Debug)] pub struct Distance(pub U256); +impl Distance { + /// Returns the base 2 logarithm of the [`Distance`]. + pub fn log2(&self) -> Option { + (256 - self.0.leading_zeros()).checked_sub(1) + } +} + #[cfg(test)] mod tests { use super::*; From 2bf00a8683f523ceb35597f4b7a4e5045fb426ed Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 26 Aug 2020 17:10:06 +0200 Subject: [PATCH 3/5] Revert "protocols/kad: Make U256 within Distance public" This reverts commit b4ec05a78ccfb33419ee1cd3098b34746ef7d71f. --- protocols/kad/src/kbucket/key.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 694dc78604a..ebd7cb981a7 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -28,7 +28,7 @@ use std::hash::{Hash, Hasher}; construct_uint! { /// 256-bit unsigned integer. - pub struct U256(4); + pub(super) struct U256(4); } /// A `Key` in the DHT keyspace with preserved preimage. @@ -167,7 +167,7 @@ impl AsRef for KeyBytes { /// A distance between two keys in the DHT keyspace. #[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Debug)] -pub struct Distance(pub U256); +pub struct Distance(pub(super) U256); impl Distance { /// Returns the base 2 logarithm of the [`Distance`]. From b1371ace2a240f7092146b46f97bb67f86d316d1 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 26 Aug 2020 18:40:47 +0200 Subject: [PATCH 4/5] protocols/kad: Rename Distance::log2 to ilog2 --- protocols/kad/src/kbucket/key.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index ebd7cb981a7..8d5ec6169b8 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -170,8 +170,10 @@ impl AsRef for KeyBytes { pub struct Distance(pub(super) U256); impl Distance { - /// Returns the base 2 logarithm of the [`Distance`]. - pub fn log2(&self) -> Option { + /// Returns the integer part of the base 2 logarithm of the [`Distance`]. + /// + /// Returns `None` if the distance is zero. + pub fn ilog2(&self) -> Option { (256 - self.0.leading_zeros()).checked_sub(1) } } From cc802e095dfb9a168d365cc3941c1d12944b73cf Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 26 Aug 2020 18:41:37 +0200 Subject: [PATCH 5/5] protocols/kad: Use Distance:ilog2 in BucketIndex::new --- protocols/kad/src/kbucket.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/protocols/kad/src/kbucket.rs b/protocols/kad/src/kbucket.rs index 3de74fad6bf..b8e38c69e1c 100644 --- a/protocols/kad/src/kbucket.rs +++ b/protocols/kad/src/kbucket.rs @@ -106,9 +106,7 @@ impl BucketIndex { /// `local_key` is the `local_key` itself, which does not belong in any /// bucket. fn new(d: &Distance) -> Option { - (NUM_BUCKETS - d.0.leading_zeros() as usize) - .checked_sub(1) - .map(BucketIndex) + d.ilog2().map(|i| BucketIndex(i as usize)) } /// Gets the index value as an unsigned integer.