From 865759d423d6a13f5f5a7b65f6961ecdc4209629 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Tue, 31 Dec 2024 16:49:29 +0800 Subject: [PATCH] let `ckb-network` and `ckb-rpc` use `NetworkAddresses` --- network/src/address.rs | 21 +++++++++++++-------- network/src/network.rs | 5 +++-- network/src/peer.rs | 2 +- network/src/protocols/identify/mod.rs | 2 +- rpc/src/module/net.rs | 9 +++++++-- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/network/src/address.rs b/network/src/address.rs index a4dc0f226f..f921b8d589 100644 --- a/network/src/address.rs +++ b/network/src/address.rs @@ -1,6 +1,6 @@ use p2p::multiaddr::{MultiAddr, Protocol}; -#[derive(Default)] +#[derive(Default, Clone, Debug)] pub struct NetworkAddresses { pub regular_addresses: Vec, @@ -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; +// 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>>; fn into_iter(self) -> Self::IntoIter { self.regular_addresses - .into_iter() - .chain(self.onion_addresses.into_iter()) - .collect::>() - .into_iter() + .iter() + .chain(self.onion_addresses.iter()) } } diff --git a/network/src/network.rs b/network/src/network.rs index a5cd199926..29e05a7d44 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -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, @@ -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() } @@ -1003,6 +1003,7 @@ impl NetworkService { config .proxy_config .proxy_url + .clone() .map(|proxy_url| ProxyConfig { proxy_url }), ); diff --git a/network/src/peer.rs b/network/src/peer.rs index 3c649c7ef3..f6235b5dd4 100644 --- a/network/src/peer.rs +++ b/network/src/peer.rs @@ -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, diff --git a/network/src/protocols/identify/mod.rs b/network/src/protocols/identify/mod.rs index e534dda78f..f3f4bc42cb 100644 --- a/network/src/protocols/identify/mod.rs +++ b/network/src/protocols/identify/mod.rs @@ -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) diff --git a/rpc/src/module/net.rs b/rpc/src/module/net.rs index de75699e68..d0928678fb 100644 --- a/rpc/src/module/net.rs +++ b/rpc/src/module/net.rs @@ -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}; @@ -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 = peer.listened_addrs.clone().into(); + addresses.extend(listened_addrs.iter()); let node_addresses = addresses .iter()