Skip to content

Commit

Permalink
feat(peerEvents): add a peerEvent Identified (#843)
Browse files Browse the repository at this point in the history
Co-authored-by: diegomrsantos <diego@status.im>
  • Loading branch information
lchenut and diegomrsantos authored Jun 21, 2024
1 parent d1d53ff commit 100f318
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion libp2p/connmanager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ type
PeerEventKind* {.pure.} = enum
Left
Joined
Identified

PeerEvent* = object
case kind*: PeerEventKind
of PeerEventKind.Joined:
of PeerEventKind.Joined, PeerEventKind.Identified:
initiator*: bool
else:
discard
Expand Down
4 changes: 4 additions & 0 deletions libp2p/dialer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ proc internalConnect(
try:
self.connManager.storeMuxer(muxed)
await self.peerStore.identify(muxed)
await self.connManager.triggerPeerEvents(
muxed.connection.peerId,
PeerEvent(kind: PeerEventKind.Identified, initiator: true),
)
except CatchableError as exc:
trace "Failed to finish outgoung upgrade", err = exc.msg
await muxed.close()
Expand Down
7 changes: 3 additions & 4 deletions libp2p/services/autorelayservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =

let hasBeenSetUp = await procCall Service(self).setup(switch)
if hasBeenSetUp:
proc handlePeerJoined(peerId: PeerId, event: PeerEvent) {.async.} =
trace "Peer Joined", peerId
proc handlePeerIdentified(peerId: PeerId, event: PeerEvent) {.async.} =
trace "Peer Identified", peerId
if self.relayPeers.len < self.maxNumRelays:
self.peerAvailable.fire()

Expand All @@ -76,7 +76,7 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
self.relayPeers.withValue(peerId, future):
future[].cancel()

switch.addPeerEventHandler(handlePeerJoined, Joined)
switch.addPeerEventHandler(handlePeerIdentified, Identified)
switch.addPeerEventHandler(handlePeerLeft, Left)
switch.peerInfo.addressMappers.add(self.addressMapper)
await self.run(switch)
Expand Down Expand Up @@ -119,7 +119,6 @@ proc innerRun(self: AutoRelayService, switch: Switch) {.async.} =
await one(toSeq(self.relayPeers.values())) or self.peerAvailable.wait()
else:
await self.peerAvailable.wait()
await sleepAsync(200.millis)

method run*(self: AutoRelayService, switch: Switch) {.async.} =
if self.running:
Expand Down
6 changes: 3 additions & 3 deletions libp2p/switch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ proc upgrader(switch: Switch, trans: Transport, conn: Connection) {.async.} =
let muxed = await trans.upgrade(conn, Opt.none(PeerId))
switch.connManager.storeMuxer(muxed)
await switch.peerStore.identify(muxed)
await switch.connManager.triggerPeerEvents(
muxed.connection.peerId, PeerEvent(kind: PeerEventKind.Identified, initiator: false)
)
trace "Connection upgrade succeeded"

proc upgradeMonitor(
Expand Down Expand Up @@ -295,9 +298,6 @@ proc stop*(s: Switch) {.async, public.} =
except CatchableError as exc:
warn "error cleaning up transports", msg = exc.msg

for service in s.services:
discard await service.stop(s)

await s.ms.stop()

trace "Switch stopped"
Expand Down

0 comments on commit 100f318

Please sign in to comment.