Skip to content

Commit cf5e1b7

Browse files
committed
fix: replace deprecated wss filter
1 parent 14085de commit cf5e1b7

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed

packages/sdk/src/create/libp2p.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { noise } from "@chainsafe/libp2p-noise";
22
import { bootstrap } from "@libp2p/bootstrap";
33
import { identify } from "@libp2p/identify";
4+
import type { ConnectionGater } from "@libp2p/interface";
45
import { mplex } from "@libp2p/mplex";
56
import { ping } from "@libp2p/ping";
67
import { webSockets } from "@libp2p/websockets";
7-
import { all as filterAll, wss } from "@libp2p/websockets/filters";
88
import { wakuMetadata } from "@waku/core";
99
import {
1010
type CreateLibp2pOptions,
@@ -53,16 +53,22 @@ export async function defaultLibp2p(
5353
? { metadata: wakuMetadata(pubsubTopics) }
5454
: {};
5555

56-
const filter =
57-
options?.filterMultiaddrs === false || isTestEnvironment()
58-
? filterAll
59-
: wss;
56+
const connectionGater: ConnectionGater = {
57+
denyDialMultiaddr: async (multiaddr) => {
58+
if (options?.filterMultiaddrs === false || isTestEnvironment()) {
59+
return false;
60+
}
61+
const protocols = multiaddr.protos().map((proto) => proto.name);
62+
return protocols.includes("ws") && !protocols.includes("wss");
63+
}
64+
};
6065

6166
return createLibp2p({
62-
transports: [webSockets({ filter: filter })],
67+
transports: [webSockets()],
6368
streamMuxers: [mplex()],
6469
connectionEncrypters: [noise()],
6570
...options,
71+
connectionGater,
6672
services: {
6773
identify: identify({
6874
agentVersion: userAgent ?? DefaultUserAgent

packages/sdk/src/filter/filter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class Filter implements IFilter {
3939

4040
this.protocol = new FilterCore(
4141
this.onIncomingMessage.bind(this),
42+
this.connectionManager.pubsubTopics,
4243
params.libp2p
4344
);
4445
}
@@ -173,7 +174,7 @@ export class Filter implements IFilter {
173174

174175
private throwIfTopicNotSupported(pubsubTopic: string): void {
175176
const supportedPubsubTopic =
176-
this.connectionManager.isTopicConfigured(pubsubTopic);
177+
this.connectionManager.pubsubTopics.includes(pubsubTopic);
177178

178179
if (!supportedPubsubTopic) {
179180
throw Error(

packages/sdk/src/light_push/light_push.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ export class LightPush implements ILightPush {
5151

5252
this.peerManager = params.peerManager;
5353
this.connectionManager = params.connectionManager;
54-
this.protocol = new LightPushCore(params.libp2p);
54+
this.protocol = new LightPushCore(
55+
this.connectionManager.pubsubTopics,
56+
params.libp2p
57+
);
5558
this.retryManager = new RetryManager({
5659
peerManager: params.peerManager,
5760
retryIntervalMs: this.config.retryIntervalMs
@@ -84,7 +87,7 @@ export class LightPush implements ILightPush {
8487

8588
log.info("send: attempting to send a message to pubsubTopic:", pubsubTopic);
8689

87-
if (!this.connectionManager.isTopicConfigured(pubsubTopic)) {
90+
if (!this.connectionManager.pubsubTopics.includes(pubsubTopic)) {
8891
return {
8992
successes: [],
9093
failures: [

packages/sdk/src/peer_manager/peer_manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class PeerManager {
114114

115115
for (const peer of connectedPeers) {
116116
const hasProtocol = this.hasPeerProtocol(peer, params.protocol);
117-
const hasSamePubsub = await this.connectionManager.isPeerOnTopic(
117+
const hasSamePubsub = await this.connectionManager.isPeerOnPubsubTopic(
118118
peer.id,
119119
params.pubsubTopic
120120
);
@@ -187,14 +187,14 @@ export class PeerManager {
187187
id: PeerId,
188188
pubsubTopic: string
189189
): Promise<boolean> {
190-
const hasShardInfo = await this.connectionManager.hasShardInfo(id);
190+
const hasShardInfo = await this.connectionManager.isPeerOnSameShard(id);
191191

192192
// allow to use peers that we don't know information about yet
193193
if (!hasShardInfo) {
194194
return true;
195195
}
196196

197-
return this.connectionManager.isPeerOnTopic(id, pubsubTopic);
197+
return this.connectionManager.isPeerOnPubsubTopic(id, pubsubTopic);
198198
}
199199

200200
private async onConnected(event: CustomEvent<IdentifyResult>): Promise<void> {

packages/sdk/src/store/store.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export class Store implements IStore {
4242
this.connectionManager = params.connectionManager;
4343
this.libp2p = params.libp2p;
4444

45-
this.protocol = new StoreCore(params.libp2p);
45+
this.protocol = new StoreCore(
46+
this.connectionManager.pubsubTopics,
47+
params.libp2p
48+
);
4649
}
4750

4851
public get multicodec(): string {
@@ -230,7 +233,7 @@ export class Store implements IStore {
230233

231234
const pubsubTopicForQuery = uniquePubsubTopicsInQuery[0];
232235
const isTopicSupported =
233-
this.connectionManager.isTopicConfigured(pubsubTopicForQuery);
236+
this.connectionManager.pubsubTopics.includes(pubsubTopicForQuery);
234237

235238
if (!isTopicSupported) {
236239
throw new Error(

packages/sdk/src/waku/waku.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type Stream,
55
TypedEventEmitter
66
} from "@libp2p/interface";
7+
import { peerIdFromString } from "@libp2p/peer-id";
78
import type { MultiaddrInput } from "@multiformats/multiaddr";
89
import { ConnectionManager, createDecoder, createEncoder } from "@waku/core";
910
import type {
@@ -85,9 +86,7 @@ export class WakuNode implements IWaku {
8586
this.connectionManager = new ConnectionManager({
8687
libp2p,
8788
relay: this.relay,
88-
events: this.events,
8989
pubsubTopics: pubsubTopics,
90-
networkConfig: this.networkConfig,
9190
config: options?.connectionManager
9291
});
9392

@@ -199,13 +198,29 @@ export class WakuNode implements IWaku {
199198

200199
log.info(`Dialing to ${peer?.toString()} with protocols ${_protocols}`);
201200

202-
return await this.connectionManager.dial(peer, codecs);
201+
return await this.connectionManager.rawDialPeerWithProtocols(peer, codecs);
203202
}
204203

205204
public async hangUp(peer: PeerId | MultiaddrInput): Promise<boolean> {
206205
log.info(`Hanging up peer:${peer?.toString()}.`);
207206

208-
return this.connectionManager.hangUp(peer);
207+
let peerId: PeerId;
208+
if (typeof peer === "string") {
209+
peerId = peerIdFromString(peer);
210+
} else if (peer && "getPeerId" in peer) {
211+
// MultiaddrInput case
212+
const peerIdStr = peer.getPeerId?.();
213+
if (!peerIdStr) {
214+
throw new Error("No peer ID in multiaddr");
215+
}
216+
peerId = peerIdFromString(peerIdStr);
217+
} else {
218+
// PeerId case
219+
peerId = peer as PeerId;
220+
}
221+
222+
await this.connectionManager.dropConnection(peerId);
223+
return true;
209224
}
210225

211226
public async start(): Promise<void> {
@@ -214,7 +229,7 @@ export class WakuNode implements IWaku {
214229
this._nodeStateLock = true;
215230

216231
await this.libp2p.start();
217-
this.connectionManager.start();
232+
// Connection manager starts automatically
218233
this.peerManager.start();
219234
this.health.start();
220235
this.lightPush?.start();

0 commit comments

Comments
 (0)