From 19d34eb87a1162ccdb790a7473d83fae1978ab5c Mon Sep 17 00:00:00 2001
From: Pierre Krieger <pierre.krieger1708@gmail.com>
Date: Tue, 4 May 2021 11:04:49 +0200
Subject: [PATCH] Add some builder functions for NonDefaultSetConfig (#8712)

---
 substrate/client/network/src/config.rs | 31 ++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/substrate/client/network/src/config.rs b/substrate/client/network/src/config.rs
index a6ce295e462..3864b77d88b 100644
--- a/substrate/client/network/src/config.rs
+++ b/substrate/client/network/src/config.rs
@@ -530,6 +530,9 @@ impl Default for SetConfig {
 }
 
 /// Extension to [`SetConfig`] for sets that aren't the default set.
+///
+/// > **Note**: As new fields might be added in the future, please consider using the `new` method
+/// >			and modifiers instead of creating this struct manually.
 #[derive(Clone, Debug)]
 pub struct NonDefaultSetConfig {
 	/// Name of the notifications protocols of this set. A substream on this set will be
@@ -544,6 +547,34 @@ pub struct NonDefaultSetConfig {
 	pub set_config: SetConfig,
 }
 
+impl NonDefaultSetConfig {
+	/// Creates a new [`NonDefaultSetConfig`]. Zero slots and accepts only reserved nodes.
+	pub fn new(notifications_protocol: Cow<'static, str>, max_notification_size: u64) -> Self {
+		NonDefaultSetConfig {
+			notifications_protocol,
+			max_notification_size,
+			set_config: SetConfig {
+				in_peers: 0,
+				out_peers: 0,
+				reserved_nodes: Vec::new(),
+				non_reserved_mode: NonReservedPeerMode::Deny,
+			},
+		}
+	}
+
+	/// Modifies the configuration to allow non-reserved nodes.
+	pub fn allow_non_reserved(&mut self, in_peers: u32, out_peers: u32) {
+		self.set_config.in_peers = in_peers;
+		self.set_config.out_peers = out_peers;
+		self.set_config.non_reserved_mode = NonReservedPeerMode::Accept;
+	}
+
+	/// Add a node to the list of reserved nodes.
+	pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
+		self.set_config.reserved_nodes.push(peer);
+	}
+}
+
 /// Configuration for the transport layer.
 #[derive(Clone, Debug)]
 pub enum TransportConfig {
-- 
GitLab