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

protocols/kad: Implement log2 for Distance #1719

Merged
merged 7 commits into from
Aug 28, 2020
Merged

Conversation

mxinden
Copy link
Member

@mxinden mxinden commented Aug 25, 2020

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.


Follow up on #1680.

With this pull request the implementation internal struct U256 is exposed on the public interface. That is not great. I have been unable to come up with a better way of exposing this information without tying the public interface to the internal 256-bit-fixed-array-implementation (see #1680 (comment) for past discussion). Instead of exposing U256 itself, one could as well offer a method on Distance returning a [u8] instead.

I don't think simply exposing a 256bit bucket identifier via Distance's Debug implementation in logs or metrics is of much help for any human being.

Thoughts?

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.
@mxinden mxinden requested a review from romanb August 25, 2020 14:01
@romanb
Copy link
Contributor

romanb commented Aug 25, 2020

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.

What format do you have in mind and what do you mean by translating it to an "8 bit bucket scheme"? External code shouldn't presume there to be 256 buckets either. Since you mention a human-readable format for logs and metrics, wouldn't it be appropriate to implement Display for Distance?

@romanb
Copy link
Contributor

romanb commented Aug 25, 2020

What format do you have in mind and what do you mean by translating it to an "8 bit bucket scheme"? External code shouldn't presume there to be 256 buckets either.

Oh, I guess you just want to show or distribute the distances on a (base 2) logarithmic scale, independent of how many buckets there are? I think I would have a slight preference to just offering functions like Distance::log2() over making uint crate types part of the public API and exposing the implementation of Distance.

@mxinden mxinden changed the title protocols/kad: Make U256 within Distance public protocols/kad: Implement log2 for Distance Aug 26, 2020
Copy link
Member Author

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

We could now use Distance::log2 in BucketIndex::new.

fn new(d: &Distance) -> Option<BucketIndex> {
(NUM_BUCKETS - d.0.leading_zeros() as usize)
.checked_sub(1)
.map(BucketIndex)
}

As a downside that would blur the lines between the public log2 and the internal bucket scheme. I am fine either way. Thoughts?

protocols/kad/src/kbucket/key.rs Show resolved Hide resolved
protocols/kad/src/kbucket/key.rs Outdated Show resolved Hide resolved
protocols/kad/src/kbucket/key.rs Show resolved Hide resolved
@romanb
Copy link
Contributor

romanb commented Aug 26, 2020

We could now use Distance::log2 in BucketIndex::new.

I think we should do that.

@mxinden mxinden merged commit 0136af5 into libp2p:master Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants