2024-04-28 11:15:17 +02:00
|
|
|
import {
|
2024-08-13 05:23:20 +05:30
|
|
|
NetworkConfig,
|
2024-04-28 11:15:17 +02:00
|
|
|
ProtocolCreateOptions,
|
2024-08-13 05:23:20 +05:30
|
|
|
Protocols
|
2024-04-28 11:15:17 +02:00
|
|
|
} from "@waku/interfaces";
|
2024-08-31 15:18:51 +02:00
|
|
|
import { createRelayNode } from "@waku/relay";
|
2024-10-09 00:43:34 +02:00
|
|
|
import { createLightNode, WakuNode } from "@waku/sdk";
|
2024-08-13 05:23:20 +05:30
|
|
|
import {
|
|
|
|
|
derivePubsubTopicsFromNetworkConfig,
|
|
|
|
|
Logger,
|
|
|
|
|
pubsubTopicsToShardInfo
|
|
|
|
|
} from "@waku/utils";
|
2024-04-28 11:15:17 +02:00
|
|
|
import { Context } from "mocha";
|
|
|
|
|
|
|
|
|
|
import { NOISE_KEY_1 } from "../constants.js";
|
|
|
|
|
import { makeLogFileName } from "../utils/index.js";
|
|
|
|
|
|
|
|
|
|
import { ServiceNode } from "./service_node.js";
|
|
|
|
|
|
|
|
|
|
export const log = new Logger("test:runNodes");
|
|
|
|
|
|
|
|
|
|
type RunNodesOptions = {
|
|
|
|
|
context: Context;
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig: NetworkConfig;
|
2024-04-28 11:15:17 +02:00
|
|
|
protocols: Protocols[];
|
|
|
|
|
createNode: typeof createLightNode | typeof createRelayNode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function runNodes<T>(
|
|
|
|
|
options: RunNodesOptions
|
|
|
|
|
): Promise<[ServiceNode, T]> {
|
2024-08-13 05:23:20 +05:30
|
|
|
const { context, networkConfig, createNode, protocols } = options;
|
2024-04-28 11:15:17 +02:00
|
|
|
|
|
|
|
|
const nwaku = new ServiceNode(makeLogFileName(context));
|
2024-08-13 05:23:20 +05:30
|
|
|
const pubsubTopics = derivePubsubTopicsFromNetworkConfig(networkConfig);
|
|
|
|
|
const shardInfo = pubsubTopicsToShardInfo(pubsubTopics);
|
2024-04-28 11:15:17 +02:00
|
|
|
|
|
|
|
|
await nwaku.start(
|
|
|
|
|
{
|
|
|
|
|
filter: true,
|
|
|
|
|
lightpush: true,
|
|
|
|
|
relay: true,
|
|
|
|
|
store: true,
|
|
|
|
|
pubsubTopic: pubsubTopics,
|
2024-08-13 05:23:20 +05:30
|
|
|
clusterId: shardInfo.clusterId
|
2024-04-28 11:15:17 +02:00
|
|
|
},
|
|
|
|
|
{ retries: 3 }
|
|
|
|
|
);
|
|
|
|
|
const waku_options: ProtocolCreateOptions = {
|
|
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
2024-08-13 05:23:20 +05:30
|
|
|
networkConfig: shardInfo
|
2024-04-28 11:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
log.info("Starting js waku node with :", JSON.stringify(waku_options));
|
|
|
|
|
let waku: WakuNode | undefined;
|
|
|
|
|
try {
|
|
|
|
|
waku = (await createNode(waku_options)) as WakuNode;
|
|
|
|
|
await waku.start();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error("jswaku node failed to start:", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (waku) {
|
|
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2024-10-09 00:43:34 +02:00
|
|
|
await waku.waitForPeers(protocols);
|
2024-04-28 11:15:17 +02:00
|
|
|
await nwaku.ensureSubscriptions(pubsubTopics);
|
|
|
|
|
return [nwaku, waku as T];
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Failed to initialize waku");
|
|
|
|
|
}
|
|
|
|
|
}
|