Skip to content

Commit 175be1e

Browse files
committed
fix: maintenance loop stops connecting at 1 peer
With `MIN_PEERS=1` used as a "should we connect more peers" check, we ended up not trying to connect more peers in the maintenance loop which leads to getting stuck with 1 peer only after initial peers are gone. Remove `MIN_PEERS` and `MAX_PEERS` and use `TARGET_PEERS` as the only peer count constant.
1 parent ab4aef8 commit 175be1e

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

dash-spv/src/network/constants.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
use std::time::Duration;
44

55
// Connection limits
6-
pub const MIN_PEERS: usize = 1;
76
pub const TARGET_PEERS: usize = 3;
8-
pub const MAX_PEERS: usize = 3;
9-
10-
// Compile-time check to ensure proper peer count relationships
11-
const _: () = assert!(MIN_PEERS <= TARGET_PEERS, "MIN_PEERS must be <= TARGET_PEERS");
12-
const _: () = assert!(TARGET_PEERS <= MAX_PEERS, "TARGET_PEERS must be <= MAX_PEERS");
137

148
// Timeouts
159
pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30);

dash-spv/src/network/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ impl PeerNetworkManager {
790790
}
791791
} else {
792792
// Normal mode: try to maintain minimum peer count with discovery
793-
if count < MIN_PEERS {
793+
if count < TARGET_PEERS {
794794
// Try known addresses first, sorted by reputation
795795
let known = this.addrv2_handler.get_known_addresses().await;
796796
let needed = TARGET_PEERS.saturating_sub(count);
@@ -851,7 +851,7 @@ impl PeerNetworkManager {
851851
}
852852
_ = dns_interval.tick(), if !this.exclusive_mode => {
853853
let count = this.pool.peer_count().await;
854-
if count >= MIN_PEERS {
854+
if count >= TARGET_PEERS {
855855
continue;
856856
}
857857
let dns_peers = tokio::select! {

dash-spv/src/network/pool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Peer pool for managing multiple peer connections
22
33
use crate::error::{NetworkError, SpvError as Error};
4-
use crate::network::constants::{MAX_PEERS, MIN_PEERS};
4+
use crate::network::constants::TARGET_PEERS;
55
use crate::network::peer::Peer;
66
use dashcore::prelude::CoreBlockHeight;
77
use std::collections::{HashMap, HashSet};
@@ -41,10 +41,10 @@ impl PeerPool {
4141
connecting.remove(&addr);
4242

4343
// Check if we're at capacity
44-
if peers.len() >= MAX_PEERS {
44+
if peers.len() >= TARGET_PEERS {
4545
return Err(Error::Network(NetworkError::ConnectionFailed(format!(
4646
"Maximum peers ({}) reached",
47-
MAX_PEERS
47+
TARGET_PEERS
4848
))));
4949
}
5050

@@ -151,12 +151,12 @@ impl PeerPool {
151151

152152
/// Check if we need more peers
153153
pub async fn needs_more_peers(&self) -> bool {
154-
self.peer_count().await < MIN_PEERS
154+
self.peer_count().await < TARGET_PEERS
155155
}
156156

157157
/// Check if we can accept more peers
158158
pub async fn can_accept_peers(&self) -> bool {
159-
self.peer_count().await < MAX_PEERS
159+
self.peer_count().await < TARGET_PEERS
160160
}
161161

162162
/// Clean up disconnected peers

dash-spv/src/network/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod pool_tests {
2828
// Test needs_more_peers logic
2929
assert!(pool.needs_more_peers().await);
3030

31-
// Can accept up to MAX_PEERS
31+
// Can accept up to TARGET_PEERS
3232
assert!(pool.can_accept_peers().await);
3333

3434
// Test peer count

dash-spv/tests/peer_test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use tokio::sync::RwLock;
88
use tokio::time;
99

1010
use dash_spv::client::{ClientConfig, DashSpvClient};
11+
use dash_spv::network::constants::TARGET_PEERS;
1112
use dash_spv::network::PeerNetworkManager;
1213
use dash_spv::storage::DiskStorageManager;
1314
use dash_spv::types::ValidationMode;
@@ -158,8 +159,6 @@ async fn test_peer_disconnection() {
158159

159160
#[tokio::test]
160161
async fn test_max_peer_limit() {
161-
use dash_spv::network::constants::MAX_PEERS;
162-
163162
let _ = env_logger::builder().is_test(true).try_init();
164163

165164
let mut config = create_test_config(Network::Testnet);
@@ -180,10 +179,10 @@ async fn test_max_peer_limit() {
180179
let _client =
181180
DashSpvClient::new(config, network_manager, storage_manager, wallet).await.unwrap();
182181

183-
// The client should never connect to more than MAX_PEERS
182+
// The client should never connect to more than TARGET_PEERS
184183
// This is enforced in the PeerPool
185-
println!("Maximum peer limit is set to: {}", MAX_PEERS);
186-
assert_eq!(MAX_PEERS, 3, "Default max peers should be 12");
184+
println!("Maximum peer limit is set to: {}", TARGET_PEERS);
185+
assert_eq!(TARGET_PEERS, 3, "Default max peers should be 12");
187186
}
188187

189188
#[cfg(test)]

0 commit comments

Comments
 (0)