tari_comms 5.2.1

A peer-to-peer messaging system
Documentation
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// Copyright 2019, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
    fmt,
    future::Future,
    sync::{
        atomic::{AtomicBool, AtomicUsize, Ordering},
        Arc,
    },
    time::{Duration, Instant},
};

use futures::{future::BoxFuture, stream::FuturesUnordered};
use log::*;
use multiaddr::Multiaddr;
use tari_shutdown::oneshot_trigger::OneshotTrigger;
use tokio::{
    sync::{mpsc, oneshot},
    time,
};
use tokio_stream::StreamExt;
use tracing::{span, Instrument, Level};

use super::{direction::ConnectionDirection, error::PeerConnectionError, manager::ConnectionManagerEvent};
#[cfg(feature = "rpc")]
use crate::protocol::rpc::{
    pool::RpcClientPool,
    pool::RpcPoolClient,
    NamedProtocolService,
    RpcClient,
    RpcClientBuilder,
    RpcError,
    RPC_MAX_FRAME_SIZE,
};
use crate::{
    framing,
    framing::CanonicalFraming,
    multiplexing::{Control, IncomingSubstreams, Substream, Yamux, YamuxControlError},
    peer_manager::{NodeId, PeerFeatures},
    protocol::{ProtocolId, ProtocolNegotiation},
    utils::atomic_ref_counter::AtomicRefCounter,
    Minimized,
};

const LOG_TARGET: &str = "comms::connection_manager::peer_connection";

const PROTOCOL_NEGOTIATION_TIMEOUT: Duration = Duration::from_secs(10);

static ID_COUNTER: AtomicUsize = AtomicUsize::new(0);

pub fn create(
    connection: Yamux,
    peer_addr: Multiaddr,
    peer_node_id: NodeId,
    peer_features: PeerFeatures,
    direction: ConnectionDirection,
    event_notifier: mpsc::Sender<ConnectionManagerEvent>,
    our_supported_protocols: Arc<Vec<ProtocolId>>,
    their_supported_protocols: Vec<ProtocolId>,
) -> PeerConnection {
    trace!(
        target: LOG_TARGET,
        "(Peer={}) Socket successfully upgraded to multiplexed socket",
        peer_node_id.short_str()
    );
    // All requests are request/response, so a channel size of 1 is all that is needed
    let (peer_tx, peer_rx) = mpsc::channel(1);
    let id = ID_COUNTER.fetch_add(1, Ordering::SeqCst); // Monotonic
    let substream_counter = connection.substream_counter();
    let peer_conn = PeerConnection::new(
        id,
        peer_tx,
        peer_node_id.clone(),
        peer_features,
        peer_addr,
        direction,
        substream_counter,
    );
    let peer_actor = PeerConnectionActor::new(
        id,
        peer_node_id,
        direction,
        connection,
        peer_rx,
        event_notifier,
        our_supported_protocols,
        their_supported_protocols,
    );
    tokio::spawn(peer_actor.run());

    peer_conn
}

/// Request types for the PeerConnection actor.
#[derive(Debug)]
pub enum PeerConnectionRequest {
    /// Open a new substream and negotiate the given protocol
    OpenSubstream {
        protocol_id: ProtocolId,
        reply_tx: oneshot::Sender<Result<NegotiatedSubstream<Substream>, PeerConnectionError>>,
    },
    /// Disconnect all substreams and close the transport connection
    Disconnect(
        bool,
        oneshot::Sender<Result<(), PeerConnectionError>>,
        Minimized,
        String,
    ),
}

/// ID type for peer connections
pub type ConnectionId = usize;

/// Request handle for an active peer connection
#[derive(Debug, Clone)]
pub struct PeerConnection {
    id: ConnectionId,
    peer_node_id: NodeId,
    peer_features: PeerFeatures,
    request_tx: mpsc::Sender<PeerConnectionRequest>,
    address: Arc<Multiaddr>,
    direction: ConnectionDirection,
    started_at: Instant,
    substream_counter: AtomicRefCounter,
    handle_counter: Arc<()>,
    drop_notifier: OneshotTrigger<NodeId>,
    force_disconnect_rpc_clients_when_clone_drops: Arc<AtomicBool>,
    rpc_session_states: Vec<Arc<AtomicBool>>,
}

impl PeerConnection {
    pub(crate) fn new(
        id: ConnectionId,
        request_tx: mpsc::Sender<PeerConnectionRequest>,
        peer_node_id: NodeId,
        peer_features: PeerFeatures,
        address: Multiaddr,
        direction: ConnectionDirection,
        substream_counter: AtomicRefCounter,
    ) -> Self {
        Self {
            id,
            request_tx,
            peer_node_id,
            peer_features,
            address: Arc::new(address),
            direction,
            started_at: Instant::now(),
            substream_counter,
            handle_counter: Arc::new(()),
            drop_notifier: OneshotTrigger::<NodeId>::new(),
            force_disconnect_rpc_clients_when_clone_drops: Arc::new(Default::default()),
            rpc_session_states: Vec::new(),
        }
    }

    pub fn peer_node_id(&self) -> &NodeId {
        &self.peer_node_id
    }

    pub fn peer_features(&self) -> PeerFeatures {
        self.peer_features
    }

    pub fn direction(&self) -> ConnectionDirection {
        self.direction
    }

    pub fn known_address(&self) -> Option<&Multiaddr> {
        if self.direction.is_outbound() {
            Some(self.address())
        } else {
            None
        }
    }

    pub fn address(&self) -> &Multiaddr {
        &self.address
    }

    pub fn id(&self) -> ConnectionId {
        self.id
    }

    pub fn is_connected(&self) -> bool {
        !self.request_tx.is_closed()
    }

    /// Returns a owned future that resolves on disconnection
    pub fn on_disconnect(&self) -> impl Future<Output = ()> + 'static {
        let request_tx = self.request_tx.clone();
        async move { request_tx.closed().await }
    }

    pub fn age(&self) -> Duration {
        self.started_at.elapsed()
    }

    pub fn substream_count(&self) -> usize {
        self.substream_counter.get()
    }

    pub fn handle_count(&self) -> usize {
        Arc::strong_count(&self.handle_counter)
    }

    pub async fn open_substream(
        &mut self,
        protocol_id: &ProtocolId,
    ) -> Result<NegotiatedSubstream<Substream>, PeerConnectionError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        let _unused = self
            .request_tx
            .send(PeerConnectionRequest::OpenSubstream {
                protocol_id: protocol_id.clone(),
                reply_tx,
            })
            .await
            .inspect_err(|e| {
                info!(
                    target: LOG_TARGET,
                    "Failed to send OpenSubstream request for protocol `{}` to peer `{}`: {}",
                    String::from_utf8_lossy(protocol_id),
                    self.peer_node_id,
                    e
                );
            });
        reply_rx
            .await
            .map_err(|_| PeerConnectionError::InternalReplyCancelled)?
    }

    pub async fn open_framed_substream(
        &mut self,
        protocol_id: &ProtocolId,
        max_frame_size: usize,
    ) -> Result<CanonicalFraming<Substream>, PeerConnectionError> {
        let substream = self.open_substream(protocol_id).await?;
        Ok(framing::canonical(substream.stream, max_frame_size))
    }

    #[cfg(feature = "rpc")]
    pub async fn connect_rpc<T>(&mut self) -> Result<T, RpcError>
    where T: From<RpcClient> + NamedProtocolService {
        self.connect_rpc_using_builder(Default::default()).await
    }

    #[cfg(feature = "rpc")]
    pub async fn connect_rpc_using_builder<T>(&mut self, builder: RpcClientBuilder<T>) -> Result<T, RpcError>
    where T: From<RpcClient> + NamedProtocolService {
        let protocol = ProtocolId::from_static(T::PROTOCOL_NAME);
        debug!(
            target: LOG_TARGET,
            "Attempting to establish RPC protocol `{}` to peer `{}`",
            String::from_utf8_lossy(&protocol),
            self.peer_node_id
        );
        let framed = self.open_framed_substream(&protocol, RPC_MAX_FRAME_SIZE).await?;
        let rpc_session_state = Arc::new(AtomicBool::new(true));

        let rpc_client = builder
            .with_protocol_id(protocol)
            .with_node_id(self.peer_node_id.clone())
            .with_terminate_signal(self.drop_notifier.to_signal())
            .with_session_state(rpc_session_state.clone())
            .connect(framed)
            .await?;
        self.rpc_session_states.push(rpc_session_state.clone());

        Ok(rpc_client)
    }

    /// Creates a new RpcClientPool that can be shared between tasks. The client pool will lazily establish up to
    /// `max_sessions` sessions and provides client session that is least used.
    #[cfg(feature = "rpc")]
    pub fn create_rpc_client_pool<T>(
        &self,
        max_sessions: usize,
        client_config: RpcClientBuilder<T>,
    ) -> RpcClientPool<T>
    where
        T: RpcPoolClient + From<RpcClient> + NamedProtocolService + Clone,
    {
        RpcClientPool::new(self.clone(), max_sessions, client_config)
    }

    fn rpc_session_count(&self) -> usize {
        self.rpc_session_states
            .iter()
            .filter(|s| s.load(Ordering::Relaxed))
            .count()
    }

    /// Immediately disconnects the peer connection. This can only fail if the peer connection worker
    /// is shut down (and the peer is already disconnected)
    pub async fn disconnect(&mut self, minimized: Minimized, requester: &str) -> Result<(), PeerConnectionError> {
        trace!(
            target: LOG_TARGET,
            "Hard disconnect - requester: '{}', peer: `{}`, RPC clients: {}, substreams {}",
            requester,
            self.peer_node_id,
            self.rpc_session_count(),
            self.substream_count()
        );
        let (reply_tx, reply_rx) = oneshot::channel();
        self.request_tx
            .send(PeerConnectionRequest::Disconnect(
                false,
                reply_tx,
                minimized,
                requester.to_string(),
            ))
            .await
            .inspect_err(|e| {
                info!(
                    target: LOG_TARGET,
                    "Failed to send Disconnect request to peer `{}`: {}",
                    self.peer_node_id,
                    e
                );
            })?;
        reply_rx
            .await
            .map_err(|_| PeerConnectionError::InternalReplyCancelled)?
    }

    /// Request to disconnect the peer connection if unused by other services.
    pub async fn disconnect_if_unused(
        &mut self,
        minimized: Minimized,
        expected_rpc: usize,
        expected_substreams: usize,
        requester: &str,
    ) -> Result<(), PeerConnectionError> {
        let number_of_rpc_clients = self.rpc_session_count();
        let substream_count = self.substream_count();
        if number_of_rpc_clients > expected_rpc || substream_count > expected_substreams {
            trace!(
                target: LOG_TARGET,
                "Soft disconnect - requester: '{}', peer: `{}`, RPC clients: {}, substreams {}, NOT disconnecting",
                requester,
                self.peer_node_id,
                number_of_rpc_clients,
                self.substream_count()
            );
            Ok(())
        } else {
            self.disconnect(minimized, requester).await
        }
    }

    pub(crate) async fn disconnect_silent(
        &mut self,
        minimized: Minimized,
        requester: &str,
    ) -> Result<(), PeerConnectionError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.request_tx
            .send(PeerConnectionRequest::Disconnect(
                true,
                reply_tx,
                minimized,
                requester.to_string(),
            ))
            .await
            .inspect_err(|e| {
                info!(
                    target: LOG_TARGET,
                    "Failed to send Disconnect request to peer `{}`: {}",
                    self.peer_node_id,
                    e
                );
            })?;
        reply_rx
            .await
            .map_err(|_| PeerConnectionError::InternalReplyCancelled)?
    }

    /// Forcefully disconnect all RPC clients when any clone is dropped - if not set (the default behaviour) all RPC
    /// clients will be disconnected when the last instance is dropped. i.e. when `self.handle_counter == 1`
    pub fn set_force_disconnect_rpc_clients_when_clone_drops(&mut self) {
        self.force_disconnect_rpc_clients_when_clone_drops
            .store(true, Ordering::Relaxed);
    }
}

impl Drop for PeerConnection {
    fn drop(&mut self) {
        if self.handle_count() <= 1 ||
            self.force_disconnect_rpc_clients_when_clone_drops
                .load(Ordering::Relaxed)
        {
            let number_of_rpc_clients = self.rpc_session_count();
            if number_of_rpc_clients > 0 {
                self.drop_notifier.broadcast(self.peer_node_id.clone());
                trace!(
                    target: LOG_TARGET,
                    "PeerConnection `{}` drop called, open sub-streams: {}, notified {} RPC clients to drop connection",
                    self.peer_node_id.clone(), self.substream_count(), number_of_rpc_clients,
                );
            } else {
                trace!(
                    target: LOG_TARGET,
                    "PeerConnection `{}` drop called, open sub-streams: {}, RPC clients: {}",
                    self.peer_node_id, self.substream_count(), number_of_rpc_clients
                );
            }
        }
    }
}

impl fmt::Display for PeerConnection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(
            f,
            "Id: {}, Node ID: {}, Direction: {}, Peer Address: {}, Age: {:.0?}, #Substreams: {}, #RPC sessions: {}, \
             #Refs: {}",
            self.id,
            self.peer_node_id.short_str(),
            self.direction,
            self.address,
            self.age(),
            self.substream_count(),
            self.rpc_session_count(),
            self.handle_count()
        )
    }
}

impl PartialEq for PeerConnection {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

/// Actor for an active connection to a peer.
struct PeerConnectionActor {
    id: ConnectionId,
    peer_node_id: NodeId,
    request_rx: mpsc::Receiver<PeerConnectionRequest>,
    direction: ConnectionDirection,
    incoming_substreams: IncomingSubstreams,
    control: Control,
    event_notifier: mpsc::Sender<ConnectionManagerEvent>,
    our_supported_protocols: Arc<Vec<ProtocolId>>,
    inbound_protocol_negotiations:
        FuturesUnordered<BoxFuture<'static, Result<(ProtocolId, Substream), PeerConnectionError>>>,
    their_supported_protocols: Vec<ProtocolId>,
}

impl PeerConnectionActor {
    fn new(
        id: ConnectionId,
        peer_node_id: NodeId,
        direction: ConnectionDirection,
        connection: Yamux,
        request_rx: mpsc::Receiver<PeerConnectionRequest>,
        event_notifier: mpsc::Sender<ConnectionManagerEvent>,
        our_supported_protocols: Arc<Vec<ProtocolId>>,
        their_supported_protocols: Vec<ProtocolId>,
    ) -> Self {
        Self {
            id,
            peer_node_id,
            direction,
            control: connection.get_yamux_control(),
            incoming_substreams: connection.into_incoming(),
            request_rx,
            event_notifier,
            our_supported_protocols,
            inbound_protocol_negotiations: FuturesUnordered::new(),
            their_supported_protocols,
        }
    }

    pub async fn run(mut self) {
        loop {
            tokio::select! {
                maybe_request = self.request_rx.recv() => {
                    match maybe_request {
                        Some(request) => self.handle_request(request).await,
                        None => {
                            debug!(target: LOG_TARGET, "[{self}] All peer connection handles dropped closing the connection");
                            break;
                        }
                    }
                },

                maybe_substream = self.incoming_substreams.next() => {
                    match maybe_substream {
                        Some(substream) => self.handle_incoming_substream(substream).await,
                        None => {
                            debug!(target: LOG_TARGET, "[{}] Peer '{}' closed the connection", self, self.peer_node_id.short_str());
                            break;
                        },
                    }
                },

                Some(result) = self.inbound_protocol_negotiations.next() => {
                    self.handle_inbound_protocol_negotiation_result(result).await;
                }
            }
        }

        if let Err(err) = self.disconnect(false, Minimized::No, "PeerConnectionActor exit").await {
            warn!(
                target: LOG_TARGET,
                "[{}] Failed to politely close connection to peer '{}' because '{}'",
                self,
                self.peer_node_id.short_str(),
                err
            );
        }
    }

    async fn handle_request(&mut self, request: PeerConnectionRequest) {
        use PeerConnectionRequest::{Disconnect, OpenSubstream};
        match request {
            OpenSubstream { protocol_id, reply_tx } => {
                let tracing_id = tracing::Span::current().id();
                let span = span!(Level::TRACE, "handle_request");
                span.follows_from(tracing_id);
                let result = self.open_negotiated_protocol_stream(protocol_id).instrument(span).await;
                log_if_error_fmt!(
                    target: LOG_TARGET,
                    reply_tx.send(result),
                    "Reply oneshot closed when sending reply",
                );
            },
            Disconnect(silent, reply_tx, minimized, requester) => {
                debug!(
                    target: LOG_TARGET,
                    "[{}] Disconnect{}requested for {} connection to peer '{}', requester: '{}'",
                    self,
                    if silent { " (silent) " } else { " " },
                    self.direction,
                    self.peer_node_id.short_str(),
                    requester,
                );
                let _result = reply_tx.send(self.disconnect(silent, minimized, &requester).await);
            },
        }
    }

    async fn handle_incoming_substream(&mut self, mut stream: Substream) {
        let our_supported_protocols = self.our_supported_protocols.clone();
        self.inbound_protocol_negotiations.push(Box::pin(async move {
            let mut protocol_negotiation = ProtocolNegotiation::new(&mut stream);

            let selected_protocol = time::timeout(
                PROTOCOL_NEGOTIATION_TIMEOUT,
                protocol_negotiation.negotiate_protocol_inbound(&our_supported_protocols),
            )
            .await
            .map_err(|_| PeerConnectionError::ProtocolNegotiationTimeout)??;
            Ok((selected_protocol, stream))
        }));
    }

    async fn handle_inbound_protocol_negotiation_result(
        &mut self,
        result: Result<(ProtocolId, Substream), PeerConnectionError>,
    ) {
        match result {
            Ok((selected_protocol, stream)) => {
                self.notify_event(ConnectionManagerEvent::NewInboundSubstream(
                    self.peer_node_id.clone(),
                    selected_protocol,
                    stream,
                ))
                .await;
            },
            Err(PeerConnectionError::ProtocolError(err)) if err.is_ban_offence() => {
                error!(
                    target: LOG_TARGET,
                    "[{}] PEER VIOLATION: Incoming substream for peer '{}' failed to open because '{}'",
                    self,
                    self.peer_node_id.short_str(),
                    err
                );

                self.notify_event(ConnectionManagerEvent::PeerViolation {
                    peer_node_id: self.peer_node_id.clone(),
                    details: err.to_string(),
                })
                .await;
            },
            Err(err) => {
                error!(
                    target: LOG_TARGET,
                    "[{}] Incoming substream for peer '{}' failed to open because '{error}'",
                    self,
                    self.peer_node_id.short_str(),
                    error = err
                );
            },
        }
    }

    async fn open_negotiated_protocol_stream(
        &mut self,
        protocol: ProtocolId,
    ) -> Result<NegotiatedSubstream<Substream>, PeerConnectionError> {
        debug!(
            target: LOG_TARGET,
            "[{}] Negotiating protocol '{}' on new substream for peer '{}'",
            self,
            String::from_utf8_lossy(&protocol),
            self.peer_node_id.short_str()
        );
        let mut stream = self.control.open_stream().await?;

        let mut negotiation = ProtocolNegotiation::new(&mut stream);

        let selected_protocol = if self.their_supported_protocols.contains(&protocol) {
            let fut = negotiation.negotiate_protocol_outbound_optimistic(&protocol);
            time::timeout(PROTOCOL_NEGOTIATION_TIMEOUT, fut).await??
        } else {
            let selected_protocols = [protocol];
            let fut = negotiation.negotiate_protocol_outbound(&selected_protocols);
            time::timeout(PROTOCOL_NEGOTIATION_TIMEOUT, fut).await??
        };

        Ok(NegotiatedSubstream::new(selected_protocol, stream))
    }

    async fn notify_event(&mut self, event: ConnectionManagerEvent) {
        let _result = self.event_notifier.send(event).await;
    }

    /// Disconnect this peer connection.
    ///
    /// # Arguments
    ///
    /// silent - true to suppress the PeerDisconnected event, false to publish the event
    async fn disconnect(
        &mut self,
        silent: bool,
        minimized: Minimized,
        requester: &str,
    ) -> Result<(), PeerConnectionError> {
        self.request_rx.close();

        // Only emit closed event once
        if let Err(e) = self.control.close().await {
            match e {
                YamuxControlError::ConnectionClosed => {
                    trace!(
                        target: LOG_TARGET,
                        "On disconnect: (Peer = {}) Connection already closed ({})",
                        self.peer_node_id.short_str(),
                        e
                    );
                },
                e => trace!(target: LOG_TARGET, "On disconnect: ({e})"),
            }
        }

        if !silent {
            self.notify_event(ConnectionManagerEvent::PeerDisconnected(
                self.id,
                self.peer_node_id.clone(),
                minimized,
            ))
            .await;
        }
        trace!(
            target: LOG_TARGET,
            "(Peer = {}) Connection closed, requester: '{}'",
            self.peer_node_id.short_str(), requester
        );

        Ok(())
    }
}

impl fmt::Display for PeerConnectionActor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "PeerConnection(id={}, peer_node_id={}, direction={})",
            self.id,
            self.peer_node_id.short_str(),
            self.direction,
        )
    }
}

/// Contains the substream and the ProtocolId that was successfully negotiated.
pub struct NegotiatedSubstream<TSubstream> {
    pub protocol: ProtocolId,
    pub stream: TSubstream,
}

impl<TSubstream> NegotiatedSubstream<TSubstream> {
    pub fn new(protocol: ProtocolId, stream: TSubstream) -> Self {
        Self { protocol, stream }
    }
}

impl<TSubstream> fmt::Debug for NegotiatedSubstream<TSubstream> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NegotiatedSubstream")
            .field("protocol", &format!("{:?}", self.protocol))
            .field("stream", &"...".to_string())
            .finish()
    }
}