Skip to content

Commit

Permalink
WIP on (no branch): 83c6795 *: Prepare v0.48.0 (libp2p#2869)
Browse files Browse the repository at this point in the history
  • Loading branch information
binier committed Sep 20, 2022
1 parent 83c6795 commit a4e06bf
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -833,7 +833,7 @@ must not be skipped!
- Reworked the API of `Multiaddr`.
- Removed the `ToMultiaddr` trait in favour of `TryFrom`.
- Added `Swarm::ban_peer_id` and `Swarm::unban_peer_id`.
- The `TPeerId` generic parameter of `RawSwarm` is now `TConnInfo` and must now implement a `ConnectionInfo` trait.
- The `TPeerId` generic parameter of `RawSwarm` is now `TConnInfo` and must now implement a `Connection` trait.
- Reworked the `PingEvent`.
- Renamed `KeepAlive::Forever` to `Yes` and `KeepAlive::Now` to `No`.

Expand Down
5 changes: 2 additions & 3 deletions Cargo.toml
Expand Up @@ -63,6 +63,7 @@ uds = ["dep:libp2p-uds"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "rand/wasm-bindgen"]
wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
wasm-ext-webrtc = ["wasm-ext", "libp2p-wasm-ext?/webrtc"]
websocket = ["dep:libp2p-websocket"]
yamux = ["dep:libp2p-yamux"]
secp256k1 = ["libp2p-core/secp256k1"]
Expand Down Expand Up @@ -100,6 +101,7 @@ libp2p-swarm-derive = { version = "0.30.0", path = "swarm-derive" }
libp2p-uds = { version = "0.35.0", path = "transports/uds", optional = true }
libp2p-wasm-ext = { version = "0.36.0", path = "transports/wasm-ext", default-features = false, optional = true }
libp2p-yamux = { version = "0.40.0", path = "muxers/yamux", optional = true }
libp2p-gossipsub = { version = "0.41.0", path = "protocols/gossipsub", optional = true }
multiaddr = { version = "0.14.0" }
parking_lot = "0.12.0"
pin-project = "1.0.0"
Expand All @@ -113,9 +115,6 @@ libp2p-mdns = { version = "0.40.0", path = "protocols/mdns", optional = true, de
libp2p-tcp = { version = "0.36.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.38.0", path = "transports/websocket", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { version = "0.41.0", path = "protocols/gossipsub", optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
async-trait = "0.1"
Expand Down
4 changes: 2 additions & 2 deletions core/CHANGELOG.md
Expand Up @@ -291,9 +291,9 @@

# 0.24.0 [2020-11-09]

- Remove `ConnectionInfo` trait and replace it with `PeerId`
- Remove `Connection` trait and replace it with `PeerId`
everywhere. This was already effectively the case because
`ConnectionInfo` was implemented on `PeerId`.
`Connection` was implemented on `PeerId`.

# 0.23.1 [2020-10-20]

Expand Down
14 changes: 14 additions & 0 deletions core/src/connection.rs
Expand Up @@ -20,6 +20,20 @@

use crate::multiaddr::{Multiaddr, Protocol};

pub trait Connection {
/// Should be `Some(_)` if transport itself handles authentication.
fn remote_peer_id(&self) -> Option<crate::PeerId> {
None
}
}

impl<C> Connection for multistream_select::Negotiated<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<crate::PeerId> {
self.inner_completed_io().and_then(|io| io.remote_peer_id())
}
}

/// Connection identifier.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionId(usize);
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Expand Up @@ -68,7 +68,7 @@ pub mod signed_envelope;
pub mod transport;
pub mod upgrade;

pub use connection::{ConnectedPoint, Endpoint};
pub use connection::{ConnectedPoint, Connection, Endpoint};
pub use identity::PublicKey;
pub use multiaddr::Multiaddr;
pub use multihash;
Expand Down
9 changes: 9 additions & 0 deletions misc/multistream-select/src/negotiated.rs
Expand Up @@ -51,6 +51,15 @@ pub struct Negotiated<TInner> {
state: State<TInner>,
}

impl<TInner> Negotiated<TInner> {
pub fn inner_completed_io(&self) -> Option<&TInner> {
match &self.state {
State::Completed { io } => Some(io),
_ => None
}
}
}

/// A `Future` that waits on the completion of protocol negotiation.
#[derive(Debug)]
pub struct NegotiatedComplete<TInner> {
Expand Down
3 changes: 2 additions & 1 deletion protocols/gossipsub/Cargo.toml
Expand Up @@ -28,7 +28,8 @@ prost = "0.11"
hex_fmt = "0.3.0"
regex = "1.5.5"
serde = { version = "1", optional = true, features = ["derive"] }
wasm-timer = "0.2.5"
# wasm-timer = "0.2.5"
wasm-timer = { git = "https://github.com/fusetim/wasm-timer", branch = "tim-add-missing-methods" }
instant = "0.1.11"
# Metrics dependencies
prometheus-client = "0.18.0"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -68,7 +68,7 @@ pub use libp2p_dns as dns;
pub use libp2p_floodsub as floodsub;
#[cfg(feature = "gossipsub")]
#[cfg_attr(docsrs, doc(cfg(feature = "gossipsub")))]
#[cfg(not(target_os = "unknown"))]
// #[cfg(not(target_os = "unknown"))]
#[doc(inline)]
pub use libp2p_gossipsub as gossipsub;
#[cfg(feature = "identify")]
Expand Down
1 change: 1 addition & 0 deletions transports/pnet/Cargo.toml
Expand Up @@ -17,6 +17,7 @@ salsa20 = "0.9"
sha3 = "0.10"
rand = "0.7"
pin-project = "1.0.2"
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false }

[dev-dependencies]
quickcheck = "0.9.0"
9 changes: 9 additions & 0 deletions transports/pnet/src/crypt_writer.rs
Expand Up @@ -28,6 +28,8 @@ use pin_project::pin_project;
use salsa20::{cipher::StreamCipher, XSalsa20};
use std::{fmt, pin::Pin};

use libp2p_core::Connection;

/// A writer that encrypts and forwards to an inner writer
#[pin_project]
pub struct CryptWriter<W> {
Expand All @@ -37,6 +39,13 @@ pub struct CryptWriter<W> {
cipher: XSalsa20,
}

impl<C> Connection for CryptWriter<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<libp2p_core::PeerId> {
self.inner.remote_peer_id()
}
}

impl<W: AsyncWrite> CryptWriter<W> {
/// Creates a new `CryptWriter` with the specified buffer capacity.
pub fn with_capacity(capacity: usize, inner: W, cipher: XSalsa20) -> CryptWriter<W> {
Expand Down
9 changes: 9 additions & 0 deletions transports/pnet/src/lib.rs
Expand Up @@ -45,6 +45,8 @@ use std::{
task::{Context, Poll},
};

use libp2p_core::Connection;

const KEY_SIZE: usize = 32;
const NONCE_SIZE: usize = 24;
const WRITE_BUFFER_SIZE: usize = 1024;
Expand Down Expand Up @@ -234,6 +236,13 @@ pub struct PnetOutput<S> {
read_cipher: XSalsa20,
}

impl<C> Connection for PnetOutput<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<libp2p_core::PeerId> {
self.inner.remote_peer_id()
}
}

impl<S: AsyncRead + AsyncWrite> PnetOutput<S> {
fn new(inner: S, write_cipher: XSalsa20, read_cipher: XSalsa20) -> Self {
Self {
Expand Down
4 changes: 3 additions & 1 deletion transports/wasm-ext/Cargo.toml
Expand Up @@ -13,10 +13,12 @@ categories = ["network-programming", "asynchronous"]
[dependencies]
futures = "0.3.1"
js-sys = "0.3.50"
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false }
libp2p-core = { version = "0.36.0", path = "../../core", default-features = false, features = ["ecdsa"] }
parity-send-wrapper = "0.1.0"
wasm-bindgen = "0.2.42"
wasm-bindgen-futures = "0.4.4"
x509-parser = "0.14.0"

[features]
websocket = []
webrtc = []
68 changes: 68 additions & 0 deletions transports/wasm-ext/src/dtls.rs
@@ -0,0 +1,68 @@
use libp2p_core::{Connection, InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};

#[derive(Debug, Clone)]
pub struct NonDTLSConnectionError;

impl std::fmt::Display for NonDTLSConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Tried to authenticate using `DTLSAuthenticated` for non DTLS transport connection!")
}
}

impl std::error::Error for NonDTLSConnectionError {
}

#[derive(Clone)]
pub struct DTLSAuthenticated;


impl DTLSAuthenticated {
pub fn new() -> Self {
Self {}
}
}

impl UpgradeInfo for DTLSAuthenticated {
type Info = &'static str;
// type InfoIter = std::iter::Once<Self::Info>;
type InfoIter = std::iter::Empty<Self::Info>;

fn protocol_info(&self) -> Self::InfoIter {
std::iter::empty()
// std::iter::once("/dtls")
}
}

impl<C> InboundUpgrade<C> for DTLSAuthenticated
where
C: Connection,
{
type Output = (PeerId, C);
type Error = NonDTLSConnectionError;
type Future = std::future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
let peer_id_result = socket
.remote_peer_id()
.map(|id| (id, socket))
.ok_or(NonDTLSConnectionError);
std::future::ready(peer_id_result)
}
}

impl<C> OutboundUpgrade<C> for DTLSAuthenticated
where
C: Connection,
{
type Output = (PeerId, C);
type Error = NonDTLSConnectionError;
type Future = std::future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
let peer_id_result = socket
.remote_peer_id()
.map(|id| (id, socket))
.ok_or(NonDTLSConnectionError);
std::future::ready(peer_id_result)
}
}

0 comments on commit a4e06bf

Please sign in to comment.