Skip to content

Commit

Permalink
Dial cached enrs before queueing subnet query
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Jul 21, 2020
1 parent 2561ff5 commit 0bb5ab8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion beacon_node/eth2_libp2p/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
.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,
Expand Down
29 changes: 22 additions & 7 deletions beacon_node/eth2_libp2p/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,26 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
.extend_peers_on_subnet(subnet_id, min_ttl);
}

// Attempt to connect to cached_enr's satisfying subnet predicate
let predicate = subnet_predicate::<TSpec>(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::<TSpec>(subnet_id, &self.log);

let subnet_peers: Vec<PeerId> = self
.discovery
.cached_enrs()
Expand All @@ -239,12 +247,19 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
}
})
.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);
}

Expand Down

0 comments on commit 0bb5ab8

Please sign in to comment.