Autonat doesn't ask an incoming peer (#857)

This commit is contained in:
diegomrsantos 2023-02-09 17:40:04 +01:00 committed by GitHub
parent f89bd0c77c
commit 444b837923
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -152,6 +152,8 @@ method setup*(self: AutonatService, switch: Switch): Future[bool] {.async.} =
if hasBeenSetup:
if self.askNewConnectedPeers:
self.newConnectedPeerHandler = proc (peerId: PeerId, event: PeerEvent): Future[void] {.async.} =
if switch.connManager.selectConn(peerId, In) != nil: # no need to ask an incoming peer
return
discard askPeer(self, switch, peerId)
await self.callHandler()
switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined)

View File

@ -320,3 +320,26 @@ suite "Autonat Service":
await allFuturesThrowing(
switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop(), switch5.stop())
asyncTest "Peer must not ask an incoming peer":
let autonatService = AutonatService.new(AutonatClient.new(), newRng())
let switch1 = createSwitch(autonatService)
let switch2 = createSwitch()
proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Option[float]) {.gcsafe, async.} =
fail()
check autonatService.networkReachability() == NetworkReachability.Unknown
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
await switch1.start()
await switch2.start()
await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs)
await sleepAsync(500.milliseconds)
await allFuturesThrowing(
switch1.stop(), switch2.stop())