pezsc_network/
lib.rs

1// This file is part of Bizinikiwi.
2
3// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
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//! Bizinikiwi-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 Bizinikiwi, the keys are based on the ed25519 curve.
30//!
31//! From a node's public key, we can derive its *identity*. In Bizinikiwi 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//! # Bizinikiwi's network protocol
40//!
41//! Bizinikiwi'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//! Bizinikiwi 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//! Bizinikiwi:
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 Pezkuwi
100//! is "hez". 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 `/hez/sync/2` and `/sub/sync/2` protocols on the same
105//! > connection, provided that the remote supports them.
106//!
107//! Bizinikiwi 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, Bizinikiwi uses the following non-libp2p-standard protocols:
119//!
120//! - **`/bizinikiwi/<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 Bizinikiwi-based communications
123//! alive. This protocol is considered legacy, and is progressively being replaced with
124//! alternatives. This is designated as "The legacy Bizinikiwi 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 Bizinikiwi substream
147//!
148//! Bizinikiwi 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 Bizinikiwi-based communications with. For these nodes, we open a connection
151//! if necessary and open a unique substream for Bizinikiwi-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 Bizinikiwi substream, messages encoded using [*parity-scale-codec*](https://github.com/pezkuwichain/parity-scale-codec) are exchanged.
163//! The detail of theses messages is not totally in place, but they can be found in the
164//! `message.rs` file.
165//!
166//! Once the substream is open, the first step is an exchange of a *status* message from both
167//! sides, containing information such as the chain root hash, head of chain, and so on.
168//!
169//! Communications within this substream include:
170//!
171//! - Syncing. Blocks are announced and requested from other nodes.
172//! - Light-client requests. When a light client requires information, a random node we have a
173//! substream open with is chosen, and the information is requested from it.
174//! - Gossiping. Used for example by grandpa.
175//!
176//! ## Request-response protocols
177//!
178//! A so-called request-response protocol is defined as follow:
179//!
180//! - When a substream is opened, the opening side sends a message whose content is
181//! protocol-specific. The message must be prefixed with an
182//! [LEB128-encoded number](https://en.wikipedia.org/wiki/LEB128) indicating its length. After the
183//! message has been sent, the writing side is closed.
184//! - The remote sends back the response prefixed with a LEB128-encoded length, and closes its
185//! side as well.
186//!
187//! Each request is performed in a new separate substream.
188//!
189//! ## Notifications protocols
190//!
191//! A so-called notifications protocol is defined as follow:
192//!
193//! - When a substream is opened, the opening side sends a handshake message whose content is
194//! protocol-specific. The handshake message must be prefixed with an
195//! [LEB128-encoded number](https://en.wikipedia.org/wiki/LEB128) indicating its length. The
196//! handshake message can be of length 0, in which case the sender has to send a single `0`.
197//! - The receiver then either immediately closes the substream, or answers with its own
198//! LEB128-prefixed protocol-specific handshake response. The message can be of length 0, in which
199//! case a single `0` has to be sent back.
200//! - Once the handshake has completed, the notifications protocol is unidirectional. Only the
201//! node which initiated the substream can push notifications. If the remote wants to send
202//! notifications as well, it has to open its own undirectional substream.
203//! - Each notification must be prefixed with an LEB128-encoded length. The encoding of the
204//! messages is specific to each protocol.
205//! - Either party can signal that it doesn't want a notifications substream anymore by closing
206//! its writing side. The other party should respond by closing its own writing side soon after.
207//!
208//! The API of `sc-network` allows one to register user-defined notification protocols.
209//! `sc-network` automatically tries to open a substream towards each node for which the legacy
210//! Substream substream is open. The handshake is then performed automatically.
211//!
212//! For example, the `sc-consensus-grandpa` crate registers the `/paritytech/grandpa/1`
213//! notifications protocol.
214//!
215//! At the moment, for backwards-compatibility, notification protocols are tied to the legacy
216//! Bizinikiwi substream. Additionally, the handshake message is hardcoded to be a single 8-bits
217//! integer representing the role of the node:
218//!
219//! - 1 for a full node.
220//! - 2 for a light node.
221//! - 4 for an authority.
222//!
223//! In the future, though, these restrictions will be removed.
224//!
225//! # Usage
226//!
227//! Using the `sc-network` crate is done through the [`NetworkWorker`] struct. Create this
228//! struct by passing a [`config::Params`], then poll it as if it was a `Future`. You can extract an
229//! `Arc<NetworkService>` from the `NetworkWorker`, which can be shared amongst multiple places
230//! in order to give orders to the networking.
231//!
232//! See the [`config`] module for more information about how to configure the networking.
233//!
234//! After the `NetworkWorker` has been created, the important things to do are:
235//!
236//! - Calling `NetworkWorker::poll` in order to advance the network. This can be done by
237//! dispatching a background task with the [`NetworkWorker`].
238//! - Calling `on_block_import` whenever a block is added to the client.
239//! - Calling `on_block_finalized` whenever a block is finalized.
240//! - Calling `trigger_repropagate` when a transaction is added to the pool.
241//!
242//! More precise usage details are still being worked on and will likely change in the future.
243
244mod behaviour;
245mod bitswap;
246mod litep2p;
247mod protocol;
248
249#[cfg(test)]
250mod mock;
251
252pub mod config;
253pub mod discovery;
254pub mod error;
255pub mod event;
256pub mod network_state;
257pub mod peer_info;
258pub mod peer_store;
259pub mod protocol_controller;
260pub mod request_responses;
261pub mod service;
262pub mod transport;
263pub mod types;
264pub mod utils;
265
266pub use crate::litep2p::Litep2pNetworkBackend;
267pub use event::{DhtEvent, Event};
268pub use pezsc_network_common::{
269	role::{ObservedRole, Roles},
270	types::ReputationChange,
271};
272pub use pezsc_network_types::{
273	multiaddr::{self, Multiaddr},
274	PeerId,
275};
276#[doc(inline)]
277pub use request_responses::{Config, IfDisconnected, RequestFailure};
278pub use service::{
279	metrics::NotificationMetrics,
280	signature::Signature,
281	traits::{
282		KademliaKey, MessageSink, NetworkBackend, NetworkBlock, NetworkDHTProvider,
283		NetworkEventStream, NetworkPeers, NetworkRequest, NetworkSigner, NetworkStateInfo,
284		NetworkStatus, NetworkStatusProvider, NetworkSyncForkRequest, NotificationConfig,
285		NotificationSender as NotificationSenderT, NotificationSenderError,
286		NotificationSenderReady, NotificationService,
287	},
288	DecodingError, Keypair, NetworkService, NetworkWorker, NotificationSender, OutboundFailure,
289	PublicKey,
290};
291pub use types::ProtocolName;
292
293/// Log target for `sc-network`.
294const LOG_TARGET: &str = "sub-libp2p";
295
296/// The maximum allowed number of established connections per peer.
297///
298/// Typically, and by design of the network behaviours in this crate,
299/// there is a single established connection per peer. However, to
300/// avoid unnecessary and nondeterministic connection closure in
301/// case of (possibly repeated) simultaneous dialing attempts between
302/// two peers, the per-peer connection limit is not set to 1 but 2.
303const MAX_CONNECTIONS_PER_PEER: usize = 2;
304
305/// The maximum number of concurrent established connections that were incoming.
306const MAX_CONNECTIONS_ESTABLISHED_INCOMING: u32 = 10_000;
307
308/// Maximum response size limit.
309pub const MAX_RESPONSE_SIZE: u64 = 16 * 1024 * 1024;