From 444b837923ea2252db9aaa1240ed817c5fe2e3df Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Thu, 9 Feb 2023 17:40:04 +0100 Subject: [PATCH] Autonat doesn't ask an incoming peer (#857) --- .../connectivity/autonat/service.nim | 2 ++ tests/testautonatservice.nim | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/libp2p/protocols/connectivity/autonat/service.nim b/libp2p/protocols/connectivity/autonat/service.nim index 6b223babc..afbf3ca81 100644 --- a/libp2p/protocols/connectivity/autonat/service.nim +++ b/libp2p/protocols/connectivity/autonat/service.nim @@ -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) diff --git a/tests/testautonatservice.nim b/tests/testautonatservice.nim index c40504bf4..d7e684abe 100644 --- a/tests/testautonatservice.nim +++ b/tests/testautonatservice.nim @@ -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())