Skip to main content

soil_network/
event.rs

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