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(gossipsub)!: initialize ProtocolConfig from GossipsubConfig #3381

Merged
merged 5 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 3 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

- Update to `libp2p-swarm` `v0.42.0`.

- Initialize `ProtocolConfig` via `GossipsubConfig`. See [PR 3381].

[PR 3207]: https://github.com/libp2p/rust-libp2p/pull/3207/
[PR 3381]: https://github.com/libp2p/rust-libp2p/pull/3381/
Comment on lines +9 to +12
Copy link
Member

Choose a reason for hiding this comment

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

Why does this need a changelog entry? This is not user facing, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is (unfortunately) part of the public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to remove it again if you think that doesn't warrant an entry!

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I am surprised. No strong opinion. Let's keep as is 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it's something I'd like to fix. See linked discussion in PR description.


# 0.43.0

Expand Down
13 changes: 4 additions & 9 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3298,15 +3298,10 @@ where
type OutEvent = GossipsubEvent;

fn new_handler(&mut self) -> Self::ConnectionHandler {
let protocol_config = ProtocolConfig::new(
self.config.protocol_id().clone(),
self.config.custom_id_version().clone(),
self.config.max_transmit_size(),
self.config.validation_mode().clone(),
self.config.support_floodsub(),
);

GossipsubHandler::new(protocol_config, self.config.idle_timeout())
GossipsubHandler::new(
ProtocolConfig::new(&self.config),
self.config.idle_timeout(),
)
}

fn on_connection_handler_event(
Expand Down
15 changes: 3 additions & 12 deletions protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,12 +885,11 @@ impl std::fmt::Debug for GossipsubConfig {
#[cfg(test)]
mod test {
use super::*;
use crate::protocol::ProtocolConfig;
use crate::topic::IdentityHash;
use crate::types::PeerKind;
use crate::Topic;
use crate::{Gossipsub, MessageAuthenticity};
use libp2p_core::UpgradeInfo;
use libp2p_swarm::{ConnectionHandler, NetworkBehaviour};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -992,11 +991,7 @@ mod test {
assert_eq!(builder.protocol_id(), "purple");
assert_eq!(builder.custom_id_version(), &None);

let mut gossipsub: Gossipsub =
Gossipsub::new(MessageAuthenticity::Anonymous, builder).expect("Correct configuration");

let handler = gossipsub.new_handler();
let (protocol_config, _) = handler.listen_protocol().into_upgrade();
let protocol_config = ProtocolConfig::new(&builder);
let protocol_ids = protocol_config.protocol_info();

assert_eq!(protocol_ids.len(), 2);
Expand All @@ -1020,11 +1015,7 @@ mod test {
assert_eq!(builder.protocol_id(), "purple");
assert_eq!(builder.custom_id_version(), &Some(GossipsubVersion::V1_0));

let mut gossipsub: Gossipsub =
Gossipsub::new(MessageAuthenticity::Anonymous, builder).expect("Correct configuration");

let handler = gossipsub.new_handler();
let (protocol_config, _) = handler.listen_protocol().into_upgrade();
let protocol_config = ProtocolConfig::new(&builder);
let protocol_ids = protocol_config.protocol_info();

assert_eq!(protocol_ids.len(), 1);
Expand Down
48 changes: 27 additions & 21 deletions protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use crate::config::{GossipsubVersion, ValidationMode};
use crate::error::{GossipsubHandlerError, ValidationError};
use crate::handler::HandlerEvent;
use crate::rpc_proto;
use crate::topic::TopicHash;
use crate::types::{
GossipsubControlAction, GossipsubRpc, GossipsubSubscription, GossipsubSubscriptionAction,
MessageId, PeerInfo, PeerKind, RawGossipsubMessage,
};
use crate::{rpc_proto, GossipsubConfig};
use asynchronous_codec::{Decoder, Encoder, Framed};
use byteorder::{BigEndian, ByteOrder};
use bytes::BytesMut;
Expand All @@ -37,7 +37,7 @@ use libp2p_core::{
};
use log::{debug, warn};
use prost::Message as ProtobufMessage;
use std::{borrow::Cow, pin::Pin};
use std::pin::Pin;
use unsigned_varint::codec;

pub(crate) const SIGNING_PREFIX: &[u8] = b"libp2p-pubsub:";
Expand All @@ -57,27 +57,33 @@ impl ProtocolConfig {
/// Builds a new [`ProtocolConfig`].
///
/// Sets the maximum gossip transmission size.
pub fn new(
id: Cow<'static, str>,
custom_id_peer_kind: Option<GossipsubVersion>,
max_transmit_size: usize,
validation_mode: ValidationMode,
support_floodsub: bool,
) -> ProtocolConfig {
let protocol_ids = match custom_id_peer_kind {
pub fn new(gossipsub_config: &GossipsubConfig) -> ProtocolConfig {
let protocol_ids = match gossipsub_config.custom_id_version() {
Some(v) => match v {
GossipsubVersion::V1_0 => vec![ProtocolId::new(id, PeerKind::Gossipsub, false)],
GossipsubVersion::V1_1 => vec![ProtocolId::new(id, PeerKind::Gossipsubv1_1, false)],
GossipsubVersion::V1_0 => vec![ProtocolId::new(
gossipsub_config.protocol_id(),
PeerKind::Gossipsub,
false,
)],
GossipsubVersion::V1_1 => vec![ProtocolId::new(
gossipsub_config.protocol_id(),
PeerKind::Gossipsubv1_1,
false,
)],
},
None => {
let mut protocol_ids = vec![
ProtocolId::new(id.clone(), PeerKind::Gossipsubv1_1, true),
ProtocolId::new(id, PeerKind::Gossipsub, true),
ProtocolId::new(
gossipsub_config.protocol_id(),
PeerKind::Gossipsubv1_1,
true,
),
ProtocolId::new(gossipsub_config.protocol_id(), PeerKind::Gossipsub, true),
];

// add floodsub support if enabled.
if support_floodsub {
protocol_ids.push(ProtocolId::new(Cow::from(""), PeerKind::Floodsub, false));
if gossipsub_config.support_floodsub() {
protocol_ids.push(ProtocolId::new("", PeerKind::Floodsub, false));
}

protocol_ids
Expand All @@ -86,8 +92,8 @@ impl ProtocolConfig {

ProtocolConfig {
protocol_ids,
max_transmit_size,
validation_mode,
max_transmit_size: gossipsub_config.max_transmit_size(),
validation_mode: gossipsub_config.validation_mode().clone(),
}
}
}
Expand All @@ -103,15 +109,15 @@ pub struct ProtocolId {

/// An RPC protocol ID.
impl ProtocolId {
pub fn new(id: Cow<'static, str>, kind: PeerKind, prefix: bool) -> Self {
pub fn new(id: &str, kind: PeerKind, prefix: bool) -> Self {
let protocol_id = match kind {
PeerKind::Gossipsubv1_1 => match prefix {
true => format!("/{}/{}", id, "1.1.0"),
false => format!("{id}"),
false => id.to_string(),
},
PeerKind::Gossipsub => match prefix {
true => format!("/{}/{}", id, "1.0.0"),
false => format!("{id}"),
false => id.to_string(),
},
PeerKind::Floodsub => format!("/{}/{}", "floodsub", "1.0.0"),
// NOTE: This is used for informing the behaviour of unsupported peers. We do not
Expand Down