From 7aa7d82685de70aad4dd1a3199131b74c9c1d025 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 11:04:02 +0800 Subject: [PATCH 01/10] feat(net/peer): implement GetPeerXXX in manager Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 48 +++++++++++++++++++++++---- crates/net/network/src/peers.rs | 3 +- crates/net/network/src/session/mod.rs | 34 +++---------------- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index b3fa43252ec1..c91c19c28f8d 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -42,7 +42,7 @@ use reth_eth_wire::{ DisconnectReason, EthVersion, Status, }; use reth_metrics::common::mpsc::UnboundedMeteredSender; -use reth_network_api::{EthProtocolInfo, NetworkStatus, ReputationChangeKind}; +use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo, ReputationChangeKind}; use reth_network_peers::{NodeRecord, PeerId}; use reth_primitives::ForkId; use reth_provider::{BlockNumReader, BlockReader}; @@ -604,17 +604,17 @@ where } } NetworkHandleMessage::GetPeerInfos(tx) => { - let _ = tx.send(self.swarm.sessions_mut().get_peer_info()); + let _ = tx.send(self.get_peer_infos()); } NetworkHandleMessage::GetPeerInfoById(peer_id, tx) => { - let _ = tx.send(self.swarm.sessions_mut().get_peer_info_by_id(peer_id)); + let _ = tx.send(self.get_peer_info_by_id(peer_id)); } NetworkHandleMessage::GetPeerInfosByIds(peer_ids, tx) => { - let _ = tx.send(self.swarm.sessions().get_peer_infos_by_ids(peer_ids)); + let _ = tx.send(self.get_peer_infos_by_ids(peer_ids)); } NetworkHandleMessage::GetPeerInfosByPeerKind(kind, tx) => { - let peers = self.swarm.state().peers().peers_by_kind(kind); - let _ = tx.send(self.swarm.sessions().get_peer_infos_by_ids(peers)); + let peer_ids = self.swarm.state().peers().peers_by_kind(kind); + let _ = tx.send(self.get_peer_infos_by_ids(peer_ids)); } NetworkHandleMessage::AddRlpxSubProtocol(proto) => self.add_rlpx_sub_protocol(proto), NetworkHandleMessage::GetTransactionsHandle(tx) => { @@ -865,6 +865,42 @@ where } } + /// Returns [`PeerInfo`] for all connected peers + fn get_peer_infos(&self) -> Vec { + let peer_manager = self.swarm.state().peers(); + let mut peers = Vec::with_capacity(peer_manager.num_known_peers()); + for (peer_id, session) in self.swarm.sessions().active_sessions() { + if let Some(_nr) = peer_manager.peer_by_id(*peer_id) { + peers.push(session.peer_info()); + } + } + peers + } + + /// Returns [`PeerInfo`] for a given peer. + /// + /// Returns `None` if there's no active session to the peer. + fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { + if let Some(session) = self.swarm.sessions().active_sessions().get(&peer_id) { + self.swarm.state().peers().peer_by_id(peer_id).map(|_nr| session.peer_info()) + } else { + None + } + } + + /// Returns [`PeerInfo`] for a given peers. + /// + /// Ignore the non-active peer. + fn get_peer_infos_by_ids(&self, peer_ids: impl IntoIterator) -> Vec { + let mut infos = Vec::new(); + for peer_id in peer_ids { + if let Some(info) = self.get_peer_info_by_id(peer_id) { + infos.push(info) + } + } + infos + } + /// Updates the metrics for active,established connections #[inline] fn update_active_connection_metrics(&self) { diff --git a/crates/net/network/src/peers.rs b/crates/net/network/src/peers.rs index ca80faf5c1bb..c10f3406bdf7 100644 --- a/crates/net/network/src/peers.rs +++ b/crates/net/network/src/peers.rs @@ -213,8 +213,7 @@ impl PeersManager { } /// Returns the [`NodeRecord`] for the given peer id - #[allow(dead_code)] - fn peer_by_id(&self, peer_id: PeerId) -> Option { + pub(crate) fn peer_by_id(&self, peer_id: PeerId) -> Option { self.peers.get(&peer_id).map(|v| { NodeRecord::new_with_ports( v.addr.tcp.ip(), diff --git a/crates/net/network/src/session/mod.rs b/crates/net/network/src/session/mod.rs index 715ed59cf635..fc6f0e6a1f1e 100644 --- a/crates/net/network/src/session/mod.rs +++ b/crates/net/network/src/session/mod.rs @@ -170,6 +170,11 @@ impl SessionManager { self.secret_key } + /// Returns a borrowed reference to the active sessions. + pub const fn active_sessions(&self) -> &HashMap { + &self.active_sessions + } + /// Returns the session hello message. pub fn hello_message(&self) -> HelloMessageWithProtocols { self.hello_message.clone() @@ -587,35 +592,6 @@ impl SessionManager { } } } - - /// Returns [`PeerInfo`] for all connected peers - pub(crate) fn get_peer_info(&self) -> Vec { - self.active_sessions.values().map(ActiveSessionHandle::peer_info).collect() - } - - /// Returns [`PeerInfo`] for a given peer. - /// - /// Returns `None` if there's no active session to the peer. - pub(crate) fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { - self.active_sessions.get(&peer_id).map(ActiveSessionHandle::peer_info) - } - /// Returns [`PeerInfo`] for a given peer. - /// - /// Returns `None` if there's no active session to the peer. - pub(crate) fn get_peer_infos_by_ids( - &self, - peer_ids: impl IntoIterator, - ) -> Vec { - let mut infos = Vec::new(); - for peer_id in peer_ids { - if let Some(info) = - self.active_sessions.get(&peer_id).map(ActiveSessionHandle::peer_info) - { - infos.push(info); - } - } - infos - } } /// Events produced by the [`SessionManager`] From 8342d83641b18cbe4bbb4e0439cbc62d5c7868ab Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 11:13:25 +0800 Subject: [PATCH 02/10] feat(net/peer): PeerInfo add tcp,udp address from NodeRecord Signed-off-by: jsvisa --- crates/net/network-api/src/lib.rs | 6 ++++-- crates/net/network/src/manager.rs | 6 +++--- crates/net/network/src/session/handle.rs | 7 ++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index e9cce0866fde..07cc25eca568 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -195,8 +195,10 @@ pub struct PeerInfo { pub remote_id: PeerId, /// The client's name and version pub client_version: Arc, - /// The peer's address we're connected to - pub remote_addr: SocketAddr, + /// The peer's TCP address we're connected to + pub tcp_addr: SocketAddr, + /// The peer's UDP address used as discovery + pub udp_addr: SocketAddr, /// The local address of the connection pub local_addr: Option, /// The direction of the session diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index c91c19c28f8d..5530cc0b998a 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -870,8 +870,8 @@ where let peer_manager = self.swarm.state().peers(); let mut peers = Vec::with_capacity(peer_manager.num_known_peers()); for (peer_id, session) in self.swarm.sessions().active_sessions() { - if let Some(_nr) = peer_manager.peer_by_id(*peer_id) { - peers.push(session.peer_info()); + if let Some(record) = peer_manager.peer_by_id(*peer_id) { + peers.push(session.peer_info(&record)); } } peers @@ -882,7 +882,7 @@ where /// Returns `None` if there's no active session to the peer. fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { if let Some(session) = self.swarm.sessions().active_sessions().get(&peer_id) { - self.swarm.state().peers().peer_by_id(peer_id).map(|_nr| session.peer_info()) + self.swarm.state().peers().peer_by_id(peer_id).map(|record| session.peer_info(&record)) } else { None } diff --git a/crates/net/network/src/session/handle.rs b/crates/net/network/src/session/handle.rs index b28b1e27e390..94ba13b1f677 100644 --- a/crates/net/network/src/session/handle.rs +++ b/crates/net/network/src/session/handle.rs @@ -12,7 +12,7 @@ use reth_eth_wire::{ DisconnectReason, EthVersion, Status, }; use reth_network_api::PeerInfo; -use reth_network_peers::PeerId; +use reth_network_peers::{NodeRecord, PeerId}; use std::{io, net::SocketAddr, sync::Arc, time::Instant}; use tokio::sync::{ mpsc::{self, error::SendError}, @@ -136,11 +136,12 @@ impl ActiveSessionHandle { } /// Extracts the [`PeerInfo`] from the session handle. - pub(crate) fn peer_info(&self) -> PeerInfo { + pub(crate) fn peer_info(&self, record: &NodeRecord) -> PeerInfo { PeerInfo { remote_id: self.remote_id, direction: self.direction, - remote_addr: self.remote_addr, + tcp_addr: record.tcp_addr(), + udp_addr: record.udp_addr(), local_addr: self.local_addr, capabilities: self.capabilities.clone(), client_version: self.client_version.clone(), From f0430d71fd8f0e685bdc8f4cfd862c7e8dfd7218 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 14:39:39 +0800 Subject: [PATCH 03/10] feat(net/admin): implement admin_peers Signed-off-by: jsvisa --- crates/net/network-api/src/lib.rs | 10 ++-- crates/net/network/src/session/handle.rs | 5 +- crates/rpc/rpc-api/src/admin.rs | 2 +- crates/rpc/rpc/src/admin.rs | 66 ++++++++++++++---------- 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index 07cc25eca568..5b684fff5179 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -195,10 +195,12 @@ pub struct PeerInfo { pub remote_id: PeerId, /// The client's name and version pub client_version: Arc, - /// The peer's TCP address we're connected to - pub tcp_addr: SocketAddr, - /// The peer's UDP address used as discovery - pub udp_addr: SocketAddr, + /// The peer's enode + pub enode: String, + /// The peer's enr + pub enr: Option, + /// The peer's address we're connected to + pub remote_addr: SocketAddr, /// The local address of the connection pub local_addr: Option, /// The direction of the session diff --git a/crates/net/network/src/session/handle.rs b/crates/net/network/src/session/handle.rs index 94ba13b1f677..d7f1fbca4b0c 100644 --- a/crates/net/network/src/session/handle.rs +++ b/crates/net/network/src/session/handle.rs @@ -140,8 +140,9 @@ impl ActiveSessionHandle { PeerInfo { remote_id: self.remote_id, direction: self.direction, - tcp_addr: record.tcp_addr(), - udp_addr: record.udp_addr(), + enode: record.to_string(), + enr: None, + remote_addr: self.remote_addr, local_addr: self.local_addr, capabilities: self.capabilities.clone(), client_version: self.client_version.clone(), diff --git a/crates/rpc/rpc-api/src/admin.rs b/crates/rpc/rpc-api/src/admin.rs index 173cd8ef7a98..66f8918a33cb 100644 --- a/crates/rpc/rpc-api/src/admin.rs +++ b/crates/rpc/rpc-api/src/admin.rs @@ -1,6 +1,6 @@ use jsonrpsee::{core::RpcResult, proc_macros::rpc}; use reth_network_peers::{AnyNode, NodeRecord}; -use reth_rpc_types::{admin::NodeInfo, PeerInfo}; +use reth_rpc_types::admin::{NodeInfo, PeerInfo}; /// Admin namespace rpc interface that gives access to several non-standard RPC methods. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "admin"))] diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index f294a52c1bf3..bfb302533a9b 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -6,12 +6,12 @@ use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::ChainSpec; use reth_network_api::{NetworkInfo, PeerKind, Peers}; -use reth_network_peers::{AnyNode, NodeRecord}; +use reth_network_peers::{id2pk, AnyNode, NodeRecord}; use reth_rpc_api::AdminApiServer; use reth_rpc_server_types::ToRpcResult; -use reth_rpc_types::{ - admin::{EthProtocolInfo, NodeInfo, Ports, ProtocolInfo}, - PeerEthProtocolInfo, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo, +use reth_rpc_types::admin::{ + EthInfo, EthPeerInfo, EthProtocolInfo, NodeInfo, PeerInfo, PeerNetworkInfo, PeerProtocolInfo, + Ports, ProtocolInfo, }; /// `admin` API implementation. @@ -63,33 +63,43 @@ where Ok(true) } + /// Handler for `admin_peers` async fn peers(&self) -> RpcResult> { let peers = self.network.get_all_peers().await.to_rpc_result()?; - let peers = peers - .into_iter() - .map(|peer| PeerInfo { - id: Some(peer.remote_id.to_string()), - name: peer.client_version.to_string(), - caps: peer.capabilities.capabilities().iter().map(|cap| cap.to_string()).collect(), - network: PeerNetworkInfo { - remote_address: peer.remote_addr.to_string(), - local_address: peer - .local_addr - .unwrap_or_else(|| self.network.local_addr()) - .to_string(), - }, - protocols: PeerProtocolsInfo { - eth: Some(PeerEthProtocolInfo { - difficulty: Some(peer.status.total_difficulty), - head: peer.status.blockhash.to_string(), - version: peer.status.version as u32, - }), - pip: None, - }, - }) - .collect(); + let mut infos = Vec::with_capacity(peers.len()); - Ok(peers) + for peer in peers.into_iter() { + if let Ok(pk) = id2pk(peer.remote_id) { + infos.push(PeerInfo { + id: pk.to_string(), + name: peer.client_version.to_string(), + enode: peer.enode, + enr: peer.enr, + caps: peer + .capabilities + .capabilities() + .iter() + .map(|cap| cap.to_string()) + .collect(), + network: PeerNetworkInfo { + remote_address: peer.remote_addr, + local_address: peer.local_addr.unwrap_or_else(|| self.network.local_addr()), + inbound: peer.direction.is_incoming(), + trusted: false, // TODO: check + static_node: false, // TODO: check + }, + protocols: PeerProtocolInfo { + eth: Some(EthPeerInfo::Info(EthInfo { + version: peer.status.version as u64, + })), + snap: None, + other: Default::default(), + }, + }) + } + } + + Ok(infos) } /// Handler for `admin_nodeInfo` From b127a22ffe3e370ddcb0e74f1886d08e68ae071b Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 15:53:23 +0800 Subject: [PATCH 04/10] feat(net/peer): return peer kind Signed-off-by: jsvisa --- crates/net/network-api/src/lib.rs | 2 ++ crates/net/network/src/manager.rs | 10 +++++++--- crates/net/network/src/peers.rs | 21 ++++++++++++--------- crates/net/network/src/session/handle.rs | 5 +++-- crates/rpc/rpc/src/admin.rs | 4 ++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index 5b684fff5179..8efaec5f0fb7 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -211,6 +211,8 @@ pub struct PeerInfo { pub status: Arc, /// The timestamp when the session to that peer has been established. pub session_established: Instant, + /// The peer's connection kind + pub kind: PeerKind, } /// The direction of the connection. diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 5530cc0b998a..71b39da2849d 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -870,8 +870,8 @@ where let peer_manager = self.swarm.state().peers(); let mut peers = Vec::with_capacity(peer_manager.num_known_peers()); for (peer_id, session) in self.swarm.sessions().active_sessions() { - if let Some(record) = peer_manager.peer_by_id(*peer_id) { - peers.push(session.peer_info(&record)); + if let Some((record, kind)) = peer_manager.peer_by_id(*peer_id) { + peers.push(session.peer_info(&record, kind)); } } peers @@ -882,7 +882,11 @@ where /// Returns `None` if there's no active session to the peer. fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { if let Some(session) = self.swarm.sessions().active_sessions().get(&peer_id) { - self.swarm.state().peers().peer_by_id(peer_id).map(|record| session.peer_info(&record)) + self.swarm + .state() + .peers() + .peer_by_id(peer_id) + .map(|(record, kind)| session.peer_info(&record, kind)) } else { None } diff --git a/crates/net/network/src/peers.rs b/crates/net/network/src/peers.rs index c10f3406bdf7..41950226282c 100644 --- a/crates/net/network/src/peers.rs +++ b/crates/net/network/src/peers.rs @@ -212,14 +212,17 @@ impl PeersManager { }) } - /// Returns the [`NodeRecord`] for the given peer id - pub(crate) fn peer_by_id(&self, peer_id: PeerId) -> Option { + /// Returns the `NodeRecord` and `PeerKind` for the given peer id + pub(crate) fn peer_by_id(&self, peer_id: PeerId) -> Option<(NodeRecord, PeerKind)> { self.peers.get(&peer_id).map(|v| { - NodeRecord::new_with_ports( - v.addr.tcp.ip(), - v.addr.tcp.port(), - v.addr.udp.map(|addr| addr.port()), - peer_id, + ( + NodeRecord::new_with_ports( + v.addr.tcp.ip(), + v.addr.tcp.port(), + v.addr.udp.map(|addr| addr.port()), + peer_id, + ), + v.kind, ) }) } @@ -1377,7 +1380,7 @@ mod tests { _ => unreachable!(), } - let record = peers.peer_by_id(peer).unwrap(); + let (record, _) = peers.peer_by_id(peer).unwrap(); assert_eq!(record.tcp_addr(), socket_addr); assert_eq!(record.udp_addr(), socket_addr); } @@ -1404,7 +1407,7 @@ mod tests { _ => unreachable!(), } - let record = peers.peer_by_id(peer).unwrap(); + let (record, _) = peers.peer_by_id(peer).unwrap(); assert_eq!(record.tcp_addr(), tcp_addr); assert_eq!(record.udp_addr(), udp_addr); } diff --git a/crates/net/network/src/session/handle.rs b/crates/net/network/src/session/handle.rs index d7f1fbca4b0c..4c1a5e5315ac 100644 --- a/crates/net/network/src/session/handle.rs +++ b/crates/net/network/src/session/handle.rs @@ -11,7 +11,7 @@ use reth_eth_wire::{ errors::EthStreamError, DisconnectReason, EthVersion, Status, }; -use reth_network_api::PeerInfo; +use reth_network_api::{PeerInfo, PeerKind}; use reth_network_peers::{NodeRecord, PeerId}; use std::{io, net::SocketAddr, sync::Arc, time::Instant}; use tokio::sync::{ @@ -136,7 +136,7 @@ impl ActiveSessionHandle { } /// Extracts the [`PeerInfo`] from the session handle. - pub(crate) fn peer_info(&self, record: &NodeRecord) -> PeerInfo { + pub(crate) fn peer_info(&self, record: &NodeRecord, kind: PeerKind) -> PeerInfo { PeerInfo { remote_id: self.remote_id, direction: self.direction, @@ -149,6 +149,7 @@ impl ActiveSessionHandle { eth_version: self.version, status: self.status.clone(), session_established: self.established, + kind, } } } diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index bfb302533a9b..de4496874868 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -85,8 +85,8 @@ where remote_address: peer.remote_addr, local_address: peer.local_addr.unwrap_or_else(|| self.network.local_addr()), inbound: peer.direction.is_incoming(), - trusted: false, // TODO: check - static_node: false, // TODO: check + trusted: peer.kind.is_trusted(), + static_node: false, // TODO: add static kind }, protocols: PeerProtocolInfo { eth: Some(EthPeerInfo::Info(EthInfo { From c3a1386c931bda1d3f96c2f7bc0d68eb23ba32d7 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 15:58:11 +0800 Subject: [PATCH 05/10] feat: more fp Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 71b39da2849d..0c8ce4d7a632 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -881,15 +881,18 @@ where /// /// Returns `None` if there's no active session to the peer. fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { - if let Some(session) = self.swarm.sessions().active_sessions().get(&peer_id) { - self.swarm - .state() - .peers() - .peer_by_id(peer_id) - .map(|(record, kind)| session.peer_info(&record, kind)) - } else { - None - } + self.swarm + .sessions() + .active_sessions() + .get(&peer_id) + .map(|session| { + self.swarm + .state() + .peers() + .peer_by_id(peer_id) + .map(|(record, kind)| session.peer_info(&record, kind)) + }) + .flatten() } /// Returns [`PeerInfo`] for a given peers. From ef7d9e89bca8bf1fbfbef4da12fbd790d2566c05 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sun, 30 Jun 2024 16:02:35 +0800 Subject: [PATCH 06/10] apply lint Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 19 +++++++------------ crates/rpc/rpc/src/admin.rs | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 0c8ce4d7a632..a3f0f92f0a31 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -881,18 +881,13 @@ where /// /// Returns `None` if there's no active session to the peer. fn get_peer_info_by_id(&self, peer_id: PeerId) -> Option { - self.swarm - .sessions() - .active_sessions() - .get(&peer_id) - .map(|session| { - self.swarm - .state() - .peers() - .peer_by_id(peer_id) - .map(|(record, kind)| session.peer_info(&record, kind)) - }) - .flatten() + self.swarm.sessions().active_sessions().get(&peer_id).and_then(|session| { + self.swarm + .state() + .peers() + .peer_by_id(peer_id) + .map(|(record, kind)| session.peer_info(&record, kind)) + }) } /// Returns [`PeerInfo`] for a given peers. diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index de4496874868..eccbd7ecffae 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -68,7 +68,7 @@ where let peers = self.network.get_all_peers().await.to_rpc_result()?; let mut infos = Vec::with_capacity(peers.len()); - for peer in peers.into_iter() { + for peer in peers { if let Ok(pk) = id2pk(peer.remote_id) { infos.push(PeerInfo { id: pk.to_string(), From 98340af6caed1a3b847d864125bd460999e6d83b Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 1 Jul 2024 22:29:29 +0800 Subject: [PATCH 07/10] use filter_map+collect instead of if let push Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index a3f0f92f0a31..38674d7af811 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -894,13 +894,7 @@ where /// /// Ignore the non-active peer. fn get_peer_infos_by_ids(&self, peer_ids: impl IntoIterator) -> Vec { - let mut infos = Vec::new(); - for peer_id in peer_ids { - if let Some(info) = self.get_peer_info_by_id(peer_id) { - infos.push(info) - } - } - infos + peer_ids.into_iter().filter_map(|peer_id| self.get_peer_info_by_id(peer_id)).collect() } /// Updates the metrics for active,established connections From ad96373ac6c73f6d06aad7df8514ce7d1ee1e0d8 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 1 Jul 2024 22:30:32 +0800 Subject: [PATCH 08/10] feat(rpc/admin): peer.kind.is_static Signed-off-by: jsvisa --- crates/rpc/rpc/src/admin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/rpc/src/admin.rs b/crates/rpc/rpc/src/admin.rs index eccbd7ecffae..1d59baa6e274 100644 --- a/crates/rpc/rpc/src/admin.rs +++ b/crates/rpc/rpc/src/admin.rs @@ -86,7 +86,7 @@ where local_address: peer.local_addr.unwrap_or_else(|| self.network.local_addr()), inbound: peer.direction.is_incoming(), trusted: peer.kind.is_trusted(), - static_node: false, // TODO: add static kind + static_node: peer.kind.is_static(), }, protocols: PeerProtocolInfo { eth: Some(EthPeerInfo::Info(EthInfo { From 143427d7e3b2b9e5e0cd55a8d0580df68bc7961c Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 2 Jul 2024 00:48:38 +0800 Subject: [PATCH 09/10] more fp Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 38674d7af811..7244a364af82 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -867,14 +867,18 @@ where /// Returns [`PeerInfo`] for all connected peers fn get_peer_infos(&self) -> Vec { - let peer_manager = self.swarm.state().peers(); - let mut peers = Vec::with_capacity(peer_manager.num_known_peers()); - for (peer_id, session) in self.swarm.sessions().active_sessions() { - if let Some((record, kind)) = peer_manager.peer_by_id(*peer_id) { - peers.push(session.peer_info(&record, kind)); - } - } - peers + self.swarm + .sessions() + .active_sessions() + .into_iter() + .filter_map(|(&peer_id, session)| { + self.swarm + .state() + .peers() + .peer_by_id(peer_id) + .map(|(record, kind)| session.peer_info(&record, kind)) + }) + .collect() } /// Returns [`PeerInfo`] for a given peer. From e298bec4152fa5c6f44cf345a3d89761804c4768 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 2 Jul 2024 00:53:06 +0800 Subject: [PATCH 10/10] clippy Signed-off-by: jsvisa --- crates/net/network/src/manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 7244a364af82..3434e9439468 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -870,7 +870,7 @@ where self.swarm .sessions() .active_sessions() - .into_iter() + .iter() .filter_map(|(&peer_id, session)| { self.swarm .state()