mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 01:51:23 +00:00
refactor(gossipsub): revise symbol naming to follow conventions (#3303)
Changes regarding the #2217
This commit is contained in:
@ -24,7 +24,7 @@ use std::time::Duration;
|
||||
|
||||
use libp2p_core::PeerId;
|
||||
|
||||
use crate::types::{FastMessageId, GossipsubMessage, MessageId, RawGossipsubMessage};
|
||||
use crate::types::{FastMessageId, Message, MessageId, RawMessage};
|
||||
|
||||
/// The types of message validation that can be employed by gossipsub.
|
||||
#[derive(Debug, Clone)]
|
||||
@ -51,16 +51,16 @@ pub enum ValidationMode {
|
||||
|
||||
/// Selector for custom Protocol Id
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum GossipsubVersion {
|
||||
pub enum Version {
|
||||
V1_0,
|
||||
V1_1,
|
||||
}
|
||||
|
||||
/// Configuration parameters that define the performance of the gossipsub network.
|
||||
#[derive(Clone)]
|
||||
pub struct GossipsubConfig {
|
||||
pub struct Config {
|
||||
protocol_id: Cow<'static, str>,
|
||||
custom_id_version: Option<GossipsubVersion>,
|
||||
custom_id_version: Option<Version>,
|
||||
history_length: usize,
|
||||
history_gossip: usize,
|
||||
mesh_n: usize,
|
||||
@ -78,9 +78,8 @@ pub struct GossipsubConfig {
|
||||
duplicate_cache_time: Duration,
|
||||
validate_messages: bool,
|
||||
validation_mode: ValidationMode,
|
||||
message_id_fn: Arc<dyn Fn(&GossipsubMessage) -> MessageId + Send + Sync + 'static>,
|
||||
fast_message_id_fn:
|
||||
Option<Arc<dyn Fn(&RawGossipsubMessage) -> FastMessageId + Send + Sync + 'static>>,
|
||||
message_id_fn: Arc<dyn Fn(&Message) -> MessageId + Send + Sync + 'static>,
|
||||
fast_message_id_fn: Option<Arc<dyn Fn(&RawMessage) -> FastMessageId + Send + Sync + 'static>>,
|
||||
allow_self_origin: bool,
|
||||
do_px: bool,
|
||||
prune_peers: usize,
|
||||
@ -101,22 +100,22 @@ pub struct GossipsubConfig {
|
||||
published_message_ids_cache_time: Duration,
|
||||
}
|
||||
|
||||
impl GossipsubConfig {
|
||||
impl Config {
|
||||
// All the getters
|
||||
|
||||
/// The protocol id to negotiate this protocol. By default, the resulting protocol id has the form
|
||||
/// `/<prefix>/<supported-versions>`, but can optionally be changed to a literal form by providing some GossipsubVersion as custom_id_version.
|
||||
/// `/<prefix>/<supported-versions>`, but can optionally be changed to a literal form by providing some Version as custom_id_version.
|
||||
/// As gossipsub supports version 1.0 and 1.1, there are two suffixes supported for the resulting protocol id.
|
||||
///
|
||||
/// Calling `GossipsubConfigBuilder::protocol_id_prefix` will set a new prefix and retain the prefix logic.
|
||||
/// Calling `GossipsubConfigBuilder::protocol_id` will set a custom `protocol_id` and disable the prefix logic.
|
||||
/// Calling [`ConfigBuilder::protocol_id_prefix`] will set a new prefix and retain the prefix logic.
|
||||
/// Calling [`ConfigBuilder::protocol_id`] will set a custom `protocol_id` and disable the prefix logic.
|
||||
///
|
||||
/// The default prefix is `meshsub`, giving the supported protocol ids: `/meshsub/1.1.0` and `/meshsub/1.0.0`, negotiated in that order.
|
||||
pub fn protocol_id(&self) -> &Cow<'static, str> {
|
||||
&self.protocol_id
|
||||
}
|
||||
|
||||
pub fn custom_id_version(&self) -> &Option<GossipsubVersion> {
|
||||
pub fn custom_id_version(&self) -> &Option<Version> {
|
||||
&self.custom_id_version
|
||||
}
|
||||
|
||||
@ -217,7 +216,7 @@ impl GossipsubConfig {
|
||||
|
||||
/// When set to `true`, prevents automatic forwarding of all received messages. This setting
|
||||
/// allows a user to validate the messages before propagating them to their peers. If set to
|
||||
/// true, the user must manually call [`crate::Gossipsub::report_message_validation_result()`]
|
||||
/// true, the user must manually call [`crate::Behaviour::report_message_validation_result()`]
|
||||
/// on the behaviour to forward message once validated (default is `false`).
|
||||
/// The default is `false`.
|
||||
pub fn validate_messages(&self) -> bool {
|
||||
@ -236,21 +235,21 @@ impl GossipsubConfig {
|
||||
/// addressing, where this function may be set to `hash(message)`. This would prevent messages
|
||||
/// of the same content from being duplicated.
|
||||
///
|
||||
/// The function takes a [`GossipsubMessage`] as input and outputs a String to be interpreted as
|
||||
/// The function takes a [`Message`] as input and outputs a String to be interpreted as
|
||||
/// the message id.
|
||||
pub fn message_id(&self, message: &GossipsubMessage) -> MessageId {
|
||||
pub fn message_id(&self, message: &Message) -> MessageId {
|
||||
(self.message_id_fn)(message)
|
||||
}
|
||||
|
||||
/// A user-defined optional function that computes fast ids from raw messages. This can be used
|
||||
/// to avoid possibly expensive transformations from [`RawGossipsubMessage`] to
|
||||
/// [`GossipsubMessage`] for duplicates. Two semantically different messages must always
|
||||
/// to avoid possibly expensive transformations from [`RawMessage`] to
|
||||
/// [`Message`] for duplicates. Two semantically different messages must always
|
||||
/// have different fast message ids, but it is allowed that two semantically identical messages
|
||||
/// have different fast message ids as long as the message_id_fn produces the same id for them.
|
||||
///
|
||||
/// The function takes a [`RawGossipsubMessage`] as input and outputs a String to be
|
||||
/// The function takes a [`RawMessage`] as input and outputs a String to be
|
||||
/// interpreted as the fast message id. Default is None.
|
||||
pub fn fast_message_id(&self, message: &RawGossipsubMessage) -> Option<FastMessageId> {
|
||||
pub fn fast_message_id(&self, message: &RawMessage) -> Option<FastMessageId> {
|
||||
self.fast_message_id_fn
|
||||
.as_ref()
|
||||
.map(|fast_message_id_fn| fast_message_id_fn(message))
|
||||
@ -391,24 +390,24 @@ impl GossipsubConfig {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GossipsubConfig {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
// use ConfigBuilder to also validate defaults
|
||||
GossipsubConfigBuilder::default()
|
||||
ConfigBuilder::default()
|
||||
.build()
|
||||
.expect("Default config parameters should be valid parameters")
|
||||
}
|
||||
}
|
||||
|
||||
/// The builder struct for constructing a gossipsub configuration.
|
||||
pub struct GossipsubConfigBuilder {
|
||||
config: GossipsubConfig,
|
||||
pub struct ConfigBuilder {
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl Default for GossipsubConfigBuilder {
|
||||
impl Default for ConfigBuilder {
|
||||
fn default() -> Self {
|
||||
GossipsubConfigBuilder {
|
||||
config: GossipsubConfig {
|
||||
ConfigBuilder {
|
||||
config: Config {
|
||||
protocol_id: Cow::Borrowed("meshsub"),
|
||||
custom_id_version: None,
|
||||
history_length: 5,
|
||||
@ -466,13 +465,13 @@ impl Default for GossipsubConfigBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GossipsubConfig> for GossipsubConfigBuilder {
|
||||
fn from(config: GossipsubConfig) -> Self {
|
||||
GossipsubConfigBuilder { config }
|
||||
impl From<Config> for ConfigBuilder {
|
||||
fn from(config: Config) -> Self {
|
||||
ConfigBuilder { config }
|
||||
}
|
||||
}
|
||||
|
||||
impl GossipsubConfigBuilder {
|
||||
impl ConfigBuilder {
|
||||
/// The protocol id prefix to negotiate this protocol (default is `/meshsub/1.0.0`).
|
||||
pub fn protocol_id_prefix(
|
||||
&mut self,
|
||||
@ -487,7 +486,7 @@ impl GossipsubConfigBuilder {
|
||||
pub fn protocol_id(
|
||||
&mut self,
|
||||
protocol_id: impl Into<Cow<'static, str>>,
|
||||
custom_id_version: GossipsubVersion,
|
||||
custom_id_version: Version,
|
||||
) -> &mut Self {
|
||||
self.config.custom_id_version = Some(custom_id_version);
|
||||
self.config.protocol_id = protocol_id.into();
|
||||
@ -600,7 +599,7 @@ impl GossipsubConfigBuilder {
|
||||
|
||||
/// When set, prevents automatic forwarding of all received messages. This setting
|
||||
/// allows a user to validate the messages before propagating them to their peers. If set,
|
||||
/// the user must manually call [`crate::Gossipsub::report_message_validation_result()`] on the
|
||||
/// the user must manually call [`crate::Behaviour::report_message_validation_result()`] on the
|
||||
/// behaviour to forward a message once validated.
|
||||
pub fn validate_messages(&mut self) -> &mut Self {
|
||||
self.config.validate_messages = true;
|
||||
@ -620,27 +619,27 @@ impl GossipsubConfigBuilder {
|
||||
/// addressing, where this function may be set to `hash(message)`. This would prevent messages
|
||||
/// of the same content from being duplicated.
|
||||
///
|
||||
/// The function takes a [`GossipsubMessage`] as input and outputs a String to be
|
||||
/// The function takes a [`Message`] as input and outputs a String to be
|
||||
/// interpreted as the message id.
|
||||
pub fn message_id_fn<F>(&mut self, id_fn: F) -> &mut Self
|
||||
where
|
||||
F: Fn(&GossipsubMessage) -> MessageId + Send + Sync + 'static,
|
||||
F: Fn(&Message) -> MessageId + Send + Sync + 'static,
|
||||
{
|
||||
self.config.message_id_fn = Arc::new(id_fn);
|
||||
self
|
||||
}
|
||||
|
||||
/// A user-defined optional function that computes fast ids from raw messages. This can be used
|
||||
/// to avoid possibly expensive transformations from [`RawGossipsubMessage`] to
|
||||
/// [`GossipsubMessage`] for duplicates. Two semantically different messages must always
|
||||
/// to avoid possibly expensive transformations from [`RawMessage`] to
|
||||
/// [`Message`] for duplicates. Two semantically different messages must always
|
||||
/// have different fast message ids, but it is allowed that two semantically identical messages
|
||||
/// have different fast message ids as long as the message_id_fn produces the same id for them.
|
||||
///
|
||||
/// The function takes a [`RawGossipsubMessage`] as input and outputs a String to be interpreted
|
||||
/// The function takes a [`Message`] as input and outputs a String to be interpreted
|
||||
/// as the fast message id. Default is None.
|
||||
pub fn fast_message_id_fn<F>(&mut self, fast_id_fn: F) -> &mut Self
|
||||
where
|
||||
F: Fn(&RawGossipsubMessage) -> FastMessageId + Send + Sync + 'static,
|
||||
F: Fn(&RawMessage) -> FastMessageId + Send + Sync + 'static,
|
||||
{
|
||||
self.config.fast_message_id_fn = Some(Arc::new(fast_id_fn));
|
||||
self
|
||||
@ -801,8 +800,8 @@ impl GossipsubConfigBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Constructs a [`GossipsubConfig`] from the given configuration and validates the settings.
|
||||
pub fn build(&self) -> Result<GossipsubConfig, &'static str> {
|
||||
/// Constructs a [`Config`] from the given configuration and validates the settings.
|
||||
pub fn build(&self) -> Result<Config, &'static str> {
|
||||
// check all constraints on config
|
||||
|
||||
if self.config.max_transmit_size < 100 {
|
||||
@ -838,7 +837,7 @@ impl GossipsubConfigBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for GossipsubConfig {
|
||||
impl std::fmt::Debug for Config {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut builder = f.debug_struct("GossipsubConfig");
|
||||
let _ = builder.field("protocol_id", &self.protocol_id);
|
||||
@ -895,7 +894,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn create_thing() {
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id_prefix("purple")
|
||||
.build()
|
||||
.unwrap();
|
||||
@ -903,8 +902,8 @@ mod test {
|
||||
dbg!(builder);
|
||||
}
|
||||
|
||||
fn get_gossipsub_message() -> GossipsubMessage {
|
||||
GossipsubMessage {
|
||||
fn get_gossipsub_message() -> Message {
|
||||
Message {
|
||||
source: None,
|
||||
data: vec![12, 34, 56],
|
||||
sequence_number: None,
|
||||
@ -918,7 +917,7 @@ mod test {
|
||||
])
|
||||
}
|
||||
|
||||
fn message_id_plain_function(message: &GossipsubMessage) -> MessageId {
|
||||
fn message_id_plain_function(message: &Message) -> MessageId {
|
||||
let mut s = DefaultHasher::new();
|
||||
message.data.hash(&mut s);
|
||||
let mut v = s.finish().to_string();
|
||||
@ -928,7 +927,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn create_config_with_message_id_as_plain_function() {
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id_prefix("purple")
|
||||
.message_id_fn(message_id_plain_function)
|
||||
.build()
|
||||
@ -940,7 +939,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn create_config_with_message_id_as_closure() {
|
||||
let closure = |message: &GossipsubMessage| {
|
||||
let closure = |message: &Message| {
|
||||
let mut s = DefaultHasher::new();
|
||||
message.data.hash(&mut s);
|
||||
let mut v = s.finish().to_string();
|
||||
@ -948,7 +947,7 @@ mod test {
|
||||
MessageId::from(v)
|
||||
};
|
||||
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id_prefix("purple")
|
||||
.message_id_fn(closure)
|
||||
.build()
|
||||
@ -961,7 +960,7 @@ mod test {
|
||||
#[test]
|
||||
fn create_config_with_message_id_as_closure_with_variable_capture() {
|
||||
let captured: char = 'e';
|
||||
let closure = move |message: &GossipsubMessage| {
|
||||
let closure = move |message: &Message| {
|
||||
let mut s = DefaultHasher::new();
|
||||
message.data.hash(&mut s);
|
||||
let mut v = s.finish().to_string();
|
||||
@ -969,7 +968,7 @@ mod test {
|
||||
MessageId::from(v)
|
||||
};
|
||||
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id_prefix("purple")
|
||||
.message_id_fn(closure)
|
||||
.build()
|
||||
@ -981,7 +980,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn create_config_with_protocol_id_prefix() {
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id_prefix("purple")
|
||||
.validation_mode(ValidationMode::Anonymous)
|
||||
.message_id_fn(message_id_plain_function)
|
||||
@ -1005,15 +1004,15 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn create_config_with_custom_protocol_id() {
|
||||
let builder: GossipsubConfig = GossipsubConfigBuilder::default()
|
||||
.protocol_id("purple", GossipsubVersion::V1_0)
|
||||
let builder: Config = ConfigBuilder::default()
|
||||
.protocol_id("purple", Version::V1_0)
|
||||
.validation_mode(ValidationMode::Anonymous)
|
||||
.message_id_fn(message_id_plain_function)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(builder.protocol_id(), "purple");
|
||||
assert_eq!(builder.custom_id_version(), &Some(GossipsubVersion::V1_0));
|
||||
assert_eq!(builder.custom_id_version(), &Some(Version::V1_0));
|
||||
|
||||
let protocol_config = ProtocolConfig::new(&builder);
|
||||
let protocol_ids = protocol_config.protocol_info();
|
||||
|
Reference in New Issue
Block a user