Skip to content

Commit

Permalink
Update to libp2p v0.4 (paritytech#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka authored and gavofyork committed Feb 21, 2019
1 parent 8cfb096 commit 3bc096a
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 210 deletions.
341 changes: 143 additions & 198 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use service::{
};
use network::{
Protocol, config::{NetworkConfiguration, NonReservedPeerMode, Secret},
multiaddr,
build_multiaddr,
};
use primitives::H256;

Expand Down Expand Up @@ -455,7 +455,7 @@ where
.map_err(|err| format!("Error obtaining network key: {}", err))?;

let peer_id = network_keys.to_peer_id();
let addr = multiaddr![
let addr = build_multiaddr![
Ip4([127, 0, 0, 1]),
Tcp(30333u16),
P2p(peer_id)
Expand Down
2 changes: 1 addition & 1 deletion core/network-libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bytes = "0.4"
error-chain = { version = "0.12", default-features = false }
fnv = "1.0"
futures = "0.1"
libp2p = { version = "0.3.1", default-features = false, features = ["secio-secp256k1", "libp2p-websocket"] }
libp2p = { version = "0.4.0", default-features = false, features = ["secio-secp256k1", "libp2p-websocket"] }
parking_lot = "0.7.1"
lazy_static = "1.2"
log = "0.4"
Expand Down
6 changes: 5 additions & 1 deletion core/network-libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<TMessage, TSubstream> NetworkBehaviourEventProcess<IdentifyEvent> for Behav
info.listen_addrs.truncate(30);
}
for addr in &info.listen_addrs {
self.discovery.kademlia.add_address(&peer_id, addr.clone());
self.discovery.kademlia.add_connected_address(&peer_id, addr.clone());
}
self.custom_protocols.add_discovered_addrs(
&peer_id,
Expand All @@ -255,6 +255,10 @@ impl<TMessage, TSubstream> NetworkBehaviourEventProcess<IdentifyEvent> for Behav
self.events.push(BehaviourOut::Identified { peer_id, info });
}
IdentifyEvent::Error { .. } => {}
IdentifyEvent::SendBack { result: Err(ref err), ref peer_id } =>
debug!(target: "sub-libp2p", "Error when sending back identify info \
to {:?} => {}", peer_id, err),
IdentifyEvent::SendBack { .. } => {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/network-libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use crate::secret::obtain_private_key;
pub use crate::service_task::{start_service, Service, ServiceEvent};
pub use crate::traits::{NetworkConfiguration, NodeIndex, NodeId, NonReservedPeerMode};
pub use crate::traits::{ProtocolId, Secret, Severity};
pub use libp2p::{Multiaddr, multiaddr::{Protocol}, multiaddr, PeerId, core::PublicKey};
pub use libp2p::{Multiaddr, multiaddr::Protocol, build_multiaddr, PeerId, core::PublicKey};

/// Check if node url is valid
pub fn validate_node_url(url: &str) -> Result<(), Error> {
Expand Down
6 changes: 3 additions & 3 deletions core/network-libp2p/src/service_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::custom_proto::{CustomMessage, RegisteredProtocol, RegisteredProtocols
use crate::{Error, NetworkConfiguration, NodeIndex, ProtocolId, parse_str_addr};
use fnv::FnvHashMap;
use futures::{prelude::*, Stream};
use libp2p::{multiaddr::Protocol, Multiaddr, PeerId, multiaddr};
use libp2p::{multiaddr::Protocol, Multiaddr, PeerId, build_multiaddr};
use libp2p::core::{Swarm, nodes::Substream, transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::core::nodes::ConnectedPoint;
use log::{debug, info, warn};
Expand Down Expand Up @@ -88,8 +88,8 @@ where TProtos: IntoIterator<Item = RegisteredProtocol<TMessage>>,
// If the format of the bootstrap node is not a multiaddr, try to parse it as
// a `SocketAddr`. This corresponds to the format `IP:PORT`.
let addr = match bootnode.parse::<SocketAddr>() {
Ok(SocketAddr::V4(socket)) => multiaddr![Ip4(*socket.ip()), Tcp(socket.port())],
Ok(SocketAddr::V6(socket)) => multiaddr![Ip6(*socket.ip()), Tcp(socket.port())],
Ok(SocketAddr::V4(socket)) => build_multiaddr![Ip4(*socket.ip()), Tcp(socket.port())],
Ok(SocketAddr::V6(socket)) => build_multiaddr![Ip6(*socket.ip()), Tcp(socket.port())],
_ => {
warn!(target: "sub-libp2p", "Not a valid bootnode address: {}", bootnode);
continue
Expand Down
6 changes: 3 additions & 3 deletions core/network-libp2p/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use futures::{future, stream, prelude::*, try_ready};
use std::{io, iter};
use substrate_network_libp2p::{CustomMessage, ServiceEvent, multiaddr};
use substrate_network_libp2p::{CustomMessage, Protocol, ServiceEvent, build_multiaddr};

/// Builds two services. The second one and further have the first one as its bootstrap node.
/// This is to be used only for testing, and a panic will happen if something goes wrong.
Expand All @@ -29,12 +29,12 @@ fn build_nodes<TMsg>(num: usize) -> Vec<substrate_network_libp2p::Service<TMsg>>
let mut boot_nodes = Vec::new();
if !result.is_empty() {
let mut bootnode = result[0].listeners().next().unwrap().clone();
bootnode.append(libp2p::multiaddr::Protocol::P2p(result[0].peer_id().clone().into()));
bootnode.append(Protocol::P2p(result[0].peer_id().clone().into()));
boot_nodes.push(bootnode.to_string());
}

let config = substrate_network_libp2p::NetworkConfiguration {
listen_addresses: vec![multiaddr![Ip4([127, 0, 0, 1]), Tcp(0u16)]],
listen_addresses: vec![build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(0u16)]],
boot_nodes,
..substrate_network_libp2p::NetworkConfiguration::default()
};
Expand Down
2 changes: 1 addition & 1 deletion core/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use protocol::{ProtocolStatus, PeerInfo, Context};
pub use sync::{Status as SyncStatus, SyncState};
pub use network_libp2p::{
NodeIndex, ProtocolId, Severity, Protocol, Multiaddr,
obtain_private_key, multiaddr, PeerId, PublicKey
obtain_private_key, build_multiaddr, PeerId, PublicKey
};
pub use message::{generic as generic_message, RequestId, Status as StatusMessage, ConsensusEngineId};
pub use error::Error;
Expand Down

0 comments on commit 3bc096a

Please sign in to comment.