Temporary canonical multihash in peer ID. (#1449)

* Temporary canonical multihash in peer ID.

* Reduce code duplication.

* Remove unnecessary impls.

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
Roman Borschel
2020-02-13 10:36:14 +01:00
committed by GitHub
parent 8238fd2100
commit b872bd9030
9 changed files with 77 additions and 63 deletions

View File

@ -22,7 +22,7 @@ use crate::PublicKey;
use bs58; use bs58;
use thiserror::Error; use thiserror::Error;
use multihash; use multihash;
use std::{convert::TryFrom, fmt, hash, str::FromStr}; use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr};
/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be /// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
/// automatically used as the peer id using an identity multihash. /// automatically used as the peer id using an identity multihash.
@ -35,6 +35,11 @@ const _MAX_INLINE_KEY_LENGTH: usize = 42;
#[derive(Clone, Eq)] #[derive(Clone, Eq)]
pub struct PeerId { pub struct PeerId {
multihash: multihash::Multihash, multihash: multihash::Multihash,
/// A (temporary) "canonical" multihash if `multihash` is of type
/// multihash::Hash::Identity, so that `Borrow<[u8]>` semantics
/// can be given, i.e. a view of a byte representation whose
/// equality is consistent with `PartialEq`.
canonical: Option<multihash::Multihash>,
} }
impl fmt::Debug for PeerId { impl fmt::Debug for PeerId {
@ -64,15 +69,19 @@ impl PeerId {
// will switch to not hashing the key (i.e. the correct behaviour). // will switch to not hashing the key (i.e. the correct behaviour).
// In other words, rust-libp2p 0.16 is compatible with all versions of rust-libp2p. // In other words, rust-libp2p 0.16 is compatible with all versions of rust-libp2p.
// Rust-libp2p 0.12 and below is **NOT** compatible with rust-libp2p 0.17 and above. // Rust-libp2p 0.12 and below is **NOT** compatible with rust-libp2p 0.17 and above.
let hash_algorithm = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH { let (hash_algorithm, canonical_algorithm) = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
multihash::Hash::Identity (multihash::Hash::Identity, Some(multihash::Hash::SHA2256))
} else {*/ } else {*/
multihash::Hash::SHA2256; (multihash::Hash::SHA2256, None);
//}; //};
let canonical = canonical_algorithm.map(|alg|
multihash::encode(alg, &key_enc).expect("SHA2256 is always supported"));
let multihash = multihash::encode(hash_algorithm, &key_enc) let multihash = multihash::encode(hash_algorithm, &key_enc)
.expect("identity and sha2-256 are always supported by known public key types"); .expect("identity and sha2-256 are always supported by known public key types");
PeerId { multihash }
PeerId { multihash, canonical }
} }
/// Checks whether `data` is a valid `PeerId`. If so, returns the `PeerId`. If not, returns /// Checks whether `data` is a valid `PeerId`. If so, returns the `PeerId`. If not, returns
@ -80,10 +89,13 @@ impl PeerId {
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> { pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
match multihash::Multihash::from_bytes(data) { match multihash::Multihash::from_bytes(data) {
Ok(multihash) => { Ok(multihash) => {
if multihash.algorithm() == multihash::Hash::SHA2256 if multihash.algorithm() == multihash::Hash::SHA2256 {
|| multihash.algorithm() == multihash::Hash::Identity Ok(PeerId { multihash, canonical: None })
{ }
Ok(PeerId { multihash }) else if multihash.algorithm() == multihash::Hash::Identity {
let canonical = multihash::encode(multihash::Hash::SHA2256, multihash.digest())
.expect("SHA2256 is always supported");
Ok(PeerId { multihash, canonical: Some(canonical) })
} else { } else {
Err(multihash.into_bytes()) Err(multihash.into_bytes())
} }
@ -95,8 +107,12 @@ impl PeerId {
/// Turns a `Multihash` into a `PeerId`. If the multihash doesn't use the correct algorithm, /// Turns a `Multihash` into a `PeerId`. If the multihash doesn't use the correct algorithm,
/// returns back the data as an error. /// returns back the data as an error.
pub fn from_multihash(data: multihash::Multihash) -> Result<PeerId, multihash::Multihash> { pub fn from_multihash(data: multihash::Multihash) -> Result<PeerId, multihash::Multihash> {
if data.algorithm() == multihash::Hash::SHA2256 || data.algorithm() == multihash::Hash::Identity { if data.algorithm() == multihash::Hash::SHA2256 {
Ok(PeerId { multihash: data }) Ok(PeerId { multihash: data, canonical: None })
} else if data.algorithm() == multihash::Hash::Identity {
let canonical = multihash::encode(multihash::Hash::SHA2256, data.digest())
.expect("SHA2256 is always supported");
Ok(PeerId { multihash: data, canonical: Some(canonical) })
} else { } else {
Err(data) Err(data)
} }
@ -107,27 +123,32 @@ impl PeerId {
/// This is useful for randomly walking on a DHT, or for testing purposes. /// This is useful for randomly walking on a DHT, or for testing purposes.
pub fn random() -> PeerId { pub fn random() -> PeerId {
PeerId { PeerId {
multihash: multihash::Multihash::random(multihash::Hash::SHA2256) multihash: multihash::Multihash::random(multihash::Hash::SHA2256),
canonical: None,
} }
} }
/// Returns a raw bytes representation of this `PeerId`. /// Returns a raw bytes representation of this `PeerId`.
/// ///
/// Note that this is not the same as the public key of the peer. /// **NOTE:** This byte representation is not necessarily consistent with
/// equality of peer IDs. That is, two peer IDs may be considered equal
/// while having a different byte representation as per `into_bytes`.
pub fn into_bytes(self) -> Vec<u8> { pub fn into_bytes(self) -> Vec<u8> {
self.multihash.into_bytes() self.multihash.into_bytes()
} }
/// Returns a raw bytes representation of this `PeerId`. /// Returns a raw bytes representation of this `PeerId`.
/// ///
/// Note that this is not the same as the public key of the peer. /// **NOTE:** This byte representation is not necessarily consistent with
/// equality of peer IDs. That is, two peer IDs may be considered equal
/// while having a different byte representation as per `as_bytes`.
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
self.multihash.as_bytes() self.multihash.as_bytes()
} }
/// Returns a base-58 encoded string of this `PeerId`. /// Returns a base-58 encoded string of this `PeerId`.
pub fn to_base58(&self) -> String { pub fn to_base58(&self) -> String {
bs58::encode(self.multihash.as_bytes()).into_string() bs58::encode(self.borrow() as &[u8]).into_string()
} }
/// Checks whether the public key passed as parameter matches the public key of this `PeerId`. /// Checks whether the public key passed as parameter matches the public key of this `PeerId`.
@ -150,17 +171,8 @@ impl hash::Hash for PeerId {
where where
H: hash::Hasher H: hash::Hasher
{ {
match self.multihash.algorithm() { let digest = self.borrow() as &[u8];
multihash::Hash::Identity => { hash::Hash::hash(digest, state)
let sha256 = multihash::encode(multihash::Hash::SHA2256, self.multihash.digest())
.expect("encoding a SHA2256 multihash never fails; qed");
hash::Hash::hash(sha256.digest(), state)
},
multihash::Hash::SHA2256 => {
hash::Hash::hash(self.multihash.digest(), state)
},
_ => unreachable!("PeerId can only be built from Identity or SHA2256; qed")
}
} }
} }
@ -189,34 +201,21 @@ impl TryFrom<multihash::Multihash> for PeerId {
impl PartialEq<PeerId> for PeerId { impl PartialEq<PeerId> for PeerId {
fn eq(&self, other: &PeerId) -> bool { fn eq(&self, other: &PeerId) -> bool {
match (self.multihash.algorithm(), other.multihash.algorithm()) { let self_digest = self.borrow() as &[u8];
(multihash::Hash::SHA2256, multihash::Hash::SHA2256) => { let other_digest = other.borrow() as &[u8];
self.multihash.digest() == other.multihash.digest() self_digest == other_digest
},
(multihash::Hash::Identity, multihash::Hash::Identity) => {
self.multihash.digest() == other.multihash.digest()
},
(multihash::Hash::SHA2256, multihash::Hash::Identity) => {
multihash::encode(multihash::Hash::SHA2256, other.multihash.digest())
.map(|mh| mh == self.multihash)
.unwrap_or(false)
},
(multihash::Hash::Identity, multihash::Hash::SHA2256) => {
multihash::encode(multihash::Hash::SHA2256, self.multihash.digest())
.map(|mh| mh == other.multihash)
.unwrap_or(false)
},
_ => false
}
} }
} }
// TODO: The semantics of that function aren't very precise. It is possible for two `PeerId`s to impl Borrow<[u8]> for PeerId {
// compare equal while their bytes representation are not. Right now, this `AsRef` fn borrow(&self) -> &[u8] {
// implementation is only used to define precedence over two `PeerId`s in case of a self.canonical.as_ref().map_or(self.multihash.as_bytes(), |c| c.as_bytes())
// simultaneous connection between two nodes. Since the simultaneous connection system }
// is planned to be removed (https://github.com/libp2p/rust-libp2p/issues/912), we went for }
// we keeping that function with the intent of removing it as soon as possible.
/// **NOTE:** This byte representation is not necessarily consistent with
/// equality of peer IDs. That is, two peer IDs may be considered equal
/// while having a different byte representation as per `AsRef<[u8]>`.
impl AsRef<[u8]> for PeerId { impl AsRef<[u8]> for PeerId {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
self.as_bytes() self.as_bytes()

View File

@ -13,6 +13,7 @@ use std::{convert::TryFrom, fmt::Write};
use bytes::{BufMut, Bytes, BytesMut}; use bytes::{BufMut, Bytes, BytesMut};
use rand::RngCore; use rand::RngCore;
use sha2::digest::{self, VariableOutput}; use sha2::digest::{self, VariableOutput};
use std::borrow::Borrow;
use unsigned_varint::{decode, encode}; use unsigned_varint::{decode, encode};
pub use self::errors::{DecodeError, DecodeOwnedError, EncodeError}; pub use self::errors::{DecodeError, DecodeOwnedError, EncodeError};
@ -245,6 +246,12 @@ impl AsRef<[u8]> for Multihash {
} }
} }
impl Borrow<[u8]> for Multihash {
fn borrow(&self) -> &[u8] {
self.as_bytes()
}
}
impl<'a> PartialEq<MultihashRef<'a>> for Multihash { impl<'a> PartialEq<MultihashRef<'a>> for Multihash {
fn eq(&self, other: &MultihashRef<'a>) -> bool { fn eq(&self, other: &MultihashRef<'a>) -> bool {
&*self.bytes == other.bytes &*self.bytes == other.bytes

View File

@ -101,7 +101,7 @@ impl Floodsub {
/// Remove a node from the list of nodes to propagate messages to. /// Remove a node from the list of nodes to propagate messages to.
#[inline] #[inline]
pub fn remove_node_from_partial_view(&mut self, peer_id: &PeerId) { pub fn remove_node_from_partial_view(&mut self, peer_id: &PeerId) {
self.target_peers.remove(&peer_id); self.target_peers.remove(peer_id);
} }
/// Subscribes to a topic. /// Subscribes to a topic.

View File

@ -578,7 +578,7 @@ impl Gossipsub {
"Handling subscriptions: {:?}, from source: {:?}", "Handling subscriptions: {:?}, from source: {:?}",
subscriptions, propagation_source subscriptions, propagation_source
); );
let subscribed_topics = match self.peer_topics.get_mut(&propagation_source) { let subscribed_topics = match self.peer_topics.get_mut(propagation_source) {
Some(topics) => topics, Some(topics) => topics,
None => { None => {
error!("Subscription by unknown peer: {:?}", &propagation_source); error!("Subscription by unknown peer: {:?}", &propagation_source);
@ -841,7 +841,7 @@ impl Gossipsub {
}) })
.collect(); .collect();
let mut prunes: Vec<GossipsubControlAction> = to_prune let mut prunes: Vec<GossipsubControlAction> = to_prune
.remove(&peer) .remove(peer)
.unwrap_or_else(|| vec![]) .unwrap_or_else(|| vec![])
.iter() .iter()
.map(|topic_hash| GossipsubControlAction::Prune { .map(|topic_hash| GossipsubControlAction::Prune {
@ -1028,7 +1028,7 @@ impl NetworkBehaviour for Gossipsub {
// remove from mesh, topic_peers, peer_topic and fanout // remove from mesh, topic_peers, peer_topic and fanout
debug!("Peer disconnected: {:?}", id); debug!("Peer disconnected: {:?}", id);
{ {
let topics = match self.peer_topics.get(&id) { let topics = match self.peer_topics.get(id) {
Some(topics) => (topics), Some(topics) => (topics),
None => { None => {
warn!("Disconnected node, not in connected nodes"); warn!("Disconnected node, not in connected nodes");

View File

@ -1100,7 +1100,7 @@ where
} }
for query in self.queries.iter_mut() { for query in self.queries.iter_mut() {
if let Some(addrs) = query.inner.addresses.get_mut(&peer_id) { if let Some(addrs) = query.inner.addresses.get_mut(peer_id) {
addrs.retain(|a| a != addr); addrs.retain(|a| a != addr);
} }
} }

View File

@ -409,8 +409,8 @@ fn put_record() {
let key = kbucket::Key::new(r.key.clone()); let key = kbucket::Key::new(r.key.clone());
let mut expected = swarm_ids.clone().split_off(1); let mut expected = swarm_ids.clone().split_off(1);
expected.sort_by(|id1, id2| expected.sort_by(|id1, id2|
kbucket::Key::new(id1).distance(&key).cmp( kbucket::Key::new(id1.clone()).distance(&key).cmp(
&kbucket::Key::new(id2).distance(&key))); &kbucket::Key::new(id2.clone()).distance(&key)));
let expected = expected let expected = expected
.into_iter() .into_iter()
@ -608,8 +608,8 @@ fn add_provider() {
let mut expected = swarm_ids.clone().split_off(1); let mut expected = swarm_ids.clone().split_off(1);
let kbucket_key = kbucket::Key::new(key); let kbucket_key = kbucket::Key::new(key);
expected.sort_by(|id1, id2| expected.sort_by(|id1, id2|
kbucket::Key::new(id1).distance(&kbucket_key).cmp( kbucket::Key::new(id1.clone()).distance(&kbucket_key).cmp(
&kbucket::Key::new(id2).distance(&kbucket_key))); &kbucket::Key::new(id2.clone()).distance(&kbucket_key)));
let expected = expected let expected = expected
.into_iter() .into_iter()

View File

@ -23,6 +23,7 @@ use libp2p_core::PeerId;
use multihash::Multihash; use multihash::Multihash;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use sha2::digest::generic_array::{GenericArray, typenum::U32}; use sha2::digest::generic_array::{GenericArray, typenum::U32};
use std::borrow::Borrow;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
construct_uint! { construct_uint! {
@ -51,9 +52,9 @@ impl<T> Key<T> {
/// [`Key::into_preimage`]. /// [`Key::into_preimage`].
pub fn new(preimage: T) -> Key<T> pub fn new(preimage: T) -> Key<T>
where where
T: AsRef<[u8]> T: Borrow<[u8]>
{ {
let bytes = KeyBytes::new(&preimage); let bytes = KeyBytes::new(preimage.borrow());
Key { preimage, bytes } Key { preimage, bytes }
} }

View File

@ -25,6 +25,7 @@ pub mod store;
use bytes::Bytes; use bytes::Bytes;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use multihash::Multihash; use multihash::Multihash;
use std::borrow::Borrow;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use wasm_timer::Instant; use wasm_timer::Instant;
@ -44,6 +45,12 @@ impl Key {
} }
} }
impl Borrow<[u8]> for Key {
fn borrow(&self) -> &[u8] {
&self.0[..]
}
}
impl AsRef<[u8]> for Key { impl AsRef<[u8]> for Key {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
&self.0[..] &self.0[..]

View File

@ -191,7 +191,7 @@ impl<'a> RecordStore<'a> for MemoryStore {
} }
fn providers(&'a self, key: &Key) -> Vec<ProviderRecord> { fn providers(&'a self, key: &Key) -> Vec<ProviderRecord> {
self.providers.get(&key).map_or_else(Vec::new, |ps| ps.clone().into_vec()) self.providers.get(key).map_or_else(Vec::new, |ps| ps.clone().into_vec())
} }
fn provided(&'a self) -> Self::ProvidedIter { fn provided(&'a self) -> Self::ProvidedIter {