remove default values for cluster id and num shards in cluster

These are high level configuration values, that need to be consistent within a given execution. Setting them as default value at such a low level is a footgun risk.
This commit is contained in:
fryorcraken 2025-07-19 12:20:08 +10:00
parent 3842d84b55
commit b4787e0e87
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 52 additions and 16 deletions

View File

@ -29,7 +29,10 @@ describe("ShardReader", function () {
const testContentTopic = "/test/1/waku-light-push/utf8";
const testClusterId = 3;
const testShardIndex = contentTopicToShardIndex(testContentTopic);
const testShardIndex = contentTopicToShardIndex(
testContentTopic,
DEFAULT_NUM_SHARDS
);
const testNetworkConfig: AutoSharding = {
clusterId: testClusterId,

View File

@ -10,10 +10,14 @@ import { utf8ToBytes } from "@waku/utils/bytes";
export const log = new Logger("test:filter");
export const TestContentTopic = "/test/1/waku-filter/default";
export const TestClusterId = 2;
export const TestShardIndex = contentTopicToShardIndex(TestContentTopic);
export const TestNumShardsInCluster = 8;
export const TestShardIndex = contentTopicToShardIndex(
TestContentTopic,
TestNumShardsInCluster
);
export const TestNetworkConfig = {
clusterId: TestClusterId,
numShardsInCluster: 8
numShardsInCluster: TestNumShardsInCluster
};
export const TestRoutingInfo = createRoutingInfo(TestNetworkConfig, {
contentTopic: TestContentTopic

View File

@ -200,7 +200,8 @@ describe("Static Sharding: Peer Management", function () {
describe("Autosharding: Peer Management", function () {
const ContentTopic = "/myapp/1/latest/proto";
const clusterId = 8;
const Shard = [contentTopicToShardIndex(ContentTopic)];
const numShardsInCluster = 8;
const Shard = [contentTopicToShardIndex(ContentTopic, numShardsInCluster)];
describe("Peer Exchange", function () {
let waku: LightNode;

View File

@ -9,6 +9,9 @@ import {
pubsubTopicToSingleShardInfo
} from "./index.js";
const ClusterId = 0;
const NumShardsInCluster = 8;
const testInvalidCases = (
contentTopics: string[],
expectedError: string
@ -112,7 +115,9 @@ describe("contentTopicToShardIndex", () => {
];
contentTopicsWithExpectedShards.forEach(([topic, expectedShard]) => {
it(`should correctly map ${topic} to shard index ${expectedShard}`, () => {
expect(contentTopicToShardIndex(topic)).to.eq(expectedShard);
expect(contentTopicToShardIndex(topic, NumShardsInCluster)).to.eq(
expectedShard
);
});
});
@ -137,8 +142,8 @@ describe("contentTopicToShardIndex", () => {
["/waku/2/content/test.js", "/waku/2/users/proto"]
];
for (const [topic1, topic2] of contentTopics) {
expect(contentTopicToShardIndex(topic1)).to.eq(
contentTopicToShardIndex(topic2)
expect(contentTopicToShardIndex(topic1, NumShardsInCluster)).to.eq(
contentTopicToShardIndex(topic2, NumShardsInCluster)
);
}
});
@ -147,9 +152,15 @@ describe("contentTopicToShardIndex", () => {
describe("contentTopicsByPubsubTopic", () => {
it("groups content topics by expected pubsub topic", () => {
const contentTopics = ["/toychat/2/huilong/proto", "/myapp/1/latest/proto"];
const grouped = contentTopicsByPubsubTopic(contentTopics);
const grouped = contentTopicsByPubsubTopic(
contentTopics,
ClusterId,
NumShardsInCluster
);
for (const contentTopic of contentTopics) {
const pubsubTopic = contentTopicToPubsubTopic(contentTopic, 0, 8);
expect(grouped.get(pubsubTopic)?.includes(contentTopic)).to.be.true;
}
});
@ -159,7 +170,11 @@ describe("contentTopicsByPubsubTopic", () => {
"/app/22/sometopic/someencoding",
"/app/22/anothertopic/otherencoding"
];
const grouped = contentTopicsByPubsubTopic(contentTopics);
const grouped = contentTopicsByPubsubTopic(
contentTopics,
ClusterId,
NumShardsInCluster
);
expect(grouped.size).to.eq(1); // Only one pubsub topic expected
const pubsubTopic = contentTopicToPubsubTopic(contentTopics[0], 0, 8);
expect(grouped.get(pubsubTopic)?.length).to.eq(2); // Both topics should be grouped under the same pubsub topic
@ -169,8 +184,16 @@ describe("contentTopicsByPubsubTopic", () => {
const contentTopics = ["/app/22/sometopic/someencoding"];
const clusterId1 = 3;
const clusterId2 = 2;
const grouped1 = contentTopicsByPubsubTopic(contentTopics, clusterId1);
const grouped2 = contentTopicsByPubsubTopic(contentTopics, clusterId2);
const grouped1 = contentTopicsByPubsubTopic(
contentTopics,
clusterId1,
NumShardsInCluster
);
const grouped2 = contentTopicsByPubsubTopic(
contentTopics,
clusterId2,
NumShardsInCluster
);
const pubsubTopic1 = contentTopicToPubsubTopic(
contentTopics[0],
clusterId1,
@ -221,7 +244,13 @@ describe("contentTopicsByPubsubTopic", () => {
it("throws an error for improperly formatted content topics", () => {
const invalidContentTopics = ["/invalid/format"];
expect(() => contentTopicsByPubsubTopic(invalidContentTopics)).to.throw();
expect(() =>
contentTopicsByPubsubTopic(
invalidContentTopics,
ClusterId,
NumShardsInCluster
)
).to.throw();
});
});

View File

@ -2,7 +2,6 @@ import { sha256 } from "@noble/hashes/sha256";
import {
type ClusterId,
ContentTopic,
DEFAULT_CLUSTER_ID,
PubsubTopic,
type ShardId
} from "@waku/interfaces";
@ -116,7 +115,7 @@ export function ensureValidContentTopic(
*/
export function contentTopicToShardIndex(
contentTopic: ContentTopic,
numShardsInCluster: number = 8
numShardsInCluster: number
): number {
const { application, version } = ensureValidContentTopic(contentTopic);
const digest = sha256(
@ -145,8 +144,8 @@ export function contentTopicToPubsubTopic(
*/
export function contentTopicsByPubsubTopic(
contentTopics: ContentTopic[],
clusterId: number = DEFAULT_CLUSTER_ID,
networkShards: number = 8
clusterId: number,
networkShards: number
): Map<string, Array<string>> {
const groupedContentTopics = new Map();
for (const contentTopic of contentTopics) {