Skip to content

Commit

Permalink
Merge master into stable-futures (#1271)
Browse files Browse the repository at this point in the history
* Configurable multistream-select protocol. Add V1Lazy variant. (#1245)

Make the multistream-select protocol (version) configurable
on transport upgrades as well as for individual substreams.

Add a "lazy" variant of multistream-select 1.0 that delays
sending of negotiation protocol frames as much as possible
but is only safe to use under additional assumptions that
go beyond what is required by the multistream-select v1
specification.

* Improve the code readability of the chat example (#1253)

* Add bridged chats (#1252)

* Try fix CI (#1261)

* Print Rust version on CI

* Don't print where not appropriate

* Change caching strategy

* Remove win32 build

* Remove win32 from list

* Update libsecp256k1 dep to 0.3.0 (#1258)

* Update libsecp256k1 dep to 0.3.0

* Sign now cannot fail

* Upgrade url and percent-encoding deps to 2.1.0 (#1267)

* Upgrade percent-encoding dep to 2.1.0

* Upgrade url dep to 2.1.0

* Fix more conflicts

* Revert CIPHERS set to null (#1273)
  • Loading branch information
tomaka authored Oct 10, 2019
1 parent 9921a33 commit abe2f2a
Show file tree
Hide file tree
Showing 37 changed files with 435 additions and 332 deletions.
33 changes: 10 additions & 23 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ workflows:
jobs:
- test
- test-wasm
- test-win32
- integration-test

jobs:
Expand Down Expand Up @@ -53,7 +52,10 @@ jobs:
- restore_cache:
keys:
- test-wasm-cache-{{ epoch }}
- test-wasm-cache
- run:
name: Print Rust version
command: |
rustc --version
- run:
name: Build for wasm32
# TODO: also run tests but with --no-run; important to detect linking errors
Expand All @@ -68,37 +70,22 @@ jobs:
- /usr/local/cargo
- /root/.cache/sccache

test-win32:
docker:
- image: tomaka/rust-mingw-docker
steps:
- checkout
- restore_cache:
key: test-win32-cache
- run:
name: Build for Windows 64 bits
command: cargo check --target x86_64-pc-windows-gnu
- run:
name: Build for Windows 32 bits
command: cargo check --target i686-pc-windows-gnu
- save_cache:
key: test-win32-cache
paths:
- "~/.cargo"
- "./target"

integration-test:
docker:
- image: rust
- image: ipfs/go-ipfs
steps:
- checkout
- restore_cache:
key: integration-test-cache
key: integration-test-cache-{{ epoch }}
- run:
name: Print Rust version
command: |
rustc --version
- run:
command: cargo run --example ipfs-kad
- save_cache:
key: integration-test-cache
key: integration-test-cache-{{ epoch }}
paths:
- "~/.cargo"
- "./target"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
<a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
<a href="https://riot.permaweb.io/#/room/#libp2p:permaweb.io"><img src="https://img.shields.io/badge/matrix-%23libp2p%3Apermaweb.io-blue.svg?style=flat-square" /> </a>
<a href="https://discord.gg/66KBrm2"><img src="https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square" /></a>
[![dependency status](https://deps.rs/repo/github/libp2p/rust-libp2p/status.svg?style=flat-square)](https://deps.rs/repo/github/libp2p/rust-libp2p)

This repository is the central place for Rust development of the [libp2p](https://libp2p.io) spec.
Expand Down
3 changes: 1 addition & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protobuf = "2.3"
quick-error = "1.2"
rand = "0.6"
rw-stream-sink = { version = "0.1.1", path = "../misc/rw-stream-sink" }
libsecp256k1 = { version = "0.2.2", optional = true }
libsecp256k1 = { version = "0.3.0", optional = true }
sha2 = "0.8.0"
smallvec = "0.6"
wasm-timer = "0.1"
Expand Down Expand Up @@ -56,4 +56,3 @@ tokio-mock-task = "0.1"
[features]
default = ["secp256k1"]
secp256k1 = ["libsecp256k1"]
async-await = []
5 changes: 1 addition & 4 deletions core/src/identity/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl SecretKey {
pub fn sign_hash(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
let m = Message::parse_slice(msg)
.map_err(|_| SigningError::new("failed to parse secp256k1 digest"))?;
secp256k1::sign(&m, &self.0)
.map(|s| s.0.serialize_der().as_ref().into())
.map_err(|_| SigningError::new("failed to create secp256k1 signature"))
Ok(secp256k1::sign(&m, &self.0).0.serialize_der().as_ref().into())
}
}

Expand Down Expand Up @@ -190,4 +188,3 @@ mod tests {
assert_eq!(sk_bytes, [0; 32]);
}
}

4 changes: 2 additions & 2 deletions core/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ pub trait Transport {
}

/// Begins a series of protocol upgrades via an [`upgrade::Builder`].
fn upgrade(self) -> upgrade::Builder<Self>
fn upgrade(self, version: upgrade::Version) -> upgrade::Builder<Self>
where
Self: Sized,
Self::Error: 'static
{
upgrade::Builder::new(self)
upgrade::Builder::new(self, version)
}
}

Expand Down
21 changes: 13 additions & 8 deletions core/src/transport/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

//! Configuration of transport protocol upgrades.

pub use crate::upgrade::Version;

use crate::{
ConnectedPoint,
ConnectionInfo,
Expand Down Expand Up @@ -67,7 +69,8 @@ use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
///
/// [`Network`]: crate::nodes::Network
pub struct Builder<T> {
inner: T
inner: T,
version: upgrade::Version,
}

impl<T> Builder<T>
Expand All @@ -76,8 +79,8 @@ where
T::Error: 'static,
{
/// Creates a `Builder` over the given (base) `Transport`.
pub fn new(transport: T) -> Builder<T> {
Builder { inner: transport }
pub fn new(inner: T, version: upgrade::Version) -> Builder<T> {
Builder { inner, version }
}

/// Upgrades the transport to perform authentication of the remote.
Expand Down Expand Up @@ -107,11 +110,12 @@ where
U: OutboundUpgrade<C, Output = (I, D), Error = E> + Clone,
E: Error + 'static,
{
let version = self.version;
Builder::new(self.inner.and_then(move |conn, endpoint| {
Authenticate {
inner: upgrade::apply(conn, upgrade, endpoint)
inner: upgrade::apply(conn, upgrade, endpoint, version)
}
}))
}), version)
}

/// Applies an arbitrary upgrade on an authenticated, non-multiplexed
Expand All @@ -138,7 +142,7 @@ where
U: OutboundUpgrade<C, Output = D, Error = E> + Clone,
E: Error + 'static,
{
Builder::new(Upgrade::new(self.inner, upgrade))
Builder::new(Upgrade::new(self.inner, upgrade), self.version)
}

/// Upgrades the transport with a (sub)stream multiplexer.
Expand Down Expand Up @@ -166,8 +170,9 @@ where
U: OutboundUpgrade<C, Output = M, Error = E> + Clone,
E: Error + 'static,
{
let version = self.version;
self.inner.and_then(move |(i, c), endpoint| {
let upgrade = upgrade::apply(c, upgrade, endpoint);
let upgrade = upgrade::apply(c, upgrade, endpoint, version);
Multiplex { info: Some(i), upgrade }
})
}
Expand Down Expand Up @@ -357,7 +362,7 @@ where
Err(err) => return Poll::Ready(Err(err)),
};
let u = up.take().expect("DialUpgradeFuture is constructed with Either::Left(Some).");
future::Either::Right((Some(i), apply_outbound(c, u)))
future::Either::Right((Some(i), apply_outbound(c, u, upgrade::Version::V1)))
}
future::Either::Right((ref mut i, ref mut up)) => {
let d = match ready!(Future::poll(Pin::new(up), cx).map_err(TransportUpgradeError::Upgrade)) {
Expand Down
36 changes: 8 additions & 28 deletions core/src/upgrade/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
// DEALINGS IN THE SOFTWARE.

use crate::ConnectedPoint;
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError};
use crate::upgrade::{ProtocolName, NegotiatedComplete};
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError, ProtocolName};
use futures::{future::Either, prelude::*, compat::Compat, compat::Compat01As03, compat::Future01CompatExt};
use log::debug;
use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture};
use std::{iter, mem, pin::Pin, task::Context, task::Poll};

pub use multistream_select::Version;

/// Applies an upgrade to the inbound and outbound direction of a connection or substream.
pub fn apply<C, U>(conn: C, up: U, cp: ConnectedPoint)
pub fn apply<C, U>(conn: C, up: U, cp: ConnectedPoint, v: Version)
-> Either<InboundUpgradeApply<C, U>, OutboundUpgradeApply<C, U>>
where
C: AsyncRead + AsyncWrite + Unpin,
Expand All @@ -36,7 +37,7 @@ where
if cp.is_listener() {
Either::Left(apply_inbound(conn, up))
} else {
Either::Right(apply_outbound(conn, up))
Either::Right(apply_outbound(conn, up, v))
}
}

Expand All @@ -54,13 +55,13 @@ where
}

/// Tries to perform an upgrade on an outbound connection or substream.
pub fn apply_outbound<C, U>(conn: C, up: U) -> OutboundUpgradeApply<C, U>
pub fn apply_outbound<C, U>(conn: C, up: U, v: Version) -> OutboundUpgradeApply<C, U>
where
C: AsyncRead + AsyncWrite + Unpin,
U: OutboundUpgrade<C>
{
let iter = up.protocol_info().into_iter().map(NameWrap as fn(_) -> NameWrap<_>);
let future = multistream_select::dialer_select_proto(Compat::new(conn), iter).compat();
let future = multistream_select::dialer_select_proto(Compat::new(conn), iter, v).compat();
OutboundUpgradeApply {
inner: OutboundUpgradeApplyState::Init { future, upgrade: up }
}
Expand Down Expand Up @@ -161,11 +162,6 @@ where
future: Compat01As03<DialerSelectFuture<Compat<C>, NameWrapIter<<U::InfoIter as IntoIterator>::IntoIter>>>,
upgrade: U
},
AwaitNegotiated {
io: Compat01As03<NegotiatedComplete<Compat<C>>>,
upgrade: U,
protocol: U::Info
},
Upgrade {
future: U::Future
},
Expand Down Expand Up @@ -198,24 +194,8 @@ where
return Poll::Pending
}
};
self.inner = OutboundUpgradeApplyState::AwaitNegotiated {
io: Compat01As03::new(connection.complete()),
protocol: info.0,
upgrade
};
}
OutboundUpgradeApplyState::AwaitNegotiated { mut io, protocol, upgrade } => {
let io = match Future::poll(Pin::new(&mut io), cx)? {
Poll::Pending => {
self.inner = OutboundUpgradeApplyState::AwaitNegotiated {
io, protocol, upgrade
};
return Poll::Pending
}
Poll::Ready(io) => io
};
self.inner = OutboundUpgradeApplyState::Upgrade {
future: upgrade.upgrade_outbound(Compat01As03::new(io), protocol)
future: upgrade.upgrade_outbound(Compat01As03::new(connection), info.0)
};
}
OutboundUpgradeApplyState::Upgrade { mut future } => {
Expand Down
2 changes: 1 addition & 1 deletion core/src/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod transfer;
use futures::future::Future;

pub use crate::Negotiated;
pub use multistream_select::{NegotiatedComplete, NegotiationError, ProtocolError};
pub use multistream_select::{Version, NegotiatedComplete, NegotiationError, ProtocolError};
pub use self::{
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
denied::DeniedUpgrade,
Expand Down
10 changes: 5 additions & 5 deletions core/tests/network_dial_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn deny_incoming_connec() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new());
Network::new(transport, local_public_key.into())
Expand All @@ -105,7 +105,7 @@ fn deny_incoming_connec() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new());
Network::new(transport, local_public_key.into())
Expand Down Expand Up @@ -170,7 +170,7 @@ fn dial_self() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
.and_then(|(peer, mplex), _| {
Expand Down Expand Up @@ -249,7 +249,7 @@ fn dial_self_by_id() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new());
Network::new(transport, local_public_key.into())
Expand All @@ -267,7 +267,7 @@ fn multiple_addresses_err() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new());
Network::new(transport, local_public_key.into())
Expand Down
4 changes: 2 additions & 2 deletions core/tests/network_simult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn raw_swarm_simultaneous_connect() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1Lazy)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
.and_then(|(peer, mplex), _| {
Expand All @@ -125,7 +125,7 @@ fn raw_swarm_simultaneous_connect() {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
.upgrade()
.upgrade(upgrade::Version::V1Lazy)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
.and_then(|(peer, mplex), _| {
Expand Down
6 changes: 3 additions & 3 deletions core/tests/transport_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod util;
use futures::prelude::*;
use libp2p_core::identity;
use libp2p_core::transport::{Transport, MemoryTransport, ListenerEvent};
use libp2p_core::upgrade::{UpgradeInfo, Negotiated, InboundUpgrade, OutboundUpgrade};
use libp2p_core::upgrade::{self, UpgradeInfo, Negotiated, InboundUpgrade, OutboundUpgrade};
use libp2p_mplex::MplexConfig;
use libp2p_secio::SecioConfig;
use multiaddr::Multiaddr;
Expand Down Expand Up @@ -76,7 +76,7 @@ fn upgrade_pipeline() {
let listener_keys = identity::Keypair::generate_ed25519();
let listener_id = listener_keys.public().into_peer_id();
let listener_transport = MemoryTransport::default()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(SecioConfig::new(listener_keys))
.apply(HelloUpgrade {})
.apply(HelloUpgrade {})
Expand All @@ -91,7 +91,7 @@ fn upgrade_pipeline() {
let dialer_keys = identity::Keypair::generate_ed25519();
let dialer_id = dialer_keys.public().into_peer_id();
let dialer_transport = MemoryTransport::default()
.upgrade()
.upgrade(upgrade::Version::V1)
.authenticate(SecioConfig::new(dialer_keys))
.apply(HelloUpgrade {})
.apply(HelloUpgrade {})
Expand Down
Loading

0 comments on commit abe2f2a

Please sign in to comment.