Autonat and HP changes (#899)

This commit is contained in:
diegomrsantos 2023-06-07 15:26:58 +02:00 committed by GitHub
parent 32085ca88a
commit 3640b4dd89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 8 deletions

View File

@ -82,11 +82,14 @@ proc hasEnoughIncomingSlots(switch: Switch): bool =
proc doesPeerHaveIncomingConn(switch: Switch, peerId: PeerId): bool = proc doesPeerHaveIncomingConn(switch: Switch, peerId: PeerId): bool =
return switch.connManager.selectMuxer(peerId, In) != nil return switch.connManager.selectMuxer(peerId, In) != nil
proc handleAnswer(self: AutonatService, ans: NetworkReachability) {.async.} = proc handleAnswer(self: AutonatService, ans: NetworkReachability): Future[bool] {.async.} =
if ans == Unknown: if ans == Unknown:
return return
let oldNetworkReachability = self.networkReachability
let oldConfidence = self.confidence
if self.answers.len == self.maxQueueSize: if self.answers.len == self.maxQueueSize:
self.answers.popFirst() self.answers.popFirst()
self.answers.addLast(ans) self.answers.addLast(ans)
@ -103,6 +106,9 @@ proc handleAnswer(self: AutonatService, ans: NetworkReachability) {.async.} =
debug "Current status", currentStats = $self.networkReachability, confidence = $self.confidence, answers = self.answers debug "Current status", currentStats = $self.networkReachability, confidence = $self.confidence, answers = self.answers
# Return whether anything has changed
return self.networkReachability != oldNetworkReachability or self.confidence != oldConfidence
proc askPeer(self: AutonatService, switch: Switch, peerId: PeerId): Future[NetworkReachability] {.async.} = proc askPeer(self: AutonatService, switch: Switch, peerId: PeerId): Future[NetworkReachability] {.async.} =
logScope: logScope:
peerId = $peerId peerId = $peerId
@ -129,9 +135,9 @@ proc askPeer(self: AutonatService, switch: Switch, peerId: PeerId): Future[Netwo
except CatchableError as error: except CatchableError as error:
debug "dialMe unexpected error", msg = error.msg debug "dialMe unexpected error", msg = error.msg
Unknown Unknown
await self.handleAnswer(ans) let hasReachabilityOrConfidenceChanged = await self.handleAnswer(ans)
if not isNil(self.statusAndConfidenceHandler): if hasReachabilityOrConfidenceChanged:
await self.statusAndConfidenceHandler(self.networkReachability, self.confidence) await self.callHandler()
await switch.peerInfo.update() await switch.peerInfo.update()
return ans return ans
@ -193,7 +199,6 @@ method setup*(self: AutonatService, switch: Switch): Future[bool] {.async.} =
method run*(self: AutonatService, switch: Switch) {.async, public.} = method run*(self: AutonatService, switch: Switch) {.async, public.} =
trace "Running AutonatService" trace "Running AutonatService"
await askConnectedPeers(self, switch) await askConnectedPeers(self, switch)
await self.callHandler()
method stop*(self: AutonatService, switch: Switch): Future[bool] {.async, public.} = method stop*(self: AutonatService, switch: Switch): Future[bool] {.async, public.} =
info "Stopping AutonatService" info "Stopping AutonatService"

View File

@ -32,6 +32,9 @@ type
addressMapper: AddressMapper addressMapper: AddressMapper
rng: ref HmacDrbgContext rng: ref HmacDrbgContext
proc isRunning*(self: AutoRelayService): bool =
return self.running
proc addressMapper( proc addressMapper(
self: AutoRelayService, self: AutoRelayService,
listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.gcsafe, async.} = listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.gcsafe, async.} =

View File

@ -105,9 +105,9 @@ method setup*(self: HPService, switch: Switch): Future[bool] {.async.} =
switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined) switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined)
self.onNewStatusHandler = proc (networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} = self.onNewStatusHandler = proc (networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} =
if networkReachability == NetworkReachability.NotReachable: if networkReachability == NetworkReachability.NotReachable and not self.autoRelayService.isRunning():
discard await self.autoRelayService.setup(switch) discard await self.autoRelayService.setup(switch)
elif networkReachability == NetworkReachability.Reachable: elif networkReachability == NetworkReachability.Reachable and self.autoRelayService.isRunning():
discard await self.autoRelayService.stop(switch) discard await self.autoRelayService.stop(switch)
# We do it here instead of in the AutonatService because this is useful only when hole punching. # We do it here instead of in the AutonatService because this is useful only when hole punching.

View File

@ -407,7 +407,8 @@ suite "Autonat Service":
# switch1 is now full, should stick to last observation # switch1 is now full, should stick to last observation
awaiter = newFuture[void]() awaiter = newFuture[void]()
await autonatService.run(switch1) await autonatService.run(switch1)
await awaiter
await sleepAsync(200.millis)
check autonatService.networkReachability == NetworkReachability.Reachable check autonatService.networkReachability == NetworkReachability.Reachable
check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1 check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1