Skip to content

Commit

Permalink
Remove loopback and duplicate addrs in net peers output
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancisMurillo committed Jul 27, 2021
1 parent ffc3019 commit 8e79908
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions forest/src/cli/net_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use forest_libp2p::{Multiaddr, Protocol};
use rpc_api::data_types::AddrInfo;
use std::collections::HashSet;
use structopt::StructOpt;

use crate::cli::cli_error_and_die;
Expand Down Expand Up @@ -50,12 +51,26 @@ impl NetCommands {
Self::Peers => match net_peers(()).await {
Ok(addrs) => {
let output: Vec<String> = addrs
.iter()
.map(|info| {
let addresses: Vec<String> =
info.addrs.iter().map(|addr| addr.to_string()).collect();
.into_iter()
.filter_map(|info| {
let addresses: Vec<String> = info
.addrs
.into_iter()
.filter(|addr| match addr.iter().next().unwrap() {
Protocol::Ip4(ip_addr) => !ip_addr.is_loopback(),
Protocol::Ip6(ip_addr) => !ip_addr.is_loopback(),
_ => true,
})
.map(|addr| addr.to_string())
.collect::<HashSet<_>>()
.into_iter()
.collect();

if addresses.is_empty() {
return None;
}

format!("{}, [{}]", info.id, addresses.join(", "))
Some(format!("{}, [{}]", info.id, addresses.join(", ")))
})
.collect();

Expand Down

0 comments on commit 8e79908

Please sign in to comment.