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 65d29e2 commit 5b9bb62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
39 changes: 39 additions & 0 deletions network/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use p2p::multiaddr::{MultiAddr, Protocol};

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>,
}

// 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::Onion(_)))
{
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
}
}
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 5b9bb62

Please sign in to comment.