Skip to content

Commit 7e21ba0

Browse files
authored
fix: cache peer ids (#519)
* fix: cache peer ids * chore: fix type errors
1 parent d9e4374 commit 7e21ba0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
296296

297297
// State
298298

299-
public readonly peers = new Set<PeerIdStr>()
299+
public readonly peers = new Map<PeerIdStr, PeerId>()
300300
public readonly streamsInbound = new Map<PeerIdStr, InboundStream>()
301301
public readonly streamsOutbound = new Map<PeerIdStr, OutboundStream>()
302302

@@ -602,7 +602,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
602602
]
603603

604604
getPeers (): PeerId[] {
605-
return [...this.peers.keys()].map((str) => peerIdFromString(str))
605+
return [...this.peers.values()]
606606
}
607607

608608
isStarted (): boolean {
@@ -915,7 +915,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
915915
if (!this.peers.has(id)) {
916916
this.log('new peer %p', peerId)
917917

918-
this.peers.add(id)
918+
this.peers.set(id, peerId)
919919

920920
// Add to peer scoring
921921
this.score.addPeer(id)
@@ -1016,7 +1016,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
10161016
*/
10171017
getSubscribers (topic: TopicStr): PeerId[] {
10181018
const peersInTopic = this.topics.get(topic)
1019-
return ((peersInTopic != null) ? Array.from(peersInTopic) : []).map((str) => peerIdFromString(str))
1019+
return ((peersInTopic != null) ? Array.from(peersInTopic) : []).map((str) => this.peers.get(str) ?? peerIdFromString(str))
10201020
}
10211021

10221022
/**
@@ -2316,7 +2316,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
23162316
}
23172317

23182318
return {
2319-
recipients: Array.from(tosend.values()).map((str) => peerIdFromString(str))
2319+
recipients: Array.from(tosend.values()).map((str) => this.peers.get(str) ?? peerIdFromString(str))
23202320
}
23212321
}
23222322

@@ -2705,7 +2705,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
27052705
// the peer ID and let the pruned peer find them in the DHT -- we can't trust
27062706
// unsigned address records through PX anyways
27072707
// Finding signed records in the DHT is not supported at the time of writing in js-libp2p
2708-
const id = peerIdFromString(peerId)
2708+
const id = this.peers.get(peerId) ?? peerIdFromString(peerId)
27092709
let peerInfo: Peer | undefined
27102710

27112711
try {
@@ -3229,7 +3229,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
32293229

32303230
private readonly tagMeshPeer = (evt: CustomEvent<MeshPeer>): void => {
32313231
const { peerId, topic } = evt.detail
3232-
this.components.peerStore.merge(peerIdFromString(peerId), {
3232+
this.components.peerStore.merge(this.peers.get(peerId) ?? peerIdFromString(peerId), {
32333233
tags: {
32343234
[topic]: {
32353235
value: 100
@@ -3240,7 +3240,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
32403240

32413241
private readonly untagMeshPeer = (evt: CustomEvent<MeshPeer>): void => {
32423242
const { peerId, topic } = evt.detail
3243-
this.components.peerStore.merge(peerIdFromString(peerId), {
3243+
this.components.peerStore.merge(this.peers.get(peerId) ?? peerIdFromString(peerId), {
32443244
tags: {
32453245
[topic]: undefined
32463246
}

0 commit comments

Comments
 (0)