From fcd8cb62c47e041cdf3175d53177883653bc2ee2 Mon Sep 17 00:00:00 2001 From: Yevhenii Babichenko Date: Thu, 7 May 2020 13:02:01 +0300 Subject: [PATCH] implement to_socketaddr in jormungandr-lib --- Cargo.lock | 1 + jormungandr-lib/Cargo.toml | 1 + jormungandr-lib/src/lib.rs | 1 + jormungandr-lib/src/multiaddr.rs | 24 +++++++++++++++++++ jormungandr/src/network/mod.rs | 12 ++++++---- jormungandr/src/network/p2p/comm/peer_map.rs | 4 +++- jormungandr/src/network/p2p/gossip.rs | 11 +++++++-- jormungandr/src/network/service.rs | 10 ++++++-- jormungandr/src/settings/start/mod.rs | 4 +++- jormungandr/src/settings/start/network.rs | 4 +++- .../jormungandr-scenario-tests/src/node.rs | 18 ++++++++------ .../src/test/features/p2p/mod.rs | 12 ++++++---- 12 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 jormungandr-lib/src/multiaddr.rs diff --git a/Cargo.lock b/Cargo.lock index 9c3c7bdb32..01eb444d47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1694,6 +1694,7 @@ dependencies = [ "ed25519-bip32", "hex", "humantime", + "multiaddr", "poldercast", "quickcheck", "rand 0.7.3", diff --git a/jormungandr-lib/Cargo.toml b/jormungandr-lib/Cargo.toml index a213e0f2f4..c3f477793d 100644 --- a/jormungandr-lib/Cargo.toml +++ b/jormungandr-lib/Cargo.toml @@ -23,6 +23,7 @@ humantime = "2.0" thiserror = "1.0" poldercast = "0.13.1" hex = "0.4" +multiaddr = "0.3.1" [dev-dependencies] rand = "0.7" diff --git a/jormungandr-lib/src/lib.rs b/jormungandr-lib/src/lib.rs index 2781abec96..eaeb7227e3 100644 --- a/jormungandr-lib/src/lib.rs +++ b/jormungandr-lib/src/lib.rs @@ -4,4 +4,5 @@ extern crate quickcheck; pub mod crypto; pub mod interfaces; +pub mod multiaddr; pub mod time; diff --git a/jormungandr-lib/src/multiaddr.rs b/jormungandr-lib/src/multiaddr.rs new file mode 100644 index 0000000000..a127c6dfa6 --- /dev/null +++ b/jormungandr-lib/src/multiaddr.rs @@ -0,0 +1,24 @@ +use multiaddr::{AddrComponent, Multiaddr}; +use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; + +pub fn multiaddr_to_socket_addr(addr: &Multiaddr) -> Option { + let mut components = addr.iter(); + + match components.next()? { + AddrComponent::IP4(ipv4) => { + if let AddrComponent::TCP(port) = components.next()? { + Some(SocketAddr::V4(SocketAddrV4::new(ipv4, port))) + } else { + None + } + } + AddrComponent::IP6(ipv6) => { + if let AddrComponent::TCP(port) = components.next()? { + Some(SocketAddr::V6(SocketAddrV6::new(ipv6, port, 0, 0))) + } else { + None + } + } + _ => None, + } +} diff --git a/jormungandr/src/network/mod.rs b/jormungandr/src/network/mod.rs index 4fc4a3b73d..5c39efe7a6 100644 --- a/jormungandr/src/network/mod.rs +++ b/jormungandr/src/network/mod.rs @@ -14,8 +14,8 @@ mod service; mod subscription; use self::convert::Encode; -use futures::future; -use futures::prelude::*; +use futures::{future, prelude::*}; +use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; use poldercast::Address; use thiserror::Error; use tokio::time; @@ -450,7 +450,7 @@ fn connect_and_propagate( channels: Channels, mut options: p2p::comm::ConnectOptions, ) { - let addr = match node.to_socketaddr() { + let addr = match multiaddr_to_socket_addr(node.multi_address()) { Some(addr) => addr, None => { debug!( @@ -527,7 +527,7 @@ fn trusted_peers_shuffled(config: &Configuration) -> Vec { let mut peers = config .trusted_peers .iter() - .filter_map(|peer| peer.address.to_socketaddr()) + .filter_map(|peer| multiaddr_to_socket_addr(peer.address.multi_address())) .collect::>(); let mut rng = rand::thread_rng(); peers.shuffle(&mut rng); @@ -577,7 +577,9 @@ async fn netboot_peers(config: &Configuration, logger: &Logger) -> BootstrapPeer let trusted_peers = config .trusted_peers .iter() - .filter_map(|tp| tp.address.to_socketaddr().map(|sa| Peer::new(sa.clone()))) + .filter_map(|tp| { + multiaddr_to_socket_addr(tp.address.multi_address()).map(|sa| Peer::new(sa.clone())) + }) .collect::>(); if config.bootstrap_from_trusted_peers { let _: usize = peers.add_peers(&trusted_peers); diff --git a/jormungandr/src/network/p2p/comm/peer_map.rs b/jormungandr/src/network/p2p/comm/peer_map.rs index 52711b2777..90e3c23d66 100644 --- a/jormungandr/src/network/p2p/comm/peer_map.rs +++ b/jormungandr/src/network/p2p/comm/peer_map.rs @@ -144,10 +144,12 @@ impl PeerMap { } pub fn infos(&self) -> Vec { + use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; + self.map .iter() .map(|(id, data)| PeerInfo { - addr: id.to_socketaddr(), + addr: multiaddr_to_socket_addr(id.multi_address()), stats: data.stats.clone(), }) .collect() diff --git a/jormungandr/src/network/p2p/gossip.rs b/jormungandr/src/network/p2p/gossip.rs index 4f801d69c7..8fb46baf83 100644 --- a/jormungandr/src/network/p2p/gossip.rs +++ b/jormungandr/src/network/p2p/gossip.rs @@ -3,6 +3,7 @@ use crate::network::convert::Encode; use bincode; use chain_core::property; use chain_network::data as net_data; +use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; use serde::{Deserialize, Serialize}; use std::net::IpAddr; @@ -21,7 +22,10 @@ impl Gossip { } pub fn has_valid_address(&self) -> bool { - let addr = match self.address().and_then(|addr| addr.to_socketaddr()) { + let addr = match self + .address() + .and_then(|addr| multiaddr_to_socket_addr(addr.multi_address())) + { None => return false, Some(addr) => addr, }; @@ -62,7 +66,10 @@ impl Gossip { return false; } - let addr = match self.address().and_then(|addr| addr.to_socketaddr()) { + let addr = match self + .address() + .and_then(|addr| multiaddr_to_socket_addr(addr.multi_address())) + { None => return false, Some(addr) => addr, }; diff --git a/jormungandr/src/network/service.rs b/jormungandr/src/network/service.rs index 8587872c44..acccd8822e 100644 --- a/jormungandr/src/network/service.rs +++ b/jormungandr/src/network/service.rs @@ -275,11 +275,13 @@ impl GossipService for NodeService { } async fn peers(&self, limit: u32) -> Result { + use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; + let topology = &self.global_state.topology; let view = topology.view(poldercast::Selection::Any).await; let mut peers = Vec::new(); for n in view.peers.into_iter() { - if let Some(addr) = n.to_socketaddr() { + if let Some(addr) = multiaddr_to_socket_addr(n.multi_address()) { peers.push(addr.into()); if peers.len() >= limit as usize { break; @@ -288,7 +290,11 @@ impl GossipService for NodeService { } if peers.len() == 0 { // No peers yet, put self as the peer to bootstrap from - if let Some(addr) = view.self_node.address().and_then(|x| x.to_socketaddr()) { + if let Some(addr) = view + .self_node + .address() + .and_then(|x| multiaddr_to_socket_addr(x.multi_address())) + { peers.push(addr.into()); } } diff --git a/jormungandr/src/settings/start/mod.rs b/jormungandr/src/settings/start/mod.rs index 060a6651ca..42ac850572 100644 --- a/jormungandr/src/settings/start/mod.rs +++ b/jormungandr/src/settings/start/mod.rs @@ -196,6 +196,8 @@ fn generate_network( config: &Option, logger: &Logger, ) -> Result { + use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; + let (mut p2p, http_fetch_block0_service, skip_bootstrap, bootstrap_from_trusted_peers) = if let Some(cfg) = config { ( @@ -238,7 +240,7 @@ fn generate_network( listen_address: match &p2p.listen_address { None => None, Some(v) => { - if let Some(addr) = v.to_socketaddr() { + if let Some(addr) = multiaddr_to_socket_addr(v.multi_address()) { Some(addr) } else { return Err(Error::ListenAddressNotValid); diff --git a/jormungandr/src/settings/start/network.rs b/jormungandr/src/settings/start/network.rs index 613e368e78..42a415561d 100644 --- a/jormungandr/src/settings/start/network.rs +++ b/jormungandr/src/settings/start/network.rs @@ -147,11 +147,13 @@ impl Configuration { /// Returns the listener configuration, if the options defining it /// were set. pub fn listen(&self) -> Option { + use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; + self.listen_address .or(self .profile .address() - .and_then(|address| address.to_socketaddr())) + .and_then(|address| multiaddr_to_socket_addr(address.multi_address()))) .map(|addr| Listen::new(addr)) } } diff --git a/testing/jormungandr-scenario-tests/src/node.rs b/testing/jormungandr-scenario-tests/src/node.rs index 0f1a3e320c..e73408f728 100644 --- a/testing/jormungandr-scenario-tests/src/node.rs +++ b/testing/jormungandr-scenario-tests/src/node.rs @@ -550,15 +550,19 @@ impl NodeController { } fn ports_are_opened(&self) -> bool { + use jormungandr_lib::multiaddr::multiaddr_to_socket_addr; + self.port_opened(self.settings.config.rest.listen.port()) && self.port_opened( - self.settings - .config - .p2p - .get_listen_address() - .to_socketaddr() - .unwrap() - .port(), + multiaddr_to_socket_addr( + self.settings + .config + .p2p + .get_listen_address() + .multi_address(), + ) + .unwrap() + .port(), ) } diff --git a/testing/jormungandr-scenario-tests/src/test/features/p2p/mod.rs b/testing/jormungandr-scenario-tests/src/test/features/p2p/mod.rs index 0de18f9367..16e938592a 100644 --- a/testing/jormungandr-scenario-tests/src/test/features/p2p/mod.rs +++ b/testing/jormungandr-scenario-tests/src/test/features/p2p/mod.rs @@ -11,7 +11,7 @@ use crate::{ test::{utils, Result}, }; -use jormungandr_lib::interfaces::PeerRecord; +use jormungandr_lib::{interfaces::PeerRecord, multiaddr::multiaddr_to_socket_addr}; pub fn assert_connected_cnt( node: &NodeController, @@ -92,7 +92,7 @@ pub fn assert_are_not_in_network_view( utils::assert( network_view .iter() - .any(|info| info.addr == peer.address().to_socketaddr()), + .any(|info| info.addr == multiaddr_to_socket_addr(peer.address().multi_address())), &format!( "{}: Peer {} is present in network view list, while it should not", info, @@ -113,7 +113,9 @@ pub fn assert_are_in_network_stats( utils::assert( network_stats.iter().any(|x| { x.addr - .and_then(|a| peer.address().to_socketaddr().map(|b| a == b)) + .and_then(|a| { + multiaddr_to_socket_addr(peer.address().multi_address()).map(|b| a == b) + }) .unwrap_or(false) }), &format!( @@ -136,7 +138,9 @@ pub fn assert_are_not_in_network_stats( utils::assert( !network_stats.iter().any(|x| { x.addr - .and_then(|a| peer.address().to_socketaddr().map(|b| a == b)) + .and_then(|a| { + multiaddr_to_socket_addr(peer.address().multi_address()).map(|b| a == b) + }) .unwrap_or(false) }), &format!(