From 3dc723f12fabcb8cd29a1e7a00931307b068ffcb Mon Sep 17 00:00:00 2001
From: chirag-bgh <chirag2bsingh@gmail.com>
Date: Mon, 4 Sep 2023 20:33:19 +0530
Subject: [PATCH 1/3] feat(gossipsub): make Err of config builder a std::Error

---
 protocols/gossipsub/src/config.rs | 21 ++++++++-----------
 protocols/gossipsub/src/error.rs  | 34 +++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/protocols/gossipsub/src/config.rs b/protocols/gossipsub/src/config.rs
index a5d31071538..f6c5d45c689 100644
--- a/protocols/gossipsub/src/config.rs
+++ b/protocols/gossipsub/src/config.rs
@@ -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())
diff --git a/protocols/gossipsub/src/error.rs b/protocols/gossipsub/src/error.rs
index 26f71a346c3..c461abc0d17 100644
--- a/protocols/gossipsub/src/error.rs
+++ b/protocols/gossipsub/src/error.rs
@@ -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"),
+        }
+    }
+}

From fe38090f2447f15e1f6bbe16c0446863a04e6c64 Mon Sep 17 00:00:00 2001
From: chirag-bgh <chirag2bsingh@gmail.com>
Date: Tue, 5 Sep 2023 12:15:05 +0530
Subject: [PATCH 2/3] update changelog

---
 Cargo.lock                       | 2 +-
 Cargo.toml                       | 2 +-
 protocols/gossipsub/CHANGELOG.md | 7 +++++++
 protocols/gossipsub/Cargo.toml   | 2 +-
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 06fb6387d50..09d6a889e36 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2732,7 +2732,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-gossipsub"
-version = "0.45.1"
+version = "0.45.2"
 dependencies = [
  "async-std",
  "asynchronous-codec",
diff --git a/Cargo.toml b/Cargo.toml
index 97c8c10adfa..ac9f3cefc94 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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" }
diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md
index a1f4ef6c973..569c46965e7 100644
--- a/protocols/gossipsub/CHANGELOG.md
+++ b/protocols/gossipsub/CHANGELOG.md
@@ -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`.
diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml
index 3572105f4a2..1eee7edfdba 100644
--- a/protocols/gossipsub/Cargo.toml
+++ b/protocols/gossipsub/Cargo.toml
@@ -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"

From 78abb98662c46f83e213d779e1755559bf37da3f Mon Sep 17 00:00:00 2001
From: chirag-bgh <chirag2bsingh@gmail.com>
Date: Tue, 5 Sep 2023 17:34:00 +0530
Subject: [PATCH 3/3] nit

---
 Cargo.lock                       | 2 +-
 Cargo.toml                       | 2 +-
 protocols/gossipsub/CHANGELOG.md | 8 ++++----
 protocols/gossipsub/Cargo.toml   | 2 +-
 protocols/gossipsub/src/lib.rs   | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 09d6a889e36..a3e6c5544e6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2732,7 +2732,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-gossipsub"
-version = "0.45.2"
+version = "0.46.0"
 dependencies = [
  "async-std",
  "asynchronous-codec",
diff --git a/Cargo.toml b/Cargo.toml
index ac9f3cefc94..6dc46d9e2fe 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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.2", path = "protocols/gossipsub" }
+libp2p-gossipsub = { version = "0.46.0", 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" }
diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md
index 569c46965e7..f82e238b05e 100644
--- a/protocols/gossipsub/CHANGELOG.md
+++ b/protocols/gossipsub/CHANGELOG.md
@@ -1,13 +1,13 @@
-## 0.45.2 - unreleased
+## 0.46.0 - unreleased
 
-- return typed error from config builder.
-  See [PR 4445]
+- 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`.
+- Add getter function to o  btain `TopicScoreParams`.
   See [PR 4231].
 
 [PR 4231]: https://github.com/libp2p/rust-libp2p/pull/4231
diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml
index 1eee7edfdba..0a07776aecd 100644
--- a/protocols/gossipsub/Cargo.toml
+++ b/protocols/gossipsub/Cargo.toml
@@ -3,7 +3,7 @@ name = "libp2p-gossipsub"
 edition = "2021"
 rust-version = { workspace = true }
 description = "Gossipsub protocol for libp2p"
-version = "0.45.2"
+version = "0.46.0"
 authors = ["Age Manning <Age@AgeManning.com>"]
 license = "MIT"
 repository = "https://github.com/libp2p/rust-libp2p"
diff --git a/protocols/gossipsub/src/lib.rs b/protocols/gossipsub/src/lib.rs
index e065319c4c3..fdc5c05ac76 100644
--- a/protocols/gossipsub/src/lib.rs
+++ b/protocols/gossipsub/src/lib.rs
@@ -158,7 +158,7 @@ mod types;
 
 pub use self::behaviour::{Behaviour, Event, MessageAuthenticity};
 pub use self::config::{Config, ConfigBuilder, ValidationMode, Version};
-pub use self::error::{PublishError, SubscriptionError, ValidationError};
+pub use self::error::{ConfigBuilderError, PublishError, SubscriptionError, ValidationError};
 pub use self::metrics::Config as MetricsConfig;
 pub use self::peer_score::{
     score_parameter_decay, score_parameter_decay_with_base, PeerScoreParams, PeerScoreThresholds,