Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dp/switch-listene…
Browse files Browse the repository at this point in the history
…rs-to-vecdeque

* upstream/master:
  Inject event by value in ProtocolsHandler (libp2p#605)
  Add a PeriodicPingHandler and a PingListenHandler (libp2p#574)
  Fix stack overflow when printing a SubstreamRef (libp2p#599)
  Add a peer id generator (libp2p#583)
  eg. -> e.g.; ie. -> i.e. via repren (libp2p#592)
  • Loading branch information
dvdplm committed Nov 6, 2018
2 parents 271d832 + 1b4dada commit 486af92
Show file tree
Hide file tree
Showing 23 changed files with 1,081 additions and 441 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ members = [
"misc/multiaddr",
"misc/multihash",
"misc/multistream-select",
"misc/peer-id-generator",
"misc/rw-stream-sink",
"transports/dns",
"protocols/floodsub",
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//!
//! The main trait that this crate provides is `Transport`, which provides the `dial` and
//! `listen_on` methods and can be used to dial or listen on a multiaddress. The `swarm` crate
//! itself does not provide any concrete (ie. non-dummy, non-adapter) implementation of this trait.
//! itself does not provide any concrete (i.e. non-dummy, non-adapter) implementation of this trait.
//! It is implemented on structs that are provided by external crates, such as `TcpConfig` from
//! `tcp-transport`, `UdpConfig`, or `WebsocketConfig` (note: as of the writing of this
//! documentation, the last two structs don't exist yet).
Expand Down Expand Up @@ -132,7 +132,7 @@
//! extern crate tokio;
//!
//! use futures::{Future, Stream};
//! use libp2p_ping::{Ping, PingOutput};
//! use libp2p_ping::protocol::{Ping, PingOutput};
//! use libp2p_core::Transport;
//! use tokio::runtime::current_thread::Runtime;
//!
Expand Down
3 changes: 2 additions & 1 deletion core/src/muxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ impl<P> fmt::Debug for SubstreamRef<P>
where
P: Deref,
P::Target: StreamMuxer,
<P::Target as StreamMuxer>::Substream: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Substream({:?})", self)
write!(f, "Substream({:?})", self.substream)
}
}

Expand Down
38 changes: 21 additions & 17 deletions core/src/nodes/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ enum StreamState {
}

/// Event that can happen on the `NodeStream`.
#[derive(Debug)]
pub enum NodeEvent<TMuxer, TUserData>
where
TMuxer: muxing::StreamMuxer,
Expand Down Expand Up @@ -346,32 +345,37 @@ where
}
}

// TODO:
/*impl<TTrans> fmt::Debug for NodeEvent<TTrans>
where TTrans: Transport,
<TTrans::Listener as Stream>::Error: fmt::Debug,
impl<TMuxer, TUserData> fmt::Debug for NodeEvent<TMuxer, TUserData>
where
TMuxer: muxing::StreamMuxer,
TMuxer::Substream: fmt::Debug,
TUserData: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
NodeEvent::Incoming { ref listen_addr, .. } => {
f.debug_struct("NodeEvent::Incoming")
.field("listen_addr", listen_addr)
NodeEvent::InboundSubstream { substream } => {
f.debug_struct("NodeEvent::OutboundClosed")
.field("substream", substream)
.finish()
},
NodeEvent::Closed { ref listen_addr, .. } => {
f.debug_struct("NodeEvent::Closed")
.field("listen_addr", listen_addr)
NodeEvent::OutboundSubstream { user_data, substream } => {
f.debug_struct("NodeEvent::OutboundSubstream")
.field("user_data", user_data)
.field("substream", substream)
.finish()
},
NodeEvent::Error { ref listen_addr, ref error, .. } => {
f.debug_struct("NodeEvent::Error")
.field("listen_addr", listen_addr)
.field("error", error)
NodeEvent::OutboundClosed { user_data } => {
f.debug_struct("NodeEvent::OutboundClosed")
.field("user_data", user_data)
.finish()
},
NodeEvent::InboundClosed => {
f.debug_struct("NodeEvent::InboundClosed")
.finish()
},
}
}
}*/
}

#[cfg(test)]
mod node_stream {
Expand Down
12 changes: 6 additions & 6 deletions core/src/nodes/protocols_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub trait ProtocolsHandler {
);

/// Injects an event coming from the outside in the handler.
fn inject_event(&mut self, event: &Self::InEvent);
fn inject_event(&mut self, event: Self::InEvent);

/// Indicates to the handler that upgrading a substream to the given protocol has failed.
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: io::Error);
Expand Down Expand Up @@ -299,7 +299,7 @@ where
}

#[inline]
fn inject_event(&mut self, _: &Self::InEvent) {}
fn inject_event(&mut self, _: Self::InEvent) {}

#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: io::Error) {}
Expand Down Expand Up @@ -337,7 +337,7 @@ pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler, TNewIn, TMap>
where
TProtoHandler: ProtocolsHandler,
TMap: Fn(&TNewIn) -> Option<&TProtoHandler::InEvent>,
TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
{
type InEvent = TNewIn;
type OutEvent = TProtoHandler::OutEvent;
Expand All @@ -360,7 +360,7 @@ where
}

#[inline]
fn inject_event(&mut self, event: &TNewIn) {
fn inject_event(&mut self, event: TNewIn) {
if let Some(event) = (self.map)(event) {
self.inner.inject_event(event);
}
Expand Down Expand Up @@ -424,7 +424,7 @@ where
}

#[inline]
fn inject_event(&mut self, event: &Self::InEvent) {
fn inject_event(&mut self, event: Self::InEvent) {
self.inner.inject_event(event)
}

Expand Down Expand Up @@ -608,7 +608,7 @@ where

#[inline]
fn inject_event(&mut self, event: Self::InEvent) {
self.handler.inject_event(&event);
self.handler.inject_event(event);
}

#[inline]
Expand Down
2 changes: 2 additions & 0 deletions core/src/tests/dummy_muxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ use muxing::{StreamMuxer, Shutdown};
use futures::prelude::*;

/// Substream type
#[derive(Debug)]
pub struct DummySubstream {}

/// OutboundSubstream type
#[derive(Debug)]
pub struct DummyOutboundSubstream {}

/// Control the muxer state by setting the "connection" state as to set up a mock
Expand Down
4 changes: 2 additions & 2 deletions core/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub use self::upgrade::UpgradedNode;
/// A transport is an object that can be used to produce connections by listening or dialing a
/// peer.
///
/// This trait is implemented on concrete transports (eg. TCP, UDP, etc.), but also on wrappers
/// This trait is implemented on concrete transports (e.g. TCP, UDP, etc.), but also on wrappers
/// around them.
///
/// > **Note**: The methods of this trait use `self` and not `&self` or `&mut self`. In other
Expand All @@ -74,7 +74,7 @@ pub trait Transport {
type Listener: Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = IoError>;

/// After a connection has been received, we may need to do some asynchronous pre-processing
/// on it (eg. an intermediary protocol negotiation). While this pre-processing takes place, we
/// on it (e.g. an intermediary protocol negotiation). While this pre-processing takes place, we
/// want to be able to continue polling on the listener.
type ListenerUpgrade: Future<Item = Self::Output, Error = IoError>;

Expand Down
2 changes: 1 addition & 1 deletion core/src/upgrade/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait ConnectionUpgrade<C> {

/// This method is called after protocol negotiation has been performed.
///
/// Because performing the upgrade may not be instantaneous (eg. it may require a handshake),
/// Because performing the upgrade may not be instantaneous (e.g. it may require a handshake),
/// this function returns a future instead of the direct output.
fn upgrade(self, socket: C, id: Self::UpgradeIdentifier, ty: Endpoint) -> Self::Future;
}
2 changes: 1 addition & 1 deletion misc/multistream-select/src/length_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
/// # Panic
///
/// Will panic if called while there is data inside the buffer. **This can only happen if
/// you call `poll()` manually**. Using this struct as it is intended to be used (ie. through
/// you call `poll()` manually**. Using this struct as it is intended to be used (i.e. through
/// the modifiers provided by the `futures` crate) will always leave the object in a state in
/// which `into_inner()` will not panic.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion misc/multistream-select/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//!
//! Whenever a new connection or a new multiplexed substream is opened, libp2p uses
//! `multistream-select` to negotiate with the remote which protocol to use. After a protocol has
//! been successfully negotiated, the stream (ie. the connection or the multiplexed substream)
//! been successfully negotiated, the stream (i.e. the connection or the multiplexed substream)
//! immediately stops using `multistream-select` and starts using the negotiated protocol.
//!
//! ## Protocol explanation
Expand Down
2 changes: 1 addition & 1 deletion misc/multistream-select/src/listener_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use ProtocolChoiceError;
/// Helps selecting a protocol amongst the ones supported.
///
/// This function expects a socket and an iterator of the list of supported protocols. The iterator
/// must be clonable (ie. iterable multiple times), because the list may need to be accessed
/// must be clonable (i.e. iterable multiple times), because the list may need to be accessed
/// multiple times.
///
/// The iterator must produce tuples of the name of the protocol that is advertised to the remote,
Expand Down
12 changes: 12 additions & 0 deletions misc/peer-id-generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "peer-id-generator"
version = "0.1.0"
description = "Generate peer ids that are prefixed with a specific string"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"

[dependencies]
libp2p-core = { path = "../../core" }
libp2p-secio = { path = "../../protocols/secio" }
num_cpus = "1.8"
rand = "0.5"
89 changes: 89 additions & 0 deletions misc/peer-id-generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

extern crate libp2p_core;
extern crate libp2p_secio;
extern crate num_cpus;
extern crate rand;

use libp2p_core::PeerId;
use libp2p_secio::SecioKeyPair;
use std::{env, str, thread, time::Duration};

fn main() {
// Due to the fact that a peer id uses a SHA-256 multihash, it always starts with the
// bytes 0x1220, meaning that only some characters are valid.
const ALLOWED_FIRST_BYTE: &'static [u8] = b"NPQRSTUVWXYZ";

let prefix =
match env::args().nth(1) {
Some(prefix) => prefix,
None => {
println!(
"Usage: {} <prefix>\n\n\
Generates a peer id that starts with the chosen prefix using a secp256k1 public \
key.\n\n\
Prefix must be a sequence of characters in the base58 \
alphabet, and must start with one of the following: {}",
env::current_exe().unwrap().file_name().unwrap().to_str().unwrap(),
str::from_utf8(ALLOWED_FIRST_BYTE).unwrap()
);
return;
}
};

// The base58 alphabet is not necessarily obvious.
const ALPHABET: &'static [u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
if prefix.as_bytes().iter().any(|c| !ALPHABET.contains(c)) {
println!("Prefix {} is not valid base58", prefix);
return;
}

// Checking conformity to ALLOWED_FIRST_BYTE.
if !prefix.is_empty() {
if !ALLOWED_FIRST_BYTE.contains(&prefix.as_bytes()[0]) {
println!("Prefix {} is not reachable", prefix);
println!(
"Only the following bytes are possible as first byte: {}",
str::from_utf8(ALLOWED_FIRST_BYTE).unwrap()
);
return;
}
}

// Find peer IDs in a multithreaded fashion.
for _ in 0..num_cpus::get() {
let prefix = prefix.clone();
thread::spawn(move || loop {
let private_key: [u8; 32] = rand::random();
let generated = SecioKeyPair::secp256k1_raw_key(private_key).unwrap();
let peer_id: PeerId = generated.to_public_key().into_peer_id();
let base58 = peer_id.to_base58();
if base58[2..].starts_with(&prefix) {
println!("Found {:?}", peer_id);
println!("=> Private key = {:?}", private_key);
}
});
}

loop {
thread::sleep(Duration::from_secs(3600));
}
}
2 changes: 1 addition & 1 deletion protocols/identify/src/periodic_id_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
}

#[inline]
fn inject_event(&mut self, _: &Self::InEvent) {}
fn inject_event(&mut self, _: Self::InEvent) {}

#[inline]
fn inject_inbound_closed(&mut self) {}
Expand Down
4 changes: 2 additions & 2 deletions protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ where
pub struct IdentifyInfo {
/// Public key of the node.
pub public_key: PublicKey,
/// Version of the "global" protocol, eg. `ipfs/1.0.0` or `polkadot/1.0.0`.
/// Version of the "global" protocol, e.g. `ipfs/1.0.0` or `polkadot/1.0.0`.
pub protocol_version: String,
/// Name and version of the client. Can be thought as similar to the `User-Agent` header
/// of HTTP.
pub agent_version: String,
/// Addresses that the node is listening on.
pub listen_addrs: Vec<Multiaddr>,
/// Protocols supported by the node, eg. `/ipfs/ping/1.0.0`.
/// Protocols supported by the node, e.g. `/ipfs/ping/1.0.0`.
pub protocols: Vec<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/kad_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
pub struct KadConnecController {
// In order to send a request, we use this sender to send a tuple. The first element of the
// tuple is the message to send to the remote, and the second element is what is used to
// receive the response. If the query doesn't expect a response (eg. `PUT_VALUE`), then the
// receive the response. If the query doesn't expect a response (e.g. `PUT_VALUE`), then the
// one-shot sender will be dropped without being used.
inner: mpsc::UnboundedSender<(KadMsg, oneshot::Sender<KadMsg>)>,
}
Expand Down
3 changes: 3 additions & 0 deletions protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"

[dependencies]
arrayvec = "0.4"
bytes = "0.4"
libp2p-core = { path = "../../core" }
log = "0.4.1"
Expand All @@ -15,6 +16,8 @@ parking_lot = "0.6"
rand = "0.5"
tokio-codec = "0.1"
tokio-io = "0.1"
tokio-timer = "0.2.6"
void = "1.0"

[dev-dependencies]
libp2p-tcp-transport = { path = "../../transports/tcp" }
Expand Down
Loading

0 comments on commit 486af92

Please sign in to comment.