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;
25use libp2p::{
26	kad::{record::Key, PeerRecord},
27	PeerId,
28};
29
30use sc_network_common::role::ObservedRole;
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	/// The DHT received a put record request.
49	PutRecordRequest(Key, Vec<u8>, Option<sc_network_types::PeerId>, Option<std::time::Instant>),
50}
51
52/// Type for events generated by networking layer.
53#[derive(Debug, Clone)]
54#[must_use]
55pub enum Event {
56	/// Event generated by a DHT.
57	Dht(DhtEvent),
58
59	/// Opened a substream with the given node with the given notifications protocol.
60	///
61	/// The protocol is always one of the notification protocols that have been registered.
62	NotificationStreamOpened {
63		/// Node we opened the substream with.
64		remote: PeerId,
65		/// The concerned protocol. Each protocol uses a different substream.
66		/// This is always equal to the value of
67		/// `sc_network::config::NonDefaultSetConfig::notifications_protocol` of one of the
68		/// configured sets.
69		protocol: ProtocolName,
70		/// If the negotiation didn't use the main name of the protocol (the one in
71		/// `notifications_protocol`), then this field contains which name has actually been
72		/// used.
73		/// Always contains a value equal to the value in
74		/// `sc_network::config::NonDefaultSetConfig::fallback_names`.
75		negotiated_fallback: Option<ProtocolName>,
76		/// Role of the remote.
77		role: ObservedRole,
78		/// Received handshake.
79		received_handshake: Vec<u8>,
80	},
81
82	/// Closed a substream with the given node. Always matches a corresponding previous
83	/// `NotificationStreamOpened` message.
84	NotificationStreamClosed {
85		/// Node we closed the substream with.
86		remote: PeerId,
87		/// The concerned protocol. Each protocol uses a different substream.
88		protocol: ProtocolName,
89	},
90
91	/// Received one or more messages from the given node using the given protocol.
92	NotificationsReceived {
93		/// Node we received the message from.
94		remote: PeerId,
95		/// Concerned protocol and associated message.
96		messages: Vec<(ProtocolName, Bytes)>,
97	},
98}