feat(peerEvents): add a peerEvent Identified (#843)

Co-authored-by: diegomrsantos <diego@status.im>
This commit is contained in:
Ludovic Chenut 2024-06-21 13:06:59 +02:00 committed by GitHub
parent d1d53ff369
commit 100f3188ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 8 deletions

View File

@ -48,10 +48,11 @@ type
PeerEventKind* {.pure.} = enum PeerEventKind* {.pure.} = enum
Left Left
Joined Joined
Identified
PeerEvent* = object PeerEvent* = object
case kind*: PeerEventKind case kind*: PeerEventKind
of PeerEventKind.Joined: of PeerEventKind.Joined, PeerEventKind.Identified:
initiator*: bool initiator*: bool
else: else:
discard discard

View File

@ -195,6 +195,10 @@ proc internalConnect(
try: try:
self.connManager.storeMuxer(muxed) self.connManager.storeMuxer(muxed)
await self.peerStore.identify(muxed) await self.peerStore.identify(muxed)
await self.connManager.triggerPeerEvents(
muxed.connection.peerId,
PeerEvent(kind: PeerEventKind.Identified, initiator: true),
)
except CatchableError as exc: except CatchableError as exc:
trace "Failed to finish outgoung upgrade", err = exc.msg trace "Failed to finish outgoung upgrade", err = exc.msg
await muxed.close() await muxed.close()

View File

@ -66,8 +66,8 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
let hasBeenSetUp = await procCall Service(self).setup(switch) let hasBeenSetUp = await procCall Service(self).setup(switch)
if hasBeenSetUp: if hasBeenSetUp:
proc handlePeerJoined(peerId: PeerId, event: PeerEvent) {.async.} = proc handlePeerIdentified(peerId: PeerId, event: PeerEvent) {.async.} =
trace "Peer Joined", peerId trace "Peer Identified", peerId
if self.relayPeers.len < self.maxNumRelays: if self.relayPeers.len < self.maxNumRelays:
self.peerAvailable.fire() self.peerAvailable.fire()
@ -76,7 +76,7 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
self.relayPeers.withValue(peerId, future): self.relayPeers.withValue(peerId, future):
future[].cancel() future[].cancel()
switch.addPeerEventHandler(handlePeerJoined, Joined) switch.addPeerEventHandler(handlePeerIdentified, Identified)
switch.addPeerEventHandler(handlePeerLeft, Left) switch.addPeerEventHandler(handlePeerLeft, Left)
switch.peerInfo.addressMappers.add(self.addressMapper) switch.peerInfo.addressMappers.add(self.addressMapper)
await self.run(switch) await self.run(switch)
@ -119,7 +119,6 @@ proc innerRun(self: AutoRelayService, switch: Switch) {.async.} =
await one(toSeq(self.relayPeers.values())) or self.peerAvailable.wait() await one(toSeq(self.relayPeers.values())) or self.peerAvailable.wait()
else: else:
await self.peerAvailable.wait() await self.peerAvailable.wait()
await sleepAsync(200.millis)
method run*(self: AutoRelayService, switch: Switch) {.async.} = method run*(self: AutoRelayService, switch: Switch) {.async.} =
if self.running: if self.running:

View File

@ -204,6 +204,9 @@ proc upgrader(switch: Switch, trans: Transport, conn: Connection) {.async.} =
let muxed = await trans.upgrade(conn, Opt.none(PeerId)) let muxed = await trans.upgrade(conn, Opt.none(PeerId))
switch.connManager.storeMuxer(muxed) switch.connManager.storeMuxer(muxed)
await switch.peerStore.identify(muxed) await switch.peerStore.identify(muxed)
await switch.connManager.triggerPeerEvents(
muxed.connection.peerId, PeerEvent(kind: PeerEventKind.Identified, initiator: false)
)
trace "Connection upgrade succeeded" trace "Connection upgrade succeeded"
proc upgradeMonitor( proc upgradeMonitor(
@ -295,9 +298,6 @@ proc stop*(s: Switch) {.async, public.} =
except CatchableError as exc: except CatchableError as exc:
warn "error cleaning up transports", msg = exc.msg warn "error cleaning up transports", msg = exc.msg
for service in s.services:
discard await service.stop(s)
await s.ms.stop() await s.ms.stop()
trace "Switch stopped" trace "Switch stopped"