Skip to content

Commit

Permalink
let ckb-network and ckb-rpc use NetworkAddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Dec 31, 2024
1 parent e2df0ef commit 865759d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
21 changes: 13 additions & 8 deletions network/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use p2p::multiaddr::{MultiAddr, Protocol};

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

Expand All @@ -24,18 +24,23 @@ impl NetworkAddresses {
pub fn contains(&self, address: &MultiAddr) -> bool {
self.regular_addresses.contains(address) || self.onion_addresses.contains(address)
}

// len
pub fn len(&self) -> usize {
self.regular_addresses.len() + self.onion_addresses.len()
}
}

impl IntoIterator for NetworkAddresses {
type Item = MultiAddr;
type IntoIter = std::vec::IntoIter<MultiAddr>;
// implement iter() for NetworkAddresses, don't take ownership
impl<'a> IntoIterator for &'a NetworkAddresses {
type Item = &'a MultiAddr;
type IntoIter =
std::iter::Chain<std::slice::Iter<'a, MultiAddr>, std::slice::Iter<'a, MultiAddr>>;

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

Expand Down
5 changes: 3 additions & 2 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl NetworkState {
peer_registry: RwLock::new(peer_registry),
dialing_addrs: RwLock::new(HashMap::default()),
public_addrs: RwLock::new(public_addrs),
listened_addrs: RwLock::new(Vec::new()),
listened_addrs: RwLock::new(NetworkAddresses::default()),
pending_observed_addrs: RwLock::new(HashSet::default()),
local_private_key,
local_peer_id,
Expand Down Expand Up @@ -367,7 +367,7 @@ impl NetworkState {
None
}
})
.chain(listened_addrs.iter().map(|addr| (addr.to_owned(), 1)))
.chain(listened_addrs.into_iter().map(|addr| (addr.to_owned(), 1)))
.map(|(addr, score)| (addr.to_string(), score))
.collect()
}
Expand Down Expand Up @@ -1003,6 +1003,7 @@ impl NetworkService {
config
.proxy_config
.proxy_url
.clone()
.map(|proxy_url| ProxyConfig { proxy_url }),
);

Expand Down
2 changes: 1 addition & 1 deletion network/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Peer {
) -> Self {
Peer {
connected_addr,
listened_addrs: Vec::new(),
listened_addrs: NetworkAddresses::default(),
identify_info: None,
ping_rtt: None,
last_ping_protocol_message_received_at: None,
Expand Down
2 changes: 1 addition & 1 deletion network/src/protocols/identify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ impl Callback for IdentifyCallback {
);
let flags = self.network_state.with_peer_registry_mut(|reg| {
if let Some(peer) = reg.get_peer_mut(session.id) {
peer.listened_addrs = addrs.clone();
peer.listened_addrs = addrs.clone().into();
peer.identify_info
.as_ref()
.map(|a| a.flags)
Expand Down
9 changes: 7 additions & 2 deletions rpc/src/module/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use ckb_jsonrpc_types::{
BannedAddr, LocalNode, LocalNodeProtocol, NodeAddress, PeerSyncState, RemoteNode,
RemoteNodeProtocol, SyncState, Timestamp,
};
use ckb_network::{extract_peer_id, multiaddr::Multiaddr, NetworkController};
use ckb_network::{
extract_peer_id,
multiaddr::{MultiAddr, Multiaddr},
NetworkController,
};
use ckb_sync::SyncShared;
use ckb_systemtime::unix_time_as_millis;
use ckb_types::prelude::{Pack, Unpack};
Expand Down Expand Up @@ -587,7 +591,8 @@ impl NetRpc for NetRpcImpl {
.iter()
.map(|(peer_index, peer)| {
let mut addresses = vec![&peer.connected_addr];
addresses.extend(peer.listened_addrs.iter());
let listened_addrs: Vec<MultiAddr> = peer.listened_addrs.clone().into();
addresses.extend(listened_addrs.iter());

let node_addresses = addresses
.iter()
Expand Down

0 comments on commit 865759d

Please sign in to comment.