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)
This commit is contained in:
Danish Arora 2023-11-29 17:37:59 +05:30 committed by GitHub
parent 7ce642c2cc
commit bbd372120a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 94 additions and 81 deletions

View File

@ -6,7 +6,7 @@ import { decodeRelayShard, encodeRelayShard } from "./relay_shard_codec.js";
describe("Relay Shard codec", () => { describe("Relay Shard codec", () => {
// Boundary test case // Boundary test case
it("should handle a minimal index list", () => { it("should handle a minimal index list", () => {
const shardInfo = { cluster: 0, indexList: [0] }; const shardInfo = { clusterId: 0, shards: [0] };
const encoded = encodeRelayShard(shardInfo); const encoded = encodeRelayShard(shardInfo);
const decoded = decodeRelayShard(encoded); const decoded = decodeRelayShard(encoded);
expect(decoded).to.deep.equal( expect(decoded).to.deep.equal(
@ -23,8 +23,8 @@ describe("Relay Shard codec", () => {
fc fc
.array(fc.nat(1023), { minLength: 1, maxLength: 63 }) // indexList .array(fc.nat(1023), { minLength: 1, maxLength: 63 }) // indexList
.map((arr) => [...new Set(arr)].sort((a, b) => a - b)), .map((arr) => [...new Set(arr)].sort((a, b) => a - b)),
(cluster, indexList) => { (clusterId, shards) => {
const shardInfo = { cluster, indexList }; const shardInfo = { clusterId, shards };
const encoded = encodeRelayShard(shardInfo); const encoded = encodeRelayShard(shardInfo);
const decoded = decodeRelayShard(encoded); const decoded = decodeRelayShard(encoded);
@ -45,8 +45,8 @@ describe("Relay Shard codec", () => {
fc fc
.array(fc.nat(1023), { minLength: 64, maxLength: 1024 }) // indexList .array(fc.nat(1023), { minLength: 64, maxLength: 1024 }) // indexList
.map((arr) => [...new Set(arr)].sort((a, b) => a - b)), .map((arr) => [...new Set(arr)].sort((a, b) => a - b)),
(cluster, indexList) => { (clusterId, shards) => {
const shardInfo = { cluster, indexList }; const shardInfo = { clusterId, shards };
const encoded = encodeRelayShard(shardInfo); const encoded = encodeRelayShard(shardInfo);
const decoded = decodeRelayShard(encoded); const decoded = decodeRelayShard(encoded);

View File

@ -8,9 +8,9 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
if (bytes.length < 3) throw new Error("Insufficient data"); if (bytes.length < 3) throw new Error("Insufficient data");
const view = new DataView(bytes.buffer); const view = new DataView(bytes.buffer);
const cluster = view.getUint16(0); const clusterId = view.getUint16(0);
const indexList = []; const shards = [];
if (bytes.length === 130) { if (bytes.length === 130) {
// rsv format (Bit Vector) // rsv format (Bit Vector)
@ -18,7 +18,7 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
const bitIndex = 7 - (i % 8); const bitIndex = 7 - (i % 8);
if (view.getUint8(byteIndex) & (1 << bitIndex)) { if (view.getUint8(byteIndex) & (1 << bitIndex)) {
indexList.push(i); shards.push(i);
} }
} }
} else { } else {
@ -26,33 +26,33 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
const numIndices = view.getUint8(2); const numIndices = view.getUint8(2);
for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) { for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
if (offset + 1 >= bytes.length) throw new Error("Unexpected end of data"); 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 => { export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => {
const { cluster, indexList } = shardInfo; const { clusterId, shards } = shardInfo;
const totalLength = indexList.length >= 64 ? 130 : 3 + 2 * indexList.length; const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
const buffer = new ArrayBuffer(totalLength); const buffer = new ArrayBuffer(totalLength);
const view = new DataView(buffer); 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) // 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 byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
const bitIndex = 7 - (index % 8); const bitIndex = 7 - (index % 8);
view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex)); view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
} }
} else { } else {
// rs format (Index List) // rs format (Index List)
view.setUint8(2, indexList.length); view.setUint8(2, shards.length);
for (let i = 0, offset = 3; i < indexList.length; i++, offset += 2) { for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
view.setUint16(offset, indexList[i]); view.setUint16(offset, shards[i]);
} }
} }

View File

@ -19,8 +19,8 @@ export interface Waku2 {
} }
export interface ShardInfo { export interface ShardInfo {
cluster: number; clusterId: number;
indexList: number[]; shards: number[];
} }
export interface IEnr extends Map<ENRKey, ENRValue> { export interface IEnr extends Map<ENRKey, ENRValue> {

View File

@ -1,8 +1,8 @@
import type { PubsubTopic } from "./misc.js"; import type { PubsubTopic } from "./misc.js";
export interface SingleShardInfo { export interface SingleShardInfo {
cluster: number; clusterId: number;
index: number; shard: number;
} }
export interface IRateLimitProof { export interface IRateLimitProof {

View File

@ -24,6 +24,7 @@ export interface Args {
discv5UdpPort?: number; discv5UdpPort?: number;
// `legacyFilter` is required to enable filter v1 with go-waku // `legacyFilter` is required to enable filter v1 with go-waku
legacyFilter?: boolean; legacyFilter?: boolean;
clusterId?: number;
} }
export enum LogLevel { export enum LogLevel {

View File

@ -32,16 +32,16 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
let messageCollector: MessageCollector; let messageCollector: MessageCollector;
const customPubsubTopic1 = singleShardInfoToPubsubTopic({ const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 1 shard: 1
}); });
const customPubsubTopic2 = singleShardInfoToPubsubTopic({ const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 2 shard: 2
}); });
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] }; const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 }; const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 }; const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
const customContentTopic1 = "/test/2/waku-filter"; const customContentTopic1 = "/test/2/waku-filter";
const customContentTopic2 = "/test/3/waku-filter"; const customContentTopic2 = "/test/3/waku-filter";
const customEncoder1 = createEncoder({ const customEncoder1 = createEncoder({

View File

@ -27,16 +27,16 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
let nwaku2: NimGoNode; let nwaku2: NimGoNode;
let messageCollector: MessageCollector; let messageCollector: MessageCollector;
const customPubsubTopic1 = singleShardInfoToPubsubTopic({ const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 1 shard: 1
}); });
const customPubsubTopic2 = singleShardInfoToPubsubTopic({ const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 2 shard: 2
}); });
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] }; const shardInfo: ShardInfo = { clusterId: 3, shards: [1, 2] };
const singleShardInfo1: SingleShardInfo = { cluster: 3, index: 1 }; const singleShardInfo1: SingleShardInfo = { clusterId: 3, shard: 1 };
const singleShardInfo2: SingleShardInfo = { cluster: 3, index: 2 }; const singleShardInfo2: SingleShardInfo = { clusterId: 3, shard: 2 };
const customContentTopic1 = "/test/2/waku-light-push/utf8"; const customContentTopic1 = "/test/2/waku-light-push/utf8";
const customContentTopic2 = "/test/3/waku-light-push/utf8"; const customContentTopic2 = "/test/3/waku-light-push/utf8";
const customEncoder1 = createEncoder({ const customEncoder1 = createEncoder({

View File

@ -27,24 +27,24 @@ describe("Waku Relay, multiple pubsub topics", function () {
let waku3: RelayNode; let waku3: RelayNode;
const customPubsubTopic1 = singleShardInfoToPubsubTopic({ const customPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 1 shard: 1
}); });
const customPubsubTopic2 = singleShardInfoToPubsubTopic({ const customPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 2 shard: 2
}); });
const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] }; const shardInfo1: ShardInfo = { clusterId: 3, shards: [1] };
const singleShardInfo1: SingleShardInfo = { const singleShardInfo1: SingleShardInfo = {
cluster: 3, clusterId: 3,
index: 1 shard: 1
}; };
const customContentTopic1 = "/test/2/waku-relay/utf8"; const customContentTopic1 = "/test/2/waku-relay/utf8";
const customContentTopic2 = "/test/3/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 = { const singleShardInfo2: SingleShardInfo = {
cluster: 3, clusterId: 3,
index: 2 shard: 2
}; };
const customEncoder1 = createEncoder({ const customEncoder1 = createEncoder({
pubsubTopicShardInfo: singleShardInfo1, pubsubTopicShardInfo: singleShardInfo1,
@ -56,7 +56,7 @@ describe("Waku Relay, multiple pubsub topics", function () {
contentTopic: customContentTopic2 contentTopic: customContentTopic2
}); });
const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2); const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2);
const shardInfoBothShards: ShardInfo = { cluster: 3, indexList: [1, 2] }; const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] };
afterEach(async function () { afterEach(async function () {
this.timeout(15000); this.timeout(15000);

View File

@ -128,7 +128,7 @@ describe("Waku Relay, Publish", function () {
it("Fails to publish message with wrong pubsubtopic", async function () { it("Fails to publish message with wrong pubsubtopic", async function () {
const wrong_encoder = createEncoder({ const wrong_encoder = createEncoder({
pubsubTopicShardInfo: { cluster: 3, index: 1 }, pubsubTopicShardInfo: { clusterId: 3, shard: 1 },
contentTopic: TestContentTopic contentTopic: TestContentTopic
}); });
const pushResponse = await waku1.relay.send(wrong_encoder, { const pushResponse = await waku1.relay.send(wrong_encoder, {

View File

@ -40,9 +40,9 @@ describe("Static Sharding: Peer Management", function () {
this.timeout(100_000); this.timeout(100_000);
const pubsubTopics = [ 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({ await nwaku1.start({
pubsubTopic: pubsubTopics, pubsubTopic: pubsubTopics,
@ -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 () { it("px service nodes not subscribed to the shard should not be dialed", async function () {
this.timeout(100_000); this.timeout(100_000);
const pubsubTopicsToDial = [ 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 = [ const pubsubTopicsToIgnore = [
singleShardInfoToPubsubTopic({ cluster: 18, index: 1 }) singleShardInfoToPubsubTopic({ clusterId: 18, shard: 1 })
]; ];
// this service node is not subscribed to the shard // this service node is not subscribed to the shard

View File

@ -8,17 +8,17 @@ import { makeLogFileName } from "../../src/log_file.js";
import { NimGoNode } from "../../src/node/node.js"; import { NimGoNode } from "../../src/node/node.js";
const PubsubTopic1 = singleShardInfoToPubsubTopic({ const PubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 0, clusterId: 0,
index: 2 shard: 2
}); });
const PubsubTopic2 = singleShardInfoToPubsubTopic({ const PubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 0, clusterId: 0,
index: 3 shard: 3
}); });
const shardInfoFirstShard: ShardInfo = { cluster: 0, indexList: [2] }; const shardInfoFirstShard: ShardInfo = { clusterId: 0, shards: [2] };
const shardInfoBothShards: ShardInfo = { cluster: 0, indexList: [2, 3] }; const shardInfoBothShards: ShardInfo = { clusterId: 0, shards: [2, 3] };
const singleShardInfo1: SingleShardInfo = { cluster: 0, index: 2 }; const singleShardInfo1: SingleShardInfo = { clusterId: 0, shard: 2 };
const singleShardInfo2: SingleShardInfo = { cluster: 0, index: 3 }; const singleShardInfo2: SingleShardInfo = { clusterId: 0, shard: 3 };
const ContentTopic = "/waku/2/content/test.js"; const ContentTopic = "/waku/2/content/test.js";
describe("Static Sharding: Running Nodes", () => { describe("Static Sharding: Running Nodes", () => {

View File

@ -19,25 +19,25 @@ export const TestContentTopic = "/test/1/waku-store/utf8";
export const TestEncoder = createEncoder({ contentTopic: TestContentTopic }); export const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
export const TestDecoder = createDecoder(TestContentTopic); export const TestDecoder = createDecoder(TestContentTopic);
export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({ export const customShardedPubsubTopic1 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 1 shard: 1
}); });
export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({ export const customShardedPubsubTopic2 = singleShardInfoToPubsubTopic({
cluster: 3, clusterId: 3,
index: 2 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 customContentTopic1 = "/test/2/waku-store/utf8";
export const customContentTopic2 = "/test/3/waku-store/utf8"; export const customContentTopic2 = "/test/3/waku-store/utf8";
export const customDecoder1 = createDecoder(customContentTopic1, { export const customDecoder1 = createDecoder(customContentTopic1, {
cluster: 3, clusterId: 3,
index: 1 shard: 1
}); });
export const customDecoder2 = createDecoder(customContentTopic2, { export const customDecoder2 = createDecoder(customContentTopic2, {
cluster: 3, clusterId: 3,
index: 2 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 totalMsgs = 20;
export const messageText = "Store Push works!"; export const messageText = "Store Push works!";

View File

@ -6,20 +6,20 @@ import { concat, utf8ToBytes } from "../bytes/index.js";
export const singleShardInfoToPubsubTopic = ( export const singleShardInfoToPubsubTopic = (
shardInfo: SingleShardInfo shardInfo: SingleShardInfo
): PubsubTopic => { ): PubsubTopic => {
if (shardInfo.cluster === undefined || shardInfo.index === undefined) if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
throw new Error("Invalid shard"); throw new Error("Invalid shard");
return `/waku/2/rs/${shardInfo.cluster}/${shardInfo.index}`; return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
}; };
export const shardInfoToPubsubTopics = ( export const shardInfoToPubsubTopics = (
shardInfo: ShardInfo shardInfo: ShardInfo
): PubsubTopic[] => { ): PubsubTopic[] => {
if (shardInfo.cluster === undefined || shardInfo.indexList === undefined) if (shardInfo.clusterId === undefined || shardInfo.shards === undefined)
throw new Error("Invalid shard"); throw new Error("Invalid shard");
return shardInfo.indexList.map( return shardInfo.shards.map(
(index) => `/waku/2/rs/${shardInfo.cluster}/${index}` (index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`
); );
}; };
@ -27,13 +27,25 @@ export const pubsubTopicToSingleShardInfo = (
pubsubTopics: PubsubTopic pubsubTopics: PubsubTopic
): SingleShardInfo => { ): SingleShardInfo => {
const parts = pubsubTopics.split("/"); const parts = pubsubTopics.split("/");
if (parts.length != 6) throw new Error("Invalid pubsub topic");
const cluster = parseInt(parts[4]); if (
const index = parseInt(parts[5]); parts.length != 6 ||
if (isNaN(cluster) || isNaN(index)) throw new Error("Invalid pubsub topic"); 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( export function ensurePubsubTopicIsConfigured(