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
32// TODO: Create a wrapper similar to upstream `BandwidthTransport` that tracks sent/received bytes
33#[allow(deprecated)]
34pub use libp2p::bandwidth::BandwidthSinks;
35
36/// Builds the transport that serves as a common ground for all connections.
37///
38/// If `memory_only` is true, then only communication within the same process are allowed. Only
39/// addresses with the format `/memory/...` are allowed.
40///
41/// Returns a `BandwidthSinks` object that allows querying the average bandwidth produced by all
42/// the connections spawned with this transport.
43#[allow(deprecated)]
44pub fn build_transport(
45 keypair: identity::Keypair,
46 memory_only: bool,
47) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
48 // Build the base layer of the transport.
49 let transport = if !memory_only {
50 // Main transport: DNS(TCP)
51 let tcp_config = tcp::Config::new().nodelay(true);
52 let tcp_trans = tcp::tokio::Transport::new(tcp_config.clone());
53 let dns_init = dns::tokio::Transport::system(tcp_trans);
54
55 Either::Left(if let Ok(dns) = dns_init {
56 // WS + WSS transport
57 //
58 // Main transport can't be used for `/wss` addresses because WSS transport needs
59 // unresolved addresses (BUT WSS transport itself needs an instance of DNS transport to
60 // resolve and dial addresses).
61 let tcp_trans = tcp::tokio::Transport::new(tcp_config);
62 let dns_for_wss = dns::tokio::Transport::system(tcp_trans)
63 .expect("same system_conf & resolver to work");
64 Either::Left(websocket::WsConfig::new(dns_for_wss).or_transport(dns))
65 } else {
66 // In case DNS can't be constructed, fallback to TCP + WS (WSS won't work)
67 let tcp_trans = tcp::tokio::Transport::new(tcp_config.clone());
68 let desktop_trans = websocket::WsConfig::new(tcp_trans)
69 .or_transport(tcp::tokio::Transport::new(tcp_config));
70 Either::Right(desktop_trans)
71 })
72 } else {
73 Either::Right(OptionalTransport::some(libp2p::core::transport::MemoryTransport::default()))
74 };
75
76 let authentication_config = noise::Config::new(&keypair).expect("Can create noise config. qed");
77 let multiplexing_config = libp2p::yamux::Config::default();
78
79 let transport = transport
80 .upgrade(upgrade::Version::V1Lazy)
81 .authenticate(authentication_config)
82 .multiplex(multiplexing_config)
83 .timeout(Duration::from_secs(20))
84 .boxed();
85
86 transport.with_bandwidth_logging()
87}