Skip to content

Commit

Permalink
chore: adding discv5 logs (#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Jun 26, 2024
1 parent a05fa06 commit 974b8a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ ifeq ($(POSTGRES), 1)
NIM_PARAMS := $(NIM_PARAMS) -d:postgres -d:nimDebugDlOpen
endif

ifeq ($(DEBUG_DISCV5), 1)
NIM_PARAMS := $(NIM_PARAMS) -d:debugDiscv5
endif

clean: | clean-libbacktrace


Expand Down
28 changes: 28 additions & 0 deletions waku/discovery/waku_discv5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@ proc updateENRShards(

return ok()

proc logDiscv5FoundPeers(discoveredRecords: seq[waku_enr.Record]) =
for record in discoveredRecords:
let recordUri = record.toURI()
let capabilities = record.getCapabilities()

let typedRecord = record.toTyped().valueOr:
warn "Could not parse to typed record", error = error, enr = recordUri
continue

let peerInfo = record.toRemotePeerInfo().valueOr:
warn "Could not generate remote peer info", error = error, enr = recordUri
continue

let addrs = peerInfo.constructMultiaddrStr()

let rs = typedRecord.relaySharding()
let shardsStr =
if rs.isSome():
$rs.get()
else:
"no shards found"

notice "Received discv5 node",
addrs = addrs, enr = recordUri, capabilities = capabilities, shards = shardsStr

proc findRandomPeers*(
wd: WakuDiscoveryV5, overridePred = none(WakuDiscv5Predicate)
): Future[seq[waku_enr.Record]] {.async.} =
Expand All @@ -180,6 +205,9 @@ proc findRandomPeers*(

var discoveredRecords = discoveredNodes.mapIt(it.record)

when defined(debugDiscv5):
logDiscv5FoundPeers(discoveredRecords)

# Filter out nodes that do not match the predicate
if overridePred.isSome():
discoveredRecords = discoveredRecords.filter(overridePred.get())
Expand Down
18 changes: 14 additions & 4 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -649,18 +649,29 @@ proc connectToNodes*(
info "Dialing multiple peers", numOfPeers = nodes.len

var futConns: seq[Future[bool]]
var connectedPeers: seq[RemotePeerInfo]
for node in nodes:
let node = parsePeerInfo(node)
if node.isOk():
futConns.add(pm.connectRelay(node.value))
connectedPeers.add(node.value)
else:
error "Couldn't parse node info", error = node.error

await allFutures(futConns)
let successfulConns = futConns.mapIt(it.read()).countIt(it == true)

# Filtering successful connectedPeers based on futConns
let combined = zip(connectedPeers, futConns)
connectedPeers = combined.filterIt(it[1].read() == true).mapIt(it[0])

when defined(debugDiscv5):
let peerIds = connectedPeers.mapIt(it.peerId)
let origin = connectedPeers.mapIt(it.origin)
notice "established connections with found peers",
peerIds = peerIds, origin = origin

info "Finished dialing multiple peers",
successfulConns = successfulConns, attempted = nodes.len
successfulConns = connectedPeers.len, attempted = nodes.len

# The issue seems to be around peers not being fully connected when
# trying to subscribe. So what we do is sleep to guarantee nodes are
Expand Down Expand Up @@ -726,8 +737,7 @@ proc connectToRelayPeers*(pm: PeerManager) {.async.} =
if outRelayPeers.len >= pm.outRelayPeersTarget:
return

let notConnectedPeers =
pm.peerStore.getNotConnectedPeers().mapIt(RemotePeerInfo.init(it.peerId, it.addrs))
let notConnectedPeers = pm.peerStore.getNotConnectedPeers()

var outsideBackoffPeers = notConnectedPeers.filterIt(pm.canBeConnected(it.peerId))

Expand Down

0 comments on commit 974b8a3

Please sign in to comment.