Skip to content

Commit

Permalink
FindPeersWithSubnet: Address Nishant's comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
nalepae committed Sep 3, 2024
1 parent ab0b0e8 commit 5e2af9f
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions beacon-chain/p2p/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,35 @@ func (s *Service) FindPeersWithSubnet(
return false, errors.Wrap(err, "node filter")
}

firstLoop := true

wg := new(sync.WaitGroup)
for {
peersSummary := func(topic string, threshold int) (int, int) {
// Retrieve how many peers we have for this topic.
peerCountForTopic := len(s.pubsub.ListPeers(topic))

// Compute how many peers we are missing to reach the threshold.
missingPeersCount := max(0, threshold-peerCountForTopic)
missingPeerCountForTopic := max(0, threshold-peerCountForTopic)

// If we have enough peers, we can exit the loop. This is the happy path.
if missingPeersCount == 0 {
break
}
return peerCountForTopic, missingPeerCountForTopic
}

if firstLoop {
firstLoop = false
// Compute how many peers we are missing to reach the threshold.
peerCountForTopic, missingPeerCountForTopic := peersSummary(topic, threshold)

log.WithFields(logrus.Fields{
"topic": topic,
"currentPeerCount": peerCountForTopic,
"targetPeerCount": threshold,
}).Debug("Searching for new peers in the network - Start")
// Exit early if we have enough peers.
if missingPeerCountForTopic == 0 {
return true, nil
}

log.WithFields(logrus.Fields{
"topic": topic,
"currentPeerCount": peerCountForTopic,
"targetPeerCount": threshold,
}).Debug("Searching for new peers in the network - Start")

wg := new(sync.WaitGroup)
for {
// If we have enough peers, we can exit the loop. This is the happy path.
if missingPeerCountForTopic == 0 {
break
}

// If the context is done, we can exit the loop. This is the unhappy path.
Expand All @@ -196,7 +202,7 @@ func (s *Service) FindPeersWithSubnet(
}

// Search for new peers in the network.
nodes := searchForPeers(iterator, batchSize, missingPeersCount, filter)
nodes := searchForPeers(iterator, batchSize, missingPeerCountForTopic, filter)

// Restrict dials if limit is applied.
maxConcurrentDials := math.MaxInt
Expand All @@ -214,6 +220,8 @@ func (s *Service) FindPeersWithSubnet(
// Wait for all dials to be completed.
wg.Wait()
}

_, missingPeerCountForTopic = peersSummary(topic, threshold)
}

log.WithField("topic", topic).Debug("Searching for new peers in the network - Success")
Expand Down

0 comments on commit 5e2af9f

Please sign in to comment.