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

[Kademlia] Rehash PeerId before inserting in a KBucketsTable #1025

Merged
merged 6 commits into from
Mar 26, 2019

Conversation

elferdo
Copy link
Contributor

@elferdo elferdo commented Mar 25, 2019

We currently use PeerId directly as a key into a Kademlia table because it happens to be a hash of the address of a peer. However, the reference implementation uses a hash of the hash. This PR introduces a new struct, KadHash that is to be used instead as key.

There are actually two use cases for PeerId: as key into the Kademlia table and as an actual identifier of a peer. KadHash supports both by storing the PeerId and the new hash together.

Function Kademlia::initialize() has become obsolete and has been removed.
Function Kademlia::without_init() has been marked deprecated.

This PR includes minor fixes for warnings.

Addresses: #694

@elferdo elferdo requested a review from tomaka March 25, 2019 17:27
@ghost ghost assigned elferdo Mar 25, 2019
@ghost ghost added the in progress label Mar 25, 2019
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/kad_hash.rs Outdated Show resolved Hide resolved
protocols/kad/src/kad_hash.rs Outdated Show resolved Hide resolved
}

fn max_distance() -> NonZeroUsize {
<Multihash as KBucketsPeerId>::max_distance()
Copy link
Member

@tomaka tomaka Mar 25, 2019

Choose a reason for hiding this comment

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

If we change the hash to a [u8; 32], then that should be 256, and distance_with adjusted accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed this as well. I think we could abstract the xor metric away, but I'm missing a trait for .leading_zeros() in U512 and U256. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also for the case where we compare KadHash and Multihash I have taken max_distance to be 512 and converted both hashes to U512.

Copy link
Member

Choose a reason for hiding this comment

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

I think we should split distance_with and max_distance in two different traits, something like:

trait MaxDistance: DistanceWith<Self> {
    fn max_distance() -> u32;
}

trait DistanceWith<TOther> {
    fn distance_with(&self, other: &TOther) -> u32;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would make that a new PR

protocols/kad/src/kbucket.rs Outdated Show resolved Hide resolved
@elferdo elferdo marked this pull request as ready for review March 26, 2019 15:16
@elferdo elferdo merged commit 603c7be into libp2p:master Mar 26, 2019
@ghost ghost removed the in progress label Mar 26, 2019
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