Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Rewrite the libp2p networking
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Sep 14, 2018
1 parent 0741b98 commit 167c45e
Show file tree
Hide file tree
Showing 16 changed files with 3,620 additions and 4,265 deletions.
380 changes: 196 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion core/network-libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bytes = "0.4"
error-chain = { version = "0.12", default-features = false }
fnv = "1.0"
futures = "0.1"
libp2p = { git = "https://github.com/libp2p/rust-libp2p", rev = "5980a4538ef6fc8af450893acb01290eaed136de", default-features = false, features = ["libp2p-secio", "libp2p-secio-secp256k1"] }
libp2p = { git = "https://github.com/libp2p/rust-libp2p", rev = "063ab178a9b2576cb6620d5435183737536cbd49", default-features = false, features = ["libp2p-secio", "libp2p-secio-secp256k1"] }
ethereum-types = "0.3"
parking_lot = "0.5"
libc = "0.2"
Expand All @@ -20,7 +20,9 @@ rand = "0.5.0"
serde = "1.0.70"
serde_derive = "1.0.70"
serde_json = "1.0.24"
smallvec = "0.6.5"
tokio = "0.1"
tokio-executor = "0.1"
tokio-io = "0.1"
tokio-timer = "0.2"
unsigned-varint = { version = "0.2.1", features = ["codec"] }
Expand Down
6 changes: 6 additions & 0 deletions core/network-libp2p/src/custom_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ where C: AsyncRead + AsyncWrite + Send + 'static, // TODO: 'static :-/
pub struct RegisteredProtocols<T>(pub Vec<RegisteredProtocol<T>>);

impl<T> RegisteredProtocols<T> {
/// Returns the number of protocols.
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}

/// Finds a protocol in the list by its id.
pub fn find_protocol(&self, protocol: ProtocolId)
-> Option<&RegisteredProtocol<T>> {
Expand Down
25 changes: 23 additions & 2 deletions core/network-libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ extern crate parking_lot;
extern crate fnv;
extern crate futures;
extern crate tokio;
extern crate tokio_executor;
extern crate tokio_io;
extern crate tokio_timer;
extern crate libc;
#[macro_use]
extern crate libp2p;
extern crate rand;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate smallvec;
extern crate bytes;
extern crate unsigned_varint;

Expand All @@ -46,18 +49,25 @@ extern crate log;
#[cfg(test)] #[macro_use]
extern crate assert_matches;

use libp2p::PeerId;

pub use connection_filter::{ConnectionFilter, ConnectionDirection};
pub use error::{Error, ErrorKind, DisconnectReason};
pub use libp2p::{Multiaddr, multiaddr::AddrComponent};
pub use traits::*;

pub type TimerToken = usize;

// TODO: remove as it is unused ; however modifying `network` causes a clusterfuck of dependencies
// resolve errors at the moment
mod connection_filter;
mod custom_proto;
mod error;
mod network_state;
mod node_handler;
mod secret;
mod service;
mod service_task;
mod swarm;
mod timeouts;
mod topology;
mod traits;
Expand All @@ -67,8 +77,19 @@ pub use service::NetworkService;

/// Check if node url is valid
pub fn validate_node_url(url: &str) -> Result<(), Error> {
match url.parse::<libp2p::multiaddr::Multiaddr>() {
match url.parse::<Multiaddr>() {
Ok(_) => Ok(()),
Err(_) => Err(ErrorKind::InvalidNodeId.into()),
}
}

/// Parses a string address and returns the component, if valid.
pub(crate) fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), Error> {
let mut addr: Multiaddr = addr_str.parse().map_err(|_| ErrorKind::AddressParse)?;
let who = match addr.pop() {
Some(AddrComponent::P2P(key)) =>
PeerId::from_multihash(key).map_err(|_| ErrorKind::AddressParse)?,
_ => return Err(ErrorKind::AddressParse.into()),
};
Ok((who, addr))
}
Loading

0 comments on commit 167c45e

Please sign in to comment.