Skip to content

Commit

Permalink
feat: add bootstrapPeers option and refactor sdk (#1871)
Browse files Browse the repository at this point in the history
* move relay related code

* move libp2p to utils

* define CreateWakuNodeOptions

* improve options

* make libp2p use from create function

* add bootstrapPeers option

* fix lint

* fix types, imports

* fix exports

* use bootstrap along default bootstrap

* fix test as REST does not return peer though connection is made

* rollback condition change

* enable gossipSub back for every node
  • Loading branch information
weboko authored Mar 4, 2024
1 parent fcc3f10 commit 9f198dd
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 248 deletions.
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export type ProtocolCreateOptions = {
* Use recommended bootstrap method to discovery and connect to new nodes.
*/
defaultBootstrap?: boolean;
/**
* List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
*/
bootstrapPeers?: string[];
};

export type Callback<T extends IDecodedMessage> = (
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"@waku/peer-exchange": "^0.0.19",
"@waku/relay": "0.0.9",
"@waku/utils": "0.0.14",
"libp2p": "^1.1.2"
"libp2p": "^1.1.2",
"@libp2p/bootstrap": "^10.0.11"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^12.0.0",
Expand All @@ -86,6 +87,9 @@
"npm-run-all": "^4.1.5",
"rollup": "^4.12.0"
},
"peerDependencies": {
"@libp2p/bootstrap": "^10"
},
"files": [
"dist",
"bundle",
Expand Down
211 changes: 8 additions & 203 deletions packages/sdk/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,22 @@
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
import { noise } from "@chainsafe/libp2p-noise";
import { identify } from "@libp2p/identify";
import type { PeerDiscovery } from "@libp2p/interface";
import { mplex } from "@libp2p/mplex";
import { ping } from "@libp2p/ping";
import { webSockets } from "@libp2p/websockets";
import { all as filterAll } from "@libp2p/websockets/filters";
import { wakuFilter, wakuLightPush, wakuMetadata, wakuStore } from "@waku/core";
import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery";
import {
type CreateLibp2pOptions,
DefaultPubsubTopic,
type FullNode,
type IMetadata,
type Libp2p,
type Libp2pComponents,
type LightNode,
type ProtocolCreateOptions,
PubsubTopic,
type ShardInfo
} from "@waku/interfaces";
import { wakuLocalPeerCacheDiscovery } from "@waku/local-peer-cache-discovery";
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
import { ensureShardingConfigured } from "@waku/utils";
import { createLibp2p } from "libp2p";
import { wakuFilter, wakuLightPush, wakuStore } from "@waku/core";
import { type Libp2pComponents, type LightNode } from "@waku/interfaces";

import { DefaultUserAgent, WakuNode, WakuOptions } from "./waku.js";

const DEFAULT_NODE_REQUIREMENTS = {
lightPush: 1,
filter: 1,
store: 1
};
import { createLibp2pAndUpdateOptions } from "./utils/libp2p.js";
import { CreateWakuNodeOptions, WakuNode, WakuOptions } from "./waku.js";

export { Libp2pComponents };

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

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

const shardInfo = ensureShardingConfigured(options.shardInfo);
options.pubsubTopics = shardInfo.pubsubTopics;
options.shardInfo = shardInfo.shardInfo;

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
if (options?.defaultBootstrap) {
peerDiscovery.push(...defaultPeerDiscoveries(shardInfo.pubsubTopics));
Object.assign(libp2pOptions, { peerDiscovery });
}

const libp2p = await defaultLibp2p(
shardInfo.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
);
const libp2p = await createLibp2pAndUpdateOptions(options);

const store = wakuStore(options);
const lightPush = wakuLightPush(options);
Expand All @@ -87,30 +37,9 @@ export async function createNode(
* Uses Waku Filter V2 by default.
*/
export async function createLightNode(
options?: ProtocolCreateOptions & Partial<WakuOptions>
options: CreateWakuNodeOptions = {}
): Promise<LightNode> {
options = options ?? {};

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

options.pubsubTopics = shardInfo?.pubsubTopics ??
options.pubsubTopics ?? [DefaultPubsubTopic];

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
if (options?.defaultBootstrap) {
peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics));
Object.assign(libp2pOptions, { peerDiscovery });
}

const libp2p = await defaultLibp2p(
shardInfo?.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
);
const libp2p = await createLibp2pAndUpdateOptions(options);

const store = wakuStore(options);
const lightPush = wakuLightPush(options);
Expand All @@ -124,127 +53,3 @@ export async function createLightNode(
filter
) as LightNode;
}

/**
* 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 &
Partial<WakuOptions> &
Partial<RelayCreateOptions>
): Promise<FullNode> {
options = options ?? { pubsubTopics: [] };

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

const pubsubTopics = shardInfo?.pubsubTopics ??
options.pubsubTopics ?? [DefaultPubsubTopic];
options.pubsubTopics = pubsubTopics;
options.shardInfo = shardInfo?.shardInfo;

const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
if (options?.defaultBootstrap) {
peerDiscovery.push(...defaultPeerDiscoveries(pubsubTopics));
Object.assign(libp2pOptions, { peerDiscovery });
}

const libp2p = await defaultLibp2p(
shardInfo?.shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
);

const store = wakuStore(options);
const lightPush = wakuLightPush(options);
const filter = wakuFilter(options);
const relay = wakuRelay(pubsubTopics);

return new WakuNode(
options as WakuOptions,
libp2p,
store,
lightPush,
filter,
relay
) as FullNode;
}

export function defaultPeerDiscoveries(
pubsubTopics: PubsubTopic[]
): ((components: Libp2pComponents) => PeerDiscovery)[] {
const discoveries = [
wakuDnsDiscovery([enrTree["PROD"]], DEFAULT_NODE_REQUIREMENTS),
wakuLocalPeerCacheDiscovery(),
wakuPeerExchangeDiscovery(pubsubTopics)
];
return discoveries;
}

type PubsubService = {
pubsub?: (components: Libp2pComponents) => GossipSub;
};

type MetadataService = {
metadata?: (components: Libp2pComponents) => IMetadata;
};

export async function defaultLibp2p(
shardInfo?: ShardInfo,
wakuGossipSub?: PubsubService["pubsub"],
options?: Partial<CreateLibp2pOptions>,
userAgent?: string
): Promise<Libp2p> {
if (!options?.hideWebSocketInfo && process.env.NODE_ENV !== "test") {
/* eslint-disable no-console */
console.info(
"%cIgnore WebSocket connection failures",
"background: gray; color: white; font-size: x-large"
);
console.info(
"%cWaku tries to discover peers and some of them are expected to fail",
"background: gray; color: white; font-size: x-large"
);
/* eslint-enable no-console */
}

const pubsubService: PubsubService = wakuGossipSub
? { pubsub: wakuGossipSub }
: {};

const metadataService: MetadataService = shardInfo
? { metadata: wakuMetadata(shardInfo) }
: {};

return createLibp2p({
connectionManager: {
minConnections: 1
},
transports: [webSockets({ filter: filterAll })],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
...options,
services: {
identify: identify({
agentVersion: userAgent ?? DefaultUserAgent
}),
ping: ping(),
...metadataService,
...pubsubService,
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;
}
3 changes: 2 additions & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export {

export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes";

export * from "./content_topic.js";
export { defaultLibp2p } from "./utils/libp2p.js";
export * from "./utils/content_topic.js";
export * from "./waku.js";
export * from "./create.js";
export * as waku from "@waku/core";
Expand Down
Loading

0 comments on commit 9f198dd

Please sign in to comment.