-
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
Conversation
📝 WalkthroughWalkthroughReturned peer-height handling was refactored: network trait/manager now return Changes
Sequence Diagram(s)sequenceDiagram
participant Sync as SyncCoordinator
participant Net as NetworkManager
participant Pool as PeerPool
participant Peer as Peer (many)
Sync->>Net: get_peer_best_height().await
Net->>Pool: get_best_height().await
Pool->>Peer: get_all_peers() / iterate
loop per peer
Pool->>Peer: read best_height / version
Peer-->>Pool: best_height (Option<CoreBlockHeight>)
end
Pool-->>Net: Some(max_height) / None
Net-->>Sync: Some(max_height) / None
Sync->>Sync: use unwrap_or(current_height) for progress
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@dash-spv/src/test_utils/network.rs`:
- Around line 158-160: The mock get_peer_best_height currently falls back to
self.headers_chain.len() and thus returns a height even when no peer is
connected; change get_peer_best_height to return None when self.peer_best_height
is None (treat None as "no peers/disconnected") and only return Some(height)
when peer_best_height is Some(value) (do not use headers_chain as a fallback).
Update the logic in get_peer_best_height to check self.peer_best_height and
return Some(h) or None accordingly, referencing the get_peer_best_height
function, the peer_best_height field, and headers_chain.
🧹 Nitpick comments (1)
dash-spv/src/network/pool.rs (1)
103-149: Consider skipping disconnected peers to avoid stale heights.
If a peer lingers in the pool after disconnect, its cached height can skew the aggregate.♻️ Proposed tweak
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 !peer_guard.is_connected() { + continue; + } if let Some(peer_height) = peer_guard.best_height() {
c261e6c to
14f5672
Compare
|
|
||
| pub async fn get_best_height(&self) -> Option<CoreBlockHeight> { | ||
| let peers = self.get_all_peers().await; | ||
|
|
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.
If you want to clean this function code in this PR I suggest a few things:
peer_countis redundant, we have the collection and can query its len (i hope)- I dont see the fi as somethign we need when updating best_height, suggested code would be:
if let Some(peer_height) = peer_guard.best_height() {
best_height = best_height.max(peer_height);
}- Why return option, in case there is nor best_height we can return the 0, its a valid value since there is always a genesius block, in fact, if a network is new and only has genesis this method would report None when Some(0) is actually correct, and, in the worst scenario, genesis is hardcoded in the lib, By returning always CoreBlockHeight we reduce nesting and branches when testing edge cases
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.
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.
ZocoLini
left a comment
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.
Appoving, the suggestions in the function body are not blocking tbh
Move the logic into
PeerPool, just makes more sense imo.Summary by CodeRabbit
Refactor
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.