Skip to content

Commit

Permalink
Merge pull request #2210 from input-output-hk/jormungandr-lib-to_sock…
Browse files Browse the repository at this point in the history
…etaddr

implement to_socketaddr in jormungandr-lib
  • Loading branch information
Mikhail Zabaluev authored May 12, 2020
2 parents 0056539 + fcd8cb6 commit bf9265a
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jormungandr-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions jormungandr-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ extern crate quickcheck;

pub mod crypto;
pub mod interfaces;
pub mod multiaddr;
pub mod time;
24 changes: 24 additions & 0 deletions jormungandr-lib/src/multiaddr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use multiaddr::{AddrComponent, Multiaddr};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};

pub fn multiaddr_to_socket_addr(addr: &Multiaddr) -> Option<SocketAddr> {
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,
}
}
12 changes: 7 additions & 5 deletions jormungandr/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -527,7 +527,7 @@ fn trusted_peers_shuffled(config: &Configuration) -> Vec<SocketAddr> {
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::<Vec<_>>();
let mut rng = rand::thread_rng();
peers.shuffle(&mut rng);
Expand Down Expand Up @@ -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::<Vec<_>>();
if config.bootstrap_from_trusted_peers {
let _: usize = peers.add_peers(&trusted_peers);
Expand Down
4 changes: 3 additions & 1 deletion jormungandr/src/network/p2p/comm/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ impl PeerMap {
}

pub fn infos(&self) -> Vec<PeerInfo> {
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()
Expand Down
11 changes: 9 additions & 2 deletions jormungandr/src/network/p2p/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down
10 changes: 8 additions & 2 deletions jormungandr/src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ impl GossipService for NodeService {
}

async fn peers(&self, limit: u32) -> Result<Peers, Error> {
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;
Expand All @@ -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());
}
}
Expand Down
4 changes: 3 additions & 1 deletion jormungandr/src/settings/start/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ fn generate_network(
config: &Option<Config>,
logger: &Logger,
) -> Result<network::Configuration, Error> {
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 {
(
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion jormungandr/src/settings/start/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ impl Configuration {
/// Returns the listener configuration, if the options defining it
/// were set.
pub fn listen(&self) -> Option<Listen> {
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))
}
}
18 changes: 11 additions & 7 deletions testing/jormungandr-scenario-tests/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}

Expand Down
12 changes: 8 additions & 4 deletions testing/jormungandr-scenario-tests/src/test/features/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down

0 comments on commit bf9265a

Please sign in to comment.