sc_network/
lib.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#![warn(unused_extern_crates)]
20#![warn(missing_docs)]
21
22//! Substrate-specific P2P networking.
23//!
24//! **Important**: This crate is unstable and the API and usage may change.
25//!
26//! # Node identities and addresses
27//!
28//! In a decentralized network, each node possesses a network private key and a network public key.
29//! In Substrate, the keys are based on the ed25519 curve.
30//!
31//! From a node's public key, we can derive its *identity*. In Substrate and libp2p, a node's
32//! identity is represented with the [`PeerId`] struct. All network communications between nodes on
33//! the network use encryption derived from both sides's keys, which means that **identities cannot
34//! be faked**.
35//!
36//! A node's identity uniquely identifies a machine on the network. If you start two or more
37//! clients using the same network key, large interferences will happen.
38//!
39//! # Substrate's network protocol
40//!
41//! Substrate's networking protocol is based upon libp2p. It is at the moment not possible and not
42//! planned to permit using something else than the libp2p network stack and the rust-libp2p
43//! library. However the libp2p framework is very flexible and the rust-libp2p library could be
44//! extended to support a wider range of protocols than what is offered by libp2p.
45//!
46//! ## Discovery mechanisms
47//!
48//! In order for our node to join a peer-to-peer network, it has to know a list of nodes that are
49//! part of said network. This includes nodes identities and their address (how to reach them).
50//! Building such a list is called the **discovery** mechanism. There are three mechanisms that
51//! Substrate uses:
52//!
53//! - Bootstrap nodes. These are hard-coded node identities and addresses passed alongside with
54//! the network configuration.
55//! - mDNS. We perform a UDP broadcast on the local network. Nodes that listen may respond with
56//! their identity. More info [here](https://github.com/libp2p/specs/blob/master/discovery/mdns.md).
57//! mDNS can be disabled in the network configuration.
58//! - Kademlia random walk. Once connected, we perform random Kademlia `FIND_NODE` requests on the
59//! configured Kademlia DHTs (one per configured chain protocol) in order for nodes to propagate to
60//! us their view of the network. More information about Kademlia can be found [on
61//! Wikipedia](https://en.wikipedia.org/wiki/Kademlia).
62//!
63//! ## Connection establishment
64//!
65//! When node Alice knows node Bob's identity and address, it can establish a connection with Bob.
66//! All connections must always use encryption and multiplexing. While some node addresses (eg.
67//! addresses using `/quic`) already imply which encryption and/or multiplexing to use, for others
68//! the **multistream-select** protocol is used in order to negotiate an encryption layer and/or a
69//! multiplexing layer.
70//!
71//! The connection establishment mechanism is called the **transport**.
72//!
73//! As of the writing of this documentation, the following base-layer protocols are supported by
74//! Substrate:
75//!
76//! - TCP/IP for addresses of the form `/ip4/1.2.3.4/tcp/5`. Once the TCP connection is open, an
77//! encryption and a multiplexing layer are negotiated on top.
78//! - WebSockets for addresses of the form `/ip4/1.2.3.4/tcp/5/ws`. A TCP/IP connection is open and
79//! the WebSockets protocol is negotiated on top. Communications then happen inside WebSockets data
80//! frames. Encryption and multiplexing are additionally negotiated again inside this channel.
81//! - DNS for addresses of the form `/dns/example.com/tcp/5` or `/dns/example.com/tcp/5/ws`. A
82//! node's address can contain a domain name.
83//! - (All of the above using IPv6 instead of IPv4.)
84//!
85//! On top of the base-layer protocol, the [Noise](https://noiseprotocol.org/) protocol is
86//! negotiated and applied. The exact handshake protocol is experimental and is subject to change.
87//!
88//! The following multiplexing protocols are supported:
89//!
90//! - [Yamux](https://github.com/hashicorp/yamux/blob/master/spec.md).
91//!
92//! ## Substreams
93//!
94//! Once a connection has been established and uses multiplexing, substreams can be opened. When
95//! a substream is open, the **multistream-select** protocol is used to negotiate which protocol
96//! to use on that given substream.
97//!
98//! Protocols that are specific to a certain chain have a `<protocol-id>` in their name. This
99//! "protocol ID" is defined in the chain specifications. For example, the protocol ID of Polkadot
100//! is "dot". In the protocol names below, `<protocol-id>` must be replaced with the corresponding
101//! protocol ID.
102//!
103//! > **Note**: It is possible for the same connection to be used for multiple chains. For example,
104//! > one can use both the `/dot/sync/2` and `/sub/sync/2` protocols on the same
105//! > connection, provided that the remote supports them.
106//!
107//! Substrate uses the following standard libp2p protocols:
108//!
109//! - **`/ipfs/ping/1.0.0`**. We periodically open an ephemeral substream in order to ping the
110//! remote and check whether the connection is still alive. Failure for the remote to reply leads
111//! to a disconnection.
112//! - **[`/ipfs/id/1.0.0`](https://github.com/libp2p/specs/tree/master/identify)**. We
113//! periodically open an ephemeral substream in order to ask information from the remote.
114//! - **[`/<protocol_id>/kad`](https://github.com/libp2p/specs/pull/108)**. We periodically open
115//! ephemeral substreams for Kademlia random walk queries. Each Kademlia query is done in a
116//! separate substream.
117//!
118//! Additionally, Substrate uses the following non-libp2p-standard protocols:
119//!
120//! - **`/substrate/<protocol-id>/<version>`** (where `<protocol-id>` must be replaced with the
121//! protocol ID of the targeted chain, and `<version>` is a number between 2 and 6). For each
122//! connection we optionally keep an additional substream for all Substrate-based communications
123//! alive. This protocol is considered legacy, and is progressively being replaced with
124//! alternatives. This is designated as "The legacy Substrate substream" in this documentation. See
125//! below for more details.
126//! - **`/<protocol-id>/sync/2`** is a request-response protocol (see below) that lets one perform
127//! requests for information about blocks. Each request is the encoding of a `BlockRequest` and
128//! each response is the encoding of a `BlockResponse`, as defined in the `api.v1.proto` file in
129//! this source tree.
130//! - **`/<protocol-id>/light/2`** is a request-response protocol (see below) that lets one perform
131//! light-client-related requests for information about the state. Each request is the encoding of
132//! a `light::Request` and each response is the encoding of a `light::Response`, as defined in the
133//! `light.v1.proto` file in this source tree.
134//! - **`/<protocol-id>/transactions/1`** is a notifications protocol (see below) where
135//! transactions are pushed to other nodes. The handshake is empty on both sides. The message
136//! format is a SCALE-encoded list of transactions, where each transaction is an opaque list of
137//! bytes.
138//! - **`/<protocol-id>/block-announces/1`** is a notifications protocol (see below) where
139//! block announces are pushed to other nodes. The handshake is empty on both sides. The message
140//! format is a SCALE-encoded tuple containing a block header followed with an opaque list of
141//! bytes containing some data associated with this block announcement, e.g. a candidate message.
142//! - Notifications protocols that are registered using
143//! `NetworkConfiguration::notifications_protocols`. For example: `/paritytech/grandpa/1`. See
144//! below for more information.
145//!
146//! ## The legacy Substrate substream
147//!
148//! Substrate uses a component named the **peerset manager (PSM)**. Through the discovery
149//! mechanism, the PSM is aware of the nodes that are part of the network and decides which nodes
150//! we should perform Substrate-based communications with. For these nodes, we open a connection
151//! if necessary and open a unique substream for Substrate-based communications. If the PSM decides
152//! that we should disconnect a node, then that substream is closed.
153//!
154//! For more information about the PSM, see the *sc-peerset* crate.
155//!
156//! Note that at the moment there is no mechanism in place to solve the issues that arise where the
157//! two sides of a connection open the unique substream simultaneously. In order to not run into
158//! issues, only the dialer of a connection is allowed to open the unique substream. When the
159//! substream is closed, the entire connection is closed as well. This is a bug that will be
160//! resolved by deprecating the protocol entirely.
161//!
162//! Within the unique Substrate substream, messages encoded using
163//! [*parity-scale-codec*](https://github.com/paritytech/parity-scale-codec) are exchanged.
164//! The detail of theses messages is not totally in place, but they can be found in the
165//! `message.rs` file.
166//!
167//! Once the substream is open, the first step is an exchange of a *status* message from both
168//! sides, containing information such as the chain root hash, head of chain, and so on.
169//!
170//! Communications within this substream include:
171//!
172//! - Syncing. Blocks are announced and requested from other nodes.
173//! - Light-client requests. When a light client requires information, a random node we have a
174//! substream open with is chosen, and the information is requested from it.
175//! - Gossiping. Used for example by grandpa.
176//!
177//! ## Request-response protocols
178//!
179//! A so-called request-response protocol is defined as follow:
180//!
181//! - When a substream is opened, the opening side sends a message whose content is
182//! protocol-specific. The message must be prefixed with an
183//! [LEB128-encoded number](https://en.wikipedia.org/wiki/LEB128) indicating its length. After the
184//! message has been sent, the writing side is closed.
185//! - The remote sends back the response prefixed with a LEB128-encoded length, and closes its
186//! side as well.
187//!
188//! Each request is performed in a new separate substream.
189//!
190//! ## Notifications protocols
191//!
192//! A so-called notifications protocol is defined as follow:
193//!
194//! - When a substream is opened, the opening side sends a handshake message whose content is
195//! protocol-specific. The handshake message must be prefixed with an
196//! [LEB128-encoded number](https://en.wikipedia.org/wiki/LEB128) indicating its length. The
197//! handshake message can be of length 0, in which case the sender has to send a single `0`.
198//! - The receiver then either immediately closes the substream, or answers with its own
199//! LEB128-prefixed protocol-specific handshake response. The message can be of length 0, in which
200//! case a single `0` has to be sent back.
201//! - Once the handshake has completed, the notifications protocol is unidirectional. Only the
202//! node which initiated the substream can push notifications. If the remote wants to send
203//! notifications as well, it has to open its own undirectional substream.
204//! - Each notification must be prefixed with an LEB128-encoded length. The encoding of the
205//! messages is specific to each protocol.
206//! - Either party can signal that it doesn't want a notifications substream anymore by closing
207//! its writing side. The other party should respond by closing its own writing side soon after.
208//!
209//! The API of `sc-network` allows one to register user-defined notification protocols.
210//! `sc-network` automatically tries to open a substream towards each node for which the legacy
211//! Substream substream is open. The handshake is then performed automatically.
212//!
213//! For example, the `sc-consensus-grandpa` crate registers the `/paritytech/grandpa/1`
214//! notifications protocol.
215//!
216//! At the moment, for backwards-compatibility, notification protocols are tied to the legacy
217//! Substrate substream. Additionally, the handshake message is hardcoded to be a single 8-bits
218//! integer representing the role of the node:
219//!
220//! - 1 for a full node.
221//! - 2 for a light node.
222//! - 4 for an authority.
223//!
224//! In the future, though, these restrictions will be removed.
225//!
226//! # Usage
227//!
228//! Using the `sc-network` crate is done through the [`NetworkWorker`] struct. Create this
229//! struct by passing a [`config::Params`], then poll it as if it was a `Future`. You can extract an
230//! `Arc<NetworkService>` from the `NetworkWorker`, which can be shared amongst multiple places
231//! in order to give orders to the networking.
232//!
233//! See the [`config`] module for more information about how to configure the networking.
234//!
235//! After the `NetworkWorker` has been created, the important things to do are:
236//!
237//! - Calling `NetworkWorker::poll` in order to advance the network. This can be done by
238//! dispatching a background task with the [`NetworkWorker`].
239//! - Calling `on_block_import` whenever a block is added to the client.
240//! - Calling `on_block_finalized` whenever a block is finalized.
241//! - Calling `trigger_repropagate` when a transaction is added to the pool.
242//!
243//! More precise usage details are still being worked on and will likely change in the future.
244
245mod behaviour;
246mod bitswap;
247mod litep2p;
248mod protocol;
249
250#[cfg(test)]
251mod mock;
252
253pub mod config;
254pub mod discovery;
255pub mod error;
256pub mod event;
257pub mod network_state;
258pub mod peer_info;
259pub mod peer_store;
260pub mod protocol_controller;
261pub mod request_responses;
262pub mod service;
263pub mod transport;
264pub mod types;
265pub mod utils;
266
267pub use crate::litep2p::Litep2pNetworkBackend;
268pub use event::{DhtEvent, Event};
269#[doc(inline)]
270pub use request_responses::{Config, IfDisconnected, RequestFailure};
271pub use sc_network_common::{
272	role::{ObservedRole, Roles},
273	types::ReputationChange,
274};
275pub use sc_network_types::{
276	multiaddr::{self, Multiaddr},
277	PeerId,
278};
279pub use service::{
280	metrics::NotificationMetrics,
281	signature::Signature,
282	traits::{
283		KademliaKey, MessageSink, NetworkBackend, NetworkBlock, NetworkDHTProvider,
284		NetworkEventStream, NetworkPeers, NetworkRequest, NetworkSigner, NetworkStateInfo,
285		NetworkStatus, NetworkStatusProvider, NetworkSyncForkRequest, NotificationConfig,
286		NotificationSender as NotificationSenderT, NotificationSenderError,
287		NotificationSenderReady, NotificationService,
288	},
289	DecodingError, Keypair, NetworkService, NetworkWorker, NotificationSender, OutboundFailure,
290	PublicKey,
291};
292pub use types::ProtocolName;
293
294/// The maximum allowed number of established connections per peer.
295///
296/// Typically, and by design of the network behaviours in this crate,
297/// there is a single established connection per peer. However, to
298/// avoid unnecessary and nondeterministic connection closure in
299/// case of (possibly repeated) simultaneous dialing attempts between
300/// two peers, the per-peer connection limit is not set to 1 but 2.
301const MAX_CONNECTIONS_PER_PEER: usize = 2;
302
303/// The maximum number of concurrent established connections that were incoming.
304const MAX_CONNECTIONS_ESTABLISHED_INCOMING: u32 = 10_000;
305
306/// Maximum response size limit.
307pub const MAX_RESPONSE_SIZE: u64 = 16 * 1024 * 1024;