Skip to content

Commit

Permalink
feat: support creating node using application info or content topic
Browse files Browse the repository at this point in the history
This commit adds SDK functions for creating a node by providing
an application/version or a list of content topics. The node is
configured to use autosharding.
  • Loading branch information
adklempner committed Jan 24, 2024
1 parent c548c42 commit e22cdd0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/lib/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IStore,
Libp2p,
PubsubTopic,
ShardingParams,
ShardInfo,
Waku
} from "@waku/interfaces";
import { DefaultPubsubTopic, Protocols } from "@waku/interfaces";
Expand Down Expand Up @@ -57,7 +57,7 @@ export class WakuNode implements Waku {
options: WakuOptions,
pubsubTopics: PubsubTopic[] = [],
libp2p: Libp2p,
private pubsubShardInfo?: ShardingParams,
private pubsubShardInfo?: ShardInfo,
store?: (libp2p: Libp2p) => IStore,
lightPush?: (libp2p: Libp2p) => ILightPush,
filter?: (libp2p: Libp2p) => IFilter,
Expand Down Expand Up @@ -110,7 +110,7 @@ export class WakuNode implements Waku {
);
}

get shardInfo(): ShardingParams | undefined {
get shardInfo(): ShardInfo | undefined {
return this.pubsubShardInfo;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import type { PeerId, Stream } from "@libp2p/interface";
import type { Multiaddr } from "@multiformats/multiaddr";

import { IConnectionManager } from "./connection_manager.js";
import { ShardInfo } from "./enr.js";
import type { IFilter } from "./filter.js";
import type { Libp2p } from "./libp2p.js";
import type { ILightPush } from "./light_push.js";
import { Protocols, ShardingParams } from "./protocols.js";
import { Protocols } from "./protocols.js";
import type { IRelay } from "./relay.js";
import type { IStore } from "./store.js";

Expand All @@ -16,7 +17,7 @@ export interface Waku {
filter?: IFilter;
lightPush?: ILightPush;

shardInfo?: ShardingParams;
shardInfo?: ShardInfo;

connectionManager: IConnectionManager;

Expand Down
42 changes: 36 additions & 6 deletions packages/sdk/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from "@waku/core";
import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery";
import {
ApplicationInfo,
ContentTopicInfo,
type CreateLibp2pOptions,
type FullNode,
type IMetadata,
Expand All @@ -39,20 +41,48 @@ const DEFAULT_NODE_REQUIREMENTS = {

export { Libp2pComponents };

export async function createNodeFromApplicationInfo(
applicationInfo: Partial<ApplicationInfo>,
options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
): Promise<LightNode> {
options = options ?? {};
const shardInfo = ensureShardingConfigured(applicationInfo);
options.shardInfo = shardInfo.shardingParams;
return _createNode(shardInfo.shardInfo, options);
}

export async function createNodeFromContentTopics(
contentTopicInfo: Partial<ContentTopicInfo>,
options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
): Promise<LightNode> {
options = options ?? {};
const shardInfo = ensureShardingConfigured(contentTopicInfo);
options.shardInfo = shardInfo.shardingParams;
return _createNode(shardInfo.shardInfo, options);
}

/**
* Create a Waku node configured to use autosharding or static sharding.
*/
export async function createNode(
options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
): Promise<LightNode> {
options = options ?? {};

if (!options.shardInfo) {
throw new Error("Shard info must be set");
}

const shardInfo = ensureShardingConfigured(options.shardInfo);
options.shardInfo = shardInfo.shardingParams;
return _createNode(shardInfo.shardInfo, options);
}

/**
* Create a Waku node configured to use autosharding or static sharding.
*/
async function _createNode(
shardInfo: ShardInfo,
options: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
): Promise<LightNode> {
const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
if (options?.defaultBootstrap) {
Expand All @@ -61,7 +91,7 @@ export async function createNode(
}

const libp2p = await defaultLibp2p(
shardInfo.shardInfo,
shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -75,7 +105,7 @@ export async function createNode(
options ?? {},
[],
libp2p,
shardInfo.shardInfo,
shardInfo,
store,
lightPush,
filter
Expand Down Expand Up @@ -118,7 +148,7 @@ export async function createLightNode(
options ?? {},
options.pubsubTopics,
libp2p,
shardInfo?.shardingParams,
shardInfo?.shardInfo,
store,
lightPush,
filter
Expand Down Expand Up @@ -170,7 +200,7 @@ export async function createFullNode(
options ?? {},
options.pubsubTopics,
libp2p,
shardInfo?.shardingParams,
shardInfo?.shardInfo,
store,
lightPush,
filter,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function createRelayNode(
options,
options.pubsubTopics,
libp2p,
shardInfo?.shardingParams,
shardInfo?.shardInfo,
undefined,
undefined,
undefined,
Expand Down
44 changes: 44 additions & 0 deletions packages/tests/tests/sharding/running_nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
DEFAULT_CLUSTER_ID,
LightNode,
Protocols,
ShardInfo,
Expand All @@ -7,6 +8,8 @@ import {
import {
createEncoder,
createLightNode,
createNodeFromApplicationInfo,
createNodeFromContentTopics,
utf8ToBytes,
waitForRemotePeer
} from "@waku/sdk";
Expand Down Expand Up @@ -166,4 +169,45 @@ describe("Autosharding: Running Nodes", () => {
expect(request1.recipients.length).to.eq(1);
expect(request2.recipients.length).to.eq(1);
});

it("create node with application and version", async function () {
this.timeout(15_000);
waku = await createNodeFromApplicationInfo({
application: "myapp",
version: "1"
});
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.LightPush]);

const encoder1 = createEncoder({
contentTopic: ContentTopic2,
pubsubTopicShardInfo: {
clusterId: DEFAULT_CLUSTER_ID,
shard: contentTopicToShardIndex(ContentTopic2)
}
});
await waku.lightPush.send(encoder1, {
payload: utf8ToBytes("Hello World")
});
});

it("create node with content topic", async function () {
this.timeout(15_000);
waku = await createNodeFromContentTopics({
contentTopics: [ContentTopic2]
});
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.LightPush]);

const encoder1 = createEncoder({
contentTopic: ContentTopic2,
pubsubTopicShardInfo: {
clusterId: DEFAULT_CLUSTER_ID,
shard: contentTopicToShardIndex(ContentTopic2)
}
});
await waku.lightPush.send(encoder1, {
payload: utf8ToBytes("Hello World")
});
});
});

0 comments on commit e22cdd0

Please sign in to comment.