Skip to content

Commit

Permalink
Added randomized peer selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Jan 9, 2024
1 parent d523505 commit 0b3a534
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ else:


import
std/[options, sugar, sets, sequtils, times, strutils, math],
std/[options, sets, sequtils, times, strutils, math, random],
chronos,
chronicles,
metrics,
Expand All @@ -19,9 +19,6 @@ import
../../waku_relay,
../../waku_enr/sharding,
../../waku_enr/capabilities,
../../waku_store/common,
../../waku_filter_v2/common,
../../waku_lightpush/common,
../../waku_metadata,
./peer_store/peer_storage,
./waku_peer_store
Expand All @@ -40,6 +37,8 @@ declarePublicGauge waku_service_peers, "Service peer protocol and multiaddress "
logScope:
topics = "waku node peer_manager"

randomize()

const
# TODO: Make configurable
DefaultDialTimeout = chronos.seconds(10)
Expand Down Expand Up @@ -710,6 +709,9 @@ proc manageRelayPeers*(pm: PeerManager) {.async.} =
relayInboundTarget = $connectedInPeers.len & "/" & $inTarget,
relayOutboundTarget = $connectedOutPeers.len & "/" & $outTarget

# Always pick random connectable relay peers
shuffle(connectablePeers)

let length = min(outPeerDiff, connectablePeers.len)
for peer in connectablePeers[0..<length]:
trace "Peer To Connect To", peerId = $peer.peerId
Expand Down Expand Up @@ -748,7 +750,10 @@ proc prunePeerStore*(pm: PeerManager) =

peersToPrune.incl(peerId)

let notConnected = pm.peerStore.getNotConnectedPeers().mapIt(it.peerId)
var notConnected = pm.peerStore.getNotConnectedPeers().mapIt(it.peerId)

# Always pick random non-connected peers
shuffle(notConnected)

var shardlessPeers: seq[PeerId]
var peersByShard = initTable[uint16, seq[PeerId]]()
Expand Down

0 comments on commit 0b3a534

Please sign in to comment.