Skip to content

Commit b187c14

Browse files
authored
chore: introduce rustfmt.toml
Pull-Request: #5695.
1 parent 930118e commit b187c14

File tree

356 files changed

+4305
-3310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

356 files changed

+4305
-3310
lines changed

core/src/connection.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,16 @@ pub enum ConnectedPoint {
7070
///
7171
/// - [`Endpoint::Dialer`] represents the default non-overriding option.
7272
///
73-
/// - [`Endpoint::Listener`] represents the overriding option.
74-
/// Realization depends on the transport protocol. E.g. in the case of
75-
/// TCP, both endpoints dial each other, resulting in a _simultaneous
76-
/// open_ TCP connection. On this new connection both endpoints assume
77-
/// to be the dialer of the connection. This is problematic during the
78-
/// connection upgrade process where an upgrade assumes one side to be
79-
/// the listener. With the help of this option, both peers can
80-
/// negotiate the roles (dialer and listener) for the new connection
81-
/// ahead of time, through some external channel, e.g. the DCUtR
82-
/// protocol, and thus have one peer dial the other and upgrade the
83-
/// connection as a dialer and one peer dial the other and upgrade the
84-
/// connection _as a listener_ overriding its role.
73+
/// - [`Endpoint::Listener`] represents the overriding option. Realization depends on the
74+
/// transport protocol. E.g. in the case of TCP, both endpoints dial each other,
75+
/// resulting in a _simultaneous open_ TCP connection. On this new connection both
76+
/// endpoints assume to be the dialer of the connection. This is problematic during the
77+
/// connection upgrade process where an upgrade assumes one side to be the listener. With
78+
/// the help of this option, both peers can negotiate the roles (dialer and listener) for
79+
/// the new connection ahead of time, through some external channel, e.g. the DCUtR
80+
/// protocol, and thus have one peer dial the other and upgrade the connection as a
81+
/// dialer and one peer dial the other and upgrade the connection _as a listener_
82+
/// overriding its role.
8583
role_override: Endpoint,
8684
/// Whether the port for the outgoing connection was reused from a listener
8785
/// or a new port was allocated. This is useful for address translation.

core/src/either.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::muxing::StreamMuxerEvent;
22-
use crate::transport::DialOpts;
23-
use crate::{
24-
muxing::StreamMuxer,
25-
transport::{ListenerId, Transport, TransportError, TransportEvent},
26-
Multiaddr,
21+
use std::{
22+
pin::Pin,
23+
task::{Context, Poll},
2724
};
25+
2826
use either::Either;
2927
use futures::prelude::*;
3028
use pin_project::pin_project;
31-
use std::{pin::Pin, task::Context, task::Poll};
29+
30+
use crate::{
31+
muxing::{StreamMuxer, StreamMuxerEvent},
32+
transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent},
33+
Multiaddr,
34+
};
3235

3336
impl<A, B> StreamMuxer for future::Either<A, B>
3437
where

core/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@
2222
//!
2323
//! The main concepts of libp2p-core are:
2424
//!
25-
//! - The [`Transport`] trait defines how to reach a remote node or listen for
26-
//! incoming remote connections. See the [`transport`] module.
27-
//! - The [`StreamMuxer`] trait is implemented on structs that hold a connection
28-
//! to a remote and can subdivide this connection into multiple substreams.
29-
//! See the [`muxing`] module.
30-
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits
31-
//! define how to upgrade each individual substream to use a protocol.
32-
//! See the `upgrade` module.
25+
//! - The [`Transport`] trait defines how to reach a remote node or listen for incoming remote
26+
//! connections. See the [`transport`] module.
27+
//! - The [`StreamMuxer`] trait is implemented on structs that hold a connection to a remote and can
28+
//! subdivide this connection into multiple substreams. See the [`muxing`] module.
29+
//! - The [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] traits define how to upgrade
30+
//! each individual substream to use a protocol. See the `upgrade` module.
3331
3432
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
3533

3634
mod proto {
3735
#![allow(unreachable_pub)]
3836
include!("generated/mod.rs");
3937
pub use self::{
40-
envelope_proto::*, peer_record_proto::mod_PeerRecord::*, peer_record_proto::PeerRecord,
38+
envelope_proto::*,
39+
peer_record_proto::{mod_PeerRecord::*, PeerRecord},
4140
};
4241
}
4342

core/src/muxing.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! has ownership of a connection, lets you open and close substreams.
2525
//!
2626
//! > **Note**: You normally don't need to use the methods of the `StreamMuxer` directly, as this
27-
//! > is managed by the library's internals.
27+
//! > is managed by the library's internals.
2828
//!
2929
//! Each substream of a connection is an isolated stream of data. All the substreams are muxed
3030
//! together so that the data read from or written to each substream doesn't influence the other
@@ -36,9 +36,9 @@
3636
//! require maintaining long-lived channels of communication.
3737
//!
3838
//! > **Example**: The Kademlia protocol opens a new substream for each request it wants to
39-
//! > perform. Multiple requests can be performed simultaneously by opening multiple
40-
//! > substreams, without having to worry about associating responses with the
41-
//! > right request.
39+
//! > perform. Multiple requests can be performed simultaneously by opening multiple
40+
//! > substreams, without having to worry about associating responses with the
41+
//! > right request.
4242
//!
4343
//! # Implementing a muxing protocol
4444
//!
@@ -50,21 +50,23 @@
5050
//! The upgrade process will take ownership of the connection, which makes it possible for the
5151
//! implementation of `StreamMuxer` to control everything that happens on the wire.
5252
53-
use futures::{task::Context, task::Poll, AsyncRead, AsyncWrite};
53+
use std::{future::Future, pin::Pin};
54+
55+
use futures::{
56+
task::{Context, Poll},
57+
AsyncRead, AsyncWrite,
58+
};
5459
use multiaddr::Multiaddr;
55-
use std::future::Future;
56-
use std::pin::Pin;
5760

58-
pub use self::boxed::StreamMuxerBox;
59-
pub use self::boxed::SubstreamBox;
61+
pub use self::boxed::{StreamMuxerBox, SubstreamBox};
6062

6163
mod boxed;
6264

6365
/// Provides multiplexing for a connection by allowing users to open substreams.
6466
///
65-
/// A substream created by a [`StreamMuxer`] is a type that implements [`AsyncRead`] and [`AsyncWrite`].
66-
/// The [`StreamMuxer`] itself is modelled closely after [`AsyncWrite`]. It features `poll`-style
67-
/// functions that allow the implementation to make progress on various tasks.
67+
/// A substream created by a [`StreamMuxer`] is a type that implements [`AsyncRead`] and
68+
/// [`AsyncWrite`]. The [`StreamMuxer`] itself is modelled closely after [`AsyncWrite`]. It features
69+
/// `poll`-style functions that allow the implementation to make progress on various tasks.
6870
pub trait StreamMuxer {
6971
/// Type of the object that represents the raw substream where data can be read and written.
7072
type Substream: AsyncRead + AsyncWrite;
@@ -90,13 +92,13 @@ pub trait StreamMuxer {
9092

9193
/// Poll to close this [`StreamMuxer`].
9294
///
93-
/// After this has returned `Poll::Ready(Ok(()))`, the muxer has become useless and may be safely
94-
/// dropped.
95+
/// After this has returned `Poll::Ready(Ok(()))`, the muxer has become useless and may be
96+
/// safely dropped.
9597
///
9698
/// > **Note**: You are encouraged to call this method and wait for it to return `Ready`, so
97-
/// > that the remote is properly informed of the shutdown. However, apart from
98-
/// > properly informing the remote, there is no difference between this and
99-
/// > immediately dropping the muxer.
99+
/// > that the remote is properly informed of the shutdown. However, apart from
100+
/// > properly informing the remote, there is no difference between this and
101+
/// > immediately dropping the muxer.
100102
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
101103

102104
/// Poll to allow the underlying connection to make progress.
@@ -120,7 +122,8 @@ pub enum StreamMuxerEvent {
120122

121123
/// Extension trait for [`StreamMuxer`].
122124
pub trait StreamMuxerExt: StreamMuxer + Sized {
123-
/// Convenience function for calling [`StreamMuxer::poll_inbound`] for [`StreamMuxer`]s that are `Unpin`.
125+
/// Convenience function for calling [`StreamMuxer::poll_inbound`]
126+
/// for [`StreamMuxer`]s that are `Unpin`.
124127
fn poll_inbound_unpin(
125128
&mut self,
126129
cx: &mut Context<'_>,
@@ -131,7 +134,8 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
131134
Pin::new(self).poll_inbound(cx)
132135
}
133136

134-
/// Convenience function for calling [`StreamMuxer::poll_outbound`] for [`StreamMuxer`]s that are `Unpin`.
137+
/// Convenience function for calling [`StreamMuxer::poll_outbound`]
138+
/// for [`StreamMuxer`]s that are `Unpin`.
135139
fn poll_outbound_unpin(
136140
&mut self,
137141
cx: &mut Context<'_>,
@@ -142,15 +146,17 @@ pub trait StreamMuxerExt: StreamMuxer + Sized {
142146
Pin::new(self).poll_outbound(cx)
143147
}
144148

145-
/// Convenience function for calling [`StreamMuxer::poll`] for [`StreamMuxer`]s that are `Unpin`.
149+
/// Convenience function for calling [`StreamMuxer::poll`]
150+
/// for [`StreamMuxer`]s that are `Unpin`.
146151
fn poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Result<StreamMuxerEvent, Self::Error>>
147152
where
148153
Self: Unpin,
149154
{
150155
Pin::new(self).poll(cx)
151156
}
152157

153-
/// Convenience function for calling [`StreamMuxer::poll_close`] for [`StreamMuxer`]s that are `Unpin`.
158+
/// Convenience function for calling [`StreamMuxer::poll_close`]
159+
/// for [`StreamMuxer`]s that are `Unpin`.
154160
fn poll_close_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>
155161
where
156162
Self: Unpin,

core/src/muxing/boxed.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1+
use std::{
2+
error::Error,
3+
fmt, io,
4+
io::{IoSlice, IoSliceMut},
5+
pin::Pin,
6+
task::{Context, Poll},
7+
};
8+
29
use futures::{AsyncRead, AsyncWrite};
310
use pin_project::pin_project;
4-
use std::error::Error;
5-
use std::fmt;
6-
use std::io;
7-
use std::io::{IoSlice, IoSliceMut};
8-
use std::pin::Pin;
9-
use std::task::{Context, Poll};
11+
12+
use crate::muxing::{StreamMuxer, StreamMuxerEvent};
1013

1114
/// Abstract `StreamMuxer`.
1215
pub struct StreamMuxerBox {
@@ -139,7 +142,8 @@ impl StreamMuxer for StreamMuxerBox {
139142
}
140143

141144
impl SubstreamBox {
142-
/// Construct a new [`SubstreamBox`] from something that implements [`AsyncRead`] and [`AsyncWrite`].
145+
/// Construct a new [`SubstreamBox`] from something
146+
/// that implements [`AsyncRead`] and [`AsyncWrite`].
143147
pub fn new<S: AsyncRead + AsyncWrite + Send + 'static>(stream: S) -> Self {
144148
Self(Box::pin(stream))
145149
}

core/src/peer_record.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use crate::signed_envelope::SignedEnvelope;
2-
use crate::{proto, signed_envelope, DecodeError, Multiaddr};
3-
use libp2p_identity::Keypair;
4-
use libp2p_identity::PeerId;
5-
use libp2p_identity::SigningError;
1+
use libp2p_identity::{Keypair, PeerId, SigningError};
62
use quick_protobuf::{BytesReader, Writer};
73
use web_time::SystemTime;
84

5+
use crate::{proto, signed_envelope, signed_envelope::SignedEnvelope, DecodeError, Multiaddr};
6+
97
const PAYLOAD_TYPE: &str = "/libp2p/routing-state-record";
108
const DOMAIN_SEP: &str = "libp2p-routing-state";
119

1210
/// Represents a peer routing record.
1311
///
14-
/// Peer records are designed to be distributable and carry a signature by being wrapped in a signed envelope.
15-
/// For more information see RFC0003 of the libp2p specifications: <https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md>
12+
/// Peer records are designed to be distributable and carry a signature by being wrapped in a signed
13+
/// envelope. For more information see RFC0003 of the libp2p specifications: <https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md>
1614
#[derive(Debug, PartialEq, Eq, Clone)]
1715
pub struct PeerRecord {
1816
peer_id: PeerId,
@@ -21,14 +19,16 @@ pub struct PeerRecord {
2119

2220
/// A signed envelope representing this [`PeerRecord`].
2321
///
24-
/// If this [`PeerRecord`] was constructed from a [`SignedEnvelope`], this is the original instance.
22+
/// If this [`PeerRecord`] was constructed from a [`SignedEnvelope`], this is the original
23+
/// instance.
2524
envelope: SignedEnvelope,
2625
}
2726

2827
impl PeerRecord {
2928
/// Attempt to re-construct a [`PeerRecord`] from a [`SignedEnvelope`].
3029
///
31-
/// If this function succeeds, the [`SignedEnvelope`] contained a peer record with a valid signature and can hence be considered authenticated.
30+
/// If this function succeeds, the [`SignedEnvelope`] contained a peer record with a valid
31+
/// signature and can hence be considered authenticated.
3232
pub fn from_signed_envelope(envelope: SignedEnvelope) -> Result<Self, FromEnvelopeError> {
3333
use quick_protobuf::MessageRead;
3434

@@ -60,7 +60,8 @@ impl PeerRecord {
6060

6161
/// Construct a new [`PeerRecord`] by authenticating the provided addresses with the given key.
6262
///
63-
/// This is the same key that is used for authenticating every libp2p connection of your application, i.e. what you use when setting up your [`crate::transport::Transport`].
63+
/// This is the same key that is used for authenticating every libp2p connection of your
64+
/// application, i.e. what you use when setting up your [`crate::transport::Transport`].
6465
pub fn new(key: &Keypair, addresses: Vec<Multiaddr>) -> Result<Self, SigningError> {
6566
use quick_protobuf::MessageWrite;
6667

core/src/signed_envelope.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::{proto, DecodeError};
2-
use libp2p_identity::SigningError;
3-
use libp2p_identity::{Keypair, PublicKey};
4-
use quick_protobuf::{BytesReader, Writer};
51
use std::fmt;
2+
3+
use libp2p_identity::{Keypair, PublicKey, SigningError};
4+
use quick_protobuf::{BytesReader, Writer};
65
use unsigned_varint::encode::usize_buffer;
76

8-
/// A signed envelope contains an arbitrary byte string payload, a signature of the payload, and the public key that can be used to verify the signature.
7+
use crate::{proto, DecodeError};
8+
9+
/// A signed envelope contains an arbitrary byte string payload, a signature of the payload, and the
10+
/// public key that can be used to verify the signature.
911
///
1012
/// For more details see libp2p RFC0002: <https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md>
1113
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -46,8 +48,9 @@ impl SignedEnvelope {
4648

4749
/// Extract the payload and signing key of this [`SignedEnvelope`].
4850
///
49-
/// You must provide the correct domain-separation string and expected payload type in order to get the payload.
50-
/// This guards against accidental mis-use of the payload where the signature was created for a different purpose or payload type.
51+
/// You must provide the correct domain-separation string and expected payload type in order to
52+
/// get the payload. This guards against accidental mis-use of the payload where the
53+
/// signature was created for a different purpose or payload type.
5154
///
5255
/// It is the caller's responsibility to check that the signing key is what
5356
/// is expected. For example, checking that the signing key is from a
@@ -156,7 +159,8 @@ pub enum DecodingError {
156159
/// Errors that occur whilst extracting the payload of a [`SignedEnvelope`].
157160
#[derive(Debug)]
158161
pub enum ReadPayloadError {
159-
/// The signature on the signed envelope does not verify with the provided domain separation string.
162+
/// The signature on the signed envelope does not verify
163+
/// with the provided domain separation string.
160164
InvalidSignature,
161165
/// The payload contained in the envelope is not of the expected type.
162166
UnexpectedPayloadType { expected: Vec<u8>, got: Vec<u8> },

core/src/transport.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
//! any desired protocols. The rest of the module defines combinators for
2626
//! modifying a transport through composition with other transports or protocol upgrades.
2727
28-
use futures::prelude::*;
29-
use multiaddr::Multiaddr;
3028
use std::{
3129
error::Error,
3230
fmt,
@@ -35,6 +33,9 @@ use std::{
3533
task::{Context, Poll},
3634
};
3735

36+
use futures::prelude::*;
37+
use multiaddr::Multiaddr;
38+
3839
pub mod and_then;
3940
pub mod choice;
4041
pub mod dummy;
@@ -48,14 +49,12 @@ pub mod upgrade;
4849
mod boxed;
4950
mod optional;
5051

52+
pub use self::{
53+
boxed::Boxed, choice::OrTransport, memory::MemoryTransport, optional::OptionalTransport,
54+
upgrade::Upgrade,
55+
};
5156
use crate::{ConnectedPoint, Endpoint};
5257

53-
pub use self::boxed::Boxed;
54-
pub use self::choice::OrTransport;
55-
pub use self::memory::MemoryTransport;
56-
pub use self::optional::OptionalTransport;
57-
pub use self::upgrade::Upgrade;
58-
5958
static NEXT_LISTENER_ID: AtomicUsize = AtomicUsize::new(1);
6059

6160
/// The port use policy for a new connection.
@@ -75,8 +74,9 @@ pub enum PortUse {
7574
pub struct DialOpts {
7675
/// The endpoint establishing a new connection.
7776
///
78-
/// When attempting a hole-punch, both parties simultaneously "dial" each other but one party has to be the "listener" on the final connection.
79-
/// This option specifies the role of this node in the final connection.
77+
/// When attempting a hole-punch, both parties simultaneously "dial" each other but one party
78+
/// has to be the "listener" on the final connection. This option specifies the role of
79+
/// this node in the final connection.
8080
pub role: Endpoint,
8181
/// The port use policy for a new connection.
8282
pub port_use: PortUse,
@@ -161,10 +161,10 @@ pub trait Transport {
161161

162162
/// Poll for [`TransportEvent`]s.
163163
///
164-
/// A [`TransportEvent::Incoming`] should be produced whenever a connection is received at the lowest
165-
/// level of the transport stack. The item must be a [`ListenerUpgrade`](Transport::ListenerUpgrade)
166-
/// future that resolves to an [`Output`](Transport::Output) value once all protocol upgrades have
167-
/// been applied.
164+
/// A [`TransportEvent::Incoming`] should be produced whenever a connection is received at the
165+
/// lowest level of the transport stack. The item must be a
166+
/// [`ListenerUpgrade`](Transport::ListenerUpgrade) future that resolves to an
167+
/// [`Output`](Transport::Output) value once all protocol upgrades have been applied.
168168
///
169169
/// Transports are expected to produce [`TransportEvent::Incoming`] events only for
170170
/// listen addresses which have previously been announced via

0 commit comments

Comments
 (0)