xstack 0.2.17

libp2p stack rewritten from ground up for rust
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
use std::ops::Deref;
use std::{fmt::Debug, sync::Arc};

use super::SwitchBuilder;
use super::{builder::SwitchOptions, PROTOCOL_IPFS_ID, PROTOCOL_IPFS_PING};
use super::{mutable::MutableSwitch, PROTOCOL_IPFS_PUSH_ID};

use futures::{lock::Mutex, TryStreamExt};
use libp2p_identity::{PeerId, PublicKey};
use multiaddr::Multiaddr;
use multistream_select::listener_select_proto;

use rasi::{task::spawn_ok, timer::TimeoutExt};

use crate::{
    book::PeerInfo,
    transport::{P2pConn, ProtocolStream, TransportListener},
    Error, ProtocolListener, Result, XStackId,
};
use crate::{AutoNAT, ConnectTo};

/// `Switch` is the entry point of the libp2p network.
///
/// via `Switch` instance, you can:
/// - create a outbound stream to peer.
/// - accept a inbound stream from peer.
///
/// # Multiaddr hit
#[derive(Clone)]
pub struct Switch {
    // pub(super) inner: Arc<InnerSwitch>,
    pub(super) public_key: Arc<PublicKey>,
    pub(super) local_peer_id: Arc<PeerId>,
    pub(super) ops: Arc<SwitchOptions>,
    pub(super) mutable: Arc<Mutex<MutableSwitch>>,
}

impl Deref for Switch {
    type Target = SwitchOptions;

    fn deref(&self) -> &Self::Target {
        &self.ops
    }
}

impl Switch {
    /// Start a background task to accept inbound stream, and make a identity request to authenticate peer.
    async fn handshake(&self, conn: &mut P2pConn) -> Result<()> {
        self.ops.stream_dispatcher.handshake(conn.id()).await;

        let this = self.clone();

        let mut this_conn = conn.clone();

        spawn_ok(async move {
            if let Err(err) = this.incoming_stream_loop(&mut this_conn).await {
                log::error!(
                    "incoming stream loop stopped, peer={}, local={}, error={}",
                    this_conn.peer_addr(),
                    this_conn.local_addr(),
                    err
                );
                _ = this_conn.close();
            } else {
                log::info!(
                    "incoming stream loop stopped, peer={}, local={}",
                    this_conn.peer_addr(),
                    this_conn.local_addr()
                );
            }
        });

        // start "/ipfs/id/1.0.0" handshake.
        self.identity_request(conn)
            .timeout(self.ops.timeout)
            .await
            .ok_or(Error::Timeout)??;

        Ok(())
    }

    async fn incoming_stream_loop(&self, conn: &mut P2pConn) -> Result<()> {
        loop {
            let stream = conn.accept().await?;

            let this = self.clone();

            spawn_ok(async move {
                let id = stream.id().to_owned();

                if let Err(err) = this.handle_incoming_stream(stream).await {
                    log::trace!("dispatch stream, id={}, err={}", id, err);
                }
            })
        }
    }

    async fn handle_incoming_stream(&self, mut stream: ProtocolStream) -> Result<()> {
        log::info!(
            "accept new stream, peer={}, local={}, id={}",
            stream.peer_addr(),
            stream.local_addr(),
            stream.id()
        );

        let protos = Self::merge_protos(self.ops.stream_dispatcher.protos().await);

        let (protoco_id, _) = listener_select_proto(&mut stream, &protos)
            .timeout(self.ops.timeout)
            .await
            .ok_or(Error::Timeout)??;

        log::trace!(
            "protocol handshake, id={}, protocol={}, peer_id={}",
            stream.id(),
            protoco_id,
            stream.public_key().to_peer_id()
        );

        let this = self.clone();
        let protoco_id = protoco_id.clone();

        let peer_addr = stream.peer_addr().clone();
        let local_addr = stream.local_addr().clone();
        let id = stream.id().to_owned();

        if let Err(err) = this.dispatch_stream(protoco_id, stream).await {
            log::error!(
                "dispatch stream, id={}, peer={}, local={}, err={}",
                id,
                peer_addr,
                local_addr,
                err
            );
        } else {
            log::trace!(
                "dispatch stream ok, id={}, peer={}, local={}",
                id,
                peer_addr,
                local_addr
            );
        }

        Ok(())
    }

    async fn dispatch_stream(&self, protocol_id: String, stream: ProtocolStream) -> Result<()> {
        let conn_peer_id = stream.public_key().to_peer_id();

        match protocol_id.as_str() {
            PROTOCOL_IPFS_ID => self.identity_response(stream).await?,
            PROTOCOL_IPFS_PUSH_ID => {
                self.identity_push(&conn_peer_id, stream).await?;
            }
            PROTOCOL_IPFS_PING => self.ping_echo(stream).await?,
            _ => {
                self.ops
                    .stream_dispatcher
                    .dispatch(stream, protocol_id)
                    .await;
            }
        }

        Ok(())
    }

    /// Create a new transport layer socket that accepts peer's inbound connections.
    ///
    pub(crate) async fn transport_bind(&self, laddr: &Multiaddr) -> Result<()> {
        let transport = self
            .ops
            .get_transport_by_address(laddr)
            .ok_or(Error::UnspportMultiAddr(laddr.to_owned()))?;

        let listener = transport.bind(self, laddr).await?;

        let laddr = listener.local_addr()?;

        self.mutable.lock().await.transport_bind_to(laddr.clone());

        self.transport_bind_with(listener).await
    }

    async fn handle_incoming(&self, listener: TransportListener) -> Result<()> {
        let mut incoming = listener.into_incoming();

        while let Some(mut conn) = incoming.try_next().await? {
            log::trace!(
                "accept a new incoming connection, peer={}, local={}",
                conn.peer_addr(),
                conn.local_addr()
            );

            let this = self.clone();

            spawn_ok(async move {
                if let Err(err) = this.handshake(&mut conn).await {
                    log::error!(
                        "setup connection, peer={}, local={}, err={}",
                        conn.peer_addr(),
                        conn.local_addr(),
                        err
                    );
                    _ = conn.close();
                } else {
                    log::trace!(
                        "setup connection, peer={}, local={}",
                        conn.peer_addr(),
                        conn.local_addr()
                    );

                    this.connector.incoming(conn).await;
                }
            })
        }

        Ok(())
    }
}

impl Switch {
    /// Uses `agent_version` string to create a switch [`builder`](SwitchBuilder).
    pub fn new<A>(agent_version: A) -> SwitchBuilder
    where
        A: AsRef<str>,
    {
        SwitchBuilder::new(agent_version.as_ref().to_owned())
    }

    /// Open a new transport connection directly to peer via `raddr`.
    ///
    /// **Note!!, that this method does not involve connection reuse,
    /// so it is safe to call it in implementations such as [`Connector`]**
    ///
    /// [`Connector`]: crate::connector_syscall::DriverConnector
    pub async fn transport_connect(&self, raddr: &Multiaddr) -> Result<P2pConn> {
        let transport = self
            .ops
            .get_transport_by_address(raddr)
            .ok_or(Error::UnspportMultiAddr(raddr.to_owned()))?;

        let mut conn = transport
            .connect(self, raddr)
            .timeout(self.timeout)
            .await
            .ok_or(Error::Timeout)??;

        if let Err(err) = self.handshake(&mut conn).await {
            log::error!("{}, setup error: {}", raddr, err);
            _ = conn.close();
            Err(err)
        } else {
            log::trace!("{}, setup success", raddr);
            Ok(conn)
        }
    }

    /// Dynamically bind a transport listener to this switch.
    pub async fn transport_bind_with(&self, listener: TransportListener) -> Result<()> {
        let this = self.clone();

        let laddr = listener.local_addr()?;

        spawn_ok(async move {
            if let Err(err) = this.handle_incoming(listener).await {
                log::error!("listener({}) stop, err={}", laddr, err);
            } else {
                log::info!("listener({}) stop", laddr);
            }
        });

        Ok(())
    }

    /// Create a protocol layer server-side socket, that accept inbound [`ProtocolStream`].
    pub async fn bind<I>(&self, protos: I) -> Result<ProtocolListener>
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        let id = XStackId::default();

        let protos = protos
            .into_iter()
            .map(|item| item.as_ref().to_owned())
            .collect::<Vec<_>>();

        self.ops.stream_dispatcher.bind(id, &protos).await?;

        Ok(ProtocolListener::new(self.clone(), id))
    }

    /// Connect to peer and negotiate a protocol. the `protos` is the list of candidate protocols.
    pub async fn connect<'a, C, E, I>(
        &self,
        target: C,
        protos: I,
    ) -> Result<(ProtocolStream, I::Item)>
    where
        C: TryInto<ConnectTo<'a>, Error = E>,
        I: IntoIterator,
        I::Item: AsRef<str>,
        E: Debug,
    {
        let mut conn = match target
            .try_into()
            .map_err(|err| Error::Other(format!("{:?}", err)))?
        {
            ConnectTo::PeerIdRef(peer_id) => self.ops.connector.connect_to(self, peer_id).await?,
            ConnectTo::MultiaddrRef(raddr) => {
                self.ops.connector.connect(self, &[raddr.clone()]).await?
            }
            ConnectTo::PeerId(peer_id) => self.ops.connector.connect_to(self, &peer_id).await?,
            ConnectTo::Multiaddr(raddr) => {
                self.ops.connector.connect(self, &[raddr.clone()]).await?
            }
            ConnectTo::MultiaddrsRef(raddrs) => self.ops.connector.connect(self, &raddrs).await?,
            ConnectTo::Multiaddrs(raddrs) => self.ops.connector.connect(self, &raddrs).await?,
        };

        log::trace!("open stream, conn_id={}", conn.id());

        Ok(conn.connect(protos).await?)
    }
}

impl Switch {
    /// Remove [`PeerInfo`] from the [`PeerBook`](crate::book::PeerBook) of this switch.
    pub async fn remove_peer_info(&self, peer_id: &PeerId) -> Result<Option<PeerInfo>> {
        Ok(self.ops.peer_book.remove(peer_id).await?)
    }

    /// insert new [`PeerInfo`] into the [`PeerBook`](crate::PeerBook) of this `Switch`
    pub async fn insert_peer_info(&self, peer_info: PeerInfo) -> Result<Option<PeerInfo>> {
        Ok(self.ops.peer_book.insert(peer_info).await?)
    }

    /// Returns the [`PeerInfo`] of the [`peer_id`](PeerId).
    pub async fn lookup_peer_info(&self, peer_id: &PeerId) -> Result<Option<PeerInfo>> {
        Ok(self.ops.peer_book.get(peer_id).await?)
    }

    /// Random select peers by protoco_id.
    pub async fn choose_peers<P: AsRef<str>>(
        &self,
        protocol_id: P,
        limits: usize,
    ) -> Result<Vec<PeerId>> {
        Ok(self
            .ops
            .peer_book
            .choose_peers(protocol_id.as_ref(), limits)
            .await?)
    }

    /// Reverse lookup [`PeerId`] for the peer indicated by the listening address.
    pub async fn lookup_peer_id(&self, raddr: &Multiaddr) -> Result<Option<PeerId>> {
        Ok(self.ops.peer_book.listen_on(raddr).await?)
    }

    /// Get this switch's public key.
    pub fn local_public_key(&self) -> &PublicKey {
        &self.public_key
    }

    /// Get this switch's node id.
    pub fn local_id(&self) -> &PeerId {
        &self.local_peer_id
    }

    /// Returns the addresses list of this switch is bound to.
    pub async fn local_addrs(&self) -> Vec<Multiaddr> {
        self.mutable.lock().await.local_addrs()
    }

    /// Returns the addresses list of this switch observed by peers.
    pub async fn observed_addrs(&self) -> Vec<Multiaddr> {
        self.mutable.lock().await.observed_addrs()
    }

    ///  Insert observed addresses list.
    pub async fn set_observed_addrs(&self, addrs: Vec<Multiaddr>) {
        self.mutable.lock().await.set_observed_addrs(addrs)
    }

    /// Returns the addresses list of this switch is listen to.
    ///
    /// Unlike the [`local_addrs`](Self::local_addrs) function, this function may returns circuit-v2 addresses.
    pub async fn listen_addrs(&self) -> Vec<Multiaddr> {
        self.mutable.lock().await.listen_addrs()
    }

    /// Sets the [`circuit-v2`] listening addresses.
    ///
    /// [`circuit-v2/stop`]: https://github.com/libp2p/specs/blob/master/relay/circuit-v2.md#stop-protocol
    pub async fn set_nat_addrs(&self, addrs: Vec<Multiaddr>) {
        self.mutable.lock().await.set_net_addrs(addrs);
    }

    /// remove [`circuit-v2`] listening addresses from this `Switch`.
    pub async fn remove_nat_addrs(&self, addrs: &[Multiaddr]) {
        self.mutable.lock().await.remove_net_addrs(addrs);
    }

    /// Returns the [*autonat protocol*](https://github.com/libp2p/specs/tree/master/autonat) [`state`](AutoNAT).
    pub async fn nat(&self) -> AutoNAT {
        self.mutable.lock().await.auto_nat()
    }

    /// Set the the [*autonat protocol*](https://github.com/libp2p/specs/tree/master/autonat) [`state`](AutoNAT).
    pub async fn set_nat(&self, state: AutoNAT) {
        self.mutable.lock().await.set_nat(state);
        self.event_mediator
            .raise(crate::Event::Network(state))
            .await;
    }

    /// Register self into global context.
    #[cfg(feature = "global_register")]
    #[cfg_attr(docsrs, doc(cfg(feature = "global_register")))]
    pub fn into_global(self) {
        use crate::register_switch;

        register_switch(self)
    }
}