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
//  Copyright 2020, 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,
    time::{Duration, Instant},
};

use futures::{future, stream::FuturesUnordered, Stream};
use log::*;
use tokio::{
    sync::{broadcast, broadcast::error::RecvError, mpsc, oneshot},
    time,
};

use super::{
    connection_pool::PeerConnectionState,
    error::ConnectivityError,
    manager::ConnectivityStatus,
    ConnectivitySelection,
};
use crate::{
    connection_manager::ConnectionManagerError,
    peer_manager::{NodeId, Peer},
    Minimized,
    NodeIdentity,
    PeerConnection,
};

const LOG_TARGET: &str = "comms::connectivity::requester";

/// Connectivity event broadcast receiver.
pub type ConnectivityEventRx = broadcast::Receiver<ConnectivityEvent>;
/// Connectivity event broadcast sender.
pub type ConnectivityEventTx = broadcast::Sender<ConnectivityEvent>;

/// Node connectivity events emitted by the ConnectivityManager.
#[derive(Debug, Clone)]
pub enum ConnectivityEvent {
    PeerDisconnected(NodeId, Minimized),
    PeerConnected(Box<PeerConnection>),
    PeerConnectFailed(NodeId),
    PeerBanned(NodeId),
    ConnectivityStateInitialized,
    ConnectivityStateOnline(usize),
    ConnectivityStateDegraded(usize),
    ConnectivityStateOffline,
}

impl fmt::Display for ConnectivityEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        #[allow(clippy::enum_glob_use)]
        use ConnectivityEvent::*;
        match self {
            PeerDisconnected(node_id, minimized) => write!(f, "PeerDisconnected({node_id}, {minimized:?})"),
            PeerConnected(node_id) => write!(f, "PeerConnected({node_id})"),
            PeerConnectFailed(node_id) => write!(f, "PeerConnectFailed({node_id})"),
            PeerBanned(node_id) => write!(f, "PeerBanned({node_id})"),
            ConnectivityStateInitialized => write!(f, "ConnectivityStateInitialized"),
            ConnectivityStateOnline(n) => write!(f, "ConnectivityStateOnline({n})"),
            ConnectivityStateDegraded(n) => write!(f, "ConnectivityStateDegraded({n})"),
            ConnectivityStateOffline => write!(f, "ConnectivityStateOffline"),
        }
    }
}

/// Request types for the ConnectivityManager actor.
#[derive(Debug)]
pub enum ConnectivityRequest {
    WaitStarted(oneshot::Sender<()>),
    DialPeer {
        node_id: NodeId,
        reply_tx: Option<oneshot::Sender<Result<PeerConnection, ConnectionManagerError>>>,
    },
    GetConnectivityStatus(oneshot::Sender<ConnectivityStatus>),
    SelectConnections(
        ConnectivitySelection,
        oneshot::Sender<Result<Vec<PeerConnection>, ConnectivityError>>,
    ),
    GetConnection(NodeId, oneshot::Sender<Option<PeerConnection>>),
    GetAllConnectionStates(oneshot::Sender<Vec<PeerConnectionState>>),
    GetMinimizeConnectionsThreshold(oneshot::Sender<Option<usize>>),
    GetActiveConnections(oneshot::Sender<Vec<PeerConnection>>),
    BanPeer(NodeId, Duration, String),
    AddPeerToAllowList(NodeId),
    RemovePeerFromAllowList(NodeId),
    GetAllowList(oneshot::Sender<Vec<NodeId>>),
    GetSeeds(oneshot::Sender<Vec<Peer>>),
    GetPeerStats(NodeId, oneshot::Sender<Option<Peer>>),
    GetNodeIdentity(oneshot::Sender<NodeIdentity>),
}

/// Handle to make requests and read events from the ConnectivityManager actor.
#[derive(Debug, Clone)]
pub struct ConnectivityRequester {
    sender: mpsc::Sender<ConnectivityRequest>,
    event_tx: ConnectivityEventTx,
}

impl ConnectivityRequester {
    pub(crate) fn new(sender: mpsc::Sender<ConnectivityRequest>, event_tx: ConnectivityEventTx) -> Self {
        Self { sender, event_tx }
    }

    /// Returns a subscription to [ConnectivityEvent]s.
    ///
    /// [ConnectivityEvent](self::ConnectivityEvent)
    pub fn get_event_subscription(&self) -> ConnectivityEventRx {
        self.event_tx.subscribe()
    }

    pub(crate) fn get_event_publisher(&self) -> ConnectivityEventTx {
        self.event_tx.clone()
    }

    /// Dial a single peer
    pub async fn dial_peer(&self, peer: NodeId) -> Result<PeerConnection, ConnectivityError> {
        let mut num_cancels = 0;
        loop {
            let (reply_tx, reply_rx) = oneshot::channel();
            self.sender
                .send(ConnectivityRequest::DialPeer {
                    node_id: peer.clone(),
                    reply_tx: Some(reply_tx),
                })
                .await
                .map_err(|_| ConnectivityError::ActorDisconnected)?;

            match reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)? {
                Ok(c) => return Ok(c),
                Err(err @ ConnectionManagerError::DialCancelled) => {
                    num_cancels += 1;
                    // Due to simultaneous dialing, it's possible for the dial to be cancelled. However, typically if
                    // dial is called again right after, the resolved connection will be returned.
                    if num_cancels == 1 {
                        continue;
                    }
                    return Err(err.into());
                },
                Err(err) => return Err(err.into()),
            }
        }
    }

    /// Dial many peers, returning a Stream that emits the dial Result as each dial completes.
    #[allow(clippy::let_with_type_underscore)]
    pub async fn dial_many_peers<I: IntoIterator<Item = NodeId>>(
        &self,
        peers: I,
    ) -> impl Stream<Item = Result<PeerConnection, ConnectivityError>> + '_ {
        peers
            .into_iter()
            .map(|peer| async move { self.dial_peer(peer).await })
            .collect::<FuturesUnordered<_>>()
    }

    /// Send a request to dial many peers without waiting for the response.
    pub async fn request_many_dials<I: IntoIterator<Item = NodeId>>(&self, peers: I) -> Result<(), ConnectivityError> {
        future::join_all(peers.into_iter().map(|peer| {
            self.sender.send(ConnectivityRequest::DialPeer {
                node_id: peer,
                reply_tx: None,
            })
        }))
        .await
        .into_iter()
        .try_for_each(|result| result.map_err(|_| ConnectivityError::ActorDisconnected))
    }

    /// Queries the ConnectivityManager and returns the matching [PeerConnection](crate::PeerConnection)s.
    pub async fn select_connections(
        &mut self,
        selection: ConnectivitySelection,
    ) -> Result<Vec<PeerConnection>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::SelectConnections(selection, reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)?
    }

    /// Get an active connection to the given node id if one exists. This will return None if the peer is not connected.
    pub async fn get_connection(&mut self, node_id: NodeId) -> Result<Option<PeerConnection>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetConnection(node_id, reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Get the peer information from the peer, will return none if the peer is not found
    pub async fn get_peer_info(&self, node_id: NodeId) -> Result<Option<Peer>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetPeerStats(node_id, reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Get the current [ConnectivityStatus](self::ConnectivityStatus).
    pub async fn get_connectivity_status(&mut self) -> Result<ConnectivityStatus, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetConnectivityStatus(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Get the full connection state that the connectivity actor.
    pub async fn get_all_connection_states(&mut self) -> Result<Vec<PeerConnectionState>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetAllConnectionStates(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Get the optional minimize connections setting.
    pub async fn get_minimize_connections_threshold(&mut self) -> Result<Option<usize>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetMinimizeConnectionsThreshold(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Get all currently connection [PeerConnection](crate::PeerConnection]s.
    pub async fn get_active_connections(&mut self) -> Result<Vec<PeerConnection>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetActiveConnections(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Ban peer for the given Duration. The ban `reason` is persisted in the peer database for reference.
    pub async fn ban_peer_until<T: Into<String>>(
        &mut self,
        node_id: NodeId,
        duration: Duration,
        reason: T,
    ) -> Result<(), ConnectivityError> {
        self.sender
            .send(ConnectivityRequest::BanPeer(node_id, duration, reason.into()))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        Ok(())
    }

    /// Ban the peer indefinitely.
    pub async fn ban_peer(&mut self, node_id: NodeId, reason: String) -> Result<(), ConnectivityError> {
        self.ban_peer_until(node_id, Duration::from_secs(u64::MAX), reason)
            .await
    }

    /// Adds a peer to an allow list, preventing it from being banned.
    pub async fn add_peer_to_allow_list(&mut self, node_id: NodeId) -> Result<(), ConnectivityError> {
        self.sender
            .send(ConnectivityRequest::AddPeerToAllowList(node_id))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        Ok(())
    }

    /// Retrieve self's allow list.
    pub async fn get_allow_list(&mut self) -> Result<Vec<NodeId>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetAllowList(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Retrieve the list of seeds.
    pub async fn get_seeds(&mut self) -> Result<Vec<Peer>, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetSeeds(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Retrieve self's node identity.
    pub async fn get_node_identity(&mut self) -> Result<NodeIdentity, ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::GetNodeIdentity(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Removes a peer from an allow list that prevents it from being banned.
    pub async fn remove_peer_from_allow_list(&mut self, node_id: NodeId) -> Result<(), ConnectivityError> {
        self.sender
            .send(ConnectivityRequest::RemovePeerFromAllowList(node_id))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        Ok(())
    }

    /// Returns a Future that resolves when the connectivity actor has started.
    pub async fn wait_started(&mut self) -> Result<(), ConnectivityError> {
        let (reply_tx, reply_rx) = oneshot::channel();
        self.sender
            .send(ConnectivityRequest::WaitStarted(reply_tx))
            .await
            .map_err(|_| ConnectivityError::ActorDisconnected)?;
        reply_rx.await.map_err(|_| ConnectivityError::ActorResponseCancelled)
    }

    /// Waits for the node to get at least one connection.
    /// This is useful for testing and is not typically be needed in application code.
    pub async fn wait_for_connectivity(&mut self, timeout: Duration) -> Result<(), ConnectivityError> {
        let mut connectivity_events = self.get_event_subscription();
        let status = self.get_connectivity_status().await?;
        if status.is_online() {
            return Ok(());
        }
        let start = Instant::now();
        let mut remaining = timeout;

        let mut last_known_peer_count = status.num_connected_nodes();
        loop {
            debug!(target: LOG_TARGET, "Waiting for connectivity event");
            let recv_result = time::timeout(remaining, connectivity_events.recv())
                .await
                .map_err(|_| ConnectivityError::OnlineWaitTimeout(last_known_peer_count))?;

            remaining = timeout
                .checked_sub(start.elapsed())
                .ok_or(ConnectivityError::OnlineWaitTimeout(last_known_peer_count))?;

            match recv_result {
                Ok(event) => match event {
                    ConnectivityEvent::ConnectivityStateOnline(_) => {
                        info!(target: LOG_TARGET, "Connectivity is ONLINE.");
                        break Ok(());
                    },
                    ConnectivityEvent::ConnectivityStateDegraded(n) => {
                        warn!(target: LOG_TARGET, "Connectivity is DEGRADED ({n} peer(s))");
                        last_known_peer_count = n;
                    },
                    ConnectivityEvent::ConnectivityStateOffline => {
                        warn!(
                            target: LOG_TARGET,
                            "Connectivity is OFFLINE. Waiting for connections..."
                        );
                        last_known_peer_count = 0;
                    },
                    event => {
                        debug!(
                            target: LOG_TARGET,
                            "Received event while waiting for connectivity: {event:?}"
                        );
                    },
                },
                Err(RecvError::Closed) => {
                    error!(
                        target: LOG_TARGET,
                        "Connectivity event stream closed unexpectedly. System may be shutting down."
                    );
                    break Err(ConnectivityError::ConnectivityEventStreamClosed);
                },
                Err(RecvError::Lagged(n)) => {
                    warn!(target: LOG_TARGET, "Lagging behind on {n} connectivity event(s)");
                    // We lagged, so could have missed the state change. Check it explicitly.
                    let status = self.get_connectivity_status().await?;
                    if status.is_online() {
                        break Ok(());
                    }
                },
            }
        }
    }
}