From a63cfa713824d151952a1a02fd1a95d5e4a5bd40 Mon Sep 17 00:00:00 2001 From: hashmap Date: Thu, 7 Feb 2019 21:26:23 +0100 Subject: [PATCH] Allow to have more than min_peers/2 outbound peers (#2537) * Allow to have more than min_peers/2 outbound peers Default value is 8, so we allow only 4 outbound peers, unfortunately if a node is behind NAT it can't get inbound peers and it stucks with 4 peers as maximum. This PR allows a number of outbound peers to be between min_peers and max_peers/2. Node behind NAT will eventually have just min_peers number of peers. * Extract into healthy_peers_mix function --- p2p/src/peers.rs | 8 +++++++- servers/src/grin/seed.rs | 7 ++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index 449ae355b..892622093 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -513,7 +513,13 @@ impl Peers { } pub fn enough_peers(&self) -> bool { - self.connected_peers().len() >= self.config.peer_min_preferred_count() as usize + self.peer_count() >= self.config.peer_min_preferred_count() + } + + /// We have enough peers, both total connected and outbound connected + pub fn healthy_peers_mix(&self) -> bool { + self.enough_peers() + && self.peer_outbound_count() >= self.config.peer_min_preferred_count() / 2 } /// Removes those peers that seem to have expired diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 5af7df96e..6561210fb 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -186,10 +186,7 @@ fn monitor_peers( // maintenance step first, clean up p2p server peers peers.clean_peers(config.peer_max_count() as usize); - // We have enough peers, both total connected and outbound connected so we are good. - if peers.peer_count() >= config.peer_min_preferred_count() - && peers.peer_outbound_count() >= config.peer_min_preferred_count() / 2 - { + if peers.healthy_peers_mix() { return; } @@ -318,7 +315,7 @@ fn listen_for_addrs( let addrs: Vec = rx.try_iter().collect(); // If we have a healthy number of outbound peers then we are done here. - if peers.peer_outbound_count() >= p2p.config.peer_min_preferred_count() / 2 { + if peers.healthy_peers_mix() { return; }