Skip to content

Commit cc7d648

Browse files
committed
feat: add legacy flag to enable lightpushv2 only
1 parent 3ecaa13 commit cc7d648

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

packages/core/src/lib/light_push/light_push.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ export class LightPushCore {
3232
private readonly streamManagerV2: StreamManager;
3333
public readonly multicodec = [CODECS.v3, CODECS.v2];
3434

35-
public constructor(private libp2p: Libp2p) {
36-
this.streamManager = new StreamManager(CODECS.v3, libp2p.components);
35+
public constructor(
36+
private libp2p: Libp2p,
37+
legacy?: boolean
38+
) {
3739
this.streamManagerV2 = new StreamManager(CODECS.v2, libp2p.components);
40+
this.streamManager = legacy
41+
? this.streamManagerV2
42+
: new StreamManager(CODECS.v3, libp2p.components);
3843
}
3944

4045
public async send(
@@ -48,7 +53,10 @@ export class LightPushCore {
4853
try {
4954
const peer = await this.libp2p.peerStore.get(peerId);
5055

51-
if (peer.protocols.includes(CODECS.v3)) {
56+
if (
57+
this.streamManager.multicodec === CODECS.v3 &&
58+
peer.protocols.includes(CODECS.v3)
59+
) {
5260
stream = await this.streamManager.getStream(peerId);
5361
protocol = CODECS.v3;
5462
} else {

packages/core/src/lib/stream_manager/stream_manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class StreamManager {
1313
private streamPool: Map<string, Promise<void>> = new Map();
1414

1515
public constructor(
16-
private multicodec: string,
16+
public multicodec: string,
1717
private readonly libp2p: Libp2pComponents
1818
) {
1919
this.log = new Logger(`stream-manager:${multicodec}`);

packages/sdk/src/light_push/light_push.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Peer, PeerId } from "@libp2p/interface";
2-
import { createEncoder, Encoder, LightPushCodec } from "@waku/core";
2+
import {
3+
createEncoder,
4+
Encoder,
5+
LightPushCodec,
6+
LightPushCodecV2
7+
} from "@waku/core";
38
import { Libp2p, LightPushError, LightPushStatusCode } from "@waku/interfaces";
49
import { createRoutingInfo } from "@waku/utils";
510
import { utf8ToBytes } from "@waku/utils/bytes";
@@ -243,7 +248,7 @@ function mockV3Peer(id: string): Peer {
243248
}
244249

245250
function mockV2AndV3Peer(id: string): Peer {
246-
return mockPeer(id, [LightPushCodec, LightPushCodec]);
251+
return mockPeer(id, [LightPushCodec, LightPushCodecV2]);
247252
}
248253

249254
function mockV3SuccessResponse(relayPeerCount?: number): {

packages/sdk/src/light_push/light_push.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type LightPushConstructorParams = {
3434
peerManager: PeerManager;
3535
libp2p: Libp2p;
3636
options?: Partial<LightPushProtocolOptions>;
37+
legacy?: boolean;
3738
};
3839

3940
export class LightPush implements ILightPush {
@@ -49,7 +50,7 @@ export class LightPush implements ILightPush {
4950
} as LightPushProtocolOptions;
5051

5152
this.peerManager = params.peerManager;
52-
this.protocol = new LightPushCore(params.libp2p);
53+
this.protocol = new LightPushCore(params.libp2p, params.legacy);
5354
this.retryManager = new RetryManager({
5455
peerManager: params.peerManager,
5556
retryIntervalMs: this.config.retryIntervalMs

packages/sdk/src/waku/waku.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ export class WakuNode implements IWaku {
111111
}
112112

113113
if (protocolsEnabled.lightpush) {
114+
const legacyFlag = (options?.lightPush as any)?.legacy;
114115
this.lightPush = new LightPush({
115116
libp2p,
116117
peerManager: this.peerManager,
117-
options: options?.lightPush
118+
options: options?.lightPush,
119+
legacy: legacyFlag
118120
});
119121
}
120122

packages/tests/tests/light-push/index.node.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import {
2121
TestRoutingInfo
2222
} from "./utils.js";
2323

24-
const runTests = (strictNodeCheck: boolean): void => {
24+
const runTests = (strictNodeCheck: boolean, legacy: boolean): void => {
2525
const numServiceNodes = 2;
26-
describe(`Waku Light Push: Multiple Nodes: Strict Check: ${strictNodeCheck}`, function () {
26+
describe(`Waku Light Push (legacy=${legacy ? "v2" : "v3"}): Multiple Nodes: Strict Check: ${strictNodeCheck}`, function () {
2727
// Set the timeout for all tests in this suite. Can be overwritten at test level
2828
this.timeout(15000);
2929
let waku: LightNode;
@@ -36,7 +36,11 @@ const runTests = (strictNodeCheck: boolean): void => {
3636
{ lightpush: true, filter: true },
3737
strictNodeCheck,
3838
numServiceNodes,
39-
true
39+
true,
40+
{
41+
// Pass legacy flag to createLightNode so that LightPush uses v2 when true
42+
lightPush: { legacy }
43+
} as any
4044
);
4145
});
4246

@@ -257,4 +261,6 @@ const runTests = (strictNodeCheck: boolean): void => {
257261
});
258262
};
259263

260-
[true, false].map(runTests);
264+
[true, false].forEach((strictNodeCheck) => {
265+
[true, false].forEach((legacy) => runTests(strictNodeCheck, legacy));
266+
});

0 commit comments

Comments
 (0)