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	ValuePut(Key),
52
53	/// An error has occurred while putting a record into the DHT.
54	ValuePutFailed(Key),
55
56	/// Successfully started providing the given key.
57	StartedProviding(Key),
58
59	/// An error occured while registering as a content provider on the DHT.
60	StartProvidingFailed(Key),
61
62	/// The DHT received a put record request.
63	PutRecordRequest(Key, Vec<u8>, Option<PeerId>, Option<std::time::Instant>),
64
65	/// The providers for [`Key`] were found. Multiple such events can be generated per provider
66	/// discovery request.
67	ProvidersFound(Key, Vec<PeerId>),
68
69	/// `GET_PROVIDERS` query finished and won't yield any more providers.
70	NoMoreProviders(Key),
71
72	/// `GET_PROVIDERS` query failed and no providers for [`Key`] were found. libp2p also emits
73	/// this event after already yielding some results via [`DhtEvent::ProvidersFound`].
74	ProvidersNotFound(Key),
75}
76
77/// Type for events generated by networking layer.
78#[derive(Debug, Clone)]
79#[must_use]
80pub enum Event {
81	/// Event generated by a DHT.
82	Dht(DhtEvent),
83
84	/// Opened a substream with the given node with the given notifications protocol.
85	///
86	/// The protocol is always one of the notification protocols that have been registered.
87	NotificationStreamOpened {
88		/// Node we opened the substream with.
89		remote: PeerId,
90		/// The concerned protocol. Each protocol uses a different substream.
91		/// This is always equal to the value of
92		/// `sc_network::config::NonDefaultSetConfig::notifications_protocol` of one of the
93		/// configured sets.
94		protocol: ProtocolName,
95		/// If the negotiation didn't use the main name of the protocol (the one in
96		/// `notifications_protocol`), then this field contains which name has actually been
97		/// used.
98		/// Always contains a value equal to the value in
99		/// `sc_network::config::NonDefaultSetConfig::fallback_names`.
100		negotiated_fallback: Option<ProtocolName>,
101		/// Role of the remote.
102		role: ObservedRole,
103		/// Received handshake.
104		received_handshake: Vec<u8>,
105	},
106
107	/// Closed a substream with the given node. Always matches a corresponding previous
108	/// `NotificationStreamOpened` message.
109	NotificationStreamClosed {
110		/// Node we closed the substream with.
111		remote: PeerId,
112		/// The concerned protocol. Each protocol uses a different substream.
113		protocol: ProtocolName,
114	},
115
116	/// Received one or more messages from the given node using the given protocol.
117	NotificationsReceived {
118		/// Node we received the message from.
119		remote: PeerId,
120		/// Concerned protocol and associated message.
121		messages: Vec<(ProtocolName, Bytes)>,
122	},
123}