diff --git a/packages/sdk/src/light-node/index.ts b/packages/sdk/src/create/create.ts similarity index 76% rename from packages/sdk/src/light-node/index.ts rename to packages/sdk/src/create/create.ts index 713aec7d1f..8b1ca9562d 100644 --- a/packages/sdk/src/light-node/index.ts +++ b/packages/sdk/src/create/create.ts @@ -1,9 +1,8 @@ -import { type Libp2pComponents, type LightNode } from "@waku/interfaces"; +import { type LightNode } from "@waku/interfaces"; -import { createLibp2pAndUpdateOptions } from "../utils/libp2p.js"; import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js"; -export { Libp2pComponents }; +import { createLibp2pAndUpdateOptions } from "./libp2p.js"; /** * Create a Waku node that uses Waku Light Push, Filter and Store to send and diff --git a/packages/sdk/src/utils/discovery.ts b/packages/sdk/src/create/discovery.ts similarity index 100% rename from packages/sdk/src/utils/discovery.ts rename to packages/sdk/src/create/discovery.ts diff --git a/packages/sdk/src/create/index.ts b/packages/sdk/src/create/index.ts new file mode 100644 index 0000000000..ea0315451a --- /dev/null +++ b/packages/sdk/src/create/index.ts @@ -0,0 +1,2 @@ +export { createLightNode } from "./create.js"; +export { defaultLibp2p } from "./libp2p.js"; diff --git a/packages/sdk/src/utils/libp2p.ts b/packages/sdk/src/create/libp2p.ts similarity index 89% rename from packages/sdk/src/utils/libp2p.ts rename to packages/sdk/src/create/libp2p.ts index bb199e40e3..4307e33d53 100644 --- a/packages/sdk/src/utils/libp2p.ts +++ b/packages/sdk/src/create/libp2p.ts @@ -92,24 +92,13 @@ 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; - - options.pubsubTopics = shardInfo?.pubsubTopics ?? - options.pubsubTopics ?? [DefaultPubsubTopic]; + const shardInfo = configureNetworkOptions(options); const libp2pOptions = options?.libp2p ?? {}; const peerDiscovery = libp2pOptions.peerDiscovery ?? []; if (options?.defaultBootstrap) { - peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics)); + peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics!)); } if (options?.bootstrapPeers) { @@ -119,7 +108,7 @@ export async function createLibp2pAndUpdateOptions( libp2pOptions.peerDiscovery = peerDiscovery; const libp2p = await defaultLibp2p( - shardInfo?.shardInfo, + shardInfo, wakuGossipSub(options), libp2pOptions, options?.userAgent @@ -128,6 +117,37 @@ export async function createLibp2pAndUpdateOptions( return libp2p; } +function configureNetworkOptions( + options: CreateWakuNodeOptions +): ShardInfo | undefined { + const flags = [ + options.contentTopics, + options.pubsubTopics, + options.shardInfo + ].filter((v) => !!v); + + if (flags.length > 1) { + throw Error( + "Too many network configurations provided. Pass only one of: pubsubTopic, contentTopics or shardInfo." + ); + } + + logWhichShardInfoIsUsed(options); + + if (options.contentTopics) { + options.shardInfo = { contentTopics: options.contentTopics }; + } + + const shardInfo = options.shardInfo + ? ensureShardingConfigured(options.shardInfo) + : undefined; + + options.pubsubTopics = shardInfo?.pubsubTopics ?? + options.pubsubTopics ?? [DefaultPubsubTopic]; + + return shardInfo?.shardInfo; +} + function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void { if (options.pubsubTopics) { logger.info("Using pubsubTopics array to bootstrap the node."); diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 4157f55c32..94be6647de 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -7,11 +7,10 @@ export { export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes"; -export { defaultLibp2p } from "./utils/libp2p.js"; export * from "./utils/content_topic.js"; export * from "./waku.js"; -export { createLightNode } from "./light-node/index.js"; +export { createLightNode, defaultLibp2p } from "./create/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/relay-node/index.ts b/packages/sdk/src/relay-node/index.ts index 3ef337c460..f30eb27113 100644 --- a/packages/sdk/src/relay-node/index.ts +++ b/packages/sdk/src/relay-node/index.ts @@ -1,7 +1,7 @@ import { type FullNode, type RelayNode } from "@waku/interfaces"; import { RelayCreateOptions } from "@waku/relay"; -import { createLibp2pAndUpdateOptions } from "../utils/libp2p.js"; +import { createLibp2pAndUpdateOptions } from "../create/libp2p.js"; import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "../waku.js"; /** diff --git a/packages/sdk/src/utils/content_topic.ts b/packages/sdk/src/utils/content_topic.ts index 68c5eeb320..2f10de0e79 100644 --- a/packages/sdk/src/utils/content_topic.ts +++ b/packages/sdk/src/utils/content_topic.ts @@ -12,7 +12,7 @@ import { shardInfoToPubsubTopics } from "@waku/utils"; -import { createLightNode } from "../light-node/index.js"; +import { createLightNode } from "../create/index.js"; interface CreateTopicOptions { waku?: LightNode; diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index 093d2e2ef0..444ba0ed15 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -88,19 +88,23 @@ export async function runMultipleNodes( withoutFilter ); - const waku_options: ProtocolCreateOptions = { + const wakuOptions: ProtocolCreateOptions = { staticNoiseKey: NOISE_KEY_1, libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } - }, - pubsubTopics, - shardInfo + } }; - log.info("Starting js waku node with :", JSON.stringify(waku_options)); + if (shardInfo) { + wakuOptions.shardInfo = shardInfo; + } else { + wakuOptions.pubsubTopics = pubsubTopics; + } + + log.info("Starting js waku node with :", JSON.stringify(wakuOptions)); let waku: LightNode | undefined; try { - waku = await createLightNode(waku_options); + waku = await createLightNode(wakuOptions); await waku.start(); } catch (error) { log.error("jswaku node failed to start:", error);