Skip to content

core: Add Serialize and Deserialize to PeerId and MessageId #2405

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext/websocket"]
websocket = ["libp2p-websocket"]
yamux = ["libp2p-yamux"]
secp256k1 = ["libp2p-core/secp256k1"]
serde = ["libp2p-core/use-serde"]

[package.metadata.docs.rs]
all-features = true
Expand All @@ -72,7 +73,7 @@ futures-timer = "3.0.2" # Explicit dependency to be used in `wasm-bindgen` featu
getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
lazy_static = "1.2"
libp2p-core = { version = "0.31.0", path = "core", default-features = false }
libp2p-core = { version = "0.31.0", path = "core", default-features = false }
libp2p-floodsub = { version = "0.33.0", path = "protocols/floodsub", optional = true }
libp2p-gossipsub = { version = "0.35.0", path = "./protocols/gossipsub", optional = true }
libp2p-identify = { version = "0.33.0", path = "protocols/identify", optional = true }
Expand Down
24 changes: 18 additions & 6 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ lazy_static = "1.2"
libsecp256k1 = { version = "0.7.0", optional = true }
log = "0.4"
multiaddr = { version = "0.13.0" }
multihash = { version = "0.14", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] }
multihash = { version = "0.14", default-features = false, features = [
"std",
"multihash-impl",
"identity",
"sha2",
] }
multistream-select = { version = "0.11", path = "../misc/multistream-select" }
p256 = { version = "0.10.0", default-features = false, features = ["ecdsa"], optional = true }
p256 = { version = "0.10.0", default-features = false, features = [
"ecdsa",
], optional = true }
parking_lot = "0.11.0"
pin-project = "1.0.0"
prost = "0.9"
Expand All @@ -37,9 +44,13 @@ thiserror = "1.0"
unsigned-varint = "0.7"
void = "1"
zeroize = "1"
serde = { version = "1", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
ring = { version = "0.16.9", features = [
"alloc",
"std",
], default-features = false }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand All @@ -56,9 +67,10 @@ rand07 = { package = "rand", version = "0.7" }
prost-build = "0.9"

[features]
default = [ "secp256k1", "ecdsa" ]
secp256k1 = [ "libsecp256k1" ]
ecdsa = [ "p256" ]
default = ["secp256k1", "ecdsa"]
secp256k1 = ["libsecp256k1"]
ecdsa = ["p256"]
use-serde = ["multihash/serde-codec", "serde"]

[[bench]]
name = "peer_id"
Expand Down
4 changes: 4 additions & 0 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ use rand::Rng;
use std::{convert::TryFrom, fmt, str::FromStr};
use thiserror::Error;

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

/// 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;

/// Identifier of a peer of the network.
///
/// The data is a multihash of the public key of the peer.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Comment on lines +27 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
/// 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;
/// Identifier of a peer of the network.
///
/// The data is a multihash of the public key of the peer.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// 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;
/// Identifier of a peer of the network.
///
/// The data is a multihash of the public key of the peer.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

This would contain it to a single line.

#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PeerId {
multihash: Multihash,
Expand Down
1 change: 1 addition & 0 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pin-project = "1.0.8"
instant = "0.1.11"
# Metrics dependencies
open-metrics-client = "0.13.0"
serde = { version = "1", optional = true }

[dev-dependencies]
async-std = "1.6.3"
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ impl GossipsubConfigBuilder {
}

/// Constructs a [`GossipsubConfig`] from the given configuration and validates the settings.
pub fn build(&self) -> Result<GossipsubConfig, &str> {
pub fn build(&self) -> Result<GossipsubConfig, &'static str> {
// check all constraints on config

if self.config.max_transmit_size < 100 {
Expand Down
4 changes: 4 additions & 0 deletions protocols/gossipsub/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use prost::Message;
use std::fmt;
use std::fmt::Debug;

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

#[derive(Debug)]
/// Validation kinds from the application for received messages.
pub enum MessageAcceptance {
Expand All @@ -42,6 +45,7 @@ pub enum MessageAcceptance {
/// Macro for declaring message id types
macro_rules! declare_message_id_type {
($name: ident, $name_string: expr) => {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct $name(pub Vec<u8>);

Expand Down