Skip to content

Commit 47b95f1

Browse files
update shardInfo terminology with RFC throughout the codebase
1 parent 96494bf commit 47b95f1

15 files changed

+81
-82
lines changed

packages/core/src/lib/message/version_0.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function createEncoder({
128128
return new Encoder(
129129
contentTopic,
130130
ephemeral,
131-
pubsubTopicShardInfo?.index
131+
pubsubTopicShardInfo?.clusterId
132132
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
133133
: DefaultPubsubTopic,
134134
metaSetter
@@ -193,7 +193,7 @@ export function createDecoder(
193193
pubsubTopicShardInfo?: SingleShardInfo
194194
): Decoder {
195195
return new Decoder(
196-
pubsubTopicShardInfo?.index
196+
pubsubTopicShardInfo?.clusterId
197197
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
198198
: DefaultPubsubTopic,
199199
contentTopic

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ class Metadata extends BaseProtocol {
5353
throw new Error("No response in query");
5454
}
5555

56-
const { shards, clusterId } = response;
57-
return {
58-
cluster: clusterId,
59-
indexList: shards
60-
} as ShardInfo;
56+
return response as ShardInfo;
6157
} catch (e) {
6258
log.error(`Error decoding response: ${e}`);
6359
throw e;

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/message-encryption/src/ecies.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function createEncoder({
107107
metaSetter
108108
}: EncoderOptions): Encoder {
109109
return new Encoder(
110-
pubsubTopicShardInfo?.index
110+
pubsubTopicShardInfo?.clusterId
111111
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
112112
: DefaultPubsubTopic,
113113
contentTopic,
@@ -200,7 +200,7 @@ export function createDecoder(
200200
pubsubTopicShardInfo?: SingleShardInfo
201201
): Decoder {
202202
return new Decoder(
203-
pubsubTopicShardInfo?.index
203+
pubsubTopicShardInfo?.clusterId
204204
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
205205
: DefaultPubsubTopic,
206206
contentTopic,

packages/message-encryption/src/symmetric.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function createEncoder({
107107
metaSetter
108108
}: EncoderOptions): Encoder {
109109
return new Encoder(
110-
pubsubTopicShardInfo?.index
110+
pubsubTopicShardInfo?.clusterId
111111
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
112112
: DefaultPubsubTopic,
113113
contentTopic,
@@ -200,7 +200,7 @@ export function createDecoder(
200200
pubsubTopicShardInfo?: SingleShardInfo
201201
): Decoder {
202202
return new Decoder(
203-
pubsubTopicShardInfo?.index
203+
pubsubTopicShardInfo?.clusterId
204204
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
205205
: DefaultPubsubTopic,
206206
contentTopic,

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ 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 = (
@@ -20,7 +20,9 @@ export const shardInfoToPubsubTopics = (
2020
);
2121
};
2222

23-
export const pubsubTopicToShardInfo = (topic: PubsubTopic): ShardInfo => {
23+
export const pubsubTopicToSingleShardInfo = (
24+
topic: PubsubTopic
25+
): SingleShardInfo => {
2426
const parts = topic.split("/"); // Split the topic string into parts
2527

2628
if (
@@ -34,7 +36,7 @@ export const pubsubTopicToShardInfo = (topic: PubsubTopic): ShardInfo => {
3436

3537
return {
3638
clusterId: parseInt(cluster),
37-
shards: [parseInt(index)]
39+
shard: parseInt(index)
3840
};
3941
} else {
4042
throw new Error(

0 commit comments

Comments
 (0)