Skip to content

*: Add Serialize and Deserialize to PeerId gossipsub MessageId and kad Key #2408

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

Merged
merged 17 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions 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/serde"]

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ thiserror = "1.0"
unsigned-varint = "0.7"
void = "1"
zeroize = "1"
_serde = { package = "serde", version = "1", optional = true, features = ["derive"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
Expand All @@ -59,6 +60,7 @@ prost-build = "0.9"
default = [ "secp256k1", "ecdsa" ]
secp256k1 = [ "libsecp256k1" ]
ecdsa = [ "p256" ]
serde = ["multihash/serde-codec", "_serde"]

[[bench]]
name = "peer_id"
Expand Down
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
//! define how to upgrade each individual substream to use a protocol.
//! See the `upgrade` module.

#[cfg(feature = "serde")]
extern crate _serde as serde;

mod keys_proto {
include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ use rand::Rng;
use std::{convert::TryFrom, fmt, str::FromStr};
use thiserror::Error;

#[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;

/// 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))]
#[cfg_attr(feature = "serde", serde(crate = "_serde"))]
#[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 @@ -30,6 +30,7 @@ regex = "1.4.0"
futures-timer = "3.0.2"
pin-project = "1.0.8"
instant = "0.1.11"
serde = { version = "1", optional = true, features = ["derive"] }
# Metrics dependencies
open-metrics-client = "0.13.0"

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::{Deserialize, Serialize};

#[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