Skip to content

Commit

Permalink
refactor(gossipsub)!: initialize ProtocolConfig from `GossipsubConf…
Browse files Browse the repository at this point in the history
…ig` (#3381)

This simplifies the tests as we don't have to go through the `new_handler` abstraction.
  • Loading branch information
thomaseizinger authored Jan 26, 2023
1 parent 4de54f0 commit e552022
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 44 deletions.
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/

# 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
3 changes: 1 addition & 2 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use async_std::net::Ipv4Addr;
use byteorder::{BigEndian, ByteOrder};
use libp2p_core::{ConnectedPoint, Endpoint};
use rand::Rng;
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::thread::sleep;
Expand Down Expand Up @@ -272,7 +271,7 @@ where
active_connections = active_connections.checked_sub(1).unwrap();

let dummy_handler = GossipsubHandler::new(
ProtocolConfig::new(Cow::from(""), None, 0, ValidationMode::None, false),
ProtocolConfig::new(&GossipsubConfig::default()),
Duration::ZERO,
);

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

0 comments on commit e552022

Please sign in to comment.