-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: extract capability lookup into PeerPool helpers
#509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v0.42-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| use crate::error::{NetworkError, SpvError as Error}; | ||
| use crate::network::constants::TARGET_PEERS; | ||
| use crate::network::peer::Peer; | ||
| use dashcore::network::constants::ServiceFlags; | ||
| use dashcore::prelude::CoreBlockHeight; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::net::SocketAddr; | ||
|
|
@@ -145,6 +146,35 @@ impl PeerPool { | |
| } | ||
| } | ||
|
|
||
| /// Find the first connected peer that advertises the given service flags. | ||
| pub(crate) async fn peer_with_service( | ||
| &self, | ||
| flags: ServiceFlags, | ||
| ) -> Option<(SocketAddr, Arc<RwLock<Peer>>)> { | ||
| let peers = self.peers.read().await; | ||
| for (addr, peer) in peers.iter() { | ||
| if peer.read().await.has_service(flags) { | ||
| return Some((*addr, Arc::clone(peer))); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// Collect all connected peers that advertise the given service flags. | ||
| pub(crate) async fn peers_with_service( | ||
| &self, | ||
| flags: ServiceFlags, | ||
| ) -> Vec<(SocketAddr, Arc<RwLock<Peer>>)> { | ||
| let peers = self.peers.read().await; | ||
| let mut result = Vec::new(); | ||
| for (addr, peer) in peers.iter() { | ||
| if peer.read().await.has_service(flags) { | ||
| result.push((*addr, peer.clone())); | ||
| } | ||
| } | ||
| result | ||
| } | ||
xdustinface marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Check if we need more peers | ||
| pub async fn needs_more_peers(&self) -> bool { | ||
| self.peer_count().await < TARGET_PEERS | ||
|
|
@@ -189,6 +219,15 @@ impl Default for PeerPool { | |
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl PeerPool { | ||
| async fn insert_peer_with_services(&self, addr: SocketAddr, flags: ServiceFlags) { | ||
| let mut peer = Peer::dummy(addr); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this into the tests module
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Its test only already and moving it into the actual tests module just adds another layer of indentation. I think it's cleaner to have a separation here with having a test impl block. |
||
| peer.set_services(flags); | ||
| self.peers.write().await.insert(addr, Arc::new(RwLock::new(peer))); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
@@ -208,4 +247,49 @@ mod tests { | |
| assert!(!pool.mark_connecting(addr).await); // Already marked | ||
| assert!(pool.is_connecting(&addr).await); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_service_lookup() { | ||
| let pool = PeerPool::new(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove one |
||
| let compact_filters = ServiceFlags::COMPACT_FILTERS; | ||
| let combined = compact_filters | ServiceFlags::NODE_HEADERS_COMPRESSED; | ||
|
|
||
| // No matches on empty pool | ||
| assert!(pool.peer_with_service(compact_filters).await.is_none()); | ||
| assert!(pool.peers_with_service(compact_filters).await.is_empty()); | ||
|
|
||
| // No matches when peers lack the requested flag | ||
| let addr1: SocketAddr = "127.0.0.1:1001".parse().unwrap(); | ||
| pool.insert_peer_with_services(addr1, ServiceFlags::NETWORK).await; | ||
| assert!(pool.peer_with_service(compact_filters).await.is_none()); | ||
| assert!(pool.peers_with_service(compact_filters).await.is_empty()); | ||
|
|
||
| // Single-flag lookup returns matching peers | ||
| let addr2: SocketAddr = "127.0.0.1:1002".parse().unwrap(); | ||
| let addr3: SocketAddr = "127.0.0.1:1003".parse().unwrap(); | ||
| pool.insert_peer_with_services(addr2, ServiceFlags::NETWORK | compact_filters).await; | ||
| pool.insert_peer_with_services(addr3, ServiceFlags::NETWORK | combined).await; | ||
|
|
||
| let (found_addr, found_peer) = pool.peer_with_service(compact_filters).await.unwrap(); | ||
| assert!(found_addr == addr2 || found_addr == addr3); | ||
| assert!(found_peer.read().await.has_service(compact_filters)); | ||
|
|
||
| let filter_peers: HashMap<SocketAddr, _> = | ||
| pool.peers_with_service(compact_filters).await.into_iter().collect(); | ||
| assert_eq!(filter_peers.len(), 2); | ||
| assert!(filter_peers.contains_key(&addr2)); | ||
| assert!(filter_peers.contains_key(&addr3)); | ||
|
|
||
| // Combined flags require all bits present | ||
| let (found_addr, _) = pool.peer_with_service(combined).await.unwrap(); | ||
| assert_eq!(found_addr, addr3); | ||
| let combined_peers = pool.peers_with_service(combined).await; | ||
| assert_eq!(combined_peers.len(), 1); | ||
| assert_eq!(combined_peers[0].0, addr3); | ||
|
|
||
| // NONE matches every peer in the pool | ||
| assert!(pool.peer_with_service(ServiceFlags::NONE).await.is_some()); | ||
| let all = pool.peers_with_service(ServiceFlags::NONE).await; | ||
| assert_eq!(all.len(), 3); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are touching this logic, couldn't we improve it by returning ServiceFlags::None, that way we remove the unnecessary Optional and if let branch. Feel free to do it if you want, if not I will take a look into ti
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some other PRs coming in this area.