sc_network/
transport.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
19//! Transport that serves as a common ground for all connections.
20
21use either::Either;
22use libp2p::{
23	core::{
24		muxing::StreamMuxerBox,
25		transport::{Boxed, OptionalTransport},
26		upgrade,
27	},
28	dns, identity, noise, tcp, websocket, PeerId, Transport, TransportExt,
29};
30use std::{sync::Arc, time::Duration};
31
32pub use libp2p::bandwidth::BandwidthSinks;
33
34/// Builds the transport that serves as a common ground for all connections.
35///
36/// If `memory_only` is true, then only communication within the same process are allowed. Only
37/// addresses with the format `/memory/...` are allowed.
38///
39/// `yamux_window_size` is the maximum size of the Yamux receive windows. `None` to leave the
40/// default (256kiB).
41///
42/// `yamux_maximum_buffer_size` is the maximum allowed size of the Yamux buffer. This should be
43/// set either to the maximum of all the maximum allowed sizes of messages frames of all
44/// high-level protocols combined, or to some generously high value if you are sure that a maximum
45/// size is enforced on all high-level protocols.
46///
47/// Returns a `BandwidthSinks` object that allows querying the average bandwidth produced by all
48/// the connections spawned with this transport.
49pub fn build_transport(
50	keypair: identity::Keypair,
51	memory_only: bool,
52	yamux_window_size: Option<u32>,
53	yamux_maximum_buffer_size: usize,
54) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
55	// Build the base layer of the transport.
56	let transport = if !memory_only {
57		// Main transport: DNS(TCP)
58		let tcp_config = tcp::Config::new().nodelay(true);
59		let tcp_trans = tcp::tokio::Transport::new(tcp_config.clone());
60		let dns_init = dns::tokio::Transport::system(tcp_trans);
61
62		Either::Left(if let Ok(dns) = dns_init {
63			// WS + WSS transport
64			//
65			// Main transport can't be used for `/wss` addresses because WSS transport needs
66			// unresolved addresses (BUT WSS transport itself needs an instance of DNS transport to
67			// resolve and dial addresses).
68			let tcp_trans = tcp::tokio::Transport::new(tcp_config);
69			let dns_for_wss = dns::tokio::Transport::system(tcp_trans)
70				.expect("same system_conf & resolver to work");
71			Either::Left(websocket::WsConfig::new(dns_for_wss).or_transport(dns))
72		} else {
73			// In case DNS can't be constructed, fallback to TCP + WS (WSS won't work)
74			let tcp_trans = tcp::tokio::Transport::new(tcp_config.clone());
75			let desktop_trans = websocket::WsConfig::new(tcp_trans)
76				.or_transport(tcp::tokio::Transport::new(tcp_config));
77			Either::Right(desktop_trans)
78		})
79	} else {
80		Either::Right(OptionalTransport::some(libp2p::core::transport::MemoryTransport::default()))
81	};
82
83	let authentication_config = noise::Config::new(&keypair).expect("Can create noise config. qed");
84	let multiplexing_config = {
85		let mut yamux_config = libp2p::yamux::Config::default();
86		// Enable proper flow-control: window updates are only sent when
87		// buffered data has been consumed.
88		yamux_config.set_window_update_mode(libp2p::yamux::WindowUpdateMode::on_read());
89		yamux_config.set_max_buffer_size(yamux_maximum_buffer_size);
90
91		if let Some(yamux_window_size) = yamux_window_size {
92			yamux_config.set_receive_window_size(yamux_window_size);
93		}
94
95		yamux_config
96	};
97
98	let transport = transport
99		.upgrade(upgrade::Version::V1Lazy)
100		.authenticate(authentication_config)
101		.multiplex(multiplexing_config)
102		.timeout(Duration::from_secs(20))
103		.boxed();
104
105	transport.with_bandwidth_logging()
106}