Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update all references of shard info to RFC terminology #1740

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/enr/src/relay_shard_codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { decodeRelayShard, encodeRelayShard } from "./relay_shard_codec.js";
describe("Relay Shard codec", () => {
// Boundary test case
it("should handle a minimal index list", () => {
const shardInfo = { cluster: 0, indexList: [0] };
const shardInfo = { clusterId: 0, shards: [0] };
const encoded = encodeRelayShard(shardInfo);
const decoded = decodeRelayShard(encoded);
expect(decoded).to.deep.equal(
Expand All @@ -23,8 +23,8 @@ describe("Relay Shard codec", () => {
fc
.array(fc.nat(1023), { minLength: 1, maxLength: 63 }) // indexList
.map((arr) => [...new Set(arr)].sort((a, b) => a - b)),
(cluster, indexList) => {
const shardInfo = { cluster, indexList };
(clusterId, shards) => {
const shardInfo = { clusterId, shards };
const encoded = encodeRelayShard(shardInfo);
const decoded = decodeRelayShard(encoded);

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

Expand Down
26 changes: 13 additions & 13 deletions packages/enr/src/relay_shard_codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
if (bytes.length < 3) throw new Error("Insufficient data");

const view = new DataView(bytes.buffer);
const cluster = view.getUint16(0);
const clusterId = view.getUint16(0);

const indexList = [];
const shards = [];

if (bytes.length === 130) {
// rsv format (Bit Vector)
for (let i = 0; i < 1024; i++) {
const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
const bitIndex = 7 - (i % 8);
if (view.getUint8(byteIndex) & (1 << bitIndex)) {
indexList.push(i);
shards.push(i);
}
}
} else {
// rs format (Index List)
const numIndices = view.getUint8(2);
for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
if (offset + 1 >= bytes.length) throw new Error("Unexpected end of data");
indexList.push(view.getUint16(offset));
shards.push(view.getUint16(offset));
}
}

return { cluster, indexList };
return { clusterId, shards };
};

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

view.setUint16(0, cluster);
view.setUint16(0, clusterId);

if (indexList.length >= 64) {
if (shards.length >= 64) {
// rsv format (Bit Vector)
for (const index of indexList) {
for (const index of shards) {
const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
const bitIndex = 7 - (index % 8);
view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
}
} else {
// rs format (Index List)
view.setUint8(2, indexList.length);
for (let i = 0, offset = 3; i < indexList.length; i++, offset += 2) {
view.setUint16(offset, indexList[i]);
view.setUint8(2, shards.length);
for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
view.setUint16(offset, shards[i]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/interfaces/src/enr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface Waku2 {
}

export interface ShardInfo {
cluster: number;
indexList: number[];
clusterId: number;
shards: number[];
}

export interface IEnr extends Map<ENRKey, ENRValue> {
Expand Down
4 changes: 2 additions & 2 deletions packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { PubsubTopic } from "./misc.js";

export interface SingleShardInfo {
cluster: number;
index: number;
clusterId: number;
shard: number;
}

export interface IRateLimitProof {
Expand Down
1 change: 1 addition & 0 deletions packages/tests/src/node/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Args {
discv5UdpPort?: number;
// `legacyFilter` is required to enable filter v1 with go-waku
legacyFilter?: boolean;
clusterId?: number;
}

export enum LogLevel {
Expand Down
14 changes: 7 additions & 7 deletions packages/tests/tests/filter/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
let messageCollector: MessageCollector;

const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 1
clusterId: 3,
shard: 1
});
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 2
clusterId: 3,
shard: 2
});
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
const customContentTopic1 = "/test/2/waku-filter";
const customContentTopic2 = "/test/3/waku-filter";
const customEncoder1 = createEncoder({
Expand Down
14 changes: 7 additions & 7 deletions packages/tests/tests/light-push/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
let nwaku2: NimGoNode;
let messageCollector: MessageCollector;
const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 1
clusterId: 3,
shard: 1
});
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 2
clusterId: 3,
shard: 2
});
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
const customContentTopic1 = "/test/2/waku-light-push/utf8";
const customContentTopic2 = "/test/3/waku-light-push/utf8";
const customEncoder1 = createEncoder({
Expand Down
22 changes: 11 additions & 11 deletions packages/tests/tests/relay/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ describe("Waku Relay, multiple pubsub topics", function () {
let waku3: RelayNode;

const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 1
clusterId: 3,
shard: 1
});
const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 2
clusterId: 3,
shard: 2
});
const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] };
const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
const singleShardInfo1: SingleShardInfo = {
cluster: 3,
index: 1
clusterId: 3,
shard: 1
};
const customContentTopic1 = "/test/2/waku-relay/utf8";
const customContentTopic2 = "/test/3/waku-relay/utf8";
const shardInfo2: ShardInfo = { cluster: 3, indexList: [2] };
const shardInfo2: ShardInfo = { clusterId: 3, shards: [2] };
const singleShardInfo2: SingleShardInfo = {
cluster: 3,
index: 2
clusterId: 3,
shard: 2
};
const customEncoder1 = createEncoder({
pubsubTopicShardInfo: singleShardInfo1,
Expand All @@ -56,7 +56,7 @@ describe("Waku Relay, multiple pubsub topics", function () {
contentTopic: customContentTopic2
});
const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2);
const shardInfoBothShards: ShardInfo = { cluster: 3, indexList: [1, 2] };
const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };

afterEach(async function () {
this.timeout(15000);
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/tests/relay/publish.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("Waku Relay, Publish", function () {

it("Fails to publish message with wrong pubsubtopic", async function () {
const wrong_encoder = createEncoder({
pubsubTopicShardInfo: { cluster: 3, index: 1 },
pubsubTopicShardInfo: { clusterId: 3, shard: 1 },
contentTopic: TestContentTopic
});
const pushResponse = await waku1.relay.send(wrong_encoder, {
Expand Down
10 changes: 5 additions & 5 deletions packages/tests/tests/sharding/peer_management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe("Static Sharding: Peer Management", function () {
this.timeout(100_000);

const pubsubTopics = [
singleShardInfoToPubsubTopic({ cluster: 18, index: 2 })
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 2 })
];
const shardInfo: ShardInfo = { cluster: 18, indexList: [2] };
const shardInfo: ShardInfo = { clusterId: 18, shards: [2] };

await nwaku1.start({
pubsubTopic: pubsubTopics,
Expand Down Expand Up @@ -112,11 +112,11 @@ describe("Static Sharding: Peer Management", function () {
it("px service nodes not subscribed to the shard should not be dialed", async function () {
this.timeout(100_000);
const pubsubTopicsToDial = [
singleShardInfoToPubsubTopic({ cluster: 18, index: 2 })
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 2 })
];
const shardInfoToDial: ShardInfo = { cluster: 18, indexList: [2] };
const shardInfoToDial: ShardInfo = { clusterId: 18, shards: [2] };
const pubsubTopicsToIgnore = [
singleShardInfoToPubsubTopic({ cluster: 18, index: 1 })
singleShardInfoToPubsubTopic({ clusterId: 18, shard: 1 })
];

// this service node is not subscribed to the shard
Expand Down
16 changes: 8 additions & 8 deletions packages/tests/tests/sharding/running_nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { makeLogFileName } from "../../src/log_file.js";
import { NimGoNode } from "../../src/node/node.js";

const PubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 0,
index: 2
clusterId: 0,
shard: 2
});
const PubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 0,
index: 3
clusterId: 0,
shard: 3
});
const shardInfoFirstShard: ShardInfo = { cluster: 0, indexList: [2] };
const shardInfoBothShards: ShardInfo = { cluster: 0, indexList: [2, 3] };
const singleShardInfo1: SingleShardInfo = { cluster: 0, index: 2 };
const singleShardInfo2: SingleShardInfo = { cluster: 0, index: 3 };
const shardInfoFirstShard: ShardInfo = { clusterId: 0, shards: [2] };
const shardInfoBothShards: ShardInfo = { clusterId: 0, shards: [2, 3] };
const singleShardInfo1: SingleShardInfo = { clusterId: 0, shard: 2 };
const singleShardInfo2: SingleShardInfo = { clusterId: 0, shard: 3 };
const ContentTopic = "/waku/2/content/test.js";

describe("Static Sharding: Running Nodes", () => {
Expand Down
20 changes: 10 additions & 10 deletions packages/tests/tests/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ export const TestContentTopic = "/test/1/waku-store/utf8";
export const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
export const TestDecoder = createDecoder(TestContentTopic);
export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 1
clusterId: 3,
shard: 1
});
export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3,
index: 2
clusterId: 3,
shard: 2
});
export const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] };
export const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
export const customContentTopic1 = "/test/2/waku-store/utf8";
export const customContentTopic2 = "/test/3/waku-store/utf8";
export const customDecoder1 = createDecoder(customContentTopic1, {
cluster: 3,
index: 1
clusterId: 3,
shard: 1
});
export const customDecoder2 = createDecoder(customContentTopic2, {
cluster: 3,
index: 2
clusterId: 3,
shard: 2
});
export const shardInfoBothShards: ShardInfo = { cluster: 3, indexList: [1, 2] };
export const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };
export const totalMsgs = 20;
export const messageText = "Store Push works!";

Expand Down
32 changes: 22 additions & 10 deletions packages/utils/src/common/sharding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@ import { concat, utf8ToBytes } from "../bytes/index.js";
export const singleShardInfoToPubsubTopic = (
shardInfo: SingleShardInfo
): PubsubTopic => {
if (shardInfo.cluster === undefined || shardInfo.index === undefined)
if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
throw new Error("Invalid shard");

return `/waku/2/rs/${shardInfo.cluster}/${shardInfo.index}`;
return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
};

export const shardInfoToPubsubTopics = (
shardInfo: ShardInfo
): PubsubTopic[] => {
if (shardInfo.cluster === undefined || shardInfo.indexList === undefined)
if (shardInfo.clusterId === undefined || shardInfo.shards === undefined)
throw new Error("Invalid shard");

return shardInfo.indexList.map(
(index) => `/waku/2/rs/${shardInfo.cluster}/${index}`
return shardInfo.shards.map(
(index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`
);
};

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

const cluster = parseInt(parts[4]);
const index = parseInt(parts[5]);
if (isNaN(cluster) || isNaN(index)) throw new Error("Invalid pubsub topic");
if (
parts.length != 6 ||
parts[1] !== "waku" ||
parts[2] !== "2" ||
parts[3] !== "rs"
)
throw new Error("Invalid pubsub topic");

return { cluster, index };
const clusterId = parseInt(parts[4]);
const shard = parseInt(parts[5]);

if (isNaN(clusterId) || isNaN(shard))
throw new Error("Invalid clusterId or shard");

return {
clusterId,
shard
};
};

export function ensurePubsubTopicIsConfigured(
Expand Down
Loading