From 5b03709dfe683b1cb001fe67c5bd013e664b4d89 Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 30 Apr 2024 01:47:45 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20lift=20contentTopics=20and=20make=20sha?= =?UTF-8?q?rdInfo=20mandatory=20for=20createLight=E2=80=A6=20(#1959)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: lift contentTopics and make shardInfo mandatory for createLightNode * add default shard info and wanring * fix * fix lint * fix * fix types * remove breaking changes --- packages/interfaces/src/protocols.ts | 8 +++++++- packages/sdk/src/index.ts | 2 +- packages/sdk/src/light-node/index.ts | 25 ------------------------ packages/sdk/src/utils/libp2p.ts | 29 +++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index a4ae7082c0..889bc0b9bc 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -61,10 +61,16 @@ export type ProtocolCreateOptions = { * - WakuRelay to receive, route and send messages, * - WakuLightPush to send messages, * - WakuStore to retrieve messages. - * See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. + * See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details. * */ shardInfo?: Partial; + /** + * Content topics are used to determine pubsubTopics + * If not provided pubsubTopics will be determined based on shardInfo + * See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md) for details. + */ + contentTopics?: string[]; /** * You can pass options to the `Libp2p` instance used by {@link @waku/sdk!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) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 8fc6b3f647..4157f55c32 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -11,7 +11,7 @@ export { defaultLibp2p } from "./utils/libp2p.js"; export * from "./utils/content_topic.js"; export * from "./waku.js"; -export { createLightNode, createNode } from "./light-node/index.js"; +export { createLightNode } from "./light-node/index.js"; export { wakuLightPush } from "./protocols/light_push.js"; export { wakuFilter } from "./protocols/filter.js"; export { wakuStore } from "./protocols/store.js"; diff --git a/packages/sdk/src/light-node/index.ts b/packages/sdk/src/light-node/index.ts index c42c2e923a..079f488063 100644 --- a/packages/sdk/src/light-node/index.ts +++ b/packages/sdk/src/light-node/index.ts @@ -8,31 +8,6 @@ import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js"; export { Libp2pComponents }; -/** - * Create a Waku node configured to use autosharding or static sharding. - */ -export async function createNode( - options: CreateWakuNodeOptions = { pubsubTopics: [] } -): Promise { - if (!options.shardInfo) { - throw new Error("Shard info must be set"); - } - - const libp2p = await createLibp2pAndUpdateOptions(options); - - const store = wakuStore(options); - const lightPush = wakuLightPush(options); - const filter = wakuFilter(options); - - return new WakuNode( - options as WakuOptions, - libp2p, - store, - lightPush, - filter - ) as LightNode; -} - /** * Create a Waku node that uses Waku Light Push, Filter and Store to send and * receive messages, enabling low resource consumption. diff --git a/packages/sdk/src/utils/libp2p.ts b/packages/sdk/src/utils/libp2p.ts index caf64829ea..d8c9445e6d 100644 --- a/packages/sdk/src/utils/libp2p.ts +++ b/packages/sdk/src/utils/libp2p.ts @@ -16,7 +16,7 @@ import { type ShardInfo } from "@waku/interfaces"; import { wakuGossipSub } from "@waku/relay"; -import { ensureShardingConfigured } from "@waku/utils"; +import { ensureShardingConfigured, Logger } from "@waku/utils"; import { createLibp2p } from "libp2p"; import { @@ -35,6 +35,8 @@ type MetadataService = { metadata?: (components: Libp2pComponents) => IMetadata; }; +const logger = new Logger("sdk:create"); + export async function defaultLibp2p( shardInfo?: ShardInfo, wakuGossipSub?: PubsubService["pubsub"], @@ -88,6 +90,12 @@ export async function defaultLibp2p( export async function createLibp2pAndUpdateOptions( options: CreateWakuNodeOptions ): Promise { + logWhichShardInfoIsUsed(options); + + if (options.contentTopics) { + options.shardInfo = { contentTopics: options.contentTopics }; + } + const shardInfo = options.shardInfo ? ensureShardingConfigured(options.shardInfo) : undefined; @@ -117,3 +125,22 @@ export async function createLibp2pAndUpdateOptions( return libp2p; } + +function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void { + if (options.pubsubTopics) { + logger.info("Using pubsubTopics array to bootstrap the node."); + return; + } + + if (options.contentTopics) { + logger.info( + "Using contentTopics and default cluster ID (1) to bootstrap the node." + ); + return; + } + + if (options.shardInfo) { + logger.info("Using shardInfo parameters to bootstrap the node."); + return; + } +}