120 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
ContentTopic,
type CreateNodeOptions,
type NetworkConfig,
Protocols,
type ShardId
} from "@waku/interfaces";
import { createRelayNode, RelayCreateOptions } from "@waku/relay";
feat: replace `waitForRemotePeers()` with `waku.waitForPeer()` method (#2161) * fix comment of default number of peers * export default number of peers from base protocol sdk * rename to light_push, move class to separate file * move waitForRemotePeer to sdk package * add todo to move waitForGossipSubPeerInMesh into @waku/relay * clean up waitForRemotePeer, split metadata await from event and optimise, decouple from protocol implementations * simplify and rename ILightPush interface * use only connected peers in light push based on connections instead of peer renewal mechanism * improve readability of result processing in light push * fix check & update tests * address tests, add new test cases, fix racing condition in StreamManager * use libp2p.getPeers * feat: confirm metadata and protocols needed in waitForRemotePeer * rely on passed protocols and fallback to mounted * add I prefix to Waku interface * implement waku.connect method * add docs to IWaku interface * remove export and usage of waitForRemotePeer * move wait for remote peer related to Realy out of @waku/sdk * change tests to use new API * fix linting * update size limit * rename .connect to .waitForPeer * export waitForRemotePeer and mark as deprecated * feat: add mocha tests to @waku/sdk and cover waitForRemotePeer (#2163) * feat: add mocha tests to @waku/sdk and cover waitForRemotePeer * add waitForRemote UTs * remove junk * feat: expose peerId and protocols from WakuNode (#2166) * chore: expose peerId and protocols from WakuNode * remove unused method * move to private method * rename to waitForPeers * up test
2024-10-09 00:43:34 +02:00
import { createLightNode, WakuNode } from "@waku/sdk";
import {
createRoutingInfo,
isAutoSharding,
isStaticSharding,
Logger,
RoutingInfo
} from "@waku/utils";
import { Context } from "mocha";
import { NOISE_KEY_1 } from "../constants.js";
import { Args } from "../types.js";
import { makeLogFileName } from "../utils/index.js";
import { ServiceNode } from "./service_node.js";
export const log = new Logger("test:runNodes");
export const DEFAULT_DISCOVERIES_ENABLED = {
feat!: re-architect connection manager (#2445) * remove public pubsub field and redundant util * add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests * improve public dial method to start keep alive checks * move dial method * implement discovery dialer * implement discovery dialer with queue with tests * add discovery dialer e2e tests, change local discovery log tag, update other tests * remove comment * add issue link, remove only * implement shard reader component * create evetns module, remove unused connection manager events and related tests * implement network indicator * implement connection limiter, change public API of connection manager, implement recovery strategy * decouple keep alive maanger * add connection manager js-doc * refactor keep alive manager, cover with tests * add tests for connection manager main facade * add tests for connection limiter * add e2e tests for connection manager modules pass js-waku config during test node init remove dns discovery for js-waku * restructure dialing tests * address last e2e tests * address review * add logging for main methods * decouple pure dialer class, update network monitor with specific metrics * remove console.log * remove usage of protocols * update sdk package tests * add connect await promise * add debug for e2e tests * enable only packages tests * use only one file * revert debugging * up interface for netwrok manager * add logs * add more logs * add more logs * add another logs * remove .only * remove log statements * skip the test with follow up
2025-07-09 21:23:14 +02:00
dns: false,
peerExchange: true,
feat!: re-architect connection manager (#2445) * remove public pubsub field and redundant util * add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests * improve public dial method to start keep alive checks * move dial method * implement discovery dialer * implement discovery dialer with queue with tests * add discovery dialer e2e tests, change local discovery log tag, update other tests * remove comment * add issue link, remove only * implement shard reader component * create evetns module, remove unused connection manager events and related tests * implement network indicator * implement connection limiter, change public API of connection manager, implement recovery strategy * decouple keep alive maanger * add connection manager js-doc * refactor keep alive manager, cover with tests * add tests for connection manager main facade * add tests for connection limiter * add e2e tests for connection manager modules pass js-waku config during test node init remove dns discovery for js-waku * restructure dialing tests * address last e2e tests * address review * add logging for main methods * decouple pure dialer class, update network monitor with specific metrics * remove console.log * remove usage of protocols * update sdk package tests * add connect await promise * add debug for e2e tests * enable only packages tests * use only one file * revert debugging * up interface for netwrok manager * add logs * add more logs * add more logs * add another logs * remove .only * remove log statements * skip the test with follow up
2025-07-09 21:23:14 +02:00
localPeerCache: false
};
type RunNodesOptions = {
context: Context;
networkConfig: NetworkConfig;
relayShards?: ShardId[]; // Only for static sharding
contentTopics?: ContentTopic[]; // Only for auto sharding
protocols: Protocols[];
createNode: typeof createLightNode | typeof createRelayNode;
};
export async function runNodes<T>(
options: RunNodesOptions
): Promise<[ServiceNode, T]> {
const { context, networkConfig, createNode, protocols } = options;
const nwaku = new ServiceNode(makeLogFileName(context));
const nwakuArgs: Args = {
filter: true,
lightpush: true,
relay: true,
store: true,
clusterId: networkConfig.clusterId
};
const jswakuArgs: CreateNodeOptions = {
staticNoiseKey: NOISE_KEY_1,
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
networkConfig,
lightPush: { numPeersToUse: 2 },
discovery: DEFAULT_DISCOVERIES_ENABLED
};
const routingInfos: RoutingInfo[] = [];
if (isAutoSharding(networkConfig)) {
nwakuArgs.numShardsInNetwork = networkConfig.numShardsInCluster;
nwakuArgs.contentTopic = options.contentTopics ?? [];
nwakuArgs.contentTopic.map((ct) =>
routingInfos.push(createRoutingInfo(networkConfig, { contentTopic: ct }))
);
if (options.relayShards && options.relayShards.length > 0)
throw "`relayShards` cannot be set for auto-sharding";
} else if (isStaticSharding(networkConfig) && options.relayShards) {
const shards = options.relayShards;
nwakuArgs.shard = shards;
2025-07-21 15:54:05 +10:00
nwakuArgs.numShardsInNetwork = 0;
shards.map((shardId) =>
routingInfos.push(createRoutingInfo(networkConfig, { shardId }))
);
if (options.contentTopics && options.contentTopics.length > 0)
throw "`contentTopics` cannot be set for static sharding";
} else {
throw "Invalid Network Config";
}
const jswakuRelayCreateOptions: RelayCreateOptions = {
routingInfos
};
await nwaku.start(nwakuArgs, { retries: 3 });
log.info("Starting js waku node with :", JSON.stringify(jswakuArgs));
let waku: WakuNode | undefined;
try {
waku = (await createNode({
...jswakuArgs,
...jswakuRelayCreateOptions
})) as unknown as WakuNode;
await waku.start();
} catch (error) {
log.error("jswaku node failed to start:", error);
}
if (waku) {
await waku.dial(await nwaku.getMultiaddrWithId());
feat: replace `waitForRemotePeers()` with `waku.waitForPeer()` method (#2161) * fix comment of default number of peers * export default number of peers from base protocol sdk * rename to light_push, move class to separate file * move waitForRemotePeer to sdk package * add todo to move waitForGossipSubPeerInMesh into @waku/relay * clean up waitForRemotePeer, split metadata await from event and optimise, decouple from protocol implementations * simplify and rename ILightPush interface * use only connected peers in light push based on connections instead of peer renewal mechanism * improve readability of result processing in light push * fix check & update tests * address tests, add new test cases, fix racing condition in StreamManager * use libp2p.getPeers * feat: confirm metadata and protocols needed in waitForRemotePeer * rely on passed protocols and fallback to mounted * add I prefix to Waku interface * implement waku.connect method * add docs to IWaku interface * remove export and usage of waitForRemotePeer * move wait for remote peer related to Realy out of @waku/sdk * change tests to use new API * fix linting * update size limit * rename .connect to .waitForPeer * export waitForRemotePeer and mark as deprecated * feat: add mocha tests to @waku/sdk and cover waitForRemotePeer (#2163) * feat: add mocha tests to @waku/sdk and cover waitForRemotePeer * add waitForRemote UTs * remove junk * feat: expose peerId and protocols from WakuNode (#2166) * chore: expose peerId and protocols from WakuNode * remove unused method * move to private method * rename to waitForPeers * up test
2024-10-09 00:43:34 +02:00
await waku.waitForPeers(protocols);
await nwaku.ensureSubscriptions(routingInfos.map((r) => r.pubsubTopic));
return [nwaku, waku as T];
} else {
throw new Error("Failed to initialize waku");
}
}