feat: running periodicaly peer exchange if discv5 is disabled (#3150)

This commit is contained in:
gabrielmer 2024-10-30 12:51:04 +02:00 committed by GitHub
parent b3656d6eea
commit 400d7a54f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 11 deletions

View File

@ -44,7 +44,7 @@ suite "Waku Keepalive":
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()]) await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
node1.startKeepalive() node1.startKeepalive(2.seconds)
check: check:
(await completionFut.withTimeout(5.seconds)) == true (await completionFut.withTimeout(5.seconds)) == true

View File

@ -422,7 +422,11 @@ proc startNode*(
desiredOutDegree = node.wakuRelay.parameters.d.uint64() desiredOutDegree = node.wakuRelay.parameters.d.uint64()
(await node.fetchPeerExchangePeers(desiredOutDegree)).isOkOr: (await node.fetchPeerExchangePeers(desiredOutDegree)).isOkOr:
error "error while fetching peers from peer exchange", error = error error "error while fetching peers from peer exchange", error = error
quit(QuitFailure)
# Use px to periodically get peers if discv5 is disabled, as discv5 nodes have their own
# periodic loop to find peers and px returned peers actually come from discv5
if conf.peerExchange and not conf.discv5Discovery:
node.startPeerExchangeLoop()
# Start keepalive, if enabled # Start keepalive, if enabled
if conf.keepAlive: if conf.keepAlive:

View File

@ -1146,7 +1146,7 @@ proc mountPeerExchange*(
error "failed to mount wakuPeerExchange", error = getCurrentExceptionMsg() error "failed to mount wakuPeerExchange", error = getCurrentExceptionMsg()
proc fetchPeerExchangePeers*( proc fetchPeerExchangePeers*(
node: Wakunode, amount: uint64 node: Wakunode, amount = DefaultPXNumPeersReq
): Future[Result[int, PeerExchangeResponseStatus]] {.async: (raises: []).} = ): Future[Result[int, PeerExchangeResponseStatus]] {.async: (raises: []).} =
if node.wakuPeerExchange.isNil(): if node.wakuPeerExchange.isNil():
error "could not get peers from px, waku peer-exchange is nil" error "could not get peers from px, waku peer-exchange is nil"
@ -1175,6 +1175,20 @@ proc fetchPeerExchangePeers*(
error = pxPeersRes.error error = pxPeersRes.error
return err(pxPeersRes.error) return err(pxPeersRes.error)
proc peerExchangeLoop(node: WakuNode) {.async.} =
while true:
await sleepAsync(1.minutes)
if not node.started:
continue
(await node.fetchPeerExchangePeers()).isOkOr:
warn "Cannot fetch peers from peer exchange", cause = error
proc startPeerExchangeLoop*(node: WakuNode) =
if node.wakuPeerExchange.isNil():
error "startPeerExchangeLoop: Peer Exchange is not mounted"
return
node.wakuPeerExchange.pxLoopHandle = node.peerExchangeLoop()
# TODO: Move to application module (e.g., wakunode2.nim) # TODO: Move to application module (e.g., wakunode2.nim)
proc setPeerExchangePeer*( proc setPeerExchangePeer*(
node: WakuNode, peer: RemotePeerInfo | MultiAddress | string node: WakuNode, peer: RemotePeerInfo | MultiAddress | string
@ -1217,7 +1231,11 @@ proc mountLibp2pPing*(node: WakuNode) {.async: (raises: []).} =
# TODO: Move this logic to PeerManager # TODO: Move this logic to PeerManager
proc keepaliveLoop(node: WakuNode, keepalive: chronos.Duration) {.async.} = proc keepaliveLoop(node: WakuNode, keepalive: chronos.Duration) {.async.} =
while node.started: while true:
await sleepAsync(keepalive)
if not node.started:
continue
# Keep connected peers alive while running # Keep connected peers alive while running
# Each node is responsible of keeping its outgoing connections alive # Each node is responsible of keeping its outgoing connections alive
trace "Running keepalive" trace "Running keepalive"
@ -1235,14 +1253,11 @@ proc keepaliveLoop(node: WakuNode, keepalive: chronos.Duration) {.async.} =
except CatchableError as exc: except CatchableError as exc:
waku_node_errors.inc(labelValues = ["keep_alive_failure"]) waku_node_errors.inc(labelValues = ["keep_alive_failure"])
await sleepAsync(keepalive) # 2 minutes default - 20% of the default chronosstream timeout duration
proc startKeepalive*(node: WakuNode, keepalive = 2.minutes) =
info "starting keepalive", keepalive = keepalive
proc startKeepalive*(node: WakuNode) = asyncSpawn node.keepaliveLoop(keepalive)
let defaultKeepalive = 2.minutes # 20% of the default chronosstream timeout duration
info "starting keepalive", keepalive = defaultKeepalive
asyncSpawn node.keepaliveLoop(defaultKeepalive)
proc mountRendezvous*(node: WakuNode) {.async: (raises: []).} = proc mountRendezvous*(node: WakuNode) {.async: (raises: []).} =
info "mounting rendezvous discovery protocol" info "mounting rendezvous discovery protocol"
@ -1372,6 +1387,9 @@ proc stop*(node: WakuNode) {.async.} =
if not node.wakuStoreResume.isNil(): if not node.wakuStoreResume.isNil():
await node.wakuStoreResume.stopWait() await node.wakuStoreResume.stopWait()
if not node.wakuPeerExchange.isNil() and not node.wakuPeerExchange.pxLoopHandle.isNil():
await node.wakuPeerExchange.pxLoopHandle.cancelAndWait()
node.started = false node.started = false
proc isReady*(node: WakuNode): Future[bool] {.async: (raises: [Exception]).} = proc isReady*(node: WakuNode): Future[bool] {.async: (raises: [Exception]).} =

View File

@ -57,6 +57,7 @@ type
cluster*: Option[uint16] cluster*: Option[uint16]
# todo: next step: ring buffer; future: implement cache satisfying https://rfc.vac.dev/spec/34/ # todo: next step: ring buffer; future: implement cache satisfying https://rfc.vac.dev/spec/34/
requestRateLimiter*: RequestRateLimiter requestRateLimiter*: RequestRateLimiter
pxLoopHandle*: Future[void]
proc request*( proc request*(
wpx: WakuPeerExchange, numPeers = DefaultPXNumPeersReq, conn: Connection wpx: WakuPeerExchange, numPeers = DefaultPXNumPeersReq, conn: Connection