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