Skip to content

Commit 587e905

Browse files
committed
Use NetworkAddresses to represent regular_addrs and onion_addrs
Signed-off-by: Eval EXEC <[email protected]>
1 parent dd17bde commit 587e905

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

network/src/address.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use p2p::multiaddr::{MultiAddr, Protocol};
2+
3+
pub struct NetworkAddresses {
4+
pub regular_addresses: Vec<MultiAddr>,
5+
6+
// onion addresses can't be solved by multiaddr_to_socketaddr or socketaddr_to_multiaddr
7+
pub onion_addresses: Vec<MultiAddr>,
8+
}
9+
10+
// convert Vec<MultiAddr> to NetworkAddresses
11+
impl From<Vec<MultiAddr>> for NetworkAddresses {
12+
fn from(addresses: Vec<MultiAddr>) -> Self {
13+
let mut regular_addresses = Vec::new();
14+
let mut onion_addresses = Vec::new();
15+
for address in addresses {
16+
if address
17+
.iter()
18+
.any(|proto| matches!(proto, Protocol::Onion(_)))
19+
{
20+
onion_addresses.push(address);
21+
} else {
22+
regular_addresses.push(address);
23+
}
24+
}
25+
NetworkAddresses {
26+
regular_addresses,
27+
onion_addresses,
28+
}
29+
}
30+
}
31+
32+
// convert NetworkAddresses to Vec<MultiAddr>
33+
impl From<NetworkAddresses> for Vec<MultiAddr> {
34+
fn from(addresses: NetworkAddresses) -> Self {
35+
let mut result = addresses.regular_addresses;
36+
result.extend(addresses.onion_addresses);
37+
result
38+
}
39+
}

network/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! And implemented several basic protocols: identify, discovery, ping, feeler, disconnect_message
77
//!
88
9+
mod address;
910
mod behaviour;
1011
/// compress module
1112
pub mod compress;

network/src/network.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Global state struct and start function
2+
use crate::address::NetworkAddresses;
23
use crate::errors::Error;
34
#[cfg(not(target_family = "wasm"))]
45
use crate::errors::P2PError;
@@ -74,7 +75,7 @@ pub struct NetworkState {
7475
pub(crate) peer_registry: RwLock<PeerRegistry>,
7576
pub(crate) peer_store: Mutex<PeerStore>,
7677
/// Node listened addresses
77-
pub(crate) listened_addrs: RwLock<Vec<Multiaddr>>,
78+
pub(crate) listened_addrs: RwLock<NetworkAddresses>,
7879
dialing_addrs: RwLock<HashMap<PeerId, Instant>>,
7980
/// Node public addresses,
8081
/// includes manually public addrs and remote peer observed addrs

network/src/peer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::address::NetworkAddresses;
12
use crate::network_group::Group;
23
use crate::{
34
multiaddr::Multiaddr, protocols::identify::Flags, ProtocolId, ProtocolVersion, SessionType,
@@ -21,7 +22,7 @@ pub struct Peer {
2122
/// Peer address
2223
pub connected_addr: Multiaddr,
2324
/// Peer listen addresses
24-
pub listened_addrs: Vec<Multiaddr>,
25+
pub listened_addrs: NetworkAddresses,
2526
/// Peer info from identify protocol message
2627
pub identify_info: Option<PeerIdentifyInfo>,
2728
/// Ping/Pong message last received time

0 commit comments

Comments
 (0)