Skip to content

Commit ee2b446

Browse files
chore: update all references of shard info to RFC terminology (#1740)
* update all references of shard info to RFC terminology * bump nwaku to v0.22.0 (#1741)
1 parent e2c2741 commit ee2b446

13 files changed

+94
-81
lines changed

packages/enr/src/relay_shard_codec.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { decodeRelayShard, encodeRelayShard } from "./relay_shard_codec.js";
66
describe("Relay Shard codec", () => {
77
// Boundary test case
88
it("should handle a minimal index list", () => {
9-
const shardInfo = { cluster: 0, indexList: [0] };
9+
const shardInfo = { clusterId: 0, shards: [0] };
1010
const encoded = encodeRelayShard(shardInfo);
1111
const decoded = decodeRelayShard(encoded);
1212
expect(decoded).to.deep.equal(
@@ -23,8 +23,8 @@ describe("Relay Shard codec", () => {
2323
fc
2424
.array(fc.nat(1023), { minLength: 1, maxLength: 63 }) // indexList
2525
.map((arr) => [...new Set(arr)].sort((a, b) => a - b)),
26-
(cluster, indexList) => {
27-
const shardInfo = { cluster, indexList };
26+
(clusterId, shards) => {
27+
const shardInfo = { clusterId, shards };
2828
const encoded = encodeRelayShard(shardInfo);
2929
const decoded = decodeRelayShard(encoded);
3030

@@ -45,8 +45,8 @@ describe("Relay Shard codec", () => {
4545
fc
4646
.array(fc.nat(1023), { minLength: 64, maxLength: 1024 }) // indexList
4747
.map((arr) => [...new Set(arr)].sort((a, b) => a - b)),
48-
(cluster, indexList) => {
49-
const shardInfo = { cluster, indexList };
48+
(clusterId, shards) => {
49+
const shardInfo = { clusterId, shards };
5050
const encoded = encodeRelayShard(shardInfo);
5151
const decoded = decodeRelayShard(encoded);
5252

packages/enr/src/relay_shard_codec.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,51 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
88
if (bytes.length < 3) throw new Error("Insufficient data");
99

1010
const view = new DataView(bytes.buffer);
11-
const cluster = view.getUint16(0);
11+
const clusterId = view.getUint16(0);
1212

13-
const indexList = [];
13+
const shards = [];
1414

1515
if (bytes.length === 130) {
1616
// rsv format (Bit Vector)
1717
for (let i = 0; i < 1024; i++) {
1818
const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
1919
const bitIndex = 7 - (i % 8);
2020
if (view.getUint8(byteIndex) & (1 << bitIndex)) {
21-
indexList.push(i);
21+
shards.push(i);
2222
}
2323
}
2424
} else {
2525
// rs format (Index List)
2626
const numIndices = view.getUint8(2);
2727
for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
2828
if (offset + 1 >= bytes.length) throw new Error("Unexpected end of data");
29-
indexList.push(view.getUint16(offset));
29+
shards.push(view.getUint16(offset));
3030
}
3131
}
3232

33-
return { cluster, indexList };
33+
return { clusterId, shards };
3434
};
3535

3636
export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => {
37-
const { cluster, indexList } = shardInfo;
38-
const totalLength = indexList.length >= 64 ? 130 : 3 + 2 * indexList.length;
37+
const { clusterId, shards } = shardInfo;
38+
const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
3939
const buffer = new ArrayBuffer(totalLength);
4040
const view = new DataView(buffer);
4141

42-
view.setUint16(0, cluster);
42+
view.setUint16(0, clusterId);
4343

44-
if (indexList.length >= 64) {
44+
if (shards.length >= 64) {
4545
// rsv format (Bit Vector)
46-
for (const index of indexList) {
46+
for (const index of shards) {
4747
const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
4848
const bitIndex = 7 - (index % 8);
4949
view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
5050
}
5151
} else {
5252
// rs format (Index List)
53-
view.setUint8(2, indexList.length);
54-
for (let i = 0, offset = 3; i < indexList.length; i++, offset += 2) {
55-
view.setUint16(offset, indexList[i]);
53+
view.setUint8(2, shards.length);
54+
for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
55+
view.setUint16(offset, shards[i]);
5656
}
5757
}
5858

packages/interfaces/src/enr.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export interface Waku2 {
2020

2121
//TODO: adhere to terminology from the metadata protocol
2222
export interface ShardInfo {
23-
cluster: number;
24-
indexList: number[];
23+
clusterId: number;
24+
shards: number[];
2525
}
2626

2727
export interface IEnr extends Map<ENRKey, ENRValue> {

packages/interfaces/src/message.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { PubsubTopic } from "./misc.js";
22

33
export interface SingleShardInfo {
4-
cluster: number;
5-
index: number;
4+
clusterId: number;
5+
shard: number;
66
}
77

88
export interface IRateLimitProof {

packages/tests/src/node/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Args {
2424
discv5UdpPort?: number;
2525
// `legacyFilter` is required to enable filter v1 with go-waku
2626
legacyFilter?: boolean;
27+
clusterId?: number;
2728
}
2829

2930
export enum LogLevel {

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
3232
let messageCollector: MessageCollector;
3333

3434
const customPubsubTopic1 = singleShardInfoToPubsubTopic({
35-
cluster: 3,
36-
index: 1
35+
clusterId: 3,
36+
shard: 1
3737
});
3838
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
39-
cluster: 3,
40-
index: 2
39+
clusterId: 3,
40+
shard: 2
4141
});
42-
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
43-
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
44-
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
42+
const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
43+
const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
44+
const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
4545
const customContentTopic1 = "/test/2/waku-filter";
4646
const customContentTopic2 = "/test/3/waku-filter";
4747
const customEncoder1 = createEncoder({

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
2727
let nwaku2: NimGoNode;
2828
let messageCollector: MessageCollector;
2929
const customPubsubTopic1 = singleShardInfoToPubsubTopic({
30-
cluster: 3,
31-
index: 1
30+
clusterId: 3,
31+
shard: 1
3232
});
3333
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
34-
cluster: 3,
35-
index: 2
34+
clusterId: 3,
35+
shard: 2
3636
});
37-
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
38-
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
39-
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
37+
const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
38+
const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
39+
const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
4040
const customContentTopic1 = "/test/2/waku-light-push/utf8";
4141
const customContentTopic2 = "/test/3/waku-light-push/utf8";
4242
const customEncoder1 = createEncoder({

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ describe("Waku Relay, multiple pubsub topics", function () {
2727
let waku3: RelayNode;
2828

2929
const customPubsubTopic1 = singleShardInfoToPubsubTopic({
30-
cluster: 3,
31-
index: 1
30+
clusterId: 3,
31+
shard: 1
3232
});
3333
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
34-
cluster: 3,
35-
index: 2
34+
clusterId: 3,
35+
shard: 2
3636
});
37-
const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] };
37+
const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
3838
const singleShardInfo1: SingleShardInfo = {
39-
cluster: 3,
40-
index: 1
39+
clusterId: 3,
40+
shard: 1
4141
};
4242
const customContentTopic1 = "/test/2/waku-relay/utf8";
4343
const customContentTopic2 = "/test/3/waku-relay/utf8";
44-
const shardInfo2: ShardInfo = { cluster: 3, indexList: [2] };
44+
const shardInfo2: ShardInfo = { clusterId: 3, shards: [2] };
4545
const singleShardInfo2: SingleShardInfo = {
46-
cluster: 3,
47-
index: 2
46+
clusterId: 3,
47+
shard: 2
4848
};
4949
const customEncoder1 = createEncoder({
5050
pubsubTopicShardInfo: singleShardInfo1,
@@ -56,7 +56,7 @@ describe("Waku Relay, multiple pubsub topics", function () {
5656
contentTopic: customContentTopic2
5757
});
5858
const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2);
59-
const shardInfoBothShards: ShardInfo = { cluster: 3, indexList: [1, 2] };
59+
const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };
6060

6161
afterEach(async function () {
6262
this.timeout(15000);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe("Waku Relay, Publish", function () {
128128

129129
it("Fails to publish message with wrong pubsubtopic", async function () {
130130
const wrong_encoder = createEncoder({
131-
pubsubTopicShardInfo: { cluster: 3, index: 1 },
131+
pubsubTopicShardInfo: { clusterId: 3, shard: 1 },
132132
contentTopic: TestContentTopic
133133
});
134134
const pushResponse = await waku1.relay.send(wrong_encoder, {

packages/tests/tests/sharding/peer_management.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ describe("Static Sharding: Peer Management", function () {
4040
this.timeout(100_000);
4141

4242
const pubsubTopics = [
43-
singleShardInfoToPubsubTopic({ cluster: 18, index: 2 })
43+
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 2 })
4444
];
45-
const shardInfo: ShardInfo = { cluster: 18, indexList: [2] };
45+
const shardInfo: ShardInfo = { clusterId: 18, shards: [2] };
4646

4747
await nwaku1.start({
4848
pubsubTopic: pubsubTopics,
@@ -112,11 +112,11 @@ describe("Static Sharding: Peer Management", function () {
112112
it("px service nodes not subscribed to the shard should not be dialed", async function () {
113113
this.timeout(100_000);
114114
const pubsubTopicsToDial = [
115-
singleShardInfoToPubsubTopic({ cluster: 18, index: 2 })
115+
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 2 })
116116
];
117-
const shardInfoToDial: ShardInfo = { cluster: 18, indexList: [2] };
117+
const shardInfoToDial: ShardInfo = { clusterId: 18, shards: [2] };
118118
const pubsubTopicsToIgnore = [
119-
singleShardInfoToPubsubTopic({ cluster: 18, index: 1 })
119+
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 1 })
120120
];
121121

122122
// this service node is not subscribed to the shard

packages/tests/tests/sharding/running_nodes.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import { makeLogFileName } from "../../src/log_file.js";
88
import { NimGoNode } from "../../src/node/node.js";
99

1010
const PubsubTopic1 = singleShardInfoToPubsubTopic({
11-
cluster: 0,
12-
index: 2
11+
clusterId: 0,
12+
shard: 2
1313
});
1414
const PubsubTopic2 = singleShardInfoToPubsubTopic({
15-
cluster: 0,
16-
index: 3
15+
clusterId: 0,
16+
shard: 3
1717
});
18-
const shardInfoFirstShard: ShardInfo = { cluster: 0, indexList: [2] };
19-
const shardInfoBothShards: ShardInfo = { cluster: 0, indexList: [2, 3] };
20-
const singleShardInfo1: SingleShardInfo = { cluster: 0, index: 2 };
21-
const singleShardInfo2: SingleShardInfo = { cluster: 0, index: 3 };
18+
const shardInfoFirstShard: ShardInfo = { clusterId: 0, shards: [2] };
19+
const shardInfoBothShards: ShardInfo = { clusterId: 0, shards: [2, 3] };
20+
const singleShardInfo1: SingleShardInfo = { clusterId: 0, shard: 2 };
21+
const singleShardInfo2: SingleShardInfo = { clusterId: 0, shard: 3 };
2222
const ContentTopic = "/waku/2/content/test.js";
2323

2424
describe("Static Sharding: Running Nodes", () => {

packages/tests/tests/store/utils.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ export const TestContentTopic = "/test/1/waku-store/utf8";
1919
export const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
2020
export const TestDecoder = createDecoder(TestContentTopic);
2121
export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({
22-
cluster: 3,
23-
index: 1
22+
clusterId: 3,
23+
shard: 1
2424
});
2525
export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({
26-
cluster: 3,
27-
index: 2
26+
clusterId: 3,
27+
shard: 2
2828
});
29-
export const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] };
29+
export const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
3030
export const customContentTopic1 = "/test/2/waku-store/utf8";
3131
export const customContentTopic2 = "/test/3/waku-store/utf8";
3232
export const customDecoder1 = createDecoder(customContentTopic1, {
33-
cluster: 3,
34-
index: 1
33+
clusterId: 3,
34+
shard: 1
3535
});
3636
export const customDecoder2 = createDecoder(customContentTopic2, {
37-
cluster: 3,
38-
index: 2
37+
clusterId: 3,
38+
shard: 2
3939
});
40-
export const shardInfoBothShards: ShardInfo = { cluster: 3, indexList: [1, 2] };
40+
export const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };
4141
export const totalMsgs = 20;
4242
export const messageText = "Store Push works!";
4343

packages/utils/src/common/sharding.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,46 @@ import { concat, utf8ToBytes } from "../bytes/index.js";
66
export const singleShardInfoToPubsubTopic = (
77
shardInfo: SingleShardInfo
88
): PubsubTopic => {
9-
if (shardInfo.cluster === undefined || shardInfo.index === undefined)
9+
if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
1010
throw new Error("Invalid shard");
1111

12-
return `/waku/2/rs/${shardInfo.cluster}/${shardInfo.index}`;
12+
return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
1313
};
1414

1515
export const shardInfoToPubsubTopics = (
1616
shardInfo: ShardInfo
1717
): PubsubTopic[] => {
18-
if (shardInfo.cluster === undefined || shardInfo.indexList === undefined)
18+
if (shardInfo.clusterId === undefined || shardInfo.shards === undefined)
1919
throw new Error("Invalid shard");
2020

21-
return shardInfo.indexList.map(
22-
(index) => `/waku/2/rs/${shardInfo.cluster}/${index}`
21+
return shardInfo.shards.map(
22+
(index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`
2323
);
2424
};
2525

2626
export const pubsubTopicToSingleShardInfo = (
2727
pubsubTopics: PubsubTopic
2828
): SingleShardInfo => {
2929
const parts = pubsubTopics.split("/");
30-
if (parts.length != 6) throw new Error("Invalid pubsub topic");
3130

32-
const cluster = parseInt(parts[4]);
33-
const index = parseInt(parts[5]);
34-
if (isNaN(cluster) || isNaN(index)) throw new Error("Invalid pubsub topic");
31+
if (
32+
parts.length != 6 ||
33+
parts[1] !== "waku" ||
34+
parts[2] !== "2" ||
35+
parts[3] !== "rs"
36+
)
37+
throw new Error("Invalid pubsub topic");
3538

36-
return { cluster, index };
39+
const clusterId = parseInt(parts[4]);
40+
const shard = parseInt(parts[5]);
41+
42+
if (isNaN(clusterId) || isNaN(shard))
43+
throw new Error("Invalid clusterId or shard");
44+
45+
return {
46+
clusterId,
47+
shard
48+
};
3749
};
3850

3951
export function ensurePubsubTopicIsConfigured(

0 commit comments

Comments
 (0)