From 0bb5ab82a95551fb6773a7233c306b1c07ef04ed Mon Sep 17 00:00:00 2001 From: pawan Date: Tue, 21 Jul 2020 18:52:02 +0530 Subject: [PATCH] Dial cached enrs before queueing subnet query --- beacon_node/eth2_libp2p/src/discovery/mod.rs | 2 +- .../eth2_libp2p/src/peer_manager/mod.rs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/beacon_node/eth2_libp2p/src/discovery/mod.rs b/beacon_node/eth2_libp2p/src/discovery/mod.rs index 0624414c5dd..2bcc3caa560 100644 --- a/beacon_node/eth2_libp2p/src/discovery/mod.rs +++ b/beacon_node/eth2_libp2p/src/discovery/mod.rs @@ -476,7 +476,7 @@ impl Discovery { .peers_on_subnet(subnet_id) .count(); - if peers_on_subnet > TARGET_SUBNET_PEERS { + if peers_on_subnet >= TARGET_SUBNET_PEERS { debug!(self.log, "Discovery ignored"; "reason" => "Already connected to desired peers", "connected_peers_on_subnet" => peers_on_subnet, diff --git a/beacon_node/eth2_libp2p/src/peer_manager/mod.rs b/beacon_node/eth2_libp2p/src/peer_manager/mod.rs index fa7c2524ade..dfbde42883f 100644 --- a/beacon_node/eth2_libp2p/src/peer_manager/mod.rs +++ b/beacon_node/eth2_libp2p/src/peer_manager/mod.rs @@ -215,18 +215,26 @@ impl PeerManager { .extend_peers_on_subnet(subnet_id, min_ttl); } - // Attempt to connect to cached_enr's satisfying subnet predicate - let predicate = subnet_predicate::(subnet_id, &self.log); - let mut connected = self + let mut peers_on_subnet = self .network_globals .peers .read() .peers_on_subnet(subnet_id) .count(); - if connected > TARGET_SUBNET_PEERS { + // Check if we already have required number of peers on the subnet + if peers_on_subnet >= TARGET_SUBNET_PEERS { + debug!(self.log, "Discovery ignored"; + "reason" => "Already connected to desired peers", + "connected_peers_on_subnet" => peers_on_subnet, + "target_subnet_peers" => TARGET_SUBNET_PEERS, + ); return; } + + // Attempt to connect to cached_enr's satisfying subnet predicate + let predicate = subnet_predicate::(subnet_id, &self.log); + let subnet_peers: Vec = self .discovery .cached_enrs() @@ -239,12 +247,19 @@ impl PeerManager { } }) .collect(); - for peer in subnet_peers { - if self.dial_peer(&peer) { - connected += 1; + for peer_id in subnet_peers { + if let Some(min_ttl) = min_ttl { + self.network_globals + .peers + .write() + .update_min_ttl(&peer_id, min_ttl); + } + if self.dial_peer(&peer_id) { + peers_on_subnet += 1; } } // request the subnet query from discovery + // if we found enough peers from the cached_enrs, we won't need to make a subnet query self.discovery.discover_subnet_peers(subnet_id, min_ttl); }