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	PeerId,
30};
31
32/// Events generated by DHT as a response to get_value and put_value requests.
33#[derive(Debug, Clone)]
34#[must_use]
35pub enum DhtEvent {
36	/// The value was found.
37	ValueFound(PeerRecord),
38
39	/// The requested record has not been found in the DHT.
40	ValueNotFound(Key),
41
42	/// The record has been successfully inserted into the DHT.
43	ValuePut(Key),
44
45	/// An error has occurred while putting a record into the DHT.
46	ValuePutFailed(Key),
47
48	/// An error occured while registering as a content provider on the DHT.
49	StartProvidingFailed(Key),
50
51	/// The DHT received a put record request.
52	PutRecordRequest(Key, Vec<u8>, Option<sc_network_types::PeerId>, Option<std::time::Instant>),
53
54	/// The providers for [`Key`] were found.
55	ProvidersFound(Key, Vec<PeerId>),
56
57	/// The providers for [`Key`] were not found.
58	ProvidersNotFound(Key),
59}
60
61/// Type for events generated by networking layer.
62#[derive(Debug, Clone)]
63#[must_use]
64pub enum Event {
65	/// Event generated by a DHT.
66	Dht(DhtEvent),
67
68	/// Opened a substream with the given node with the given notifications protocol.
69	///
70	/// The protocol is always one of the notification protocols that have been registered.
71	NotificationStreamOpened {
72		/// Node we opened the substream with.
73		remote: PeerId,
74		/// The concerned protocol. Each protocol uses a different substream.
75		/// This is always equal to the value of
76		/// `sc_network::config::NonDefaultSetConfig::notifications_protocol` of one of the
77		/// configured sets.
78		protocol: ProtocolName,
79		/// If the negotiation didn't use the main name of the protocol (the one in
80		/// `notifications_protocol`), then this field contains which name has actually been
81		/// used.
82		/// Always contains a value equal to the value in
83		/// `sc_network::config::NonDefaultSetConfig::fallback_names`.
84		negotiated_fallback: Option<ProtocolName>,
85		/// Role of the remote.
86		role: ObservedRole,
87		/// Received handshake.
88		received_handshake: Vec<u8>,
89	},
90
91	/// Closed a substream with the given node. Always matches a corresponding previous
92	/// `NotificationStreamOpened` message.
93	NotificationStreamClosed {
94		/// Node we closed the substream with.
95		remote: PeerId,
96		/// The concerned protocol. Each protocol uses a different substream.
97		protocol: ProtocolName,
98	},
99
100	/// Received one or more messages from the given node using the given protocol.
101	NotificationsReceived {
102		/// Node we received the message from.
103		remote: PeerId,
104		/// Concerned protocol and associated message.
105		messages: Vec<(ProtocolName, Bytes)>,
106	},
107}