mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-25 03:13:11 +00:00
* set peer-exchange with default bootstrap * only initialise protocols with bootstrap peers * update package * update package-lock * refactor `getPeers` while setting up a protocol * move codecs to `@waku/interfaces` * lightpush: send messages to multiple peers * only use multiple peers for LP and Filter * fix: ts warnings * lightpush: tests pass * update breaking changes for new API * move codecs back into protocol files * refactor: `getPeers()` * rm: log as an arg * add tsdoc for getPeers * add import * add prettier rule to eslint * add: peer exchange to sdk as a dep * fix eslint error * add try catch * revert unecessary diff * revert unecessary diff * fix imports * convert relaycodecs to array * remove: peerId as an arg for protocol methods * keep peerId as an arg for peer-exchange * remove: peerId from getPeers() * lightpush: extract hardcoded numPeers as a constant * return all peers if numPeers is 0 and increase readability for random peers * refactor considering more than 1 bootstrap peers can exist * use `getPeers` * change arg for `getPeers` to object * address comments * refactor tests for new API * lightpush: make constant the class variable * use `maxBootstrapPeers` instead of `includeBootstrap` * refactor protocols for new API * add tests for `getPeers` * skip getPeers test * rm: only from test * move tests to `base_protocol.spec.ts` * break down `getPeers` into a `filter` method * return all bootstrap peers if arg is 0 * refactor test without stubbing * address comments * update test title * move `filterPeers` to a separate file * address comments & add more test * make test title more verbose * address comments * remove ProtocolOptions * chore: refactor tests for new API * add defaults for getPeers * address comments * rm unneeded comment * address comment: add diversity of node tags to test * address comments * fix: imports
189 lines
4.9 KiB
TypeScript
189 lines
4.9 KiB
TypeScript
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
|
|
import { noise } from "@chainsafe/libp2p-noise";
|
|
import type { PeerDiscovery } from "@libp2p/interface/peer-discovery";
|
|
import { mplex } from "@libp2p/mplex";
|
|
import { webSockets } from "@libp2p/websockets";
|
|
import { all as filterAll } from "@libp2p/websockets/filters";
|
|
import {
|
|
DefaultUserAgent,
|
|
wakuFilter,
|
|
wakuLightPush,
|
|
WakuNode,
|
|
WakuOptions,
|
|
wakuStore
|
|
} from "@waku/core";
|
|
import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery";
|
|
import type {
|
|
FullNode,
|
|
Libp2p,
|
|
Libp2pComponents,
|
|
LightNode,
|
|
ProtocolCreateOptions,
|
|
RelayNode
|
|
} from "@waku/interfaces";
|
|
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
|
|
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
|
|
import { createLibp2p, Libp2pOptions } from "libp2p";
|
|
import { identifyService } from "libp2p/identify";
|
|
import { pingService } from "libp2p/ping";
|
|
|
|
const DEFAULT_NODE_REQUIREMENTS = {
|
|
lightPush: 1,
|
|
filter: 1,
|
|
store: 1
|
|
};
|
|
|
|
export { Libp2pComponents };
|
|
|
|
/**
|
|
* Create a Waku node that uses Waku Light Push, Filter and Store to send and
|
|
* receive messages, enabling low resource consumption.
|
|
* Uses Waku Filter V2 by default.
|
|
*/
|
|
export async function createLightNode(
|
|
options?: ProtocolCreateOptions & WakuOptions
|
|
): Promise<LightNode> {
|
|
const libp2pOptions = options?.libp2p ?? {};
|
|
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
|
|
if (options?.defaultBootstrap) {
|
|
peerDiscovery.push(...defaultPeerDiscoveries());
|
|
Object.assign(libp2pOptions, { peerDiscovery });
|
|
}
|
|
|
|
const libp2p = await defaultLibp2p(
|
|
undefined,
|
|
libp2pOptions,
|
|
options?.userAgent
|
|
);
|
|
|
|
const store = wakuStore(options);
|
|
const lightPush = wakuLightPush(options);
|
|
const filter = wakuFilter(options);
|
|
|
|
return new WakuNode(
|
|
options ?? {},
|
|
libp2p,
|
|
store,
|
|
lightPush,
|
|
filter
|
|
) as LightNode;
|
|
}
|
|
|
|
/**
|
|
* Create a Waku node that uses Waku Relay to send and receive messages,
|
|
* enabling some privacy preserving properties.
|
|
*/
|
|
export async function createRelayNode(
|
|
options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
|
|
): Promise<RelayNode> {
|
|
const libp2pOptions = options?.libp2p ?? {};
|
|
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
|
|
if (options?.defaultBootstrap) {
|
|
peerDiscovery.push(...defaultPeerDiscoveries());
|
|
Object.assign(libp2pOptions, { peerDiscovery });
|
|
}
|
|
|
|
const libp2p = await defaultLibp2p(
|
|
wakuGossipSub(options),
|
|
libp2pOptions,
|
|
options?.userAgent
|
|
);
|
|
|
|
const relay = wakuRelay(options);
|
|
|
|
return new WakuNode(
|
|
options ?? {},
|
|
libp2p,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
relay
|
|
) as RelayNode;
|
|
}
|
|
|
|
/**
|
|
* Create a Waku node that uses all Waku protocols.
|
|
*
|
|
* This helper is not recommended except if:
|
|
* - you are interfacing with nwaku v0.11 or below
|
|
* - you are doing some form of testing
|
|
*
|
|
* If you are building a full node, it is recommended to use
|
|
* [nwaku](github.com/status-im/nwaku) and its JSON RPC API or wip REST API.
|
|
*
|
|
* @see https://github.com/status-im/nwaku/issues/1085
|
|
* @internal
|
|
*/
|
|
export async function createFullNode(
|
|
options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions>
|
|
): Promise<FullNode> {
|
|
const libp2pOptions = options?.libp2p ?? {};
|
|
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
|
|
if (options?.defaultBootstrap) {
|
|
peerDiscovery.push(...defaultPeerDiscoveries());
|
|
Object.assign(libp2pOptions, { peerDiscovery });
|
|
}
|
|
|
|
const libp2p = await defaultLibp2p(
|
|
wakuGossipSub(options),
|
|
libp2pOptions,
|
|
options?.userAgent
|
|
);
|
|
|
|
const store = wakuStore(options);
|
|
const lightPush = wakuLightPush(options);
|
|
const filter = wakuFilter(options);
|
|
const relay = wakuRelay(options);
|
|
|
|
return new WakuNode(
|
|
options ?? {},
|
|
libp2p,
|
|
store,
|
|
lightPush,
|
|
filter,
|
|
relay
|
|
) as FullNode;
|
|
}
|
|
|
|
export function defaultPeerDiscoveries(): ((
|
|
components: Libp2pComponents
|
|
) => PeerDiscovery)[] {
|
|
const discoveries = [
|
|
wakuDnsDiscovery([enrTree["PROD"]], DEFAULT_NODE_REQUIREMENTS),
|
|
wakuPeerExchangeDiscovery()
|
|
];
|
|
return discoveries;
|
|
}
|
|
|
|
type PubsubService = {
|
|
pubsub?: (components: Libp2pComponents) => GossipSub;
|
|
};
|
|
|
|
export async function defaultLibp2p(
|
|
wakuGossipSub?: PubsubService["pubsub"],
|
|
options?: Partial<Libp2pOptions>,
|
|
userAgent?: string
|
|
): Promise<Libp2p> {
|
|
const pubsubService: PubsubService = wakuGossipSub
|
|
? { pubsub: wakuGossipSub }
|
|
: {};
|
|
|
|
return createLibp2p({
|
|
connectionManager: {
|
|
minConnections: 1
|
|
},
|
|
transports: [webSockets({ filter: filterAll })],
|
|
streamMuxers: [mplex()],
|
|
connectionEncryption: [noise()],
|
|
...options,
|
|
services: {
|
|
identify: identifyService({
|
|
agentVersion: userAgent ?? DefaultUserAgent
|
|
}),
|
|
ping: pingService(),
|
|
...pubsubService,
|
|
...options?.services
|
|
}
|
|
}) as any as Libp2p; // TODO: make libp2p include it;
|
|
}
|