Skip to content

Commit f1d611a

Browse files
committed
fix: replace deprecated wss filter
1 parent 35acdf8 commit f1d611a

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
@@ -112,7 +112,7 @@ export class PeerManager {
112112

113113
for (const peer of connectedPeers) {
114114
const hasProtocol = this.hasPeerProtocol(peer, params.protocol);
115-
const hasSamePubsub = await this.connectionManager.isPeerOnTopic(
115+
const hasSamePubsub = await this.connectionManager.isPeerOnPubsubTopic(
116116
peer.id,
117117
params.pubsubTopic
118118
);
@@ -185,14 +185,14 @@ export class PeerManager {
185185
id: PeerId,
186186
pubsubTopic: string
187187
): Promise<boolean> {
188-
const hasShardInfo = await this.connectionManager.hasShardInfo(id);
188+
const hasShardInfo = await this.connectionManager.isPeerOnSameShard(id);
189189

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

195-
return this.connectionManager.isPeerOnTopic(id, pubsubTopic);
195+
return this.connectionManager.isPeerOnPubsubTopic(id, pubsubTopic);
196196
}
197197

198198
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 {
@@ -89,9 +90,7 @@ export class WakuNode implements IWaku {
8990
this.connectionManager = new ConnectionManager({
9091
libp2p,
9192
relay: this.relay,
92-
events: this.events,
9393
pubsubTopics: pubsubTopics,
94-
networkConfig: this.networkConfig,
9594
config: options?.connectionManager
9695
});
9796

@@ -207,13 +206,29 @@ export class WakuNode implements IWaku {
207206

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

210-
return await this.connectionManager.dial(peer, codecs);
209+
return await this.connectionManager.rawDialPeerWithProtocols(peer, codecs);
211210
}
212211

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

216-
return this.connectionManager.hangUp(peer);
215+
let peerId: PeerId;
216+
if (typeof peer === "string") {
217+
peerId = peerIdFromString(peer);
218+
} else if (peer && "getPeerId" in peer) {
219+
// MultiaddrInput case
220+
const peerIdStr = peer.getPeerId?.();
221+
if (!peerIdStr) {
222+
throw new Error("No peer ID in multiaddr");
223+
}
224+
peerId = peerIdFromString(peerIdStr);
225+
} else {
226+
// PeerId case
227+
peerId = peer as PeerId;
228+
}
229+
230+
await this.connectionManager.dropConnection(peerId);
231+
return true;
217232
}
218233

219234
public async start(): Promise<void> {
@@ -222,7 +237,7 @@ export class WakuNode implements IWaku {
222237
this._nodeStateLock = true;
223238

224239
await this.libp2p.start();
225-
this.connectionManager.start();
240+
// Connection manager starts automatically
226241
this.peerManager.start();
227242
this.healthIndicator.start();
228243
this.lightPush?.start();

0 commit comments

Comments
 (0)