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

refactor: don't depend on multihash features #3514

Merged
merged 13 commits into from
Mar 30, 2023
Merged
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ instant = "0.1.11"
libp2p-identity = { version = "0.1", path = "../identity", features = ["peerid", "ed25519"] }
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"] }
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
multistream-select = { version = "0.12.1", path = "../misc/multistream-select" }
once_cell = "1.17.1"
parking_lot = "0.12.0"
Expand Down
4 changes: 2 additions & 2 deletions identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand Down
8 changes: 8 additions & 0 deletions identity/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl Keypair {
#[cfg(feature = "ed25519")]
pub fn into_ed25519(self) -> Option<ed25519::Keypair> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
Keypair::Ed25519(inner) => Some(inner),
_ => None,
Expand All @@ -119,6 +120,7 @@ impl Keypair {
#[cfg(feature = "secp256k1")]
pub fn into_secp256k1(self) -> Option<secp256k1::Keypair> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
Keypair::Secp256k1(inner) => Some(inner),
_ => None,
Expand All @@ -128,6 +130,7 @@ impl Keypair {
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn into_rsa(self) -> Option<rsa::Keypair> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
Keypair::Rsa(inner) => Some(inner),
_ => None,
Expand All @@ -137,6 +140,7 @@ impl Keypair {
#[cfg(feature = "ecdsa")]
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
Keypair::Ecdsa(inner) => Some(inner),
_ => None,
Expand Down Expand Up @@ -354,6 +358,7 @@ impl PublicKey {
#[cfg(feature = "ed25519")]
pub fn into_ed25519(self) -> Option<ed25519::PublicKey> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
PublicKey::Ed25519(inner) => Some(inner),
_ => None,
Expand All @@ -363,6 +368,7 @@ impl PublicKey {
#[cfg(feature = "secp256k1")]
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
PublicKey::Secp256k1(inner) => Some(inner),
_ => None,
Expand All @@ -372,6 +378,7 @@ impl PublicKey {
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
PublicKey::Rsa(inner) => Some(inner),
_ => None,
Expand All @@ -381,6 +388,7 @@ impl PublicKey {
#[cfg(feature = "ecdsa")]
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
#[allow(deprecated)]
#[allow(unreachable_patterns)]
match self {
PublicKey::Ecdsa(inner) => Some(inner),
_ => None,
Expand Down
31 changes: 20 additions & 11 deletions identity/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@
// DEALINGS IN THE SOFTWARE.

use multiaddr::{Multiaddr, Protocol};
use multihash::{Code, Error, Multihash};
use multihash::{Code, Error, MultihashGeneric};
use rand::Rng;
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>;
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
/// 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
Expand All @@ -55,18 +66,16 @@ impl fmt::Display for PeerId {
impl PeerId {
/// Builds a `PeerId` from a public key.
pub fn from_public_key(key: &crate::keypair::PublicKey) -> PeerId {
use multihash::MultihashDigest as _;

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 }
}

Expand All @@ -82,9 +91,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<PeerId, Multihash> {
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),
Expand Down
11 changes: 7 additions & 4 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ 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::{
connection::ConnectedPoint,
multiaddr::{multiaddr, Multiaddr, Protocol},
multihash::{Code, Multihash, MultihashDigest},
multihash::Multihash,
transport::MemoryTransport,
upgrade, Endpoint, Transport,
};
Expand Down Expand Up @@ -138,7 +138,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(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()).unwrap()
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1100,7 +1100,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(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());
let record_trudy = Record::new(key.clone(), b"trudy".to_vec());

Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/kbucket/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Distance {
#[cfg(test)]
mod tests {
use super::*;
use libp2p_core::multihash::Code;
use crate::SHA_256_MH;
use quickcheck::*;

impl Arbitrary for Key<PeerId> {
Expand All @@ -208,7 +208,7 @@ mod tests {
impl Arbitrary for Key<Multihash> {
fn arbitrary(g: &mut Gen) -> Key<Multihash> {
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(SHA_256_MH, &hash).unwrap())
}
}

Expand Down
4 changes: 4 additions & 0 deletions protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 5 additions & 7 deletions protocols/kad/src/query/peers/closest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ enum PeerState {
#[cfg(test)]
mod tests {
use super::*;
use libp2p_core::multihash::{Code, Multihash};
use crate::SHA_256_MH;
use libp2p_core::multihash::Multihash;
use libp2p_identity::PeerId;
use quickcheck::*;
use rand::{rngs::StdRng, Rng, SeedableRng};
Expand All @@ -484,10 +485,8 @@ mod tests {
fn random_peers<R: Rng>(n: usize, g: &mut R) -> Vec<PeerId> {
(0..n)
.map(|_| {
PeerId::from_multihash(
Multihash::wrap(Code::Sha2_256.into(), &g.gen::<[u8; 32]>()).unwrap(),
)
.unwrap()
PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &g.gen::<[u8; 32]>()).unwrap())
.unwrap()
})
.collect()
}
Expand All @@ -505,8 +504,7 @@ mod tests {
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();
PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &hash).unwrap()).unwrap();
ArbitraryPeerId(peer_id)
}
}
Expand Down
7 changes: 3 additions & 4 deletions protocols/kad/src/query/peers/closest/disjoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ impl<I: Iterator<Item = Key<PeerId>>> Iterator for ResultIter<I> {
mod tests {
use super::*;

use crate::K_VALUE;
use libp2p_core::multihash::{Code, Multihash};
use crate::{K_VALUE, SHA_256_MH};
use libp2p_core::multihash::Multihash;
use quickcheck::*;
use std::collections::HashSet;
use std::iter;
Expand Down Expand Up @@ -531,8 +531,7 @@ mod tests {
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();
PeerId::from_multihash(Multihash::wrap(SHA_256_MH, &hash).unwrap()).unwrap();
ArbitraryPeerId(peer_id)
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ impl ProviderRecord {
#[cfg(test)]
mod tests {
use super::*;
use libp2p_core::multihash::Code;
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(Code::Sha2_256.into(), &hash).unwrap())
Key::from(Multihash::wrap(SHA_256_MH, &hash).unwrap())
}
}

Expand Down
5 changes: 3 additions & 2 deletions protocols/kad/src/record/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ impl RecordStore for MemoryStore {
#[cfg(test)]
mod tests {
use super::*;
use libp2p_core::multihash::{Code, Multihash};
use crate::SHA_256_MH;
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(SHA_256_MH, &rand::thread_rng().gen::<[u8; 32]>()).unwrap()
}

fn distance(r: &ProviderRecord) -> kbucket::Distance {
Expand Down
3 changes: 2 additions & 1 deletion transports/webrtc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ libp2p-core = { version = "0.39.0", path = "../../core" }
libp2p-noise = { version = "0.42.0", path = "../../transports/noise" }
libp2p-identity = { version = "0.1.0", path = "../../identity" }
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 }
quick-protobuf = "0.8"
quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" }
rand = "0.8"
Expand Down
Loading