1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2023 litep2p developers
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::{
    error::{ImmediateDialError, SubstreamError},
    multistream_select::ProtocolError,
    types::{protocol::ProtocolName, RequestId},
    Error, PeerId,
};

use futures::channel;
use tokio::sync::{
    mpsc::{Receiver, Sender},
    oneshot,
};

use std::{
    collections::HashMap,
    io::ErrorKind,
    pin::Pin,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    task::{Context, Poll},
};

/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::request-response::handle";

/// Request-response error.
#[derive(Debug, PartialEq)]
pub enum RequestResponseError {
    /// Request was rejected.
    Rejected(RejectReason),

    /// Request was canceled by the local node.
    Canceled,

    /// Request timed out.
    Timeout,

    /// The peer is not connected and the dialing option was [`DialOptions::Reject`].
    NotConnected,

    /// Too large payload.
    TooLargePayload,

    /// Protocol not supported.
    UnsupportedProtocol,
}

/// The reason why a request was rejected.
#[derive(Debug, PartialEq)]
pub enum RejectReason {
    /// Substream error.
    SubstreamOpenError(SubstreamError),

    /// The peer disconnected before the request was processed.
    ConnectionClosed,

    /// The substream was closed before the request was processed.
    SubstreamClosed,

    /// The dial failed.
    ///
    /// If the dial failure is immediate, the error is included.
    ///
    /// If the dialing process is happening in parallel on multiple
    /// addresses (potentially with multiple protocols), the dialing
    /// process is not considered immediate and the given errors are not
    /// propagated for simplicity.
    DialFailed(Option<ImmediateDialError>),
}

impl From<SubstreamError> for RejectReason {
    fn from(error: SubstreamError) -> Self {
        // Convert `ErrorKind::NotConnected` to `RejectReason::ConnectionClosed`.
        match error {
            SubstreamError::IoError(error) if error == ErrorKind::NotConnected =>
                RejectReason::ConnectionClosed,
            SubstreamError::YamuxError(crate::yamux::ConnectionError::Io(error), _)
                if error.kind() == ErrorKind::NotConnected =>
                RejectReason::ConnectionClosed,
            SubstreamError::NegotiationError(crate::error::NegotiationError::IoError(error))
                if error == ErrorKind::NotConnected =>
                RejectReason::ConnectionClosed,
            SubstreamError::NegotiationError(
                crate::error::NegotiationError::MultistreamSelectError(
                    crate::multistream_select::NegotiationError::ProtocolError(
                        ProtocolError::IoError(error),
                    ),
                ),
            ) if error.kind() == ErrorKind::NotConnected => RejectReason::ConnectionClosed,
            error => RejectReason::SubstreamOpenError(error),
        }
    }
}

/// Request-response events.
pub(super) enum InnerRequestResponseEvent {
    /// Request received from remote
    RequestReceived {
        /// Peer Id.
        peer: PeerId,

        /// Fallback protocol, if the substream was negotiated using a fallback.
        fallback: Option<ProtocolName>,

        /// Request ID.
        request_id: RequestId,

        /// Received request.
        request: Vec<u8>,

        /// `oneshot::Sender` for response.
        response_tx: oneshot::Sender<(Vec<u8>, Option<channel::oneshot::Sender<()>>)>,
    },

    /// Response received.
    ResponseReceived {
        /// Peer Id.
        peer: PeerId,

        /// Fallback protocol, if the substream was negotiated using a fallback.
        fallback: Option<ProtocolName>,

        /// Request ID.
        request_id: RequestId,

        /// Received request.
        response: Vec<u8>,
    },

    /// Request failed.
    RequestFailed {
        /// Peer Id.
        peer: PeerId,

        /// Request ID.
        request_id: RequestId,

        /// Request-response error.
        error: RequestResponseError,
    },
}

impl From<InnerRequestResponseEvent> for RequestResponseEvent {
    fn from(event: InnerRequestResponseEvent) -> Self {
        match event {
            InnerRequestResponseEvent::ResponseReceived {
                peer,
                request_id,
                response,
                fallback,
            } => RequestResponseEvent::ResponseReceived {
                peer,
                request_id,
                response,
                fallback,
            },
            InnerRequestResponseEvent::RequestFailed {
                peer,
                request_id,
                error,
            } => RequestResponseEvent::RequestFailed {
                peer,
                request_id,
                error,
            },
            _ => panic!("unhandled event"),
        }
    }
}

/// Request-response events.
#[derive(Debug, PartialEq)]
pub enum RequestResponseEvent {
    /// Request received from remote
    RequestReceived {
        /// Peer Id.
        peer: PeerId,

        /// Fallback protocol, if the substream was negotiated using a fallback.
        fallback: Option<ProtocolName>,

        /// Request ID.
        ///
        /// While `request_id` is guaranteed to be unique for this protocols, the request IDs are
        /// not unique across different request-response protocols, meaning two different
        /// request-response protocols can both assign `RequestId(123)` for any given request.
        request_id: RequestId,

        /// Received request.
        request: Vec<u8>,
    },

    /// Response received.
    ResponseReceived {
        /// Peer Id.
        peer: PeerId,

        /// Request ID.
        request_id: RequestId,

        /// Fallback protocol, if the substream was negotiated using a fallback.
        fallback: Option<ProtocolName>,

        /// Received request.
        response: Vec<u8>,
    },

    /// Request failed.
    RequestFailed {
        /// Peer Id.
        peer: PeerId,

        /// Request ID.
        request_id: RequestId,

        /// Request-response error.
        error: RequestResponseError,
    },
}

/// Dial behavior when sending requests.
#[derive(Debug)]
pub enum DialOptions {
    /// If the peer is not currently connected, attempt to dial them before sending a request.
    ///
    /// If the dial succeeds, the request is sent to the peer once the peer has been registered
    /// to the protocol.
    ///
    /// If the dial fails, [`RequestResponseError::Rejected`] is returned.
    Dial,

    /// If the peer is not connected, immediately reject the request and return
    /// [`RequestResponseError::NotConnected`].
    Reject,
}

/// Request-response commands.
pub(crate) enum RequestResponseCommand {
    /// Send request to remote peer.
    SendRequest {
        /// Peer ID.
        peer: PeerId,

        /// Request ID.
        ///
        /// When a response is received or the request fails, the event contains this ID that
        /// the user protocol can associate with the correct request.
        ///
        /// If the user protocol only has one active request per peer, this ID can be safely
        /// discarded.
        request_id: RequestId,

        /// Request.
        request: Vec<u8>,

        /// Dial options, see [`DialOptions`] for more details.
        dial_options: DialOptions,
    },

    SendRequestWithFallback {
        /// Peer ID.
        peer: PeerId,

        /// Request ID.
        request_id: RequestId,

        /// Request that is sent over the main protocol, if negotiated.
        request: Vec<u8>,

        /// Request that is sent over the fallback protocol, if negotiated.
        fallback: (ProtocolName, Vec<u8>),

        /// Dial options, see [`DialOptions`] for more details.
        dial_options: DialOptions,
    },

    /// Cancel outbound request.
    CancelRequest {
        /// Request ID.
        request_id: RequestId,
    },
}

/// Handle given to the user protocol which allows it to interact with the request-response
/// protocol.
pub struct RequestResponseHandle {
    /// TX channel for sending commands to the request-response protocol.
    event_rx: Receiver<InnerRequestResponseEvent>,

    /// RX channel for receiving events from the request-response protocol.
    command_tx: Sender<RequestResponseCommand>,

    /// Pending responses.
    pending_responses:
        HashMap<RequestId, oneshot::Sender<(Vec<u8>, Option<channel::oneshot::Sender<()>>)>>,

    /// Next ephemeral request ID.
    next_request_id: Arc<AtomicUsize>,
}

impl RequestResponseHandle {
    /// Create new [`RequestResponseHandle`].
    pub(super) fn new(
        event_rx: Receiver<InnerRequestResponseEvent>,
        command_tx: Sender<RequestResponseCommand>,
        next_request_id: Arc<AtomicUsize>,
    ) -> Self {
        Self {
            event_rx,
            command_tx,
            next_request_id,
            pending_responses: HashMap::new(),
        }
    }

    /// Reject an inbound request.
    ///
    /// Reject request received from a remote peer. The substream is dropped which signals
    /// to the remote peer that request was rejected.
    pub fn reject_request(&mut self, request_id: RequestId) {
        match self.pending_responses.remove(&request_id) {
            None => {
                tracing::debug!(target: LOG_TARGET, ?request_id, "rejected request doesn't exist")
            }
            Some(sender) => {
                tracing::debug!(target: LOG_TARGET, ?request_id, "reject request");
                drop(sender);
            }
        }
    }

    /// Cancel an outbound request.
    ///
    /// Allows canceling an in-flight request if the local node is not interested in the answer
    /// anymore. If the request was canceled, no event is reported to the user as the cancelation
    /// always succeeds and it's assumed that the user does the necessary state clean up in their
    /// end after calling [`RequestResponseHandle::cancel_request()`].
    pub async fn cancel_request(&mut self, request_id: RequestId) {
        tracing::trace!(target: LOG_TARGET, ?request_id, "cancel request");

        let _ = self.command_tx.send(RequestResponseCommand::CancelRequest { request_id }).await;
    }

    /// Get next request ID.
    fn next_request_id(&self) -> RequestId {
        let request_id = self.next_request_id.fetch_add(1usize, Ordering::Relaxed);
        RequestId::from(request_id)
    }

    /// Send request to remote peer.
    ///
    /// While the returned `RequestId` is guaranteed to be unique for this request-response
    /// protocol, it's not unique across all installed request-response protocols. That is,
    /// multiple request-response protocols can return the same `RequestId` and this must be
    /// handled by the calling code correctly if the `RequestId`s are stored somewhere.
    pub async fn send_request(
        &mut self,
        peer: PeerId,
        request: Vec<u8>,
        dial_options: DialOptions,
    ) -> crate::Result<RequestId> {
        tracing::trace!(target: LOG_TARGET, ?peer, "send request to peer");

        let request_id = self.next_request_id();
        self.command_tx
            .send(RequestResponseCommand::SendRequest {
                peer,
                request_id,
                request,
                dial_options,
            })
            .await
            .map(|_| request_id)
            .map_err(From::from)
    }

    /// Attempt to send request to peer and if the channel is clogged, return
    /// `Error::ChannelClogged`.
    ///
    /// While the returned `RequestId` is guaranteed to be unique for this request-response
    /// protocol, it's not unique across all installed request-response protocols. That is,
    /// multiple request-response protocols can return the same `RequestId` and this must be
    /// handled by the calling code correctly if the `RequestId`s are stored somewhere.
    pub fn try_send_request(
        &mut self,
        peer: PeerId,
        request: Vec<u8>,
        dial_options: DialOptions,
    ) -> crate::Result<RequestId> {
        tracing::trace!(target: LOG_TARGET, ?peer, "send request to peer");

        let request_id = self.next_request_id();
        self.command_tx
            .try_send(RequestResponseCommand::SendRequest {
                peer,
                request_id,
                request,
                dial_options,
            })
            .map(|_| request_id)
            .map_err(|_| Error::ChannelClogged)
    }

    /// Send request to remote peer with fallback.
    pub async fn send_request_with_fallback(
        &mut self,
        peer: PeerId,
        request: Vec<u8>,
        fallback: (ProtocolName, Vec<u8>),
        dial_options: DialOptions,
    ) -> crate::Result<RequestId> {
        tracing::trace!(
            target: LOG_TARGET,
            ?peer,
            fallback = %fallback.0,
            ?dial_options,
            "send request with fallback to peer",
        );

        let request_id = self.next_request_id();
        self.command_tx
            .send(RequestResponseCommand::SendRequestWithFallback {
                peer,
                request_id,
                fallback,
                request,
                dial_options,
            })
            .await
            .map(|_| request_id)
            .map_err(From::from)
    }

    /// Attempt to send request to peer with fallback and if the channel is clogged,
    /// return `Error::ChannelClogged`.
    pub fn try_send_request_with_fallback(
        &mut self,
        peer: PeerId,
        request: Vec<u8>,
        fallback: (ProtocolName, Vec<u8>),
        dial_options: DialOptions,
    ) -> crate::Result<RequestId> {
        tracing::trace!(
            target: LOG_TARGET,
            ?peer,
            fallback = %fallback.0,
            ?dial_options,
            "send request with fallback to peer",
        );

        let request_id = self.next_request_id();
        self.command_tx
            .try_send(RequestResponseCommand::SendRequestWithFallback {
                peer,
                request_id,
                fallback,
                request,
                dial_options,
            })
            .map(|_| request_id)
            .map_err(|_| Error::ChannelClogged)
    }

    /// Send response to remote peer.
    pub fn send_response(&mut self, request_id: RequestId, response: Vec<u8>) {
        match self.pending_responses.remove(&request_id) {
            None => {
                tracing::debug!(target: LOG_TARGET, ?request_id, "pending response doens't exist");
            }
            Some(response_tx) => {
                tracing::trace!(target: LOG_TARGET, ?request_id, "send response to peer");

                if let Err(_) = response_tx.send((response, None)) {
                    tracing::debug!(target: LOG_TARGET, ?request_id, "substream closed");
                }
            }
        }
    }

    /// Send response to remote peer with feedback.
    ///
    /// The feedback system is inherited from Polkadot SDK's `sc-network` and it's used to notify
    /// the sender of the response whether it was sent successfully or not. Once the response has
    /// been sent over the substream successfully, `()` will be sent over the feedback channel
    /// to the sender to notify them about it. If the substream has been closed or the substream
    /// failed while sending the response, the feedback channel will be dropped, notifying the
    /// sender that sending the response failed.
    pub fn send_response_with_feedback(
        &mut self,
        request_id: RequestId,
        response: Vec<u8>,
        feedback: channel::oneshot::Sender<()>,
    ) {
        match self.pending_responses.remove(&request_id) {
            None => {
                tracing::debug!(target: LOG_TARGET, ?request_id, "pending response doens't exist");
            }
            Some(response_tx) => {
                tracing::trace!(target: LOG_TARGET, ?request_id, "send response to peer");

                if let Err(_) = response_tx.send((response, Some(feedback))) {
                    tracing::debug!(target: LOG_TARGET, ?request_id, "substream closed");
                }
            }
        }
    }
}

impl futures::Stream for RequestResponseHandle {
    type Item = RequestResponseEvent;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match futures::ready!(self.event_rx.poll_recv(cx)) {
            None => Poll::Ready(None),
            Some(event) => match event {
                InnerRequestResponseEvent::RequestReceived {
                    peer,
                    fallback,
                    request_id,
                    request,
                    response_tx,
                } => {
                    self.pending_responses.insert(request_id, response_tx);
                    Poll::Ready(Some(RequestResponseEvent::RequestReceived {
                        peer,
                        fallback,
                        request_id,
                        request,
                    }))
                }
                event => Poll::Ready(Some(event.into())),
            },
        }
    }
}