Skip to content

Commit

Permalink
Use upstream version of multihash instead of a fork
Browse files Browse the repository at this point in the history
The changes from the libp2p fork have been backported to upstream, hence
upstream can now be used instead.
  • Loading branch information
vmx committed Feb 26, 2020
1 parent 62ea7c1 commit ebfbfa7
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 806 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ secp256k1 = ["libp2p-core/secp256k1", "libp2p-secio/secp256k1"]
bytes = "0.5"
futures = "0.3.1"
multiaddr = { package = "parity-multiaddr", version = "0.7.2", path = "misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.2.1", path = "misc/multihash" }
multihash = "0.10"
lazy_static = "1.2"
libp2p-mplex = { version = "0.16.0", path = "muxers/mplex" }
libp2p-identify = { version = "0.16.0", path = "protocols/identify" }
Expand Down Expand Up @@ -56,7 +56,6 @@ members = [
"core",
"misc/core-derive",
"misc/multiaddr",
"misc/multihash",
"misc/multistream-select",
"misc/peer-id-generator",
"muxers/mplex",
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lazy_static = "1.2"
libsecp256k1 = { version = "0.3.1", optional = true }
log = "0.4"
multiaddr = { package = "parity-multiaddr", version = "0.7.2", path = "../misc/multiaddr" }
multihash = { package = "parity-multihash", version = "0.2.1", path = "../misc/multihash" }
multihash = "0.10"
multistream-select = { version = "0.7.0", path = "../misc/multistream-select" }
parking_lot = "0.10.0"
pin-project = "0.4.6"
Expand Down
57 changes: 26 additions & 31 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use crate::PublicKey;
use bs58;
use thiserror::Error;
use multihash;
use multihash::{self, Code, Sha2_256};
use rand::Rng;
use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr};

/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
Expand Down Expand Up @@ -69,17 +70,16 @@ impl PeerId {
// 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.
// Rust-libp2p 0.12 and below is **NOT** compatible with rust-libp2p 0.17 and above.
let (hash_algorithm, canonical_algorithm) = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
let (hash_algorithm, canonical_algorithm): (_, Option<Code>) = /*if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
(multihash::Hash::Identity, Some(multihash::Hash::SHA2256))
} else {*/
(multihash::Hash::SHA2256, None);
(Code::Sha2_256, None);
//};

let canonical = canonical_algorithm.map(|alg|
multihash::encode(alg, &key_enc).expect("SHA2256 is always supported"));
alg.hasher().expect("canonical hasher exists").digest(&key_enc));

let multihash = multihash::encode(hash_algorithm, &key_enc)
.expect("identity and sha2-256 are always supported by known public key types");
let multihash = hash_algorithm.hasher().expect("hasher exists").digest(&key_enc);

PeerId { multihash, canonical }
}
Expand All @@ -89,12 +89,11 @@ impl PeerId {
pub fn from_bytes(data: Vec<u8>) -> Result<PeerId, Vec<u8>> {
match multihash::Multihash::from_bytes(data) {
Ok(multihash) => {
if multihash.algorithm() == multihash::Hash::SHA2256 {
if multihash.algorithm() == multihash::Code::Sha2_256 {
Ok(PeerId { multihash, canonical: None })
}
else if multihash.algorithm() == multihash::Hash::Identity {
let canonical = multihash::encode(multihash::Hash::SHA2256, multihash.digest())
.expect("SHA2256 is always supported");
else if multihash.algorithm() == multihash::Code::Identity {
let canonical = Sha2_256::digest(&multihash.digest());
Ok(PeerId { multihash, canonical: Some(canonical) })
} else {
Err(multihash.into_bytes())
Expand All @@ -107,11 +106,10 @@ impl PeerId {
/// Turns a `Multihash` into a `PeerId`. If the multihash doesn't use the correct algorithm,
/// returns back the data as an error.
pub fn from_multihash(data: multihash::Multihash) -> Result<PeerId, multihash::Multihash> {
if data.algorithm() == multihash::Hash::SHA2256 {
if data.algorithm() == multihash::Code::Sha2_256 {
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");
} else if data.algorithm() == multihash::Code::Identity {
let canonical = Sha2_256::digest(data.digest());
Ok(PeerId { multihash: data, canonical: Some(canonical) })
} else {
Err(data)
Expand All @@ -122,8 +120,9 @@ impl PeerId {
///
/// This is useful for randomly walking on a DHT, or for testing purposes.
pub fn random() -> PeerId {
let peer_id = rand::thread_rng().gen::<[u8; 32]>();
PeerId {
multihash: multihash::Multihash::random(multihash::Hash::SHA2256),
multihash: multihash::wrap(multihash::Code::Sha2_256, &peer_id),
canonical: None,
}
}
Expand Down Expand Up @@ -158,11 +157,7 @@ impl PeerId {
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
let alg = self.multihash.algorithm();
let enc = public_key.clone().into_protobuf_encoding();
match multihash::encode(alg, &enc) {
Ok(h) => Some(h == self.multihash),
Err(multihash::EncodeError::UnsupportedType) => None,
Err(multihash::EncodeError::UnsupportedInputLength) => None,
}
Some(alg.hasher()?.digest(&enc) == self.multihash)
}
}

Expand Down Expand Up @@ -283,8 +278,8 @@ mod tests {
#[test]
fn peer_id_identity_equal_to_sha2256() {
let random_bytes = (0..64).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
let mh1 = multihash::encode(multihash::Hash::SHA2256, &random_bytes).unwrap();
let mh2 = multihash::encode(multihash::Hash::Identity, &random_bytes).unwrap();
let mh1 = multihash::Sha2_256::digest(&random_bytes);
let mh2 = multihash::Identity::digest(&random_bytes);
let peer_id1 = PeerId::try_from(mh1).unwrap();
let peer_id2 = PeerId::try_from(mh2).unwrap();
assert_eq!(peer_id1, peer_id2);
Expand All @@ -294,8 +289,8 @@ mod tests {
#[test]
fn peer_id_identity_hashes_equal_to_sha2256() {
let random_bytes = (0..64).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
let mh1 = multihash::encode(multihash::Hash::SHA2256, &random_bytes).unwrap();
let mh2 = multihash::encode(multihash::Hash::Identity, &random_bytes).unwrap();
let mh1 = multihash::Sha2_256::digest(&random_bytes);
let mh2 = multihash::Identity::digest(&random_bytes);
let peer_id1 = PeerId::try_from(mh1).unwrap();
let peer_id2 = PeerId::try_from(mh2).unwrap();

Expand All @@ -309,26 +304,26 @@ mod tests {

#[test]
fn peer_id_equal_across_algorithms() {
use multihash::Hash;
use multihash::Code;
use quickcheck::{Arbitrary, Gen};

#[derive(Debug, Clone, PartialEq, Eq)]
struct HashAlgo(Hash);
struct HashAlgo(Code);

impl Arbitrary for HashAlgo {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
match g.next_u32() % 4 { // make Hash::Identity more likely
0 => HashAlgo(Hash::SHA2256),
_ => HashAlgo(Hash::Identity)
0 => HashAlgo(Code::Sha2_256),
_ => HashAlgo(Code::Identity)
}
}
}

fn property(data: Vec<u8>, algo1: HashAlgo, algo2: HashAlgo) -> bool {
let a = PeerId::try_from(multihash::encode(algo1.0, &data).unwrap()).unwrap();
let b = PeerId::try_from(multihash::encode(algo2.0, &data).unwrap()).unwrap();
let a = PeerId::try_from(algo1.0.hasher().unwrap().digest(&data)).unwrap();
let b = PeerId::try_from(algo2.0.hasher().unwrap().digest(&data)).unwrap();

if algo1 == algo2 || algo1.0 == Hash::Identity || algo2.0 == Hash::Identity {
if algo1 == algo2 || algo1.0 == Code::Identity || algo2.0 == Code::Identity {
a == b
} else {
a != b
Expand Down
2 changes: 1 addition & 1 deletion misc/multiaddr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ arrayref = "0.3"
bs58 = "0.3.0"
byteorder = "1.3.1"
data-encoding = "2.1"
multihash = { package = "parity-multihash", version = "0.2.1", path = "../multihash" }
multihash = "0.10"
percent-encoding = "2.1.0"
serde = "1.0.70"
static_assertions = "1.1"
Expand Down
3 changes: 0 additions & 3 deletions misc/multihash/.gitignore

This file was deleted.

34 changes: 0 additions & 34 deletions misc/multihash/.travis.yml

This file was deleted.

19 changes: 0 additions & 19 deletions misc/multihash/Cargo.toml

This file was deleted.

21 changes: 0 additions & 21 deletions misc/multihash/LICENSE

This file was deleted.

66 changes: 0 additions & 66 deletions misc/multihash/src/errors.rs

This file was deleted.

Loading

0 comments on commit ebfbfa7

Please sign in to comment.