referrerpolicy=no-referrer-when-downgrade

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	///
62	/// A `webrtc-direct` address must be given as
63	/// `/<ip4|ip6|dns|dns4|dns6>/<host>/udp/<port>/webrtc-direct`: the node appends its own
64	/// `/certhash/...` and `/p2p/...`, and doesn't start if either is supplied.
65	#[arg(long, value_name = "PUBLIC_ADDR", num_args = 1..)]
66	pub public_addr: Vec<Multiaddr>,
67
68	/// Listen on this multiaddress.
69	///
70	/// By default:
71	/// If `--validator` is passed: `/ip4/0.0.0.0/tcp/<port>` and `/ip6/[::]/tcp/<port>`.
72	/// Otherwise: `/ip4/0.0.0.0/tcp/<port>/ws` and `/ip6/[::]/tcp/<port>/ws`, plus, on the
73	/// litep2p network backend, `/ip4/0.0.0.0/udp/<port>/webrtc-direct` and
74	/// `/ip6/[::]/udp/<port>/webrtc-direct`.
75	///
76	/// WebRTC addresses (`/ip4/<ip>/udp/<port>/webrtc-direct` and
77	/// `/ip6/<ip>/udp/<port>/webrtc-direct`) only work on the litep2p network backend.
78	#[arg(long, value_name = "LISTEN_ADDR", num_args = 1..)]
79	pub listen_addr: Vec<Multiaddr>,
80
81	/// Listen on WebRTC addresses even on a validator or collator (on para- &
82	/// relaychain side of the node depending on the position of the flag).
83	///
84	/// Only applies if no explicit `--listen-addr` is passed. Only works on the
85	/// litep2p network backend.
86	#[arg(long)]
87	pub force_enable_webrtc: bool,
88
89	/// Specify p2p protocol TCP port.
90	#[arg(long, value_name = "PORT", conflicts_with_all = &[ "listen_addr" ])]
91	pub port: Option<u16>,
92
93	/// Always forbid connecting to private IPv4/IPv6 addresses.
94	///
95	/// The option doesn't apply to addresses passed with `--reserved-nodes` or
96	/// `--bootnodes`. Enabled by default for chains marked as "live" in their chain
97	/// specifications.
98	///
99	/// Address allocation for private networks is specified by
100	/// [RFC1918](https://tools.ietf.org/html/rfc1918)).
101	#[arg(long, alias = "no-private-ipv4", conflicts_with_all = &["allow_private_ip"])]
102	pub no_private_ip: bool,
103
104	/// Always accept connecting to private IPv4/IPv6 addresses.
105	///
106	/// Enabled by default for chains marked as "local" in their chain specifications,
107	/// or when `--dev` is passed.
108	///
109	/// Address allocation for private networks is specified by
110	/// [RFC1918](https://tools.ietf.org/html/rfc1918)).
111	#[arg(long, alias = "allow-private-ipv4", conflicts_with_all = &["no_private_ip"])]
112	pub allow_private_ip: bool,
113
114	/// Number of outgoing connections we're trying to maintain.
115	#[arg(long, value_name = "COUNT", default_value_t = 8)]
116	pub out_peers: u32,
117
118	/// Maximum number of inbound full nodes peers.
119	#[arg(long, value_name = "COUNT", default_value_t = 32)]
120	pub in_peers: u32,
121
122	/// Maximum number of inbound light nodes peers.
123	#[arg(long, value_name = "COUNT", default_value_t = 500)]
124	pub in_peers_light: u32,
125
126	/// Disable mDNS discovery (default: true).
127	///
128	/// By default, the network will use mDNS to discover other nodes on the
129	/// local network. This disables it. Automatically implied when using --dev.
130	#[arg(long)]
131	pub no_mdns: bool,
132
133	/// Maximum number of peers from which to ask for the same blocks in parallel.
134	///
135	/// This allows downloading announced blocks from multiple peers.
136	/// Decrease to save traffic and risk increased latency.
137	#[arg(long, value_name = "COUNT", default_value_t = 5)]
138	pub max_parallel_downloads: u32,
139
140	#[allow(missing_docs)]
141	#[clap(flatten)]
142	pub node_key_params: NodeKeyParams,
143
144	/// Enable peer discovery on local networks.
145	///
146	/// By default this option is `true` for `--dev` or when the chain type is
147	/// `Local`/`Development` and false otherwise.
148	#[arg(long)]
149	pub discover_local: bool,
150
151	/// Require iterative Kademlia DHT queries to use disjoint paths.
152	///
153	/// Disjoint paths increase resiliency in the presence of potentially adversarial nodes.
154	///
155	/// See the S/Kademlia paper for more information on the high level design as well as its
156	/// security improvements.
157	#[arg(long)]
158	pub kademlia_disjoint_query_paths: bool,
159
160	/// Kademlia replication factor.
161	///
162	/// Determines to how many closest peers a record is replicated to.
163	///
164	/// Discovery mechanism requires successful replication to all
165	/// `kademlia_replication_factor` peers to consider record successfully put.
166	#[arg(long, default_value = "20")]
167	pub kademlia_replication_factor: NonZeroUsize,
168
169	/// Join the IPFS network and serve transactions over bitswap protocol.
170	#[arg(long)]
171	pub ipfs_server: bool,
172
173	/// Specify a list of IPFS bootstrap nodes.
174	#[arg(long, value_name = "ADDR", num_args = 1.., requires = "ipfs_server")]
175	pub ipfs_bootnodes: Vec<MultiaddrWithPeerId>,
176
177	/// Blockchain syncing mode.
178	#[arg(
179		long,
180		value_enum,
181		value_name = "SYNC_MODE",
182		default_value_t = SyncMode::Full,
183		ignore_case = true,
184		verbatim_doc_comment
185	)]
186	pub sync: SyncMode,
187
188	/// Maximum number of blocks per request.
189	///
190	/// Try reducing this number from the default value if you have a slow network connection
191	/// and observe block requests timing out.
192	#[arg(long, value_name = "COUNT", default_value_t = 64)]
193	pub max_blocks_per_request: u32,
194
195	/// Network backend used for P2P networking.
196	///
197	/// Litep2p is a lightweight alternative to libp2p, that is designed to be more
198	/// efficient and easier to use. At the same time, litep2p brings performance
199	/// improvements and reduces the CPU usage significantly.
200	///
201	/// Libp2p is the old network backend, that may still be used for compatibility
202	/// reasons until the whole ecosystem is migrated to litep2p.
203	#[arg(
204		long,
205		value_enum,
206		value_name = "NETWORK_BACKEND",
207		default_value_t = NetworkBackendType::Litep2p,
208		ignore_case = true,
209		verbatim_doc_comment
210	)]
211	pub network_backend: NetworkBackendType,
212}
213
214impl NetworkParams {
215	/// Fill the given `NetworkConfiguration` by looking at the cli parameters.
216	pub fn network_config(
217		&self,
218		chain_spec: &Box<dyn ChainSpec>,
219		is_dev: bool,
220		is_validator: bool,
221		net_config_path: Option<PathBuf>,
222		client_id: &str,
223		node_name: &str,
224		node_key: NodeKeyConfig,
225		default_listen_port: u16,
226	) -> NetworkConfiguration {
227		let port = self.port.unwrap_or(default_listen_port);
228
229		if self.force_enable_webrtc && !matches!(self.network_backend, NetworkBackendType::Litep2p)
230		{
231			log::warn!(
232				"`--force-enable-webrtc` has no effect: WebRTC is only supported by the litep2p \
233				 network backend",
234			);
235		}
236
237		let listen_addresses = if self.listen_addr.is_empty() {
238			let mut listen_addresses = if is_validator || is_dev {
239				vec![
240					Multiaddr::empty()
241						.with(Protocol::Ip6([0, 0, 0, 0, 0, 0, 0, 0].into()))
242						.with(Protocol::Tcp(port)),
243					Multiaddr::empty()
244						.with(Protocol::Ip4([0, 0, 0, 0].into()))
245						.with(Protocol::Tcp(port)),
246				]
247			} else {
248				vec![
249					Multiaddr::empty()
250						.with(Protocol::Ip6([0, 0, 0, 0, 0, 0, 0, 0].into()))
251						.with(Protocol::Tcp(port))
252						.with(Protocol::Ws(Cow::Borrowed("/"))),
253					Multiaddr::empty()
254						.with(Protocol::Ip4([0, 0, 0, 0].into()))
255						.with(Protocol::Tcp(port))
256						.with(Protocol::Ws(Cow::Borrowed("/"))),
257				]
258			};
259
260			if matches!(self.network_backend, NetworkBackendType::Litep2p) &&
261				(self.force_enable_webrtc || !is_validator)
262			{
263				listen_addresses.extend([
264					Multiaddr::empty()
265						.with(Protocol::Ip6([0, 0, 0, 0, 0, 0, 0, 0].into()))
266						.with(Protocol::Udp(port))
267						.with(Protocol::WebRTCDirect),
268					Multiaddr::empty()
269						.with(Protocol::Ip4([0, 0, 0, 0].into()))
270						.with(Protocol::Udp(port))
271						.with(Protocol::WebRTCDirect),
272				]);
273			}
274
275			listen_addresses
276		} else {
277			self.listen_addr.clone()
278		};
279
280		let public_addresses = self.public_addr.clone();
281
282		let mut boot_nodes = chain_spec.boot_nodes().to_vec();
283		boot_nodes.extend(self.bootnodes.clone());
284
285		let chain_type = chain_spec.chain_type();
286		// Activate if the user explicitly requested local discovery, `--dev` is given or the
287		// chain type is `Local`/`Development`
288		let allow_non_globals_in_dht =
289			self.discover_local ||
290				is_dev || matches!(chain_type, ChainType::Local | ChainType::Development);
291
292		let allow_private_ip = match (self.allow_private_ip, self.no_private_ip) {
293			(true, true) => unreachable!("`*_private_ip` flags are mutually exclusive; qed"),
294			(true, false) => true,
295			(false, true) => false,
296			(false, false) => {
297				is_dev || matches!(chain_type, ChainType::Local | ChainType::Development)
298			},
299		};
300
301		NetworkConfiguration {
302			boot_nodes,
303			net_config_path,
304			default_peers_set: SetConfig {
305				in_peers: self.in_peers + self.in_peers_light,
306				out_peers: self.out_peers,
307				reserved_nodes: self.reserved_nodes.clone(),
308				non_reserved_mode: if self.reserved_only {
309					NonReservedPeerMode::Deny
310				} else {
311					NonReservedPeerMode::Accept
312				},
313			},
314			default_peers_set_num_full: self.in_peers + self.out_peers,
315			listen_addresses,
316			public_addresses,
317			node_key,
318			node_name: node_name.to_string(),
319			client_version: client_id.to_string(),
320			transport: TransportConfig::Normal {
321				enable_mdns: !is_dev && !self.no_mdns,
322				allow_private_ip,
323			},
324			idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT,
325			max_parallel_downloads: self.max_parallel_downloads,
326			max_blocks_per_request: self.max_blocks_per_request,
327			min_peers_to_start_warp_sync: None,
328			enable_dht_random_walk: !self.reserved_only,
329			allow_non_globals_in_dht,
330			kademlia_disjoint_query_paths: self.kademlia_disjoint_query_paths,
331			kademlia_replication_factor: self.kademlia_replication_factor,
332			ipfs_server: self.ipfs_server,
333			ipfs_bootnodes: self.ipfs_bootnodes.clone(),
334			sync_mode: self.sync.into(),
335			network_backend: self.network_backend.into(),
336		}
337	}
338}
339
340#[cfg(test)]
341mod tests {
342	use super::*;
343	use clap::Parser;
344
345	#[derive(Parser)]
346	struct Cli {
347		#[clap(flatten)]
348		network_params: NetworkParams,
349	}
350
351	#[test]
352	fn reserved_nodes_multiple_values_and_occurrences() {
353		let params = Cli::try_parse_from([
354			"",
355			"--reserved-nodes",
356			"/ip4/0.0.0.0/tcp/501/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
357			"/ip4/0.0.0.0/tcp/502/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
358			"--reserved-nodes",
359			"/ip4/0.0.0.0/tcp/503/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS",
360		])
361		.expect("Parses network params");
362
363		let expected = vec![
364			MultiaddrWithPeerId::try_from(
365				"/ip4/0.0.0.0/tcp/501/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
366					.to_string(),
367			)
368			.unwrap(),
369			MultiaddrWithPeerId::try_from(
370				"/ip4/0.0.0.0/tcp/502/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
371					.to_string(),
372			)
373			.unwrap(),
374			MultiaddrWithPeerId::try_from(
375				"/ip4/0.0.0.0/tcp/503/p2p/12D3KooWEBo1HUPQJwiBmM5kSeg4XgiVxEArArQdDarYEsGxMfbS"
376					.to_string(),
377			)
378			.unwrap(),
379		];
380
381		assert_eq!(expected, params.network_params.reserved_nodes);
382	}
383
384	#[test]
385	fn sync_ignores_case() {
386		let params = Cli::try_parse_from(["", "--sync", "wArP"]).expect("Parses network params");
387
388		assert_eq!(SyncMode::Warp, params.network_params.sync);
389	}
390}