mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 09:41:34 +00:00
refactor(gossipsub)!: initialize ProtocolConfig
from GossipsubConfig
(#3381)
This simplifies the tests as we don't have to go through the `new_handler` abstraction.
This commit is contained in:
@ -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
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
@ -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,
|
||||
);
|
||||
|
||||
|
@ -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};
|
||||
|
||||
@ -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);
|
||||
@ -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);
|
||||
|
@ -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;
|
||||
@ -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:";
|
||||
@ -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
|
||||
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
Reference in New Issue
Block a user