-
Notifications
You must be signed in to change notification settings - Fork 9
refactor: move get_peer_best_height into PeerPool
#392
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
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 |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| //! Peer pool for managing multiple peer connections | ||
|
|
||
| use crate::error::{NetworkError, SpvError as Error}; | ||
| use crate::network::constants::{MAX_PEERS, MIN_PEERS}; | ||
| use crate::network::peer::Peer; | ||
| use dashcore::prelude::CoreBlockHeight; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::net::SocketAddr; | ||
| use std::sync::Arc; | ||
| use tokio::sync::RwLock; | ||
|
|
||
| use crate::error::{NetworkError, SpvError as Error}; | ||
| use crate::network::constants::{MAX_PEERS, MIN_PEERS}; | ||
| use crate::network::peer::Peer; | ||
|
|
||
| /// Pool for managing multiple peer instances | ||
| pub struct PeerPool { | ||
| /// Active peers mapped by address | ||
|
|
@@ -100,6 +100,54 @@ impl PeerPool { | |
| self.peers.read().await.keys().copied().collect() | ||
| } | ||
|
|
||
| pub async fn get_best_height(&self) -> Option<CoreBlockHeight> { | ||
| let peers = self.get_all_peers().await; | ||
|
|
||
|
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. If you want to clean this function code in this PR I suggest a few things:
if let Some(peer_height) = peer_guard.best_height() {
best_height = best_height.max(peer_height);
}
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. Surely there logic can be improved but im just moving the method around here to avoid some extra housekeeping in for the peer best height in the sync rewrite PR. The optional return might make sense i was also thinking about it but something made me not do it, dont quite remember. |
||
| if peers.is_empty() { | ||
| log::debug!("get_peer_best_height: No peers available"); | ||
| return None; | ||
| } | ||
|
|
||
| let mut best_height = 0u32; | ||
| let mut peer_count = 0; | ||
|
|
||
| for (addr, peer) in peers.iter() { | ||
| let peer_guard = peer.read().await; | ||
| peer_count += 1; | ||
|
|
||
| log::debug!( | ||
| "get_peer_best_height: Peer {} - best_height: {:?}, version: {:?}, connected: {}", | ||
| addr, | ||
| peer_guard.best_height(), | ||
| peer_guard.version(), | ||
| peer_guard.is_connected(), | ||
| ); | ||
|
|
||
| if let Some(peer_height) = peer_guard.best_height() { | ||
| if peer_height > 0 { | ||
| best_height = best_height.max(peer_height); | ||
| log::debug!( | ||
| "get_peer_best_height: Updated best_height to {} from peer {}", | ||
| best_height, | ||
| addr | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| log::debug!( | ||
| "get_peer_best_height: Checked {} peers, best_height: {}", | ||
| peer_count, | ||
| best_height | ||
| ); | ||
|
|
||
| if best_height > 0 { | ||
| Some(best_height) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Check if we need more peers | ||
| pub async fn needs_more_peers(&self) -> bool { | ||
| self.peer_count().await < MIN_PEERS | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.