litep2p/protocol/notification/
types.rs

1// Copyright 2023 litep2p developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::{
22    protocol::notification::handle::NotificationSink, types::protocol::ProtocolName, PeerId,
23};
24
25use bytes::BytesMut;
26use tokio::sync::oneshot;
27
28use std::collections::HashSet;
29
30/// Default channel size for synchronous notifications.
31pub(super) const SYNC_CHANNEL_SIZE: usize = 2048;
32
33/// Default channel size for asynchronous notifications.
34pub(super) const ASYNC_CHANNEL_SIZE: usize = 8;
35
36/// Direction of the connection.
37#[derive(Debug, Copy, Clone, PartialEq, Eq)]
38pub enum Direction {
39    /// Connection is considered inbound, i.e., it was initiated by the remote node.
40    Inbound,
41
42    /// Connection is considered outbound, i.e., it was initiated by the local node.
43    Outbound,
44}
45
46/// Validation result.
47#[derive(Debug, Copy, Clone, PartialEq, Eq)]
48pub enum ValidationResult {
49    /// Accept the inbound substream.
50    Accept,
51
52    /// Reject the inbound substream.
53    Reject,
54}
55
56/// Notification error.
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum NotificationError {
59    /// Remote rejected the substream.
60    Rejected,
61
62    /// Connection to peer doesn't exist.
63    NoConnection,
64
65    /// Synchronous notification channel is clogged.
66    ChannelClogged,
67
68    /// Validation for a previous substream still pending.
69    ValidationPending,
70
71    /// Failed to dial peer.
72    DialFailure,
73
74    /// Notification protocol has been closed.
75    EssentialTaskClosed,
76}
77
78/// Notification events.
79pub(crate) enum InnerNotificationEvent {
80    /// Validate substream.
81    ValidateSubstream {
82        /// Protocol name.
83        protocol: ProtocolName,
84
85        /// Fallback, if the substream was negotiated using a fallback protocol.
86        fallback: Option<ProtocolName>,
87
88        /// Peer ID.
89        peer: PeerId,
90
91        /// Handshake.
92        handshake: Vec<u8>,
93
94        /// `oneshot::Sender` for sending the validation result back to the protocol.
95        tx: oneshot::Sender<ValidationResult>,
96    },
97
98    /// Notification stream opened.
99    NotificationStreamOpened {
100        /// Protocol name.
101        protocol: ProtocolName,
102
103        /// Fallback, if the substream was negotiated using a fallback protocol.
104        fallback: Option<ProtocolName>,
105
106        /// Direction of the substream.
107        direction: Direction,
108
109        /// Peer ID.
110        peer: PeerId,
111
112        /// Handshake.
113        handshake: Vec<u8>,
114
115        /// Notification sink.
116        sink: NotificationSink,
117    },
118
119    /// Notification stream closed.
120    NotificationStreamClosed {
121        /// Peer ID.
122        peer: PeerId,
123    },
124
125    /// Failed to open notification stream.
126    NotificationStreamOpenFailure {
127        /// Peer ID.
128        peer: PeerId,
129
130        /// Error.
131        error: NotificationError,
132    },
133}
134
135/// Notification events.
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub enum NotificationEvent {
138    /// Validate substream.
139    ValidateSubstream {
140        /// Protocol name.
141        protocol: ProtocolName,
142
143        /// Fallback, if the substream was negotiated using a fallback protocol.
144        fallback: Option<ProtocolName>,
145
146        /// Peer ID.
147        peer: PeerId,
148
149        /// Handshake.
150        handshake: Vec<u8>,
151    },
152
153    /// Notification stream opened.
154    NotificationStreamOpened {
155        /// Protocol name.
156        protocol: ProtocolName,
157
158        /// Fallback, if the substream was negotiated using a fallback protocol.
159        fallback: Option<ProtocolName>,
160
161        /// Direction of the substream.
162        ///
163        /// [`Direction::Inbound`](crate::protocol::Direction::Outbound) indicates that the
164        /// substream was opened by the remote peer and
165        /// [`Direction::Outbound`](crate::protocol::Direction::Outbound) that it was
166        /// opened by the local node.
167        direction: Direction,
168
169        /// Peer ID.
170        peer: PeerId,
171
172        /// Handshake.
173        handshake: Vec<u8>,
174    },
175
176    /// Notification stream closed.
177    NotificationStreamClosed {
178        /// Peer ID.
179        peer: PeerId,
180    },
181
182    /// Failed to open notification stream.
183    NotificationStreamOpenFailure {
184        /// Peer ID.
185        peer: PeerId,
186
187        /// Error.
188        error: NotificationError,
189    },
190
191    /// Notification received.
192    NotificationReceived {
193        /// Peer ID.
194        peer: PeerId,
195
196        /// Notification.
197        notification: BytesMut,
198    },
199}
200
201/// Notification commands sent to the protocol.
202pub(crate) enum NotificationCommand {
203    /// Open substreams to one or more peers.
204    OpenSubstream {
205        /// Peer IDs.
206        peers: HashSet<PeerId>,
207    },
208
209    /// Close substreams to one or more peers.
210    CloseSubstream {
211        /// Peer IDs.
212        peers: HashSet<PeerId>,
213    },
214
215    /// Force close the connection because notification channel is clogged.
216    ForceClose {
217        /// Peer to disconnect.
218        peer: PeerId,
219    },
220}