sc_network/
network_state.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//! Information about the networking, for diagnostic purposes.
20//!
21//! **Warning**: These APIs are not stable.
22
23use libp2p::{
24	core::{ConnectedPoint, Endpoint as CoreEndpoint},
25	Multiaddr,
26};
27use serde::{Deserialize, Serialize};
28use std::{
29	collections::{HashMap, HashSet},
30	time::Duration,
31};
32
33/// Returns general information about the networking.
34///
35/// Meant for general diagnostic purposes.
36///
37/// **Warning**: This API is not stable.
38#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39#[serde(rename_all = "camelCase")]
40pub struct NetworkState {
41	/// PeerId of the local node.
42	pub peer_id: String,
43	/// List of addresses the node is currently listening on.
44	pub listened_addresses: HashSet<Multiaddr>,
45	/// List of addresses the node knows it can be reached as.
46	pub external_addresses: HashSet<Multiaddr>,
47	/// List of node we're connected to.
48	pub connected_peers: HashMap<String, Peer>,
49	/// List of node that we know of but that we're not connected to.
50	pub not_connected_peers: HashMap<String, NotConnectedPeer>,
51	/// State of the peerset manager.
52	pub peerset: serde_json::Value,
53}
54
55/// Part of the `NetworkState` struct. Unstable.
56#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
57#[serde(rename_all = "camelCase")]
58pub struct Peer {
59	/// How we are connected to the node.
60	pub endpoint: PeerEndpoint,
61	/// Node information, as provided by the node itself. Can be empty if not known yet.
62	pub version_string: Option<String>,
63	/// Latest ping duration with this node.
64	pub latest_ping_time: Option<Duration>,
65	/// List of addresses known for this node.
66	pub known_addresses: HashSet<Multiaddr>,
67}
68
69/// Part of the `NetworkState` struct. Unstable.
70#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub struct NotConnectedPeer {
73	/// List of addresses known for this node.
74	pub known_addresses: HashSet<Multiaddr>,
75	/// Node information, as provided by the node itself, if we were ever connected to this node.
76	pub version_string: Option<String>,
77	/// Latest ping duration with this node, if we were ever connected to this node.
78	pub latest_ping_time: Option<Duration>,
79}
80
81/// Part of the `NetworkState` struct. Unstable.
82#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
83#[serde(rename_all = "camelCase")]
84pub enum PeerEndpoint {
85	/// We are dialing the given address.
86	Dialing(Multiaddr, Endpoint),
87	/// We are listening.
88	Listening {
89		/// Local address of the connection.
90		local_addr: Multiaddr,
91		/// Address data is sent back to.
92		send_back_addr: Multiaddr,
93	},
94}
95
96/// Part of the `NetworkState` struct. Unstable.
97#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
98#[serde(rename_all = "camelCase")]
99pub enum Endpoint {
100	/// The socket comes from a dialer.
101	Dialer,
102	/// The socket comes from a listener.
103	Listener,
104}
105
106impl From<ConnectedPoint> for PeerEndpoint {
107	fn from(endpoint: ConnectedPoint) -> Self {
108		match endpoint {
109			ConnectedPoint::Dialer { address, role_override } =>
110				Self::Dialing(address, role_override.into()),
111			ConnectedPoint::Listener { local_addr, send_back_addr } =>
112				Self::Listening { local_addr, send_back_addr },
113		}
114	}
115}
116
117impl From<CoreEndpoint> for Endpoint {
118	fn from(endpoint: CoreEndpoint) -> Self {
119		match endpoint {
120			CoreEndpoint::Dialer => Self::Dialer,
121			CoreEndpoint::Listener => Self::Listener,
122		}
123	}
124}