From fabceaf4e5050e64091da3b675ca8b48b54c9047 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 27 Feb 2023 18:22:08 +1100 Subject: [PATCH 1/8] Don't depend on multihash features --- Cargo.lock | 3 +-- core/Cargo.toml | 2 +- core/src/peer_id.rs | 24 ++++++++++++------- protocols/kad/src/behaviour/test.rs | 9 ++++--- protocols/kad/src/kbucket/key.rs | 3 +-- protocols/kad/src/query/peers/closest.rs | 15 ++++-------- .../kad/src/query/peers/closest/disjoint.rs | 6 ++--- protocols/kad/src/record.rs | 3 +-- protocols/kad/src/record/store/memory.rs | 4 ++-- transports/webrtc/Cargo.toml | 3 ++- transports/webrtc/src/tokio/fingerprint.rs | 23 +++++++----------- 11 files changed, 44 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5e93182a37..e3ab99249a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2827,6 +2827,7 @@ dependencies = [ "rand 0.8.5", "rcgen 0.9.3", "serde", + "sha2 0.10.6", "stun", "thiserror", "tinytemplate", @@ -3113,13 +3114,11 @@ checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" dependencies = [ "arbitrary", "core2", - "digest 0.10.6", "multihash-derive", "quickcheck", "rand 0.8.5", "serde", "serde-big-array", - "sha2 0.10.6", "unsigned-varint", ] diff --git a/core/Cargo.toml b/core/Cargo.toml index bb38bdc3973..09107c811cb 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -22,7 +22,7 @@ instant = "0.1.11" libsecp256k1 = { version = "0.7.0", optional = true } log = "0.4" multiaddr = { version = "0.17.0" } -multihash = { version = "0.17.0", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] } +multihash = { version = "0.17.0", default-features = false, features = ["std"] } multistream-select = { version = "0.12.1", path = "../misc/multistream-select" } p256 = { version = "0.11.1", default-features = false, features = ["ecdsa", "std"], optional = true } parking_lot = "0.12.0" diff --git a/core/src/peer_id.rs b/core/src/peer_id.rs index 014f1fc240d..8a9303200ea 100644 --- a/core/src/peer_id.rs +++ b/core/src/peer_id.rs @@ -20,11 +20,14 @@ use crate::PublicKey; use multiaddr::{Multiaddr, Protocol}; -use multihash::{Code, Error, Multihash, MultihashDigest}; +use multihash::{Code, Error, MultihashDigest, MultihashGeneric}; use rand::Rng; +use sha2::Digest as _; use std::{convert::TryFrom, fmt, str::FromStr}; use thiserror::Error; +type Multihash = MultihashGeneric<64>; + #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -32,6 +35,9 @@ use serde::{Deserialize, Serialize}; /// automatically used as the peer id using an identity multihash. const MAX_INLINE_KEY_LENGTH: usize = 42; +const MULTIHASH_IDENTITY_CODE: u64 = 0; +const MULTIHASH_SHA256_CODE: u64 = 0x12; + /// Identifier of a peer of the network. /// /// The data is a CIDv0 compatible multihash of the protobuf encoded public key of the peer @@ -58,14 +64,14 @@ impl PeerId { pub fn from_public_key(key: &PublicKey) -> PeerId { let key_enc = key.to_protobuf_encoding(); - let hash_algorithm = if key_enc.len() <= MAX_INLINE_KEY_LENGTH { - Code::Identity + let multihash = if key_enc.len() <= MAX_INLINE_KEY_LENGTH { + Multihash::wrap(MULTIHASH_IDENTITY_CODE, &key_enc) + .expect("64 byte multihash provides sufficient space") } else { - Code::Sha2_256 + Multihash::wrap(MULTIHASH_SHA256_CODE, &sha2::Sha256::digest(key_enc)) + .expect("64 byte multihash provides sufficient space") }; - let multihash = hash_algorithm.digest(&key_enc); - PeerId { multihash } } @@ -81,9 +87,9 @@ impl PeerId { /// or the hash value does not satisfy the constraints for a hashed /// peer ID, it is returned as an `Err`. pub fn from_multihash(multihash: Multihash) -> Result { - match Code::try_from(multihash.code()) { - Ok(Code::Sha2_256) => Ok(PeerId { multihash }), - Ok(Code::Identity) if multihash.digest().len() <= MAX_INLINE_KEY_LENGTH => { + match multihash.code() { + MULTIHASH_SHA256_CODE => Ok(PeerId { multihash }), + MULTIHASH_IDENTITY_CODE if multihash.digest().len() <= MAX_INLINE_KEY_LENGTH => { Ok(PeerId { multihash }) } _ => Err(multihash), diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 2f51eaef52d..49ded04fcd6 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -31,7 +31,7 @@ use libp2p_core::{ connection::ConnectedPoint, identity, multiaddr::{multiaddr, Multiaddr, Protocol}, - multihash::{Code, Multihash, MultihashDigest}, + multihash::Multihash, transport::MemoryTransport, upgrade, Endpoint, PeerId, Transport, }; @@ -137,7 +137,7 @@ fn build_fully_connected_nodes_with_config( } fn random_multihash() -> Multihash { - Multihash::wrap(Code::Sha2_256.into(), &thread_rng().gen::<[u8; 32]>()).unwrap() + Multihash::wrap(0x12, &thread_rng().gen::<[u8; 32]>()).unwrap() } #[derive(Clone, Debug)] @@ -1099,7 +1099,10 @@ fn disjoint_query_does_not_finish_before_all_paths_did() { let mut trudy = build_node(); // Trudy the intrudor, an adversary. let mut bob = build_node(); - let key = Key::from(Code::Sha2_256.digest(&thread_rng().gen::<[u8; 32]>())); + let key = Key::from( + Multihash::wrap(0x12, &thread_rng().gen::<[u8; 32]>()) + .expect("32 array to fit into 64 byte multihash"), + ); let record_bob = Record::new(key.clone(), b"bob".to_vec()); let record_trudy = Record::new(key.clone(), b"trudy".to_vec()); diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 62401c46095..7e99bce7c19 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -195,7 +195,6 @@ impl Distance { #[cfg(test)] mod tests { use super::*; - use libp2p_core::multihash::Code; use quickcheck::*; impl Arbitrary for Key { @@ -207,7 +206,7 @@ mod tests { impl Arbitrary for Key { fn arbitrary(g: &mut Gen) -> Key { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - Key::from(Multihash::wrap(Code::Sha2_256.into(), &hash).unwrap()) + Key::from(Multihash::wrap(0x12, &hash).unwrap()) } } diff --git a/protocols/kad/src/query/peers/closest.rs b/protocols/kad/src/query/peers/closest.rs index d56f6ce5160..9655fff11a4 100644 --- a/protocols/kad/src/query/peers/closest.rs +++ b/protocols/kad/src/query/peers/closest.rs @@ -475,10 +475,7 @@ enum PeerState { #[cfg(test)] mod tests { use super::*; - use libp2p_core::{ - multihash::{Code, Multihash}, - PeerId, - }; + use libp2p_core::{multihash::Multihash, PeerId}; use quickcheck::*; use rand::{rngs::StdRng, Rng, SeedableRng}; use std::{iter, time::Duration}; @@ -486,10 +483,8 @@ mod tests { fn random_peers(n: usize, g: &mut R) -> Vec { (0..n) .map(|_| { - PeerId::from_multihash( - Multihash::wrap(Code::Sha2_256.into(), &g.gen::<[u8; 32]>()).unwrap(), - ) - .unwrap() + PeerId::from_multihash(Multihash::wrap(0x12, &g.gen::<[u8; 32]>()).unwrap()) + .unwrap() }) .collect() } @@ -506,9 +501,7 @@ mod tests { impl Arbitrary for ArbitraryPeerId { fn arbitrary(g: &mut Gen) -> ArbitraryPeerId { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - let peer_id = - PeerId::from_multihash(Multihash::wrap(Code::Sha2_256.into(), &hash).unwrap()) - .unwrap(); + let peer_id = PeerId::from_multihash(Multihash::wrap(0x12, &hash).unwrap()).unwrap(); ArbitraryPeerId(peer_id) } } diff --git a/protocols/kad/src/query/peers/closest/disjoint.rs b/protocols/kad/src/query/peers/closest/disjoint.rs index d8713bb33d2..daf56c280bb 100644 --- a/protocols/kad/src/query/peers/closest/disjoint.rs +++ b/protocols/kad/src/query/peers/closest/disjoint.rs @@ -445,7 +445,7 @@ mod tests { use super::*; use crate::K_VALUE; - use libp2p_core::multihash::{Code, Multihash}; + use libp2p_core::multihash::Multihash; use quickcheck::*; use std::collections::HashSet; use std::iter; @@ -530,9 +530,7 @@ mod tests { impl Arbitrary for ArbitraryPeerId { fn arbitrary(g: &mut Gen) -> ArbitraryPeerId { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - let peer_id = - PeerId::from_multihash(Multihash::wrap(Code::Sha2_256.into(), &hash).unwrap()) - .unwrap(); + let peer_id = PeerId::from_multihash(Multihash::wrap(0x12, &hash).unwrap()).unwrap(); ArbitraryPeerId(peer_id) } } diff --git a/protocols/kad/src/record.rs b/protocols/kad/src/record.rs index 0da60e704e9..f03aa0ab43c 100644 --- a/protocols/kad/src/record.rs +++ b/protocols/kad/src/record.rs @@ -160,14 +160,13 @@ impl ProviderRecord { #[cfg(test)] mod tests { use super::*; - use libp2p_core::multihash::Code; use quickcheck::*; use std::time::Duration; impl Arbitrary for Key { fn arbitrary(g: &mut Gen) -> Key { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - Key::from(Multihash::wrap(Code::Sha2_256.into(), &hash).unwrap()) + Key::from(Multihash::wrap(0x12, &hash).unwrap()) } } diff --git a/protocols/kad/src/record/store/memory.rs b/protocols/kad/src/record/store/memory.rs index 87951637889..e90549c6687 100644 --- a/protocols/kad/src/record/store/memory.rs +++ b/protocols/kad/src/record/store/memory.rs @@ -216,12 +216,12 @@ impl RecordStore for MemoryStore { #[cfg(test)] mod tests { use super::*; - use libp2p_core::multihash::{Code, Multihash}; + use libp2p_core::multihash::Multihash; use quickcheck::*; use rand::Rng; fn random_multihash() -> Multihash { - Multihash::wrap(Code::Sha2_256.into(), &rand::thread_rng().gen::<[u8; 32]>()).unwrap() + Multihash::wrap(0x12, &rand::thread_rng().gen::<[u8; 32]>()).unwrap() } fn distance(r: &ProviderRecord) -> kbucket::Distance { diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 990d09101a7..83e01e23c1d 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -21,7 +21,8 @@ if-watch = "3.0" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-noise = { version = "0.42.0", path = "../../transports/noise" } log = "0.4" -multihash = { version = "0.17.0", default-features = false, features = ["sha2"] } +sha2 = "0.10.6" +multihash = { version = "0.17.0", default-features = false } prost = "0.11" prost-codec = { version = "0.3.0", path = "../../misc/prost-codec" } rand = "0.8" diff --git a/transports/webrtc/src/tokio/fingerprint.rs b/transports/webrtc/src/tokio/fingerprint.rs index 8a03044cc7d..e61299f2dea 100644 --- a/transports/webrtc/src/tokio/fingerprint.rs +++ b/transports/webrtc/src/tokio/fingerprint.rs @@ -18,12 +18,15 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use multihash::{Code, Hasher, Multihash, MultihashDigest}; -use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint; - +use multihash::MultihashGeneric; +use sha2::Digest as _; use std::fmt; +use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint; const SHA256: &str = "sha-256"; +const MULTIHASH_SHA256_CODE: u64 = 0x12; + +type Multihash = MultihashGeneric<64>; /// A certificate fingerprint that is assumed to be created using the SHA256 hash algorithm. #[derive(Eq, PartialEq, Copy, Clone)] @@ -39,13 +42,7 @@ impl Fingerprint { /// Creates a fingerprint from a raw certificate. pub fn from_certificate(bytes: &[u8]) -> Self { - let mut h = multihash::Sha2_256::default(); - h.update(bytes); - - let mut bytes: [u8; 32] = [0; 32]; - bytes.copy_from_slice(h.finalize()); - - Fingerprint(bytes) + Fingerprint(sha2::Sha256::digest(bytes).into()) } /// Converts [`RTCDtlsFingerprint`] to [`Fingerprint`]. @@ -62,7 +59,7 @@ impl Fingerprint { /// Converts [`type@Multihash`] to [`Fingerprint`]. pub fn try_from_multihash(hash: Multihash) -> Option { - if hash.code() != u64::from(Code::Sha2_256) { + if hash.code() != MULTIHASH_SHA256_CODE { // Only support SHA256 for now. return None; } @@ -74,9 +71,7 @@ impl Fingerprint { /// Converts this fingerprint to [`type@Multihash`]. pub fn to_multihash(self) -> Multihash { - Code::Sha2_256 - .wrap(&self.0) - .expect("fingerprint's len to be 32 bytes") + Multihash::wrap(MULTIHASH_SHA256_CODE, &self.0).expect("fingerprint's len to be 32 bytes") } /// Formats this fingerprint as uppercase hex, separated by colons (`:`). From 7f1b0027248215de10c502346ef2310542d0b265 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 13 Mar 2023 08:35:28 +0100 Subject: [PATCH 2/8] Fix compile error --- identity/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 46c257a0bb2..b26ed5cf9dd 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -38,7 +38,7 @@ secp256k1 = [ "libsecp256k1", "asn1_der", "prost", "rand", "sha2", "zeroize" ] ecdsa = [ "p256", "prost", "rand", "void", "zeroize", "sec1" ] rsa = [ "dep:ring", "asn1_der", "prost", "rand", "zeroize" ] ed25519 = [ "ed25519-dalek", "prost", "rand", "zeroize" ] -peerid = [ "multihash", "multiaddr", "bs58", "rand", "thiserror" ] +peerid = [ "multihash", "multiaddr", "bs58", "rand", "thiserror", "sha2" ] [dev-dependencies] quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" } From 951f198a40dccd813e55c49526c0d4dfcb5354bb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 13 Mar 2023 08:35:41 +0100 Subject: [PATCH 3/8] Fix warnings --- identity/src/keypair.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 2c1f565d4ed..2597f684917 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -110,6 +110,7 @@ impl Keypair { #[cfg(feature = "ed25519")] pub fn into_ed25519(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { Keypair::Ed25519(inner) => Some(inner), _ => None, @@ -119,6 +120,7 @@ impl Keypair { #[cfg(feature = "secp256k1")] pub fn into_secp256k1(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { Keypair::Secp256k1(inner) => Some(inner), _ => None, @@ -128,6 +130,7 @@ impl Keypair { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn into_rsa(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { Keypair::Rsa(inner) => Some(inner), _ => None, @@ -137,6 +140,7 @@ impl Keypair { #[cfg(feature = "ecdsa")] pub fn into_ecdsa(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { Keypair::Ecdsa(inner) => Some(inner), _ => None, @@ -322,6 +326,7 @@ impl PublicKey { #[cfg(feature = "ed25519")] pub fn into_ed25519(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { PublicKey::Ed25519(inner) => Some(inner), _ => None, @@ -331,6 +336,7 @@ impl PublicKey { #[cfg(feature = "secp256k1")] pub fn into_secp256k1(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { PublicKey::Secp256k1(inner) => Some(inner), _ => None, @@ -340,6 +346,7 @@ impl PublicKey { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn into_rsa(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { PublicKey::Rsa(inner) => Some(inner), _ => None, @@ -349,6 +356,7 @@ impl PublicKey { #[cfg(feature = "ecdsa")] pub fn into_ecdsa(self) -> Option { #[allow(deprecated)] + #[allow(unreachable_patterns)] match self { PublicKey::Ecdsa(inner) => Some(inner), _ => None, From 37eb7f59bfe261ced275c792ea5ee3ea6dd4acff Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 13 Mar 2023 08:35:45 +0100 Subject: [PATCH 4/8] Fix formatting --- protocols/kad/src/query/peers/closest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/kad/src/query/peers/closest.rs b/protocols/kad/src/query/peers/closest.rs index 3a75cf76f08..815f37e5559 100644 --- a/protocols/kad/src/query/peers/closest.rs +++ b/protocols/kad/src/query/peers/closest.rs @@ -475,7 +475,7 @@ enum PeerState { #[cfg(test)] mod tests { use super::*; - use libp2p_core::multihash::{Multihash}; + use libp2p_core::multihash::Multihash; use libp2p_identity::PeerId; use quickcheck::*; use rand::{rngs::StdRng, Rng, SeedableRng}; From f598e3c32792a2948ca588f59be71c48b32cb1b5 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 13:38:11 +0100 Subject: [PATCH 5/8] Actually remove features --- Cargo.lock | 2 -- identity/Cargo.toml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 157cf237a09..8a61959345c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3135,13 +3135,11 @@ checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" dependencies = [ "arbitrary", "core2", - "digest 0.10.6", "multihash-derive", "quickcheck", "rand 0.8.5", "serde", "serde-big-array", - "sha2 0.10.6", "unsigned-varint", ] diff --git a/identity/Cargo.toml b/identity/Cargo.toml index b26ed5cf9dd..cca4233be8b 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -18,7 +18,7 @@ ed25519-dalek = { version = "1.0.1", optional = true } libsecp256k1 = { version = "0.7.0", optional = true } log = "0.4" multiaddr = { version = "0.17.0", optional = true } -multihash = { version = "0.17.0", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"], optional = true } +multihash = { version = "0.17.0", default-features = false, features = ["std"], optional = true } p256 = { version = "0.12", default-features = false, features = ["ecdsa", "std"], optional = true } prost = { version = "0.11", optional = true } quick-protobuf = "0.8.1" From b0677b583e979fe4fa67624123d13255fbe1885b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 13:44:55 +0100 Subject: [PATCH 6/8] Fix rustdoc --- transports/webrtc/src/tokio/fingerprint.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transports/webrtc/src/tokio/fingerprint.rs b/transports/webrtc/src/tokio/fingerprint.rs index e61299f2dea..3776f0dc24e 100644 --- a/transports/webrtc/src/tokio/fingerprint.rs +++ b/transports/webrtc/src/tokio/fingerprint.rs @@ -57,7 +57,7 @@ impl Fingerprint { Some(Self(buf)) } - /// Converts [`type@Multihash`] to [`Fingerprint`]. + /// Converts [`Multihash`](MultihashGeneric) to [`Fingerprint`]. pub fn try_from_multihash(hash: Multihash) -> Option { if hash.code() != MULTIHASH_SHA256_CODE { // Only support SHA256 for now. @@ -69,7 +69,7 @@ impl Fingerprint { Some(Self(bytes)) } - /// Converts this fingerprint to [`type@Multihash`]. + /// Converts this fingerprint to [`Multihash`](MultihashGeneric). pub fn to_multihash(self) -> Multihash { Multihash::wrap(MULTIHASH_SHA256_CODE, &self.0).expect("fingerprint's len to be 32 bytes") } From 393a184050ee82b22254685836b0c30614914d6e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 15:20:00 +0100 Subject: [PATCH 7/8] Document origin of 64 --- identity/src/peer_id.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 5e0d3849de3..1a96f4311e9 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -25,6 +25,11 @@ use sha2::Digest as _; use std::{convert::TryFrom, fmt, str::FromStr}; use thiserror::Error; +/// Local type-alias for multihash. +/// +/// Must be big enough to accommodate for `MAX_INLINE_KEY_LENGTH`. +/// 64 satisfies that and can hold 512 bit hashes which is what the ecosystem typically uses. +/// Given that this appears in our type-signature, using a "common" number here makes us more compatible. type Multihash = MultihashGeneric<64>; #[cfg(feature = "serde")] From c61e95c01b575d3e1ed955563decd86350b1b728 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 15:23:00 +0100 Subject: [PATCH 8/8] Avoid magic numbers --- protocols/kad/src/behaviour/test.rs | 6 +++--- protocols/kad/src/kbucket/key.rs | 3 ++- protocols/kad/src/lib.rs | 4 ++++ protocols/kad/src/query/peers/closest.rs | 6 ++++-- protocols/kad/src/query/peers/closest/disjoint.rs | 5 +++-- protocols/kad/src/record.rs | 3 ++- protocols/kad/src/record/store/memory.rs | 3 ++- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 2ba0d8534c6..33e48377e0b 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -24,7 +24,7 @@ use super::*; use crate::kbucket::Distance; use crate::record::{store::MemoryStore, Key}; -use crate::K_VALUE; +use crate::{K_VALUE, SHA_256_MH}; use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures_timer::Delay; use libp2p_core::{ @@ -138,7 +138,7 @@ fn build_fully_connected_nodes_with_config( } fn random_multihash() -> Multihash { - Multihash::wrap(0x12, &thread_rng().gen::<[u8; 32]>()).unwrap() + Multihash::wrap(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()).unwrap() } #[derive(Clone, Debug)] @@ -1101,7 +1101,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() { let mut bob = build_node(); let key = Key::from( - Multihash::wrap(0x12, &thread_rng().gen::<[u8; 32]>()) + Multihash::wrap(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()) .expect("32 array to fit into 64 byte multihash"), ); let record_bob = Record::new(key.clone(), b"bob".to_vec()); diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index e6e105a5442..af2999ebea5 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -196,6 +196,7 @@ impl Distance { #[cfg(test)] mod tests { use super::*; + use crate::SHA_256_MH; use quickcheck::*; impl Arbitrary for Key { @@ -207,7 +208,7 @@ mod tests { impl Arbitrary for Key { fn arbitrary(g: &mut Gen) -> Key { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - Key::from(Multihash::wrap(0x12, &hash).unwrap()) + Key::from(Multihash::wrap(SHA_256_MH, &hash).unwrap()) } } diff --git a/protocols/kad/src/lib.rs b/protocols/kad/src/lib.rs index 3e9dcecc0e4..979aedc963f 100644 --- a/protocols/kad/src/lib.rs +++ b/protocols/kad/src/lib.rs @@ -100,3 +100,7 @@ pub const K_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) }; /// /// The current value is `3`. pub const ALPHA_VALUE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) }; + +/// Constant shared across tests for the [`Multihash`](libp2p_core::multihash::Multihash) type. +#[cfg(test)] +const SHA_256_MH: u64 = 0x12; diff --git a/protocols/kad/src/query/peers/closest.rs b/protocols/kad/src/query/peers/closest.rs index 815f37e5559..a6611c1b962 100644 --- a/protocols/kad/src/query/peers/closest.rs +++ b/protocols/kad/src/query/peers/closest.rs @@ -475,6 +475,7 @@ enum PeerState { #[cfg(test)] mod tests { use super::*; + use crate::SHA_256_MH; use libp2p_core::multihash::Multihash; use libp2p_identity::PeerId; use quickcheck::*; @@ -484,7 +485,7 @@ mod tests { fn random_peers(n: usize, g: &mut R) -> Vec { (0..n) .map(|_| { - PeerId::from_multihash(Multihash::wrap(0x12, &g.gen::<[u8; 32]>()).unwrap()) + PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &g.gen::<[u8; 32]>()).unwrap()) .unwrap() }) .collect() @@ -502,7 +503,8 @@ mod tests { impl Arbitrary for ArbitraryPeerId { fn arbitrary(g: &mut Gen) -> ArbitraryPeerId { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - let peer_id = PeerId::from_multihash(Multihash::wrap(0x12, &hash).unwrap()).unwrap(); + let peer_id = + PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &hash).unwrap()).unwrap(); ArbitraryPeerId(peer_id) } } diff --git a/protocols/kad/src/query/peers/closest/disjoint.rs b/protocols/kad/src/query/peers/closest/disjoint.rs index e745fee38d1..61c9225745b 100644 --- a/protocols/kad/src/query/peers/closest/disjoint.rs +++ b/protocols/kad/src/query/peers/closest/disjoint.rs @@ -444,7 +444,7 @@ impl>> Iterator for ResultIter { mod tests { use super::*; - use crate::K_VALUE; + use crate::{K_VALUE, SHA_256_MH}; use libp2p_core::multihash::Multihash; use quickcheck::*; use std::collections::HashSet; @@ -530,7 +530,8 @@ mod tests { impl Arbitrary for ArbitraryPeerId { fn arbitrary(g: &mut Gen) -> ArbitraryPeerId { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - let peer_id = PeerId::from_multihash(Multihash::wrap(0x12, &hash).unwrap()).unwrap(); + let peer_id = + PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &hash).unwrap()).unwrap(); ArbitraryPeerId(peer_id) } } diff --git a/protocols/kad/src/record.rs b/protocols/kad/src/record.rs index 3068fa8dc67..2abe32d5894 100644 --- a/protocols/kad/src/record.rs +++ b/protocols/kad/src/record.rs @@ -161,13 +161,14 @@ impl ProviderRecord { #[cfg(test)] mod tests { use super::*; + use crate::SHA_256_MH; use quickcheck::*; use std::time::Duration; impl Arbitrary for Key { fn arbitrary(g: &mut Gen) -> Key { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - Key::from(Multihash::wrap(0x12, &hash).unwrap()) + Key::from(Multihash::wrap(SHA_256_MH, &hash).unwrap()) } } diff --git a/protocols/kad/src/record/store/memory.rs b/protocols/kad/src/record/store/memory.rs index c3e3f03e342..1d4caab3bd7 100644 --- a/protocols/kad/src/record/store/memory.rs +++ b/protocols/kad/src/record/store/memory.rs @@ -216,12 +216,13 @@ impl RecordStore for MemoryStore { #[cfg(test)] mod tests { use super::*; + use crate::SHA_256_MH; use libp2p_core::multihash::Multihash; use quickcheck::*; use rand::Rng; fn random_multihash() -> Multihash { - Multihash::wrap(0x12, &rand::thread_rng().gen::<[u8; 32]>()).unwrap() + Multihash::wrap(SHA_256_MH, &rand::thread_rng().gen::<[u8; 32]>()).unwrap() } fn distance(r: &ProviderRecord) -> kbucket::Distance {