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

feat: set cluster ID as optional when specifying shard info #1780

Merged
merged 1 commit into from
Jan 24, 2024
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
8 changes: 7 additions & 1 deletion packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {
PubsubTopic
} from "@waku/interfaces";
import { DefaultPubsubTopic } from "@waku/interfaces";
import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
import {
ensureShardingConfigured,
Logger,
shardInfoToPubsubTopics
} from "@waku/utils";
import {
getConnectedPeersForProtocolAndShard,
getPeersForProtocol,
Expand Down Expand Up @@ -108,6 +112,8 @@ export class BaseProtocol implements IBaseProtocol {
this.peerStore,
[this.multicodec],
this.options?.shardInfo
? ensureShardingConfigured(this.options.shardInfo).shardInfo
: undefined
);

// Filter the peers based on discovery & number of peers requested
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Metadata extends BaseProtocol implements IMetadata {
}

export function wakuMetadata(
shardInfo: ShardingParams
shardInfo: ShardInfo
): (components: Libp2pComponents) => IMetadata {
return (components: Libp2pComponents) => new Metadata(shardInfo, components);
}
5 changes: 5 additions & 0 deletions packages/interfaces/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
*/
export const DefaultPubsubTopic = "/waku/2/default-waku/proto";

/**
* The default cluster ID for The Waku Network
*/
export const DEFAULT_CLUSTER_ID = 1;
2 changes: 1 addition & 1 deletion packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface SingleShardInfo {
/**
* Specifying this field indicates to the encoder/decoder that static sharding must be used.
*/
shard?: number;
shard: number;
}

export interface IRateLimitProof {
Expand Down
2 changes: 1 addition & 1 deletion packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type ProtocolCreateOptions = {
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
*
*/
shardInfo?: ShardingParams;
shardInfo?: Partial<ShardingParams>;
/**
* You can pass options to the `Libp2p` instance used by {@link @waku/core!WakuNode} using the `libp2p` property.
* This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
Expand Down
58 changes: 24 additions & 34 deletions packages/sdk/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
wakuStore
} from "@waku/core";
import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery";
import type {
CreateLibp2pOptions,
FullNode,
IMetadata,
Libp2p,
Libp2pComponents,
LightNode,
ProtocolCreateOptions,
ShardingParams
import {
type CreateLibp2pOptions,
type FullNode,
type IMetadata,
type Libp2p,
type Libp2pComponents,
type LightNode,
type ProtocolCreateOptions,
type ShardInfo
} from "@waku/interfaces";
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
import { ensureShardingConfigured } from "@waku/utils";
import { createLibp2p } from "libp2p";

const DEFAULT_NODE_REQUIREMENTS = {
Expand All @@ -38,17 +39,6 @@

export { Libp2pComponents };

const ensureShardingConfigured = (shardInfo: ShardingParams): void => {
if (
("shards" in shardInfo && shardInfo.shards.length < 1) ||
("contentTopics" in shardInfo && shardInfo.contentTopics.length < 1)
) {
throw new Error(
"Missing required configuration options for static sharding or autosharding."
);
}
};

/**
* Create a Waku node configured to use autosharding or static sharding.
*/
Expand All @@ -61,7 +51,7 @@
throw new Error("Shard info must be set");
}

ensureShardingConfigured(options.shardInfo);
const shardInfo = ensureShardingConfigured(options.shardInfo);

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
Expand All @@ -71,7 +61,7 @@
}

const libp2p = await defaultLibp2p(
undefined,
shardInfo.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -85,7 +75,7 @@
options ?? {},
[],
libp2p,
options.shardInfo,
shardInfo.shardInfo,
store,
lightPush,
filter
Expand All @@ -102,9 +92,9 @@
): Promise<LightNode> {
options = options ?? {};

if (options.shardInfo) {
ensureShardingConfigured(options.shardInfo);
}
const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
Expand All @@ -114,7 +104,7 @@
}

const libp2p = await defaultLibp2p(
options.shardInfo,
shardInfo?.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -128,7 +118,7 @@
options ?? {},
options.pubsubTopics,
libp2p,
options.shardInfo,
shardInfo?.shardingParams,
store,
lightPush,
filter
Expand All @@ -153,9 +143,9 @@
): Promise<FullNode> {
options = options ?? {};

if (options.shardInfo) {
ensureShardingConfigured(options.shardInfo);
}
const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
Expand All @@ -165,7 +155,7 @@
}

const libp2p = await defaultLibp2p(
options.shardInfo,
shardInfo?.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -180,7 +170,7 @@
options ?? {},
options.pubsubTopics,
libp2p,
options.shardInfo,
shardInfo?.shardingParams,
store,
lightPush,
filter,
Expand All @@ -207,7 +197,7 @@
};

export async function defaultLibp2p(
shardInfo?: ShardingParams,
shardInfo?: ShardInfo,
wakuGossipSub?: PubsubService["pubsub"],
options?: Partial<CreateLibp2pOptions>,
userAgent?: string
Expand Down Expand Up @@ -250,5 +240,5 @@
...pubsubService,
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;

Check warning on line 243 in packages/sdk/src/create.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 243 in packages/sdk/src/create.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}
9 changes: 7 additions & 2 deletions packages/sdk/src/relay/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WakuNode, WakuOptions } from "@waku/core";
import type { ProtocolCreateOptions, RelayNode } from "@waku/interfaces";
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
import { ensureShardingConfigured } from "@waku/utils";

import { defaultLibp2p, defaultPeerDiscoveries } from "../create.js";

Expand All @@ -26,8 +27,12 @@ export async function createRelayNode(
Object.assign(libp2pOptions, { peerDiscovery });
}

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

const libp2p = await defaultLibp2p(
options.shardInfo,
shardInfo?.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -39,7 +44,7 @@ export async function createRelayNode(
options,
options.pubsubTopics,
libp2p,
options.shardInfo,
shardInfo?.shardingParams,
undefined,
undefined,
undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import { Protocols } from "@waku/interfaces";
import {
contentTopicToPubsubTopic,
contentTopicToShardIndex,
pubsubTopicToSingleShardInfo,
singleShardInfoToPubsubTopic
} from "@waku/utils";
Expand Down Expand Up @@ -207,17 +208,25 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
const customEncoder1 = createEncoder({
contentTopic: customContentTopic1,
pubsubTopicShardInfo: {
clusterId: 3
clusterId: 3,
shard: contentTopicToShardIndex(customContentTopic1)
}
});
const customDecoder1 = createDecoder(customContentTopic1, { clusterId: 3 });
const customDecoder1 = createDecoder(customContentTopic1, {
clusterId: 3,
shard: contentTopicToShardIndex(customContentTopic1)
});
const customEncoder2 = createEncoder({
contentTopic: customContentTopic2,
pubsubTopicShardInfo: {
clusterId: 3
clusterId: 3,
shard: contentTopicToShardIndex(customContentTopic2)
}
});
const customDecoder2 = createDecoder(customContentTopic2, { clusterId: 3 });
const customDecoder2 = createDecoder(customContentTopic2, {
clusterId: 3,
shard: contentTopicToShardIndex(customContentTopic2)
});

this.beforeEach(async function () {
this.timeout(15000);
Expand Down
10 changes: 5 additions & 5 deletions packages/tests/tests/getPeers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Tags,
utf8ToBytes
} from "@waku/sdk";
import { shardInfoToPubsubTopics } from "@waku/utils";
import { ensureShardingConfigured, shardInfoToPubsubTopics } from "@waku/utils";
import { getConnectedPeersForProtocolAndShard } from "@waku/utils/libp2p";
import { expect } from "chai";
import fc from "fast-check";
Expand Down Expand Up @@ -237,7 +237,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
waku.libp2p.getConnections(),
waku.libp2p.peerStore,
waku.libp2p.getProtocols(),
shardInfo
ensureShardingConfigured(shardInfo).shardInfo
);
expect(peers.length).to.be.greaterThan(0);
});
Expand Down Expand Up @@ -289,7 +289,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
waku.libp2p.getConnections(),
waku.libp2p.peerStore,
waku.libp2p.getProtocols(),
shardInfo2
ensureShardingConfigured(shardInfo2).shardInfo
);
expect(peers.length).to.be.equal(1);
});
Expand Down Expand Up @@ -341,7 +341,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
waku.libp2p.getConnections(),
waku.libp2p.peerStore,
waku.libp2p.getProtocols(),
shardInfo2
ensureShardingConfigured(shardInfo2).shardInfo
);
expect(peers.length).to.be.equal(1);
});
Expand Down Expand Up @@ -393,7 +393,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
waku.libp2p.getConnections(),
waku.libp2p.peerStore,
waku.libp2p.getProtocols(),
shardInfo2
ensureShardingConfigured(shardInfo2).shardInfo
);
expect(peers.length).to.be.equal(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from "@waku/interfaces";
import {
contentTopicToPubsubTopic,
contentTopicToShardIndex,
pubsubTopicToSingleShardInfo,
singleShardInfoToPubsubTopic
} from "@waku/utils";
import { utf8ToBytes } from "@waku/utils/bytes";
Expand Down Expand Up @@ -202,11 +204,11 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () {
};
const customEncoder1 = createEncoder({
contentTopic: customContentTopic1,
pubsubTopicShardInfo: shardInfo
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
});
const customEncoder2 = createEncoder({
contentTopic: customContentTopic2,
pubsubTopicShardInfo: shardInfo
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
});

let nimPeerId: PeerId;
Expand Down Expand Up @@ -356,12 +358,16 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function ()
const customEncoder1 = createEncoder({
contentTopic: customContentTopic1,
pubsubTopicShardInfo: {
clusterId
clusterId,
shard: contentTopicToShardIndex(customContentTopic1)
}
});
const customEncoder2 = createEncoder({
contentTopic: customContentTopic2,
pubsubTopicShardInfo: { clusterId }
pubsubTopicShardInfo: {
clusterId,
shard: contentTopicToShardIndex(customContentTopic2)
}
});

let nimPeerId: PeerId;
Expand Down
17 changes: 11 additions & 6 deletions packages/tests/tests/relay/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Protocols } from "@waku/interfaces";
import { createRelayNode } from "@waku/sdk/relay";
import {
contentTopicToPubsubTopic,
pubsubTopicToSingleShardInfo,
singleShardInfoToPubsubTopic
} from "@waku/utils";
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
Expand Down Expand Up @@ -340,16 +341,20 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () {
};
const customEncoder1 = createEncoder({
contentTopic: customContentTopic1,
pubsubTopicShardInfo: {
clusterId: 3
}
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
});
const customDecoder1 = createDecoder(customContentTopic1, { clusterId: 3 });
const customDecoder1 = createDecoder(
customContentTopic1,
pubsubTopicToSingleShardInfo(autoshardingPubsubTopic1)
);
const customEncoder2 = createEncoder({
contentTopic: customContentTopic2,
pubsubTopicShardInfo: { clusterId: 3 }
pubsubTopicShardInfo: pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
});
const customDecoder2 = createDecoder(customContentTopic2, { clusterId: 3 });
const customDecoder2 = createDecoder(
customContentTopic2,
pubsubTopicToSingleShardInfo(autoshardingPubsubTopic2)
);
const contentTopicInfoBothShards: ContentTopicInfo = {
clusterId: 3,
contentTopics: [customContentTopic1, customContentTopic2]
Expand Down
Loading
Loading