Skip to main content

sc_cli/params/
network_params.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::{
20	arg_enums::{NetworkBackendType, SyncMode},
21	params::node_key_params::NodeKeyParams,
22};
23use clap::Args;
24use sc_network::{
25	config::{
26		NetworkConfiguration, NodeKeyConfig, NonReservedPeerMode, SetConfig, TransportConfig,
27		DEFAULT_IDLE_CONNECTION_TIMEOUT,
28	},
29	multiaddr::Protocol,
30};
31use sc_service::{
32	config::{Multiaddr, MultiaddrWithPeerId},
33	ChainSpec, ChainType,
34};
35use std::{borrow::Cow, num::NonZeroUsize, path::PathBuf};
36
37/// Parameters used to create the network configuration.
38#[derive(Debug, Clone, Args)]
39pub struct NetworkParams {
40	/// Specify a list of bootnodes.
41	#[arg(long, value_name = "ADDR", num_args = 1..)]
42	pub bootnodes: Vec<MultiaddrWithPeerId>,
43
44	/// Specify a list of reserved node addresses.
45	#[arg(long, value_name = "ADDR", num_args = 1..)]
46	pub reserved_nodes: Vec<MultiaddrWithPeerId>,
47
48	/// Whether to only synchronize the chain with reserved nodes.
49	///
50	/// Also disables automatic peer discovery.
51	/// TCP connections might still be established with non-reserved nodes.
52	/// In particular, if you are a validator your node might still connect to other
53	/// validator nodes and collator nodes regardless of whether they are defined as
54	/// reserved nodes.
55	#[arg(long)]
56	pub reserved_only: bool,
57
58	/// Public address that other nodes will use to connect to this node.
59	///
60	/// This can be used if there's a proxy in front of this node.
61	#[arg(long, value_name = "PUBLIC_ADDR", num_args = 1..)]
62	pub public_addr: Vec<Multiaddr>,
63
64	/// Listen on this multiaddress.
65	///
66	/// By default:
67	/// If `--validator` is passed: `/ip4/0.0.0.0/tcp/<port>` and `/ip6/[::]/tcp/<port>`.
68	/// Otherwise: `/ip4/0.0.0.0/tcp/<port>/ws` and `/ip6/[::]/tcp/<port>/ws`.
69	#[arg(long, value_name = "LISTEN_ADDR", num_args = 1..)]
70	pub listen_addr: Vec<Multiaddr>,
71
72	/// Specify p2p protocol TCP port.
73	#[arg(long, value_name = "PORT", conflicts_with_all = &[ "listen_addr" ])]
74	pub port: Option<u16>,
75
76	/// Always forbid connecting to private IPv4/IPv6 addresses.
77	///
78	/// The option doesn't apply to addresses passed with `--reserved-nodes` or
79	/// `--bootnodes`. Enabled by default for chains marked as "live" in their chain
80	/// specifications.
81	///
82	/// Address allocation for private networks is specified by
83	/// [RFC1918](https://tools.ietf.org/html/rfc1918)).
84	#[arg(long, alias = "no-private-ipv4", conflicts_with_all = &["allow_private_ip"])]
85	pub no_private_ip: bool,
86
87	/// Always accept connecting to private IPv4/IPv6 addresses.
88	///
89	/// Enabled by default for chains marked as "local" in their chain specifications,
90	/// or when `--dev` is passed.
91	///
92	/// Address allocation for private networks is specified by
93	/// [RFC1918](https://tools.ietf.org/html/rfc1918)).
94	#[arg(long, alias = "allow-private-ipv4", conflicts_with_all = &["no_private_ip"])]
95	pub allow_private_ip: bool,
96
97	/// Number of outgoing connections we're trying to maintain.
98	#[arg(long, value_name = "COUNT", default_value_t = 8)]
99	pub out_peers: u32,
100
101	/// Maximum number of inbound full nodes peers.
102	#[arg(long, value_name = "COUNT", default_value_t = 32)]
103	pub in_peers: u32,
104
105	/// Maximum number of inbound light nodes peers.
106	#[arg(long, value_name = "COUNT", default_value_t = 100)]
107	pub in_peers_light: u32,
108
109	/// Disable mDNS discovery (default: true).
110	///
111	/// By default, the network will use mDNS to discover other nodes on the
112	/// local network. This disables it. Automatically implied when using --dev.
113	#[arg(long)]
114	pub no_mdns: bool,
115
116	/// Maximum number of peers from which to ask for the same blocks in parallel.
117	///
118	/// This allows downloading announced blocks from multiple peers.
119	/// Decrease to save traffic and risk increased latency.
120	#[arg(long, value_name = "COUNT", default_value_t = 5)]
121	pub max_parallel_downloads: u32,
122
123	#[allow(missing_docs)]
124	#[clap(flatten)]
125	pub node_key_params: NodeKeyParams,
126
127	/// Enable peer discovery on local networks.
128	///
129	/// By default this option is `true` for `--dev` or when the chain type is
130	/// `Local`/`Development` and false otherwise.
131	#[arg(long)]
132	pub discover_local: bool,
133
134	/// Require iterative Kademlia DHT queries to use disjoint paths.
135	///
136	/// Disjoint paths increase resiliency in the presence of potentially adversarial nodes.
137	///
138	/// See the S/Kademlia paper for more information on the high level design as well as its
139	/// security improvements.
140	#[arg(long)]
141	pub kademlia_disjoint_query_paths: bool,
142
143	/// Kademlia replication factor.
144	///
145	/// Determines to how many closest peers a record is replicated to.
146	///
147	/// Discovery mechanism requires successful replication to all
148	/// `kademlia_replication_factor` peers to consider record successfully put.
149	#[arg(long, default_value = "20")]
150	pub kademlia_replication_factor: NonZeroUsize,
151
152	/// Join the IPFS network and serve transactions over bitswap protocol.
153	#[arg(long)]
154	pub ipfs_server: bool,
155
156	/// Specify a list of IPFS bootstrap nodes.
157	#[arg(long, value_name = "ADDR", num_args = 1.., requires = "ipfs_server")]
158	pub ipfs_bootnodes: Vec<MultiaddrWithPeerId>,
159
160	/// Blockchain syncing mode.
161	#[arg(
162		long,
163		value_enum,
164		value_name = "SYNC_MODE",
165		default_value_t = SyncMode::Full,
166		ignore_case = true,
167		verbatim_doc_comment
168	)]
169	pub sync: SyncMode,
170
171	/// Maximum number of blocks per request.
172	///
173	/// Try reducing this number from the default value if you have a slow network connection
174	/// and observe block requests timing out.
175	#[arg(long, value_name = "COUNT", default_value_t = 64)]
176	pub max_blocks_per_request: u32,
177
178	/// Network backend used for P2P networking.
179	///
180	/// Litep2p is a lightweight alternative to libp2p, that is designed to be more
181	/// efficient and easier to use. At the same time, litep2p brings performance
182	/// improvements and reduces the CPU usage significantly.
183	///
184	/// Libp2p is the old network backend, that may still be used for compatibility
185	/// reasons until the whole ecosystem is migrated to litep2p.
186	#[arg(
187		long,
188		value_enum,
189		value_name = "NETWORK_BACKEND",
190		default_value_t = NetworkBackendType::Litep2p,
191		ignore_case = true,
192		verbatim_doc_comment
193	)]
194	pub network_backend: NetworkBackendType,
195}
196
197impl NetworkParams {
198	/// Fill the given `NetworkConfiguration` by looking at the cli parameters.
199	pub fn network_config(
200		&self,
201		chain_spec: &Box<dyn ChainSpec>,
202		is_dev: bool,
203		is_validator: bool,
204		net_config_path: Option<PathBuf>,
205		client_id: &str,
206		node_name: &str,
207		node_key: NodeKeyConfig,
208		default_listen_port: u16,
209	) -> NetworkConfiguration {
210		let port = self.port.unwrap_or(default_listen_port);
211
212		let listen_addresses = if self.listen_addr.is_empty() {
213			if is_validator || is_dev {
214				vec![
215					Multiaddr::empty()
216						.with(Protocol::Ip6([0, 0, 0, 0, 0, 0, 0, 0].into()))
217						.with(Protocol::Tcp(port)),
218					Multiaddr::empty()
219						.with(Protocol::Ip4([0, 0, 0, 0].into()))
220						.with(Protocol::Tcp(port)),
221				]
222			} else {
223				vec![
224					Multiaddr::empty()
225						.with(Protocol::Ip6([0, 0, 0, 0, 0, 0, 0, 0].into()))
226						.with(Protocol::Tcp(port))
227						.with(Protocol::Ws(Cow::Borrowed("/"))),
228					Multiaddr::empty()
229						.with(Protocol::Ip4([0, 0, 0, 0].into()))
230						.with(Protocol::Tcp(port))
231						.with(Protocol::Ws(Cow::Borrowed("/"))),
232				]
233			}
234		} else {
235			self.listen_addr.clone()
236		};
237
238		let public_addresses = self.public_addr.clone();
239
240		let mut boot_nodes = chain_spec.boot_nodes().to_vec();
241		boot_nodes.extend(self.bootnodes.clone());
242
243		let chain_type = chain_spec.chain_type();
244		// Activate if the user explicitly requested local discovery, `--dev` is given or the
245		// chain type is `Local`/`Development`
246		let allow_non_globals_in_dht =
247			self.discover_local ||
248				is_dev || matches!(chain_type, ChainType::Local | ChainType::Development);
249
250		let allow_private_ip = match (self.allow_private_ip, self.no_private_ip) {
251			(true, true) => unreachable!("`*_private_ip` flags are mutually exclusive; qed"),
252			(true, false) => true,
253			(false, true) => false,
254			(false, false) => {
255				is_dev || matches!(chain_type, ChainType::Local | ChainType::Development)
256			},
257		};
258
259		NetworkConfiguration {
260			boot_nodes,
261			net_config_path,
262			default_peers_set: SetConfig {
263				in_peers: self.in_peers + self.in_peers_light,
264				out_peers: self.out_peers,
265				reserved_nodes: self.reserved_nodes.clone(),
266				non_reserved_mode: if self.reserved_only {
267					NonReservedPeerMode::Deny
268				} else {
269					NonReservedPeerMode::Accept
270				},
271			},
272			default_peers_set_num_full: self.in_peers + self.out_peers,
273			listen_addresses,
274			public_addresses,
275			node_key,
276			node_name: node_name.to_string(),
277			client_version: client_id.to_string(),
278			transport: TransportConfig::Normal {
279				enable_mdns: !is_dev && !self.no_mdns,
280				allow_private_ip,
281			},
282			idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT,
283			max_parallel_downloads: self.max_parallel_downloads,
284			max_blocks_per_request: self.max_blocks_per_request,
285			min_peers_to_start_warp_sync: None,
286			enable_dht_random_walk: !self.reserved_only,
287			allow_non_globals_in_dht,
288			kademlia_disjoint_query_paths: self.kademlia_disjoint_query_paths,
289			kademlia_replication_factor: self.kademlia_replication_factor,
290			ipfs_server: self.ipfs_server,
291			ipfs_bootnodes: self.ipfs_bootnodes.clone(),
292			sync_mode: self.sync.into(),
293			network_backend: self.network_backend.into(),
294		}
295	}
296}
297
298#[cfg(test)]
299mod tests {
300	use super::*;
301	use clap::Parser;
302
303	#[derive(Parser)]
304	struct Cli {
305		#[clap(flatten)]
306		network_params: NetworkParams,
307	}
308
309	#[test]
310	fn reserved_nodes_multiple_values_and_occurrences() {
311		let params = Cli::try_parse_from([
312			"",
313			"--reserved-nodes",
314			"/ip4/0.0.0.0/tcp/501/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
315			"/ip4/0.0.0.0/tcp/502/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
316			"--reserved-nodes",
317			"/ip4/0.0.0.0/tcp/503/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
318		])
319		.expect("Parses network params");
320
321		let expected = vec![
322			MultiaddrWithPeerId::try_from(
323				"/ip4/0.0.0.0/tcp/501/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
324					.to_string(),
325			)
326			.unwrap(),
327			MultiaddrWithPeerId::try_from(
328				"/ip4/0.0.0.0/tcp/502/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
329					.to_string(),
330			)
331			.unwrap(),
332			MultiaddrWithPeerId::try_from(
333				"/ip4/0.0.0.0/tcp/503/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
334					.to_string(),
335			)
336			.unwrap(),
337		];
338
339		assert_eq!(expected, params.network_params.reserved_nodes);
340	}
341
342	#[test]
343	fn sync_ignores_case() {
344		let params = Cli::try_parse_from(["", "--sync", "wArP"]).expect("Parses network params");
345
346		assert_eq!(SyncMode::Warp, params.network_params.sync);
347	}
348}