diff --git a/network/src/address.rs b/network/src/address.rs new file mode 100644 index 0000000000..3904d1ae87 --- /dev/null +++ b/network/src/address.rs @@ -0,0 +1,37 @@ +use p2p::multiaddr::{MultiAddr, Protocol}; + +pub struct NetworkAddresses { + pub regular_addresses: Vec, + pub onion_addresses: Vec, +} + +// convert Vec to NetworkAddresses +impl From> for NetworkAddresses { + fn from(addresses: Vec) -> Self { + let mut regular_addresses = Vec::new(); + let mut onion_addresses = Vec::new(); + for address in addresses { + if address + .iter() + .any(|proto| matches!(proto, Protocol::Onion(_))) + { + onion_addresses.push(address); + } else { + regular_addresses.push(address); + } + } + NetworkAddresses { + regular_addresses, + onion_addresses, + } + } +} + +// convert NetworkAddresses to Vec +impl From for Vec { + fn from(addresses: NetworkAddresses) -> Self { + let mut result = addresses.regular_addresses; + result.extend(addresses.onion_addresses); + result + } +} diff --git a/network/src/network.rs b/network/src/network.rs index a8003aa3d3..4d46d6912c 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,4 +1,5 @@ //! Global state struct and start function +use crate::address::NetworkAddresses; use crate::errors::Error; #[cfg(not(target_family = "wasm"))] use crate::errors::P2PError; @@ -73,7 +74,7 @@ pub struct NetworkState { pub(crate) peer_registry: RwLock, pub(crate) peer_store: Mutex, /// Node listened addresses - pub(crate) listened_addrs: RwLock>, + pub(crate) listened_addrs: RwLock, dialing_addrs: RwLock>, /// Node public addresses, /// includes manually public addrs and remote peer observed addrs diff --git a/network/src/peer.rs b/network/src/peer.rs index fe7ee3dfb3..3c649c7ef3 100644 --- a/network/src/peer.rs +++ b/network/src/peer.rs @@ -1,3 +1,4 @@ +use crate::address::NetworkAddresses; use crate::network_group::Group; use crate::{ multiaddr::Multiaddr, protocols::identify::Flags, ProtocolId, ProtocolVersion, SessionType, @@ -21,7 +22,7 @@ pub struct Peer { /// Peer address pub connected_addr: Multiaddr, /// Peer listen addresses - pub listened_addrs: Vec, + pub listened_addrs: NetworkAddresses, /// Peer info from identify protocol message pub identify_info: Option, /// Ping/Pong message last received time