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

to/into consistency for PublicKey and PeerId #2145

Merged
merged 7 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@

# `libp2p` facade crate

## Version 0.40.0 [unreleased]

- Update individual crates.
- `libp2p-core`
- `libp2p-deflate`
- `libp2p-dns`
- `libp2p-floodsub`
- `libp2p-gossipsub`
- `libp2p-identify`
- `libp2p-kad`
- `libp2p-mdns`
- `libp2p-mplex`
- `libp2p-noise`
- `libp2p-ping`
- `libp2p-plaintext`
- `libp2p-relay`
- `libp2p-request-response`
- `libp2p-swarm`
- `libp2p-tcp`
- `libp2p-uds`
- `libp2p-wasm-ext`
- `libp2p-websocket`
- `libp2p-yamux`

## Version 0.39.1 [2021-07-12]

- Update individual crates.
Expand Down
42 changes: 21 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p"
edition = "2018"
description = "Peer-to-peer networking library"
version = "0.39.1"
version = "0.40.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down Expand Up @@ -64,35 +64,35 @@ atomic = "0.5.0"
bytes = "1"
futures = "0.3.1"
lazy_static = "1.2"
libp2p-core = { version = "0.29.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.30.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.32.0", path = "./protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.30.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.31.0", path = "protocols/kad", optional = true }
libp2p-mplex = { version = "0.29.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.32.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.30.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.29.0", path = "transports/plaintext", optional = true }
libp2p-core = { version = "0.30.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.31.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.33.0", path = "./protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.31.0", path = "protocols/identify", optional = true }
libp2p-kad = { version = "0.32.0", path = "protocols/kad", optional = true }
libp2p-mplex = { version = "0.30.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.31.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.21.0", path = "transports/pnet", optional = true }
libp2p-relay = { version = "0.3.0", path = "protocols/relay", optional = true }
libp2p-request-response = { version = "0.12.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.30.0", path = "swarm" }
libp2p-relay = { version = "0.4.0", path = "protocols/relay", optional = true }
libp2p-request-response = { version = "0.13.0", path = "protocols/request-response", optional = true }
libp2p-swarm = { version = "0.31.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.24.0", path = "swarm-derive" }
libp2p-uds = { version = "0.29.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.29.0", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.33.0", path = "muxers/yamux", optional = true }
libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.30.0", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.34.0", path = "muxers/yamux", optional = true }
multiaddr = { version = "0.13.0" }
parking_lot = "0.11.0"
pin-project = "1.0.0"
smallvec = "1.6.1"
wasm-timer = "0.2.4"

[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.29.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.29.0", path = "transports/dns", optional = true, default-features = false }
libp2p-mdns = { version = "0.31.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.29.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.30.0", path = "transports/websocket", optional = true }
libp2p-deflate = { version = "0.30.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.30.0", path = "transports/dns", optional = true, default-features = false }
libp2p-mdns = { version = "0.32.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.31.0", path = "transports/websocket", optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand Down
12 changes: 12 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 0.30.0 [unreleased]

- Change `PublicKey::into_protobuf_encoding` to `PublicKey::to_protobuf_encoding` (see [PR 2145]).

- Change `PublicKey::into_peer_id` to `PublicKey::to_peer_id` (see [PR 2145]).

- Change `PeerId::from_public_key(PublicKey)` to `PeerId::from_public_key(&PublicKey)` (see [PR 2145]).

- Add `From<&PublicKey> for PeerId` (see [PR 2145]).

[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145

# 0.29.0 [2021-07-12]

- Switch from `parity-multiaddr` to upstream `multiaddr`.
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-core"
edition = "2018"
description = "Core traits and structs of libp2p"
version = "0.29.0"
version = "0.30.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
6 changes: 3 additions & 3 deletions core/benches/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use libp2p_core::{identity, PeerId};
fn from_bytes(c: &mut Criterion) {
let peer_id_bytes = identity::Keypair::generate_ed25519()
.public()
.into_peer_id()
.to_peer_id()
.to_bytes();

c.bench_function("from_bytes", |b| {
Expand All @@ -37,7 +37,7 @@ fn from_bytes(c: &mut Criterion) {
fn clone(c: &mut Criterion) {
let peer_id = identity::Keypair::generate_ed25519()
.public()
.into_peer_id();
.to_peer_id();

c.bench_function("clone", |b| {
b.iter(|| {
Expand All @@ -51,7 +51,7 @@ fn sort_vec(c: &mut Criterion) {
.map(|_| {
identity::Keypair::generate_ed25519()
.public()
.into_peer_id()
.to_peer_id()
})
.collect();

Expand Down
6 changes: 3 additions & 3 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl PublicKey {

/// Encode the public key into a protobuf structure for storage or
/// exchange with other nodes.
pub fn into_protobuf_encoding(self) -> Vec<u8> {
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
use prost::Message;

let public_key = match self {
Expand Down Expand Up @@ -245,7 +245,7 @@ impl PublicKey {
}

/// Convert the `PublicKey` into the corresponding `PeerId`.
pub fn into_peer_id(self) -> PeerId {
pub fn to_peer_id(&self) -> PeerId {
self.into()
}
}
Expand All @@ -264,7 +264,7 @@ mod tests {
let encoded = base64::decode(base_64_encoded).unwrap();

let keypair = Keypair::from_protobuf_encoding(&encoded).unwrap();
let peer_id = keypair.public().into_peer_id();
let peer_id = keypair.public().to_peer_id();

assert_eq!(expected_peer_id, peer_id);
}
Expand Down
18 changes: 12 additions & 6 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl fmt::Display for PeerId {

impl PeerId {
/// Builds a `PeerId` from a public key.
pub fn from_public_key(key: PublicKey) -> PeerId {
let key_enc = key.into_protobuf_encoding();
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
Expand Down Expand Up @@ -114,13 +114,19 @@ impl PeerId {
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
let alg = Code::try_from(self.multihash.code())
.expect("Internal multihash is always a valid `Code`");
let enc = public_key.clone().into_protobuf_encoding();
let enc = public_key.to_protobuf_encoding();
Some(alg.digest(&enc) == self.multihash)
}
}

impl From<PublicKey> for PeerId {
fn from(key: PublicKey) -> PeerId {
PeerId::from_public_key(&key)
}
}

impl From<&PublicKey> for PeerId {
fn from(key: &PublicKey) -> PeerId {
PeerId::from_public_key(key)
}
}
Expand Down Expand Up @@ -184,20 +190,20 @@ mod tests {
#[test]
fn peer_id_is_public_key() {
let key = identity::Keypair::generate_ed25519().public();
let peer_id = key.clone().into_peer_id();
let peer_id = key.to_peer_id();
assert_eq!(peer_id.is_public_key(&key), Some(true));
}

#[test]
fn peer_id_into_bytes_then_from_bytes() {
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();
let second = PeerId::from_bytes(&peer_id.to_bytes()).unwrap();
assert_eq!(peer_id, second);
}

#[test]
fn peer_id_to_base58_then_back() {
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();
let second: PeerId = peer_id.to_base58().parse().unwrap();
assert_eq!(peer_id, second);
}
Expand Down
4 changes: 2 additions & 2 deletions core/tests/transport_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
#[test]
fn upgrade_pipeline() {
let listener_keys = identity::Keypair::generate_ed25519();
let listener_id = listener_keys.public().into_peer_id();
let listener_id = listener_keys.public().to_peer_id();
let listener_noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&listener_keys).unwrap();
let listener_transport = MemoryTransport::default()
.upgrade(upgrade::Version::V1)
Expand All @@ -96,7 +96,7 @@ fn upgrade_pipeline() {
});

let dialer_keys = identity::Keypair::generate_ed25519();
let dialer_id = dialer_keys.public().into_peer_id();
let dialer_id = dialer_keys.public().to_peer_id();
let dialer_noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&dialer_keys).unwrap();
let dialer_transport = MemoryTransport::default()
.upgrade(upgrade::Version::V1)
Expand Down
2 changes: 1 addition & 1 deletion misc/multistream-select/tests/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type TestNetwork = Network<TestTransport, (), (), TestHandler>;

fn mk_transport(up: upgrade::Version) -> (PeerId, TestTransport) {
let keys = identity::Keypair::generate_ed25519();
let id = keys.public().into_peer_id();
let id = keys.public().to_peer_id();
(id, MemoryTransport::default()
.upgrade(up)
.authenticate(PlainText2Config { local_public_key: keys.public() })
Expand Down
2 changes: 1 addition & 1 deletion misc/peer-id-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() {
thread::spawn(move || loop {
let keypair = identity::ed25519::Keypair::generate();
let secret = keypair.secret();
let peer_id = identity::PublicKey::Ed25519(keypair.public()).into_peer_id();
let peer_id = identity::PublicKey::Ed25519(keypair.public()).to_peer_id();
let base58 = peer_id.to_base58();
if base58[2..].starts_with(&prefix) {
println!("Found {:?}", peer_id);
Expand Down
4 changes: 4 additions & 0 deletions muxers/mplex/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.30.0 [unreleased]

- Update dependencies.

# 0.29.0 [2021-07-12]

- Update dependencies.
Expand Down
4 changes: 2 additions & 2 deletions muxers/mplex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-mplex"
edition = "2018"
description = "Mplex multiplexing protocol for libp2p"
version = "0.29.0"
version = "0.30.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"]
bytes = "1"
futures = "0.3.1"
asynchronous-codec = "0.6"
libp2p-core = { version = "0.29.0", path = "../../core" }
libp2p-core = { version = "0.30.0", path = "../../core" }
log = "0.4"
nohash-hasher = "0.2"
parking_lot = "0.11"
Expand Down
4 changes: 4 additions & 0 deletions muxers/yamux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.34.0 [unreleased]

- Update dependencies.

# 0.33.0 [2021-07-12]

- Update dependencies.
Expand Down
4 changes: 2 additions & 2 deletions muxers/yamux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-yamux"
edition = "2018"
description = "Yamux multiplexing protocol for libp2p"
version = "0.33.0"
version = "0.34.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]

[dependencies]
futures = "0.3.1"
libp2p-core = { version = "0.29.0", path = "../../core" }
libp2p-core = { version = "0.30.0", path = "../../core" }
parking_lot = "0.11"
thiserror = "1.0"
yamux = "0.9.0"
4 changes: 4 additions & 0 deletions protocols/floodsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.31.0 [unreleased]

- Update dependencies.

# 0.30.0 [2021-07-12]

- Update dependencies.
Expand Down
6 changes: 3 additions & 3 deletions protocols/floodsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-floodsub"
edition = "2018"
description = "Floodsub protocol for libp2p"
version = "0.30.0"
version = "0.31.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -13,8 +13,8 @@ categories = ["network-programming", "asynchronous"]
cuckoofilter = "0.5.0"
fnv = "1.0"
futures = "0.3.1"
libp2p-core = { version = "0.29.0", path = "../../core" }
libp2p-swarm = { version = "0.30.0", path = "../../swarm" }
libp2p-core = { version = "0.30.0", path = "../../core" }
libp2p-swarm = { version = "0.31.0", path = "../../swarm" }
log = "0.4"
prost = "0.8"
rand = "0.7"
Expand Down
4 changes: 4 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.33.0 [unreleased]

- Update dependencies.

# 0.32.0 [2021-07-12]

- Update dependencies.
Expand Down
6 changes: 3 additions & 3 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
name = "libp2p-gossipsub"
edition = "2018"
description = "Gossipsub protocol for libp2p"
version = "0.32.0"
version = "0.33.0"
authors = ["Age Manning <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
libp2p-swarm = { version = "0.30.0", path = "../../swarm" }
libp2p-core = { version = "0.29.0", path = "../../core" }
libp2p-swarm = { version = "0.31.0", path = "../../swarm" }
libp2p-core = { version = "0.30.0", path = "../../core" }
bytes = "1.0"
byteorder = "1.3.4"
fnv = "1.0.7"
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl From<MessageAuthenticity> for PublishConfig {
match authenticity {
MessageAuthenticity::Signed(keypair) => {
let public_key = keypair.public();
let key_enc = public_key.clone().into_protobuf_encoding();
let key_enc = public_key.to_protobuf_encoding();
let key = if key_enc.len() <= 42 {
// The public key can be inlined in [`rpc_proto::Message::from`], so we don't include it
// specifically in the [`rpc_proto::Message::key`] field.
Expand All @@ -181,7 +181,7 @@ impl From<MessageAuthenticity> for PublishConfig {

PublishConfig::Signing {
keypair,
author: public_key.into_peer_id(),
author: public_key.to_peer_id(),
inline_key: key,
}
}
Expand Down
Loading