referrerpolicy=no-referrer-when-downgrade

sc_network/
event.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//! Network event types. These are not the part of the protocol, but rather
20//! events that happen on the network like DHT get/put results received.
21
22use crate::types::ProtocolName;
23
24use bytes::Bytes;
25
26use sc_network_common::role::ObservedRole;
27use sc_network_types::{
28	kad::{Key, PeerRecord},
29	multiaddr::Multiaddr,
30	PeerId,
31};
32
33/// Events generated by DHT as a response to get_value and put_value requests.
34#[derive(Debug, Clone)]
35#[must_use]
36pub enum DhtEvent {
37	/// Found closest peers to the target `PeerId`. With libp2p also delivers a partial result
38	/// in case the query timed out, because it can contain the target peer's address.
39	ClosestPeersFound(PeerId, Vec<(PeerId, Vec<Multiaddr>)>),
40
41	/// Closest peers to the target `PeerId` has not been found.
42	ClosestPeersNotFound(PeerId),
43
44	/// The value was found.
45	ValueFound(PeerRecord),
46
47	/// The requested record has not been found in the DHT.
48	ValueNotFound(Key),
49
50	/// The record has been successfully inserted into the DHT.
51	// TODO: this is not implemented with litep2p network backend.
52	ValuePut(Key),
53
54	/// An error has occurred while putting a record into the DHT.
55	ValuePutFailed(Key),
56
57	/// Successfully started providing the given key.
58	// TODO: this is not implemented with litep2p network backend.
59	StartedProviding(Key),
60
61	/// An error occured while registering as a content provider on the DHT.
62	StartProvidingFailed(Key),
63
64	/// The DHT received a put record request.
65	PutRecordRequest(Key, Vec<u8>, Option<PeerId>, Option<std::time::Instant>),
66
67	/// The providers for [`Key`] were found. Multiple such events can be generated per provider
68	/// discovery request.
69	ProvidersFound(Key, Vec<PeerId>),
70
71	/// `GET_PROVIDERS` query finished and won't yield any more providers.
72	NoMoreProviders(Key),
73
74	/// `GET_PROVIDERS` query failed and no providers for [`Key`] were found. libp2p also emits
75	/// this event after already yielding some results via [`DhtEvent::ProvidersFound`].
76	ProvidersNotFound(Key),
77}
78
79/// Type for events generated by networking layer.
80#[derive(Debug, Clone)]
81#[must_use]
82pub enum Event {
83	/// Event generated by a DHT.
84	Dht(DhtEvent),
85
86	/// Opened a substream with the given node with the given notifications protocol.
87	///
88	/// The protocol is always one of the notification protocols that have been registered.
89	NotificationStreamOpened {
90		/// Node we opened the substream with.
91		remote: PeerId,
92		/// The concerned protocol. Each protocol uses a different substream.
93		/// This is always equal to the value of
94		/// `sc_network::config::NonDefaultSetConfig::notifications_protocol` of one of the
95		/// configured sets.
96		protocol: ProtocolName,
97		/// If the negotiation didn't use the main name of the protocol (the one in
98		/// `notifications_protocol`), then this field contains which name has actually been
99		/// used.
100		/// Always contains a value equal to the value in
101		/// `sc_network::config::NonDefaultSetConfig::fallback_names`.
102		negotiated_fallback: Option<ProtocolName>,
103		/// Role of the remote.
104		role: ObservedRole,
105		/// Received handshake.
106		received_handshake: Vec<u8>,
107	},
108
109	/// Closed a substream with the given node. Always matches a corresponding previous
110	/// `NotificationStreamOpened` message.
111	NotificationStreamClosed {
112		/// Node we closed the substream with.
113		remote: PeerId,
114		/// The concerned protocol. Each protocol uses a different substream.
115		protocol: ProtocolName,
116	},
117
118	/// Received one or more messages from the given node using the given protocol.
119	NotificationsReceived {
120		/// Node we received the message from.
121		remote: PeerId,
122		/// Concerned protocol and associated message.
123		messages: Vec<(ProtocolName, Bytes)>,
124	},
125}