Skip to content

Commit

Permalink
Use NetworkAddresses to represent regular_addrs and onion_addrs
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Dec 31, 2024
1 parent dd17bde commit e2df0ef
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
71 changes: 71 additions & 0 deletions network/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use p2p::multiaddr::{MultiAddr, Protocol};

#[derive(Default)]
pub struct NetworkAddresses {
pub regular_addresses: Vec<MultiAddr>,

// onion addresses can't be solved by multiaddr_to_socketaddr or socketaddr_to_multiaddr
pub onion_addresses: Vec<MultiAddr>,
}

impl NetworkAddresses {
pub fn push(&mut self, address: MultiAddr) {
if address
.iter()
.any(|proto| matches!(proto, Protocol::Onion3(_)))
{
self.onion_addresses.push(address);
} else {
self.regular_addresses.push(address);
}
}

// contains
pub fn contains(&self, address: &MultiAddr) -> bool {
self.regular_addresses.contains(address) || self.onion_addresses.contains(address)
}
}

impl IntoIterator for NetworkAddresses {
type Item = MultiAddr;
type IntoIter = std::vec::IntoIter<MultiAddr>;

fn into_iter(self) -> Self::IntoIter {
self.regular_addresses
.into_iter()
.chain(self.onion_addresses.into_iter())
.collect::<Vec<_>>()
.into_iter()
}
}

// convert Vec<MultiAddr> to NetworkAddresses
impl From<Vec<MultiAddr>> for NetworkAddresses {
fn from(addresses: Vec<MultiAddr>) -> 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::Onion3(_)))
{
onion_addresses.push(address);
} else {
regular_addresses.push(address);
}
}
NetworkAddresses {
regular_addresses,
onion_addresses,
}
}
}

// convert NetworkAddresses to Vec<MultiAddr>
impl From<NetworkAddresses> for Vec<MultiAddr> {
fn from(addresses: NetworkAddresses) -> Self {
let mut result = addresses.regular_addresses;
result.extend(addresses.onion_addresses);
result
}
}
1 change: 1 addition & 0 deletions network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! And implemented several basic protocols: identify, discovery, ping, feeler, disconnect_message
//!
mod address;
mod behaviour;
/// compress module
pub mod compress;
Expand Down
3 changes: 2 additions & 1 deletion network/src/network.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ pub struct NetworkState {
pub(crate) peer_registry: RwLock<PeerRegistry>,
pub(crate) peer_store: Mutex<PeerStore>,
/// Node listened addresses
pub(crate) listened_addrs: RwLock<Vec<Multiaddr>>,
pub(crate) listened_addrs: RwLock<NetworkAddresses>,
dialing_addrs: RwLock<HashMap<PeerId, Instant>>,
/// Node public addresses,
/// includes manually public addrs and remote peer observed addrs
Expand Down
3 changes: 2 additions & 1 deletion network/src/peer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::address::NetworkAddresses;
use crate::network_group::Group;
use crate::{
multiaddr::Multiaddr, protocols::identify::Flags, ProtocolId, ProtocolVersion, SessionType,
Expand All @@ -21,7 +22,7 @@ pub struct Peer {
/// Peer address
pub connected_addr: Multiaddr,
/// Peer listen addresses
pub listened_addrs: Vec<Multiaddr>,
pub listened_addrs: NetworkAddresses,
/// Peer info from identify protocol message
pub identify_info: Option<PeerIdentifyInfo>,
/// Ping/Pong message last received time
Expand Down

0 comments on commit e2df0ef

Please sign in to comment.