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

feat(gossipsub)!: return typed error from config builder #4445

Merged
merged 7 commits into from
Oct 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ libp2p-dcutr = { version = "0.10.0", path = "protocols/dcutr" }
libp2p-deflate = { version = "0.40.0", path = "transports/deflate" }
libp2p-dns = { version = "0.40.0", path = "transports/dns" }
libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" }
libp2p-gossipsub = { version = "0.45.2", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.43.0", path = "protocols/identify" }
libp2p-identity = { version = "0.2.3" }
libp2p-kad = { version = "0.44.4", path = "protocols/kad" }
7 changes: 7 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.45.2 - unreleased

- return typed error from config builder.
See [PR 4445]

[PR 4445]: https://github.com/libp2p/rust-libp2p/pull/4445

## 0.45.1

- Add getter function to obtain `TopicScoreParams`.
2 changes: 1 addition & 1 deletion protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ name = "libp2p-gossipsub"
edition = "2021"
rust-version = { workspace = true }
description = "Gossipsub protocol for libp2p"
version = "0.45.1"
version = "0.45.2"
authors = ["Age Manning <Age@AgeManning.com>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
21 changes: 8 additions & 13 deletions protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ use std::borrow::Cow;
use std::sync::Arc;
use std::time::Duration;

use crate::error::ConfigBuilderError;
use crate::protocol::{ProtocolConfig, ProtocolId, FLOODSUB_PROTOCOL};
use crate::types::{FastMessageId, Message, MessageId, PeerKind, RawMessage};

@@ -831,40 +832,34 @@ impl ConfigBuilder {
}

/// Constructs a [`Config`] from the given configuration and validates the settings.
pub fn build(&self) -> Result<Config, &'static str> {
pub fn build(&self) -> Result<Config, ConfigBuilderError> {
// check all constraints on config

if self.config.protocol.max_transmit_size < 100 {
return Err("The maximum transmission size must be greater than 100 to permit basic control messages");
return Err(ConfigBuilderError::MaxTransmissionSizeTooSmall);
}

if self.config.history_length < self.config.history_gossip {
return Err(
"The history_length must be greater than or equal to the history_gossip \
length",
);
return Err(ConfigBuilderError::HistoryLengthTooSmall);
}

if !(self.config.mesh_outbound_min <= self.config.mesh_n_low
&& self.config.mesh_n_low <= self.config.mesh_n
&& self.config.mesh_n <= self.config.mesh_n_high)
{
return Err("The following inequality doesn't hold \
mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high");
return Err(ConfigBuilderError::MeshParametersInvalid);
}

if self.config.mesh_outbound_min * 2 > self.config.mesh_n {
return Err(
"The following inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2",
);
return Err(ConfigBuilderError::MeshOutboundInvalid);
}

if self.config.unsubscribe_backoff.as_millis() == 0 {
return Err("The unsubscribe_backoff parameter should be positive.");
return Err(ConfigBuilderError::UnsubscribeBackoffIsZero);
}

if self.invalid_protocol {
return Err("The provided protocol is invalid, it must start with a forward-slash");
return Err(ConfigBuilderError::InvalidProtocol);
}

Ok(self.config.clone())
34 changes: 34 additions & 0 deletions protocols/gossipsub/src/error.rs
Original file line number Diff line number Diff line change
@@ -120,3 +120,37 @@ impl From<std::io::Error> for PublishError {
PublishError::TransformFailed(error)
}
}

/// Error associated with Config building.
#[derive(Debug)]
pub enum ConfigBuilderError {
/// Maximum transmission size is too small.
MaxTransmissionSizeTooSmall,
/// Histroy length less than history gossip length.
HistoryLengthTooSmall,
/// The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high
MeshParametersInvalid,
/// The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2
MeshOutboundInvalid,
/// unsubscribe_backoff is zero
UnsubscribeBackoffIsZero,
/// Invalid protocol
InvalidProtocol,
}

impl std::error::Error for ConfigBuilderError {}

impl std::fmt::Display for ConfigBuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::MaxTransmissionSizeTooSmall => {
write!(f, "Maximum transmission size is too small")
}
Self::HistoryLengthTooSmall => write!(f, "Histroy length less than history gossip length"),
Self::MeshParametersInvalid => write!(f, "The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high"),
Self::MeshOutboundInvalid => write!(f, "The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2"),
Self::UnsubscribeBackoffIsZero => write!(f, "unsubscribe_backoff is zero"),
Self::InvalidProtocol => write!(f, "Invalid protocol"),
}
}
}