Skip to content

Commit 68d3229

Browse files
committed
feat: make ShardingParams optional in sdk, required internally
1 parent f772dc3 commit 68d3229

File tree

15 files changed

+189
-79
lines changed

15 files changed

+189
-79
lines changed

packages/core/src/lib/base_protocol.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import type {
77
PubsubTopic
88
} from "@waku/interfaces";
99
import { DefaultPubsubTopic } from "@waku/interfaces";
10-
import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
10+
import {
11+
ensureShardingConfigured,
12+
Logger,
13+
shardInfoToPubsubTopics
14+
} from "@waku/utils";
1115
import {
1216
getConnectedPeersForProtocolAndShard,
1317
getPeersForProtocol,
@@ -108,6 +112,8 @@ export class BaseProtocol implements IBaseProtocol {
108112
this.peerStore,
109113
[this.multicodec],
110114
this.options?.shardInfo
115+
? ensureShardingConfigured(this.options.shardInfo).shardInfo
116+
: undefined
111117
);
112118

113119
// Filter the peers based on discovery & number of peers requested

packages/core/src/lib/metadata/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Metadata extends BaseProtocol implements IMetadata {
132132
}
133133

134134
export function wakuMetadata(
135-
shardInfo: ShardingParams
135+
shardInfo: ShardInfo
136136
): (components: Libp2pComponents) => IMetadata {
137137
return (components: Libp2pComponents) => new Metadata(shardInfo, components);
138138
}

packages/interfaces/src/constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
33
*/
44
export const DefaultPubsubTopic = "/waku/2/default-waku/proto";
5+
6+
/**
7+
* The default cluster ID for The Waku Network
8+
*/
9+
export const DEFAULT_CLUSTER_ID = 1;

packages/interfaces/src/message.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface SingleShardInfo {
55
/**
66
* Specifying this field indicates to the encoder/decoder that static sharding must be used.
77
*/
8-
shard?: number;
8+
shard: number;
99
}
1010

1111
export interface IRateLimitProof {

packages/interfaces/src/protocols.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export type ProtocolCreateOptions = {
6060
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
6161
*
6262
*/
63-
shardInfo?: ShardingParams;
63+
shardInfo?: Partial<ShardingParams>;
6464
/**
6565
* You can pass options to the `Libp2p` instance used by {@link @waku/core!WakuNode} using the `libp2p` property.
6666
* This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)

packages/sdk/src/create.ts

+24-34
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ import {
1616
wakuStore
1717
} from "@waku/core";
1818
import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery";
19-
import type {
20-
CreateLibp2pOptions,
21-
FullNode,
22-
IMetadata,
23-
Libp2p,
24-
Libp2pComponents,
25-
LightNode,
26-
ProtocolCreateOptions,
27-
ShardingParams
19+
import {
20+
type CreateLibp2pOptions,
21+
type FullNode,
22+
type IMetadata,
23+
type Libp2p,
24+
type Libp2pComponents,
25+
type LightNode,
26+
type ProtocolCreateOptions,
27+
type ShardInfo
2828
} from "@waku/interfaces";
2929
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
3030
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
31+
import { ensureShardingConfigured } from "@waku/utils";
3132
import { createLibp2p } from "libp2p";
3233

3334
const DEFAULT_NODE_REQUIREMENTS = {
@@ -38,17 +39,6 @@ const DEFAULT_NODE_REQUIREMENTS = {
3839

3940
export { Libp2pComponents };
4041

41-
const ensureShardingConfigured = (shardInfo: ShardingParams): void => {
42-
if (
43-
("shards" in shardInfo && shardInfo.shards.length < 1) ||
44-
("contentTopics" in shardInfo && shardInfo.contentTopics.length < 1)
45-
) {
46-
throw new Error(
47-
"Missing required configuration options for static sharding or autosharding."
48-
);
49-
}
50-
};
51-
5242
/**
5343
* Create a Waku node configured to use autosharding or static sharding.
5444
*/
@@ -61,7 +51,7 @@ export async function createNode(
6151
throw new Error("Shard info must be set");
6252
}
6353

64-
ensureShardingConfigured(options.shardInfo);
54+
const shardInfo = ensureShardingConfigured(options.shardInfo);
6555

6656
const libp2pOptions = options?.libp2p ?? {};
6757
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
@@ -71,7 +61,7 @@ export async function createNode(
7161
}
7262

7363
const libp2p = await defaultLibp2p(
74-
undefined,
64+
shardInfo.shardInfo,
7565
wakuGossipSub(options),
7666
libp2pOptions,
7767
options?.userAgent
@@ -85,7 +75,7 @@ export async function createNode(
8575
options ?? {},
8676
[],
8777
libp2p,
88-
options.shardInfo,
78+
shardInfo.shardInfo,
8979
store,
9080
lightPush,
9181
filter
@@ -102,9 +92,9 @@ export async function createLightNode(
10292
): Promise<LightNode> {
10393
options = options ?? {};
10494

105-
if (options.shardInfo) {
106-
ensureShardingConfigured(options.shardInfo);
107-
}
95+
const shardInfo = options.shardInfo
96+
? ensureShardingConfigured(options.shardInfo)
97+
: undefined;
10898

10999
const libp2pOptions = options?.libp2p ?? {};
110100
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
@@ -114,7 +104,7 @@ export async function createLightNode(
114104
}
115105

116106
const libp2p = await defaultLibp2p(
117-
options.shardInfo,
107+
shardInfo?.shardInfo,
118108
wakuGossipSub(options),
119109
libp2pOptions,
120110
options?.userAgent
@@ -128,7 +118,7 @@ export async function createLightNode(
128118
options ?? {},
129119
options.pubsubTopics,
130120
libp2p,
131-
options.shardInfo,
121+
shardInfo?.shardingParams,
132122
store,
133123
lightPush,
134124
filter
@@ -153,9 +143,9 @@ export async function createFullNode(
153143
): Promise<FullNode> {
154144
options = options ?? {};
155145

156-
if (options.shardInfo) {
157-
ensureShardingConfigured(options.shardInfo);
158-
}
146+
const shardInfo = options.shardInfo
147+
? ensureShardingConfigured(options.shardInfo)
148+
: undefined;
159149

160150
const libp2pOptions = options?.libp2p ?? {};
161151
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
@@ -165,7 +155,7 @@ export async function createFullNode(
165155
}
166156

167157
const libp2p = await defaultLibp2p(
168-
options.shardInfo,
158+
shardInfo?.shardInfo,
169159
wakuGossipSub(options),
170160
libp2pOptions,
171161
options?.userAgent
@@ -180,7 +170,7 @@ export async function createFullNode(
180170
options ?? {},
181171
options.pubsubTopics,
182172
libp2p,
183-
options.shardInfo,
173+
shardInfo?.shardingParams,
184174
store,
185175
lightPush,
186176
filter,
@@ -207,7 +197,7 @@ type MetadataService = {
207197
};
208198

209199
export async function defaultLibp2p(
210-
shardInfo?: ShardingParams,
200+
shardInfo?: ShardInfo,
211201
wakuGossipSub?: PubsubService["pubsub"],
212202
options?: Partial<CreateLibp2pOptions>,
213203
userAgent?: string

packages/sdk/src/relay/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WakuNode, WakuOptions } from "@waku/core";
22
import type { ProtocolCreateOptions, RelayNode } from "@waku/interfaces";
33
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
4+
import { ensureShardingConfigured } from "@waku/utils";
45

56
import { defaultLibp2p, defaultPeerDiscoveries } from "../create.js";
67

@@ -26,8 +27,12 @@ export async function createRelayNode(
2627
Object.assign(libp2pOptions, { peerDiscovery });
2728
}
2829

30+
const shardInfo = options.shardInfo
31+
? ensureShardingConfigured(options.shardInfo)
32+
: undefined;
33+
2934
const libp2p = await defaultLibp2p(
30-
options.shardInfo,
35+
shardInfo?.shardInfo,
3136
wakuGossipSub(options),
3237
libp2pOptions,
3338
options?.userAgent
@@ -39,7 +44,7 @@ export async function createRelayNode(
3944
options,
4045
options.pubsubTopics,
4146
libp2p,
42-
options.shardInfo,
47+
shardInfo?.shardingParams,
4348
undefined,
4449
undefined,
4550
undefined,

packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
import { Protocols } from "@waku/interfaces";
1010
import {
1111
contentTopicToPubsubTopic,
12+
contentTopicToShardIndex,
1213
pubsubTopicToSingleShardInfo,
1314
singleShardInfoToPubsubTopic
1415
} from "@waku/utils";
@@ -207,17 +208,25 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
207208
const customEncoder1 = createEncoder({
208209
contentTopic: customContentTopic1,
209210
pubsubTopicShardInfo: {
210-
clusterId: 3
211+
clusterId: 3,
212+
shard: contentTopicToShardIndex(customContentTopic1)
211213
}
212214
});
213-
const customDecoder1 = createDecoder(customContentTopic1, { clusterId: 3 });
215+
const customDecoder1 = createDecoder(customContentTopic1, {
216+
clusterId: 3,
217+
shard: contentTopicToShardIndex(customContentTopic1)
218+
});
214219
const customEncoder2 = createEncoder({
215220
contentTopic: customContentTopic2,
216221
pubsubTopicShardInfo: {
217-
clusterId: 3
222+
clusterId: 3,
223+
shard: contentTopicToShardIndex(customContentTopic2)
218224
}
219225
});
220-
const customDecoder2 = createDecoder(customContentTopic2, { clusterId: 3 });
226+
const customDecoder2 = createDecoder(customContentTopic2, {
227+
clusterId: 3,
228+
shard: contentTopicToShardIndex(customContentTopic2)
229+
});
221230

222231
this.beforeEach(async function () {
223232
this.timeout(15000);

packages/tests/tests/getPeers.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
Tags,
1212
utf8ToBytes
1313
} from "@waku/sdk";
14-
import { shardInfoToPubsubTopics } from "@waku/utils";
14+
import { ensureShardingConfigured, shardInfoToPubsubTopics } from "@waku/utils";
1515
import { getConnectedPeersForProtocolAndShard } from "@waku/utils/libp2p";
1616
import { expect } from "chai";
1717
import fc from "fast-check";
@@ -237,7 +237,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
237237
waku.libp2p.getConnections(),
238238
waku.libp2p.peerStore,
239239
waku.libp2p.getProtocols(),
240-
shardInfo
240+
ensureShardingConfigured(shardInfo).shardInfo
241241
);
242242
expect(peers.length).to.be.greaterThan(0);
243243
});
@@ -289,7 +289,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
289289
waku.libp2p.getConnections(),
290290
waku.libp2p.peerStore,
291291
waku.libp2p.getProtocols(),
292-
shardInfo2
292+
ensureShardingConfigured(shardInfo2).shardInfo
293293
);
294294
expect(peers.length).to.be.equal(1);
295295
});
@@ -341,7 +341,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
341341
waku.libp2p.getConnections(),
342342
waku.libp2p.peerStore,
343343
waku.libp2p.getProtocols(),
344-
shardInfo2
344+
ensureShardingConfigured(shardInfo2).shardInfo
345345
);
346346
expect(peers.length).to.be.equal(1);
347347
});
@@ -393,7 +393,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
393393
waku.libp2p.getConnections(),
394394
waku.libp2p.peerStore,
395395
waku.libp2p.getProtocols(),
396-
shardInfo2
396+
ensureShardingConfigured(shardInfo2).shardInfo
397397
);
398398
expect(peers.length).to.be.equal(1);
399399
});

packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
} from "@waku/interfaces";
1111
import {
1212
contentTopicToPubsubTopic,
13+
contentTopicToShardIndex,
14+
pubsubTopicToSingleShardInfo,
1315
singleShardInfoToPubsubTopic
1416
} from "@waku/utils";
1517
import { utf8ToBytes } from "@waku/utils/bytes";
@@ -202,11 +204,11 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () {
202204
};
203205
const customEncoder1 = createEncoder({
204206
contentTopic: customContentTopic1,
205-
pubsubTopicShardInfo: shardInfo
207+
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
206208
});
207209
const customEncoder2 = createEncoder({
208210
contentTopic: customContentTopic2,
209-
pubsubTopicShardInfo: shardInfo
211+
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
210212
});
211213

212214
let nimPeerId: PeerId;
@@ -356,12 +358,16 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function ()
356358
const customEncoder1 = createEncoder({
357359
contentTopic: customContentTopic1,
358360
pubsubTopicShardInfo: {
359-
clusterId
361+
clusterId,
362+
shard: contentTopicToShardIndex(customContentTopic1)
360363
}
361364
});
362365
const customEncoder2 = createEncoder({
363366
contentTopic: customContentTopic2,
364-
pubsubTopicShardInfo: { clusterId }
367+
pubsubTopicShardInfo: {
368+
clusterId,
369+
shard: contentTopicToShardIndex(customContentTopic2)
370+
}
365371
});
366372

367373
let nimPeerId: PeerId;

packages/tests/tests/relay/multiple_pubsub.node.spec.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Protocols } from "@waku/interfaces";
1414
import { createRelayNode } from "@waku/sdk/relay";
1515
import {
1616
contentTopicToPubsubTopic,
17+
pubsubTopicToSingleShardInfo,
1718
singleShardInfoToPubsubTopic
1819
} from "@waku/utils";
1920
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
@@ -340,16 +341,20 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () {
340341
};
341342
const customEncoder1 = createEncoder({
342343
contentTopic: customContentTopic1,
343-
pubsubTopicShardInfo: {
344-
clusterId: 3
345-
}
344+
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
346345
});
347-
const customDecoder1 = createDecoder(customContentTopic1, { clusterId: 3 });
346+
const customDecoder1 = createDecoder(
347+
customContentTopic1,
348+
pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
349+
);
348350
const customEncoder2 = createEncoder({
349351
contentTopic: customContentTopic2,
350-
pubsubTopicShardInfo: { clusterId: 3 }
352+
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
351353
});
352-
const customDecoder2 = createDecoder(customContentTopic2, { clusterId: 3 });
354+
const customDecoder2 = createDecoder(
355+
customContentTopic2,
356+
pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
357+
);
353358
const contentTopicInfoBothShards: ContentTopicInfo = {
354359
clusterId: 3,
355360
contentTopics: [customContentTopic1, customContentTopic2]

0 commit comments

Comments
 (0)