From f914eefbdd613ac433bf99067941c8d6d1f9f9c5 Mon Sep 17 00:00:00 2001 From: Yevhenii Babichenko Date: Wed, 20 Nov 2019 19:39:00 +0200 Subject: [PATCH] avoid empty vectors in TrustedAddress::to_addresses --- jormungandr/src/settings/start/config.rs | 29 +++++++++++++++--------- jormungandr/src/settings/start/mod.rs | 16 ++++++++----- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/jormungandr/src/settings/start/config.rs b/jormungandr/src/settings/start/config.rs index 62808cc1b5..6578c630b8 100644 --- a/jormungandr/src/settings/start/config.rs +++ b/jormungandr/src/settings/start/config.rs @@ -222,7 +222,7 @@ custom_error! {pub AddressError } impl TrustedAddress { - pub fn to_addresses(&self) -> Result, AddressError> { + pub fn to_addresses(&self) -> Result>, AddressError> { use multiaddr::AddrComponent; use std::iter::FromIterator; use trust_dns_proto::rr::{record_data::RData, RecordType}; @@ -254,20 +254,27 @@ impl TrustedAddress { .collect(), Some(AddrComponent::IP4(addr)) => vec![AddrComponent::IP4(addr)], Some(AddrComponent::IP6(addr)) => vec![AddrComponent::IP6(addr)], - _ => Vec::new(), + _ => return Ok(None), }; + if addresses.is_empty() { + return Ok(None); + } + if let Some(AddrComponent::TCP(port)) = components.next() { - Ok(addresses - .into_iter() - .map(|addr| { - let new_components = vec![addr, AddrComponent::TCP(port)]; - let new_multiaddr = multiaddr::Multiaddr::from_iter(new_components.into_iter()); - Address(poldercast::Address::new(new_multiaddr).unwrap()) - }) - .collect()) + Ok(Some( + addresses + .into_iter() + .map(|addr| { + let new_components = vec![addr, AddrComponent::TCP(port)]; + let new_multiaddr = + multiaddr::Multiaddr::from_iter(new_components.into_iter()); + Address(poldercast::Address::new(new_multiaddr).unwrap()) + }) + .collect(), + )) } else { - Ok(Vec::new()) + Ok(None) } } } diff --git a/jormungandr/src/settings/start/mod.rs b/jormungandr/src/settings/start/mod.rs index 2bb03b5e28..cfac2c7191 100644 --- a/jormungandr/src/settings/start/mod.rs +++ b/jormungandr/src/settings/start/mod.rs @@ -182,6 +182,8 @@ fn generate_network( command_arguments: &StartArguments, config: &Option, ) -> Result { + use crate::settings::start::network::TrustedPeer; + let mut p2p = if let Some(cfg) = config { cfg.p2p.clone() } else { @@ -235,14 +237,16 @@ fn generate_network( .unwrap_or(vec![]) .into_iter() .filter_map(|tp| { - tp.address.to_addresses().ok().map(|addrs| { - addrs.into_iter().map(move |address| { - crate::settings::start::network::TrustedPeer { + tp.address + .to_addresses() + .ok() + .and_then(std::convert::identity) + .map(|addrs| { + addrs.into_iter().map(move |addr| TrustedPeer { id: tp.id.clone(), - address: address.0, - } + address: addr.0, + }) }) - }) }) .flatten() .collect(),