Merge branch 'master' into chore-update-docs-link

This commit is contained in:
Sasha 2023-07-25 21:53:46 +02:00 committed by GitHub
commit 1feea5c24b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
54 changed files with 5311 additions and 13422 deletions

View File

@ -49,7 +49,10 @@
"error",
{ "ignoreDeclarationSort": true, "ignoreCase": true }
],
"no-console": "error"
"no-console": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/no-misused-promises": "error"
},
"overrides": [
{

View File

@ -9,7 +9,7 @@ jobs:
name: Add issue to project
runs-on: ubuntu-latest
steps:
- uses: actions/add-to-project@v0.3.0
- uses: actions/add-to-project@v0.5.0
with:
project-url: https://github.com/orgs/waku-org/projects/2
github-token: ${{ secrets.ADD_TO_PROJECT_PAT }}

17400
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -46,7 +46,16 @@
"lint-staged": "^13.2.2",
"size-limit": "^8.1.2",
"typedoc": "^0.23.26",
"typedoc-plugin-resolve-crossmodule-references": "^0.3.3"
"typedoc-plugin-resolve-crossmodule-references": "^0.3.3",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.8"
},
"lint-staged": {
"*.{ts,js}": [

View File

@ -0,0 +1,24 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
},
env: {
browser: true,
es2021: true,
node: true,
},
extends: ["eslint:recommended"],
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"],
"no-unused-vars": [
"error",
{ vars: "all", args: "after-used", ignoreRestSiblings: false },
],
"no-console": "warn",
},
};

View File

@ -21,14 +21,5 @@
"bugs": {
"url": "https://github.com/waku-org/js-waku/issues"
},
"homepage": "https://github.com/waku-org/js-waku#readme",
"devDependencies": {
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.8"
}
"homepage": "https://github.com/waku-org/js-waku#readme"
}

View File

@ -85,11 +85,11 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-libp2p": "^1.1.2",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@libp2p/interface-connection": "^5.1.1",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interface-registrar": "^2.0.12",
"@multiformats/multiaddr": "^12.0.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
@ -98,17 +98,9 @@
"@types/debug": "^4.1.7",
"@types/mocha": "^10.0.1",
"@types/uuid": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"fast-check": "^3.8.1",
"ignore-loader": "^0.1.2",
"isomorphic-fetch": "^3.0.0",
@ -128,7 +120,7 @@
},
"peerDependencies": {
"@multiformats/multiaddr": "^12.0.0",
"libp2p": "^0.42.2"
"libp2p": "^0.45.9"
},
"peerDependenciesMeta": {
"@multiformats/multiaddr": {

View File

@ -1,6 +1,8 @@
import type { Connection, Stream } from "@libp2p/interface-connection";
import type { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import { Peer, PeerStore } from "@libp2p/interface-peer-store";
import type { IBaseProtocol, Libp2pComponents } from "@waku/interfaces";
import {
getPeersForProtocol,
selectConnection,
@ -11,19 +13,29 @@ import {
* A class with predefined helpers, to be used as a base to implement Waku
* Protocols.
*/
export class BaseProtocol {
constructor(
public multicodec: string,
public peerStore: PeerStore,
protected getConnections: (peerId?: PeerId) => Connection[]
) {}
export class BaseProtocol implements IBaseProtocol {
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
constructor(public multicodec: string, private components: Libp2pComponents) {
this.addLibp2pEventListener = components.events.addEventListener.bind(
components.events
);
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
components.events
);
}
public get peerStore(): PeerStore {
return this.components.peerStore;
}
/**
* Returns known peers from the address book (`libp2p.peerStore`) that support
* the class protocol. Waku may or may not be currently connected to these
* peers.
*/
async peers(): Promise<Peer[]> {
public async peers(): Promise<Peer[]> {
return getPeersForProtocol(this.peerStore, [this.multicodec]);
}
@ -36,7 +48,9 @@ export class BaseProtocol {
return peer;
}
protected async newStream(peer: Peer): Promise<Stream> {
const connections = this.getConnections(peer.id);
const connections = this.components.connectionManager.getConnections(
peer.id
);
const connection = selectConnection(connections);
if (!connection) {
throw new Error("Failed to get a connection to the peer");

View File

@ -1,9 +1,7 @@
import type { Connection } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { ConnectionManagerOptions, IRelay } from "@waku/interfaces";
import { Tags } from "@waku/interfaces";
import { Libp2p, Tags } from "@waku/interfaces";
import debug from "debug";
import { KeepAliveManager, KeepAliveOptions } from "./keep_alive_manager.js";
@ -18,7 +16,7 @@ export class ConnectionManager {
private static instances = new Map<string, ConnectionManager>();
private keepAliveManager: KeepAliveManager;
private options: ConnectionManagerOptions;
private libp2pComponents: Libp2p;
private libp2p: Libp2p;
private dialAttemptsForPeer: Map<string, number> = new Map();
private dialErrorsForPeer: Map<string, any> = new Map();
@ -47,12 +45,12 @@ export class ConnectionManager {
}
private constructor(
libp2pComponents: Libp2p,
libp2p: Libp2p,
keepAliveOptions: KeepAliveOptions,
relay?: IRelay,
options?: Partial<ConnectionManagerOptions>
) {
this.libp2pComponents = libp2pComponents;
this.libp2p = libp2p;
this.options = {
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
@ -69,17 +67,17 @@ export class ConnectionManager {
// libp2p emits `peer:discovery` events during its initialization
// which means that before the ConnectionManager is initialized, some peers may have been discovered
// we will dial the peers in peerStore ONCE before we start to listen to the `peer:discovery` events within the ConnectionManager
this.dialPeerStorePeers();
this.dialPeerStorePeers().catch((error) =>
log(`Unexpected error while dialing peer store peers`, error)
);
}
private async dialPeerStorePeers(): Promise<void> {
const peerInfos = await this.libp2pComponents.peerStore.all();
const peerInfos = await this.libp2p.peerStore.all();
const dialPromises = [];
for (const peerInfo of peerInfos) {
if (
this.libp2pComponents
.getConnections()
.find((c) => c.remotePeer === peerInfo.id)
this.libp2p.getConnections().find((c) => c.remotePeer === peerInfo.id)
)
continue;
@ -101,15 +99,15 @@ export class ConnectionManager {
stop(): void {
this.keepAliveManager.stopAll();
this.libp2pComponents.removeEventListener(
this.libp2p.removeEventListener(
"peer:connect",
this.onEventHandlers["peer:connect"]
);
this.libp2pComponents.removeEventListener(
this.libp2p.removeEventListener(
"peer:disconnect",
this.onEventHandlers["peer:disconnect"]
);
this.libp2pComponents.removeEventListener(
this.libp2p.removeEventListener(
"peer:discovery",
this.onEventHandlers["peer:discovery"]
);
@ -121,12 +119,12 @@ export class ConnectionManager {
while (dialAttempt <= this.options.maxDialAttemptsForPeer) {
try {
log(`Dialing peer ${peerId.toString()}`);
await this.libp2pComponents.dial(peerId);
await this.libp2p.dial(peerId);
const tags = await this.getTagNamesForPeer(peerId);
// add tag to connection describing discovery mechanism
// don't add duplicate tags
this.libp2pComponents
this.libp2p
.getConnections(peerId)
.forEach(
(conn) => (conn.tags = Array.from(new Set([...conn.tags, ...tags])))
@ -157,7 +155,7 @@ export class ConnectionManager {
}`
);
this.dialErrorsForPeer.delete(peerId.toString());
return await this.libp2pComponents.peerStore.delete(peerId);
return await this.libp2p.peerStore.delete(peerId);
} catch (error) {
throw `Error deleting undialable peer ${peerId.toString()} from peer store - ${error}`;
} finally {
@ -168,7 +166,7 @@ export class ConnectionManager {
async dropConnection(peerId: PeerId): Promise<void> {
try {
await this.libp2pComponents.hangUp(peerId);
await this.libp2p.hangUp(peerId);
log(`Dropped connection with peer ${peerId.toString()}`);
} catch (error) {
log(
@ -177,7 +175,7 @@ export class ConnectionManager {
}
}
private async processDialQueue(): Promise<void> {
private processDialQueue(): void {
if (
this.pendingPeerDialQueue.length > 0 &&
this.currentActiveDialCount < this.options.maxParallelDials
@ -191,14 +189,14 @@ export class ConnectionManager {
}
private startPeerDiscoveryListener(): void {
this.libp2pComponents.peerStore.addEventListener(
"peer",
this.libp2p.addEventListener(
"peer:discovery",
this.onEventHandlers["peer:discovery"]
);
}
private startPeerConnectionListener(): void {
this.libp2pComponents.addEventListener(
this.libp2p.addEventListener(
"peer:connect",
this.onEventHandlers["peer:connect"]
);
@ -217,7 +215,7 @@ export class ConnectionManager {
* >this event will **only** be triggered when the last connection is closed.
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
*/
this.libp2pComponents.addEventListener(
this.libp2p.addEventListener(
"peer:disconnect",
this.onEventHandlers["peer:disconnect"]
);
@ -237,41 +235,44 @@ export class ConnectionManager {
}
private onEventHandlers = {
"peer:discovery": async (evt: CustomEvent<PeerInfo>): Promise<void> => {
const { id: peerId } = evt.detail;
"peer:discovery": (evt: CustomEvent<PeerInfo>): void => {
void (async () => {
const { id: peerId } = evt.detail;
this.attemptDial(peerId).catch((err) =>
log(`Error dialing peer ${peerId.toString()} : ${err}`)
);
},
"peer:connect": async (evt: CustomEvent<Connection>): Promise<void> => {
const { remotePeer: peerId } = evt.detail;
this.keepAliveManager.start(
peerId,
this.libp2pComponents.ping.bind(this)
);
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
Tags.BOOTSTRAP
);
if (isBootstrap) {
const bootstrapConnections = this.libp2pComponents
.getConnections()
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
// If we have too many bootstrap connections, drop one
if (
bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
) {
await this.dropConnection(peerId);
try {
await this.attemptDial(peerId);
} catch (error) {
log(`Error dialing peer ${peerId.toString()} : ${error}`);
}
}
})();
},
"peer:connect": (evt: CustomEvent<PeerId>): void => {
void (async () => {
const peerId = evt.detail;
this.keepAliveManager.start(peerId, this.libp2p.services.ping);
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
Tags.BOOTSTRAP
);
if (isBootstrap) {
const bootstrapConnections = this.libp2p
.getConnections()
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
// If we have too many bootstrap connections, drop one
if (
bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
) {
await this.dropConnection(peerId);
}
}
})();
},
"peer:disconnect": () => {
return (evt: CustomEvent<Connection>): void => {
this.keepAliveManager.stop(evt.detail.remotePeer);
return (evt: CustomEvent<PeerId>): void => {
this.keepAliveManager.stop(evt.detail);
};
},
};
@ -282,19 +283,19 @@ export class ConnectionManager {
* 2. If the peer is not a bootstrap peer
*/
private async shouldDialPeer(peerId: PeerId): Promise<boolean> {
const isConnected = this.libp2pComponents.getConnections(peerId).length > 0;
const isConnected = this.libp2p.getConnections(peerId).length > 0;
if (isConnected) return false;
const isBootstrap = (await this.getTagNamesForPeer(peerId)).some(
(tagName) => tagName === Tags.BOOTSTRAP
);
const tagNames = await this.getTagNamesForPeer(peerId);
const isBootstrap = tagNames.some((tagName) => tagName === Tags.BOOTSTRAP);
if (isBootstrap) {
const currentBootstrapConnections = this.libp2pComponents
const currentBootstrapConnections = this.libp2p
.getConnections()
.filter((conn) => {
conn.tags.find((name) => name === Tags.BOOTSTRAP);
return conn.tags.find((name) => name === Tags.BOOTSTRAP);
}).length;
if (currentBootstrapConnections < this.options.maxBootstrapPeersAllowed)
return true;
@ -309,9 +310,12 @@ export class ConnectionManager {
* Fetches the tag names for a given peer
*/
private async getTagNamesForPeer(peerId: PeerId): Promise<string[]> {
const tags = (await this.libp2pComponents.peerStore.getTags(peerId)).map(
(tag) => tag.name
);
return tags;
try {
const peer = await this.libp2p.peerStore.get(peerId);
return Array.from(peer.tags.keys());
} catch (error) {
log(`Failed to get peer ${peerId}, error: ${error}`);
return [];
}
}
}

View File

@ -1,4 +1,3 @@
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { Peer } from "@libp2p/interface-peer-store";
import type { IncomingStreamData } from "@libp2p/interface-registrar";
import type {
@ -9,6 +8,7 @@ import type {
IDecodedMessage,
IDecoder,
IFilter,
Libp2p,
ProtocolCreateOptions,
ProtocolOptions,
} from "@waku/interfaces";
@ -50,11 +50,11 @@ class Filter extends BaseProtocol implements IFilter {
options: ProtocolCreateOptions;
private subscriptions: Map<RequestID, unknown>;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(FilterCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(FilterCodec, libp2p.components);
this.options = options ?? {};
this.subscriptions = new Map();
this.libp2p
libp2p
.handle(this.multicodec, this.onRequest.bind(this))
.catch((e) => log("Failed to register filter protocol", e));
}
@ -200,7 +200,7 @@ class Filter extends BaseProtocol implements IFilter {
// We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works
// noinspection ES6MissingAwait
decoders.forEach(async (dec: IDecoder<T>) => {
for (const dec of decoders) {
if (didDecodeMsg) return;
const decoded = await dec.fromProtoObj(
pubSubTopic,
@ -208,13 +208,13 @@ class Filter extends BaseProtocol implements IFilter {
);
if (!decoded) {
log("Not able to decode message");
return;
continue;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
}
}
}

View File

@ -1,5 +1,4 @@
import { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { Peer } from "@libp2p/interface-peer-store";
import type { IncomingStreamData } from "@libp2p/interface-registrar";
@ -12,6 +11,7 @@ import type {
IFilterV2,
IProtoMessage,
IReceiver,
Libp2p,
PeerIdStr,
ProtocolCreateOptions,
ProtocolOptions,
@ -245,18 +245,12 @@ class FilterV2 extends BaseProtocol implements IReceiver {
return subscription;
}
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(
FilterV2Codecs.SUBSCRIBE,
libp2p.peerStore,
libp2p.getConnections.bind(libp2p)
);
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(FilterV2Codecs.SUBSCRIBE, libp2p.components);
this.libp2p
.handle(FilterV2Codecs.PUSH, this.onRequest.bind(this))
.catch((e) => {
log("Failed to register ", FilterV2Codecs.PUSH, e);
});
libp2p.handle(FilterV2Codecs.PUSH, this.onRequest.bind(this)).catch((e) => {
log("Failed to register ", FilterV2Codecs.PUSH, e);
});
this.activeSubscriptions = new Map();
@ -312,7 +306,7 @@ class FilterV2 extends BaseProtocol implements IReceiver {
): Promise<Unsubscribe> {
const subscription = await this.createSubscription(undefined, opts?.peerId);
subscription.subscribe(decoders, callback);
await subscription.subscribe(decoders, callback);
const contentTopics = Array.from(
groupByContentTopic(
@ -394,20 +388,19 @@ async function pushMessage<T extends IDecodedMessage>(
// We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works
// noinspection ES6MissingAwait
decoders.forEach(async (dec: IDecoder<T>) => {
if (didDecodeMsg) return;
for (const dec of decoders) {
if (didDecodeMsg) break;
const decoded = await dec.fromProtoObj(
pubSubTopic,
message as IProtoMessage
);
// const decoded = await dec.fromProtoObj(pubSubTopic, message);
if (!decoded) {
log("Not able to decode message");
return;
continue;
}
// This is just to prevent more decoding attempt
// TODO: Could be better if we were to abort promises
didDecodeMsg = Boolean(decoded);
await callback(decoded);
});
}
}

View File

@ -1,7 +1,7 @@
import type { PeerId } from "@libp2p/interface-peer-id";
import type { IRelay } from "@waku/interfaces";
import debug from "debug";
import type { Libp2p } from "libp2p";
import type { PingService } from "libp2p/ping";
import { createEncoder } from "../index.js";
@ -26,7 +26,7 @@ export class KeepAliveManager {
this.relay = relay;
}
public start(peerId: PeerId, libp2pPing: Libp2p["ping"]): void {
public start(peerId: PeerId, libp2pPing: PingService): void {
// Just in case a timer already exist for this peer
this.stop(peerId);
@ -37,7 +37,7 @@ export class KeepAliveManager {
if (pingPeriodSecs !== 0) {
const interval = setInterval(() => {
libp2pPing(peerId).catch((e) => {
libp2pPing.ping(peerId).catch((e) => {
log(`Ping failed (${peerIdStr})`, e);
});
}, pingPeriodSecs * 1000);

View File

@ -1,9 +1,9 @@
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import {
IEncoder,
ILightPush,
IMessage,
Libp2p,
ProtocolCreateOptions,
ProtocolOptions,
SendError,
@ -33,8 +33,8 @@ export { PushResponse };
class LightPush extends BaseProtocol implements ILightPush {
options: ProtocolCreateOptions;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(LightPushCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(LightPushCodec, libp2p.components);
this.options = options || {};
}

View File

@ -1,5 +1,4 @@
import type { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import { sha256 } from "@noble/hashes/sha256";
import {
@ -7,6 +6,7 @@ import {
IDecodedMessage,
IDecoder,
IStore,
Libp2p,
ProtocolCreateOptions,
} from "@waku/interfaces";
import { proto_store as proto } from "@waku/proto";
@ -81,8 +81,8 @@ export interface QueryOptions {
class Store extends BaseProtocol implements IStore {
options: ProtocolCreateOptions;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(StoreCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(StoreCodec, libp2p.components);
this.options = options ?? {};
}

View File

@ -1,5 +1,5 @@
import type { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces";
import type { IdentifyResult } from "@libp2p/interface-libp2p";
import type { IBaseProtocol, IRelay, Waku } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import debug from "debug";
import { pEvent } from "p-event";
@ -74,9 +74,7 @@ export async function waitForRemotePeer(
/**
* Wait for a peer with the given protocol to be connected.
*/
async function waitForConnectedPeer(
protocol: PointToPointProtocol
): Promise<void> {
async function waitForConnectedPeer(protocol: IBaseProtocol): Promise<void> {
const codec = protocol.multicodec;
const peers = await protocol.peers();
@ -86,14 +84,14 @@ async function waitForConnectedPeer(
}
await new Promise<void>((resolve) => {
const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
if (evt.detail.protocols.includes(codec)) {
const cb = (evt: CustomEvent<IdentifyResult>): void => {
if (evt.detail?.protocols?.includes(codec)) {
log("Resolving for", codec, evt.detail.protocols);
protocol.peerStore.removeEventListener("change:protocols", cb);
protocol.removeLibp2pEventListener("peer:identify", cb);
resolve();
}
};
protocol.peerStore.addEventListener("change:protocols", cb);
protocol.addLibp2pEventListener("peer:identify", cb);
});
}

View File

@ -1,5 +1,4 @@
import type { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import { isPeerId, PeerId } from "@libp2p/interface-peer-id";
import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
import type {
@ -8,6 +7,7 @@ import type {
ILightPush,
IRelay,
IStore,
Libp2p,
Waku,
} from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";

View File

@ -51,8 +51,8 @@
"node": ">=16"
},
"dependencies": {
"@libp2p/interface-peer-discovery": "^1.0.5",
"@libp2p/interfaces": "^3.3.1",
"@libp2p/interface-peer-discovery": "^2.0.0",
"@libp2p/interfaces": "^3.3.2",
"@waku/enr": "0.0.14",
"@waku/utils": "0.0.8",
"debug": "^4.3.4",
@ -61,27 +61,19 @@
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/peer-id": "^2.0.3",
"@libp2p/peer-id-factory": "^2.0.3",
"@libp2p/interface-peer-info": "^1.0.10",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/peer-id": "^2.0.4",
"@libp2p/peer-id-factory": "^2.0.4",
"@multiformats/multiaddr": "^12.0.0",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@types/chai": "^4.3.4",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-mocha": "^2.0.1",

View File

@ -2,7 +2,7 @@ import type {
PeerDiscovery,
PeerDiscoveryEvents,
} from "@libp2p/interface-peer-discovery";
import { symbol } from "@libp2p/interface-peer-discovery";
import { peerDiscovery as symbol } from "@libp2p/interface-peer-discovery";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { PeerStore } from "@libp2p/interface-peer-store";
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
@ -97,30 +97,47 @@ export class PeerDiscoveryDns
);
}
for await (const peer of this.nextPeer()) {
if (!this._started) return;
for await (const peerEnr of this.nextPeer()) {
if (!this._started) {
return;
}
const peerInfo = peer.peerInfo;
if (!peerInfo) continue;
const peerInfo = peerEnr.peerInfo;
if (
(await this._components.peerStore.getTags(peerInfo.id)).find(
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
)
)
if (!peerInfo) {
continue;
}
await this._components.peerStore.tagPeer(
peerInfo.id,
DEFAULT_BOOTSTRAP_TAG_NAME,
{
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL,
const tagsToUpdate = {
tags: {
[DEFAULT_BOOTSTRAP_TAG_NAME]: {
value: this._options.tagValue ?? DEFAULT_BOOTSTRAP_TAG_VALUE,
ttl: this._options.tagTTL ?? DEFAULT_BOOTSTRAP_TAG_TTL,
},
},
};
let isPeerChanged = false;
const isPeerExists = await this._components.peerStore.has(peerInfo.id);
if (isPeerExists) {
const peer = await this._components.peerStore.get(peerInfo.id);
const hasBootstrapTag = peer.tags.has(DEFAULT_BOOTSTRAP_TAG_NAME);
if (!hasBootstrapTag) {
isPeerChanged = true;
await this._components.peerStore.merge(peerInfo.id, tagsToUpdate);
}
);
this.dispatchEvent(
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
);
} else {
isPeerChanged = true;
await this._components.peerStore.save(peerInfo.id, tagsToUpdate);
}
if (isPeerChanged) {
this.dispatchEvent(
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
);
}
}
}

View File

@ -53,7 +53,7 @@
"dependencies": {
"@ethersproject/rlp": "^5.7.0",
"@libp2p/crypto": "^1.0.17",
"@libp2p/peer-id": "^2.0.3",
"@libp2p/peer-id": "^2.0.4",
"@multiformats/multiaddr": "^12.0.0",
"@noble/secp256k1": "^1.7.1",
"@waku/utils": "0.0.8",
@ -61,26 +61,18 @@
"js-sha3": "^0.8.0"
},
"devDependencies": {
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/peer-id-factory": "^2.0.3",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-info": "^1.0.10",
"@libp2p/peer-id-factory": "^2.0.4",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-mocha": "^2.0.1",

View File

@ -129,11 +129,11 @@ describe("ENR", function () {
}
});
it("should throw error - no public key", () => {
it("should throw error - no public key", async () => {
try {
const txt =
"enr:-IS4QJ2d11eu6dC7E7LoXeLMgMP3kom1u3SE8esFSWvaHoo0dP1jg8O3-nx9ht-EO3CmG7L6OkHcMmoIh00IYWB92QABgmlkgnY0gmlwhH8AAAGJc2d11eu6dCsxoQIB_c-jQMOXsbjWkbN-kj99H57gfId5pfb4wa1qxwV4CIN1ZHCCIyk";
EnrDecoder.fromString(txt);
await EnrDecoder.fromString(txt);
assert.fail("Expect error here");
} catch (err: unknown) {
const e = err as Error;
@ -306,7 +306,7 @@ describe("ENR", function () {
});
});
describe("Location multiaddr", async () => {
describe("Location multiaddr", () => {
const ip4 = "127.0.0.1";
const ip6 = "::1";
const tcp = 8080;
@ -414,7 +414,7 @@ describe("ENR", function () {
});
});
describe("waku2 key round trip", async () => {
describe("waku2 key round trip", () => {
let peerId;
let enr: ENR;
let waku2Protocols: Waku2;

View File

@ -47,27 +47,20 @@
"node": ">=16"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^6.1.0",
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-libp2p": "^1.1.2",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@chainsafe/libp2p-gossipsub": "^9.1.0",
"@libp2p/interface-connection": "^5.1.1",
"@libp2p/interface-connection-manager": "^3.0.1",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-info": "^1.0.10",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interface-registrar": "^2.0.12",
"@multiformats/multiaddr": "^12.0.0",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"libp2p": "^0.45.9"
},
"typedoc": {
"entryPoint": "./src/index.ts"

View File

@ -2,14 +2,14 @@ import type { PeerId } from "@libp2p/interface-peer-id";
import type { IDecodedMessage, IDecoder } from "./message.js";
import type { ContentTopic } from "./misc.js";
import type { Callback, PointToPointProtocol } from "./protocols.js";
import type { Callback, IBaseProtocol } from "./protocols.js";
import type { IReceiver } from "./receiver.js";
export type ContentFilter = {
contentTopic: string;
};
export type IFilter = IReceiver & PointToPointProtocol;
export type IFilter = IReceiver & IBaseProtocol;
export interface IFilterV2Subscription {
subscribe<T extends IDecodedMessage>(
@ -25,7 +25,7 @@ export interface IFilterV2Subscription {
}
export type IFilterV2 = IReceiver &
PointToPointProtocol & {
IBaseProtocol & {
createSubscription(
pubSubTopic?: string,
peerId?: PeerId

View File

@ -11,3 +11,4 @@ export * from "./connection_manager.js";
export * from "./sender.js";
export * from "./receiver.js";
export * from "./misc.js";
export * from "./libp2p.js";

View File

@ -0,0 +1,21 @@
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
import type { Libp2p as BaseLibp2p } from "@libp2p/interface-libp2p";
import type { Libp2pInit } from "libp2p";
import type { identifyService } from "libp2p/identify";
import type { PingService } from "libp2p/ping";
export type Libp2pServices = {
ping: PingService;
pubsub?: GossipSub;
identify: ReturnType<ReturnType<typeof identifyService>>;
};
// TODO: Get libp2p to export this.
export type Libp2pComponents = Parameters<
Exclude<Libp2pInit["metrics"], undefined>
>[0];
// thought components are not defined on the Libp2p interface they are present on Libp2pNode class
export type Libp2p = BaseLibp2p<Libp2pServices> & {
components: Libp2pComponents;
};

View File

@ -1,4 +1,4 @@
import type { PointToPointProtocol } from "./protocols.js";
import type { IBaseProtocol } from "./protocols.js";
import type { ISender } from "./sender.js";
export type ILightPush = ISender & PointToPointProtocol;
export type ILightPush = ISender & IBaseProtocol;

View File

@ -3,9 +3,9 @@ import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerStore } from "@libp2p/interface-peer-store";
import { IEnr } from "./enr.js";
import { PointToPointProtocol } from "./protocols.js";
import { IBaseProtocol } from "./protocols.js";
export interface IPeerExchange extends PointToPointProtocol {
export interface IPeerExchange extends IBaseProtocol {
query(params: PeerExchangeQueryParams): Promise<PeerInfo[] | undefined>;
}

View File

@ -1,3 +1,4 @@
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import type { Libp2pOptions } from "libp2p";
@ -11,10 +12,12 @@ export enum Protocols {
Filter = "filter",
}
export interface PointToPointProtocol {
export interface IBaseProtocol {
multicodec: string;
peerStore: PeerStore;
peers: () => Promise<Peer[]>;
addLibp2pEventListener: Libp2p["addEventListener"];
removeLibp2pEventListener: Libp2p["removeEventListener"];
}
export type ProtocolCreateOptions = {

View File

@ -1,5 +1,5 @@
import type { IDecodedMessage, IDecoder } from "./message.js";
import type { PointToPointProtocol, ProtocolOptions } from "./protocols.js";
import type { IBaseProtocol, ProtocolOptions } from "./protocols.js";
export enum PageDirection {
BACKWARD = "backward",
@ -45,7 +45,7 @@ export type StoreQueryOptions = {
cursor?: Cursor;
} & ProtocolOptions;
export interface IStore extends PointToPointProtocol {
export interface IStore extends IBaseProtocol {
queryOrderedCallback: <T extends IDecodedMessage>(
decoders: IDecoder<T>[],
callback: (message: T) => Promise<void | boolean> | boolean | void,

View File

@ -1,9 +1,9 @@
import type { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { Multiaddr } from "@multiformats/multiaddr";
import type { IFilter, IFilterV2 } from "./filter.js";
import type { Libp2p } from "./libp2p.js";
import type { ILightPush } from "./light_push.js";
import { Protocols } from "./protocols.js";
import type { IRelay } from "./relay.js";

View File

@ -80,28 +80,20 @@
"js-sha3": "^0.8.0"
},
"devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-libp2p": "^1.1.2",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@libp2p/interface-connection": "^5.1.1",
"@libp2p/interface-connection-manager": "^3.0.1",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interface-registrar": "^2.0.12",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"fast-check": "^3.8.1",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",

View File

@ -127,7 +127,7 @@ export async function encrypt(
): Promise<Uint8Array> {
const ephemPrivateKey = randomBytes(32);
const sharedPx = await derive(ephemPrivateKey, publicKeyTo);
const sharedPx = derive(ephemPrivateKey, publicKeyTo);
const hash = await kdf(sharedPx, 32);

View File

@ -148,7 +148,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
return;
}
const res = await postCipher(payload);
const res = postCipher(payload);
if (!res) {
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);

View File

@ -138,7 +138,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
return;
}
const res = await postCipher(payload);
const res = postCipher(payload);
if (!res) {
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);

View File

@ -62,18 +62,10 @@
"@types/chai": "^4.3.4",
"@types/debug": "^4.1.7",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"chai": "^4.3.7",
"cspell": "^6.28.0",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"fast-check": "^3.7.0",
"ignore-loader": "^0.1.2",
"isomorphic-fetch": "^3.0.0",

View File

@ -48,38 +48,31 @@
"node": ">=16"
},
"dependencies": {
"@libp2p/interface-peer-discovery": "^1.0.5",
"@libp2p/interfaces": "^3.3.1",
"@libp2p/interface-peer-discovery": "^2.0.0",
"@libp2p/interfaces": "^3.3.2",
"@waku/core": "0.0.20",
"@waku/enr": "0.0.14",
"@waku/proto": "0.0.5",
"@waku/utils": "0.0.8",
"@waku/interfaces": "0.0.15",
"debug": "^4.3.4",
"it-all": "^3.0.2",
"it-length-prefixed": "^9.0.1",
"it-pipe": "^3.0.1"
},
"devDependencies": {
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
"@libp2p/interface-connection-manager": "^3.0.1",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-info": "^1.0.10",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interface-registrar": "^2.0.12",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rollup": "^3.21.3",

View File

@ -1,9 +1,8 @@
import type { ConnectionManager } from "@libp2p/interface-connection-manager";
import type { PeerStore } from "@libp2p/interface-peer-store";
import { BaseProtocol } from "@waku/core/lib/base_protocol";
import { EnrDecoder } from "@waku/enr";
import type {
IPeerExchange,
Libp2pComponents,
PeerExchangeQueryParams,
PeerInfo,
} from "@waku/interfaces";
@ -20,11 +19,6 @@ export const PeerExchangeCodec = "/vac/waku/peer-exchange/2.0.0-alpha1";
const log = debug("waku:peer-exchange");
export interface PeerExchangeComponents {
peerStore: PeerStore;
connectionManager: ConnectionManager;
}
/**
* Implementation of the Peer Exchange protocol (https://rfc.vac.dev/spec/34/)
*/
@ -34,14 +28,8 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
/**
* @param components - libp2p components
*/
constructor(public components: PeerExchangeComponents) {
super(
PeerExchangeCodec,
components.peerStore,
components.connectionManager.getConnections.bind(
components.connectionManager
)
);
constructor(components: Libp2pComponents) {
super(PeerExchangeCodec, components);
this.multicodec = PeerExchangeCodec;
}
@ -102,8 +90,7 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
* @returns A function that creates a new peer exchange protocol
*/
export function wakuPeerExchange(): (
components: PeerExchangeComponents
components: Libp2pComponents
) => WakuPeerExchange {
return (components: PeerExchangeComponents) =>
new WakuPeerExchange(components);
return (components: Libp2pComponents) => new WakuPeerExchange(components);
}

View File

@ -1,19 +1,16 @@
import type { PeerUpdate } from "@libp2p/interface-libp2p";
import type {
PeerDiscovery,
PeerDiscoveryEvents,
} from "@libp2p/interface-peer-discovery";
import { symbol } from "@libp2p/interface-peer-discovery";
import { peerDiscovery as symbol } from "@libp2p/interface-peer-discovery";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
import type { Libp2pComponents } from "@waku/interfaces";
import debug from "debug";
import {
PeerExchangeCodec,
PeerExchangeComponents,
WakuPeerExchange,
} from "./waku_peer_exchange.js";
import { PeerExchangeCodec, WakuPeerExchange } from "./waku_peer_exchange.js";
const log = debug("waku:peer-exchange-discovery");
@ -56,17 +53,19 @@ export class PeerExchangeDiscovery
extends EventEmitter<PeerDiscoveryEvents>
implements PeerDiscovery
{
private readonly components: PeerExchangeComponents;
private readonly components: Libp2pComponents;
private readonly peerExchange: WakuPeerExchange;
private readonly options: Options;
private isStarted: boolean;
private queryingPeers: Set<string> = new Set();
private queryAttempts: Map<string, number> = new Map();
private readonly eventHandler = async (
event: CustomEvent<PeerProtocolsChangeData>
): Promise<void> => {
const { protocols, peerId } = event.detail;
private readonly handleDiscoveredPeer = (
event: CustomEvent<PeerUpdate>
): void => {
const {
peer: { protocols, id: peerId },
} = event.detail;
if (
!protocols.includes(PeerExchangeCodec) ||
this.queryingPeers.has(peerId.toString())
@ -79,7 +78,7 @@ export class PeerExchangeDiscovery
);
};
constructor(components: PeerExchangeComponents, options: Options = {}) {
constructor(components: Libp2pComponents, options: Options = {}) {
super();
this.components = components;
this.peerExchange = new WakuPeerExchange(components);
@ -97,9 +96,10 @@ export class PeerExchangeDiscovery
log("Starting peer exchange node discovery, discovering peers");
this.components.peerStore.addEventListener(
"change:protocols",
this.eventHandler
// might be better to use "peer:identify" or "peer:update"
this.components.events.addEventListener(
"peer:update",
this.handleDiscoveredPeer
);
}
@ -111,9 +111,9 @@ export class PeerExchangeDiscovery
log("Stopping peer exchange node discovery");
this.isStarted = false;
this.queryingPeers.clear();
this.components.peerStore.removeEventListener(
"change:protocols",
this.eventHandler
this.components.events.removeEventListener(
"peer:update",
this.handleDiscoveredPeer
);
}
@ -143,9 +143,11 @@ export class PeerExchangeDiscovery
return;
}
setTimeout(async () => {
setTimeout(() => {
this.queryAttempts.set(peerIdStr, currentAttempt + 1);
await this.startRecurringQueries(peerId);
this.startRecurringQueries(peerId).catch((error) => {
log(`Error in startRecurringQueries: ${error}`);
});
}, queryInterval * currentAttempt);
};
@ -168,33 +170,31 @@ export class PeerExchangeDiscovery
}
const { peerId, peerInfo } = ENR;
if (!peerId || !peerInfo) continue;
const { multiaddrs } = peerInfo;
if (
(await this.components.peerStore.getTags(peerId)).find(
({ name }) => name === DEFAULT_PEER_EXCHANGE_TAG_NAME
)
)
if (!peerId || !peerInfo) {
continue;
}
await this.components.peerStore.tagPeer(
peerId,
DEFAULT_PEER_EXCHANGE_TAG_NAME,
{
value: this.options.tagValue ?? DEFAULT_PEER_EXCHANGE_TAG_VALUE,
ttl: this.options.tagTTL ?? DEFAULT_PEER_EXCHANGE_TAG_TTL,
}
);
const hasPeer = await this.components.peerStore.has(peerId);
if (hasPeer) {
continue;
}
// update the tags for the peer
await this.components.peerStore.save(peerId, {
tags: {
[DEFAULT_PEER_EXCHANGE_TAG_NAME]: {
value: this.options.tagValue ?? DEFAULT_PEER_EXCHANGE_TAG_VALUE,
ttl: this.options.tagTTL ?? DEFAULT_PEER_EXCHANGE_TAG_TTL,
},
},
});
this.dispatchEvent(
new CustomEvent<PeerInfo>("peer", {
detail: {
id: peerId,
multiaddrs,
protocols: [],
multiaddrs: peerInfo.multiaddrs,
},
})
);
@ -209,8 +209,8 @@ export class PeerExchangeDiscovery
}
export function wakuPeerExchangeDiscovery(): (
components: PeerExchangeComponents
components: Libp2pComponents
) => PeerExchangeDiscovery {
return (components: PeerExchangeComponents) =>
return (components: Libp2pComponents) =>
new PeerExchangeDiscovery(components);
}

View File

@ -50,8 +50,6 @@
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"cspell": "^6.31.1",
"npm-run-all": "^4.1.5",

View File

@ -49,7 +49,7 @@
"node": ">=16"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^6.1.0",
"@chainsafe/libp2p-gossipsub": "^9.1.0",
"@noble/hashes": "^1.3.0",
"@waku/core": "0.0.20",
"@waku/interfaces": "0.0.15",
@ -60,16 +60,11 @@
"fast-check": "^3.8.1"
},
"devDependencies": {
"@libp2p/interface-pubsub": "^4.0.1",
"@rollup/plugin-commonjs": "^24.1.0",
"@waku/build-utils": "*",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.8",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"rollup": "^3.15.0",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",

View File

@ -6,7 +6,6 @@ import {
} from "@chainsafe/libp2p-gossipsub";
import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types";
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PubSub } from "@libp2p/interface-pubsub";
import { sha256 } from "@noble/hashes/sha256";
import { DefaultPubSubTopic } from "@waku/core";
@ -19,6 +18,7 @@ import {
IEncoder,
IMessage,
IRelay,
Libp2p,
ProtocolCreateOptions,
ProtocolOptions,
SendError,
@ -59,13 +59,13 @@ class Relay implements IRelay {
private observers: Map<ContentTopic, Set<unknown>>;
constructor(libp2p: Libp2p, options?: Partial<RelayCreateOptions>) {
if (!this.isRelayPubSub(libp2p.pubsub)) {
if (!this.isRelayPubSub(libp2p.services.pubsub)) {
throw Error(
`Failed to initialize Relay. libp2p.pubsub does not support ${Relay.multicodec}`
);
}
this.gossipSub = libp2p.pubsub as GossipSub;
this.gossipSub = libp2p.services.pubsub as GossipSub;
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
if (this.gossipSub.isStarted()) {
@ -193,18 +193,26 @@ class Relay implements IRelay {
return;
}
await Promise.all(
Array.from(observers).map(async ({ decoder, callback }) => {
const protoMsg = await decoder.fromWireToProtoObj(bytes);
if (!protoMsg) {
log("Internal error: message previously decoded failed on 2nd pass.");
return;
}
const msg = await decoder.fromProtoObj(pubSubTopic, protoMsg);
if (msg) {
callback(msg);
} else {
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
}
Array.from(observers).map(({ decoder, callback }) => {
return (async () => {
try {
const protoMsg = await decoder.fromWireToProtoObj(bytes);
if (!protoMsg) {
log(
"Internal error: message previously decoded failed on 2nd pass."
);
return;
}
const msg = await decoder.fromProtoObj(pubSubTopic, protoMsg);
if (msg) {
await callback(msg);
} else {
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
}
} catch (error) {
log("Error while decoding message:", error);
}
})();
})
);
}
@ -217,7 +225,7 @@ class Relay implements IRelay {
private gossipSubSubscribe(pubSubTopic: string): void {
this.gossipSub.addEventListener(
"gossipsub:message",
async (event: CustomEvent<GossipsubMessage>) => {
(event: CustomEvent<GossipsubMessage>) => {
if (event.detail.msg.topic !== pubSubTopic) return;
log(`Message received on ${pubSubTopic}`);
@ -232,7 +240,7 @@ class Relay implements IRelay {
this.gossipSub.subscribe(pubSubTopic);
}
private isRelayPubSub(pubsub: PubSub): boolean {
private isRelayPubSub(pubsub: PubSub | undefined): boolean {
return pubsub?.multicodecs?.includes(Relay.multicodec) || false;
}
}

View File

@ -48,44 +48,37 @@
"node": ">=16"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@libp2p/mplex": "^7.1.1",
"@libp2p/websockets": "^5.0.3",
"@chainsafe/libp2p-noise": "^12.0.1",
"@libp2p/mplex": "^8.0.4",
"@libp2p/websockets": "^6.0.3",
"@waku/utils": "0.0.8",
"@waku/relay": "0.0.3",
"@waku/core": "0.0.20",
"@waku/interfaces": "0.0.15",
"@waku/dns-discovery": "0.0.14",
"libp2p": "^0.42.2"
"libp2p": "^0.45.9"
},
"devDependencies": {
"@libp2p/interface-address-manager": "^2.0.4",
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-address-manager": "^3.0.1",
"@libp2p/interface-connection": "^5.1.1",
"@libp2p/interface-connection-manager": "^3.0.1",
"@libp2p/interface-content-routing": "^2.1.1",
"@libp2p/interface-dht": "^2.0.1",
"@libp2p/interface-libp2p": "^1.1.2",
"@libp2p/interface-metrics": "^4.0.7",
"@libp2p/interface-peer-discovery": "^1.0.5",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-routing": "^1.0.8",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-pubsub": "^3.0.6",
"@libp2p/interface-registrar": "^2.0.8",
"@libp2p/interface-transport": "^2.1.1",
"@libp2p/interface-dht": "^2.0.3",
"@libp2p/interface-libp2p": "^3.2.0",
"@libp2p/interface-metrics": "^4.0.8",
"@libp2p/interface-peer-discovery": "^2.0.0",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-routing": "^1.1.1",
"@libp2p/interface-peer-store": "^2.0.4",
"@libp2p/interface-pubsub": "^4.0.1",
"@libp2p/interface-registrar": "^2.0.12",
"@libp2p/interface-transport": "^4.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@chainsafe/libp2p-gossipsub": "^9.1.0",
"cspell": "^6.31.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"interface-datastore": "^7.0.4",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",

View File

@ -1,6 +1,5 @@
import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
import { noise } from "@chainsafe/libp2p-noise";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerDiscovery } from "@libp2p/interface-peer-discovery";
import { mplex } from "@libp2p/mplex";
import { webSockets } from "@libp2p/websockets";
@ -19,14 +18,16 @@ import type {
FullNode,
IFilter,
IFilterV2,
Libp2p,
Libp2pComponents,
LightNode,
ProtocolCreateOptions,
RelayNode,
} from "@waku/interfaces";
import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay";
import { createLibp2p, Libp2pOptions } from "libp2p";
import type { Libp2pComponents } from "./libp2p_components.js";
import { identifyService } from "libp2p/identify";
import { pingService } from "libp2p/ping";
const DEFAULT_NODE_REQUIREMENTS = {
lightPush: 1,
@ -174,25 +175,31 @@ export function defaultPeerDiscovery(): (
return wakuDnsDiscovery([enrTree["PROD"]], DEFAULT_NODE_REQUIREMENTS);
}
type PubsubService = {
pubsub?: (components: Libp2pComponents) => GossipSub;
};
export async function defaultLibp2p(
wakuGossipSub?: (components: Libp2pComponents) => GossipSub,
wakuGossipSub?: PubsubService["pubsub"],
options?: Partial<Libp2pOptions>,
userAgent?: string
): Promise<Libp2p> {
const libp2pOpts = Object.assign(
{
transports: [webSockets({ filter: filterAll })],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
identify: {
host: {
agentVersion: userAgent ?? DefaultUserAgent,
},
},
} as Libp2pOptions,
wakuGossipSub ? { pubsub: wakuGossipSub } : {},
options ?? {}
);
const pubsubService: PubsubService = wakuGossipSub
? { pubsub: wakuGossipSub }
: {};
return createLibp2p(libp2pOpts);
return createLibp2p({
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;
}

View File

@ -1,39 +0,0 @@
import type { AddressManager } from "@libp2p/interface-address-manager";
import type {
ConnectionGater,
ConnectionProtector,
} from "@libp2p/interface-connection";
import type {
ConnectionManager,
Dialer,
} from "@libp2p/interface-connection-manager";
import type { ContentRouting } from "@libp2p/interface-content-routing";
import type { DualDHT } from "@libp2p/interface-dht";
import type { Metrics } from "@libp2p/interface-metrics";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerRouting } from "@libp2p/interface-peer-routing";
import type { PeerStore } from "@libp2p/interface-peer-store";
import type { PubSub } from "@libp2p/interface-pubsub";
import type { Registrar } from "@libp2p/interface-registrar";
import type { TransportManager, Upgrader } from "@libp2p/interface-transport";
import type { Datastore } from "interface-datastore";
// TODO: Get libp2p to export this.
export interface Libp2pComponents {
peerId: PeerId;
addressManager: AddressManager;
peerStore: PeerStore;
upgrader: Upgrader;
registrar: Registrar;
connectionManager: ConnectionManager;
transportManager: TransportManager;
connectionGater: ConnectionGater;
contentRouting: ContentRouting;
peerRouting: PeerRouting;
datastore: Datastore;
connectionProtector?: ConnectionProtector;
dialer: Dialer;
metrics?: Metrics;
dht?: DualDHT;
pubsub?: PubSub;
}

View File

@ -49,7 +49,7 @@
"node": ">=16"
},
"dependencies": {
"@libp2p/peer-id": "^2.0.3",
"@libp2p/peer-id": "^2.0.4",
"@waku/core": "*",
"@waku/enr": "*",
"@waku/interfaces": "*",
@ -59,35 +59,33 @@
"dockerode": "^3.3.5",
"p-timeout": "^6.1.0",
"portfinder": "^1.0.32",
"sinon": "^15.2.0",
"tail": "^2.2.6"
},
"devDependencies": {
"@libp2p/bootstrap": "^6.0.3",
"@libp2p/components": "^3.1.1",
"@libp2p/bootstrap": "^8.0.0",
"@libp2p/interface-peer-discovery-compliance-tests": "^2.0.8",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-id": "^2.0.2",
"@types/chai": "^4.3.4",
"@types/dockerode": "^3.3.17",
"@types/mocha": "^10.0.1",
"@types/tail": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/sdk": "*",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.62.0",
"@waku/dns-discovery": "*",
"@waku/message-encryption": "*",
"@waku/peer-exchange": "*",
"@waku/sdk": "*",
"chai": "^4.3.7",
"cspell": "^6.31.1",
"debug": "^4.3.4",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"mocha": "^10.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"interface-datastore": "^8.2.3",
"libp2p": "^0.45.9",
"datastore-core": "^9.2.0"
}
}

View File

@ -170,7 +170,7 @@ export default class Dockerode {
await this.container.stop();
await this.container.remove();
this.containerId = undefined;
delete this.containerId;
}
private async confirmImageExistsOrPull(): Promise<void> {

View File

@ -165,7 +165,8 @@ export class NimGoNode {
}
public async stop(): Promise<void> {
this.docker?.stop();
await this.docker?.container?.stop();
delete this.docker;
}
async waitForLog(msg: string, timeout: number): Promise<void> {

View File

@ -0,0 +1,196 @@
import { CustomEvent } from "@libp2p/interfaces/events";
import { ConnectionManager, KeepAliveOptions } from "@waku/core";
import { LightNode, Tags } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
import sinon, { SinonSpy, SinonStub } from "sinon";
import { delay } from "../dist/delay.js";
const KEEP_ALIVE_OPTIONS: KeepAliveOptions = {
pingKeepAlive: 0,
relayKeepAlive: 5 * 1000,
};
const TEST_TIMEOUT = 10_000;
const DELAY_MS = 1_000;
describe("ConnectionManager", function () {
let connectionManager: ConnectionManager | undefined;
let waku: LightNode;
let peerId: string;
let getConnectionsStub: SinonStub;
let getTagNamesForPeerStub: SinonStub;
let dialPeerStub: SinonStub;
beforeEach(async function () {
waku = await createLightNode();
peerId = Math.random().toString(36).substring(7);
connectionManager = ConnectionManager.create(
peerId,
waku.libp2p,
KEEP_ALIVE_OPTIONS
);
});
afterEach(async () => {
await waku.stop();
sinon.restore();
});
describe("attemptDial method", function () {
let attemptDialSpy: SinonSpy;
beforeEach(function () {
attemptDialSpy = sinon.spy(connectionManager as any, "attemptDial");
});
afterEach(function () {
attemptDialSpy.restore();
});
it("should be called on all `peer:discovery` events", async function () {
this.timeout(TEST_TIMEOUT);
const totalPeerIds = 1;
for (let i = 1; i <= totalPeerIds; i++) {
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", { detail: `peer-id-${i}` })
);
}
expect(attemptDialSpy.callCount).to.equal(
totalPeerIds,
"attemptDial should be called once for each peer:discovery event"
);
});
});
describe("dialPeer method", function () {
beforeEach(function () {
getConnectionsStub = sinon.stub(
(connectionManager as any).libp2p,
"getConnections"
);
getTagNamesForPeerStub = sinon.stub(
connectionManager as any,
"getTagNamesForPeer"
);
dialPeerStub = sinon.stub(connectionManager as any, "dialPeer");
});
afterEach(function () {
dialPeerStub.restore();
getTagNamesForPeerStub.restore();
getConnectionsStub.restore();
});
describe("For bootstrap peers", function () {
it("should be called for bootstrap peers", async function () {
this.timeout(TEST_TIMEOUT);
// simulate that the peer is not connected
getConnectionsStub.returns([]);
// simulate that the peer is a bootstrap peer
getTagNamesForPeerStub.resolves([Tags.BOOTSTRAP]);
// emit a peer:discovery event
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", { detail: "bootstrap-peer" })
);
// wait for the async function calls within attemptDial to finish
await delay(DELAY_MS);
// check that dialPeer was called once
expect(dialPeerStub.callCount).to.equal(
1,
"dialPeer should be called for bootstrap peers"
);
});
it("should not be called more than DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED times for bootstrap peers", async function () {
this.timeout(TEST_TIMEOUT);
// simulate that the peer is not connected
getConnectionsStub.returns([]);
// simulate that the peer is a bootstrap peer
getTagNamesForPeerStub.resolves([Tags.BOOTSTRAP]);
// emit first peer:discovery event
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", { detail: "bootstrap-peer" })
);
// simulate that the peer is connected
getConnectionsStub.returns([{ tags: [{ name: Tags.BOOTSTRAP }] }]);
// emit multiple peer:discovery events
const totalBootstrapPeers = 5;
for (let i = 1; i <= totalBootstrapPeers; i++) {
await delay(500);
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", {
detail: `bootstrap-peer-id-${i}`,
})
);
}
// check that dialPeer was called only once
expect(dialPeerStub.callCount).to.equal(
1,
"dialPeer should not be called more than once for bootstrap peers"
);
});
});
describe("For peer-exchange peers", function () {
it("should be called for peers with PEER_EXCHANGE tags", async function () {
this.timeout(TEST_TIMEOUT);
// simulate that the peer is not connected
getConnectionsStub.returns([]);
// simulate that the peer has a PEER_EXCHANGE tag
getTagNamesForPeerStub.resolves([Tags.PEER_EXCHANGE]);
// emit a peer:discovery event
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", { detail: "px-peer" })
);
// wait for the async function calls within attemptDial to finish
await delay(DELAY_MS);
// check that dialPeer was called once
expect(dialPeerStub.callCount).to.equal(
1,
"dialPeer should be called for peers with PEER_EXCHANGE tags"
);
});
it("should be called for every peer with PEER_EXCHANGE tags", async function () {
this.timeout(TEST_TIMEOUT);
// simulate that the peer is not connected
getConnectionsStub.returns([]);
// simulate that the peer has a PEER_EXCHANGE tag
getTagNamesForPeerStub.resolves([Tags.PEER_EXCHANGE]);
// emit multiple peer:discovery events
const totalPxPeers = 5;
for (let i = 0; i < totalPxPeers; i++) {
waku.libp2p.dispatchEvent(
new CustomEvent("peer:discovery", { detail: `px-peer-id-${i}` })
);
await delay(500);
}
// check that dialPeer was called for each peer with PEER_EXCHANGE tags
expect(dialPeerStub.callCount).to.equal(totalPxPeers);
});
});
});
});

View File

@ -1,6 +1,5 @@
import { Components } from "@libp2p/components";
import tests from "@libp2p/interface-peer-discovery-compliance-tests";
import { Peer } from "@libp2p/interface-peer-store";
import { EventEmitter } from "@libp2p/interfaces/events";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { PersistentPeerStore } from "@libp2p/peer-store";
import {
@ -9,25 +8,28 @@ import {
PeerDiscoveryDns,
wakuDnsDiscovery,
} from "@waku/dns-discovery";
import { Libp2pComponents } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
import { MemoryDatastore } from "datastore-core";
import { MemoryDatastore } from "datastore-core/memory";
import { Datastore } from "interface-datastore";
import { delay } from "../src/delay.js";
const maxQuantity = 3;
describe("DNS Discovery: Compliance Test", async function () {
describe("DNS Discovery: Compliance Test", function () {
this.timeout(10000);
tests({
async setup() {
// create libp2p mock peerStore
const components = new Components({
const components = {
peerStore: new PersistentPeerStore({
events: new EventEmitter(),
peerId: await createSecp256k1PeerId(),
datastore: new MemoryDatastore(),
datastore: new MemoryDatastore() as any as Datastore,
}),
});
} as unknown as Libp2pComponents;
return new PeerDiscoveryDns(components, {
enrUrls: [enrTree["PROD"]],
@ -69,22 +71,16 @@ describe("DNS Node Discovery [live data]", function () {
await waku.start();
const allPeers = await waku.libp2p.peerStore.all();
const dnsPeers: Peer[] = [];
let dnsPeers = 0;
for (const peer of allPeers) {
const tags = await waku.libp2p.peerStore.getTags(peer.id);
let hasTag = false;
for (const tag of tags) {
hasTag = tag.name === "bootstrap";
if (hasTag) {
dnsPeers.push(peer);
break;
}
const hasTag = peer.tags.has("bootstrap");
if (hasTag) {
dnsPeers += 1;
}
expect(hasTag).to.be.eq(true);
}
expect(dnsPeers.length).to.eq(maxQuantity);
expect(dnsPeers).to.eq(maxQuantity);
});
it(`should retrieve ${maxQuantity} multiaddrs for test.waku.nodes.status.im`, async function () {

View File

@ -57,8 +57,8 @@ describe("Waku Light Push [node only]", () => {
afterEach(async function () {
try {
nwaku?.stop();
waku?.stop();
await nwaku?.stop();
await waku?.stop();
} catch (e) {
console.error("Failed to stop nodes: ", e);
}
@ -124,8 +124,8 @@ describe("Waku Light Push [node only] - custom pubsub topic", () => {
afterEach(async function () {
try {
nwaku?.stop();
waku?.stop();
await nwaku?.stop();
await waku?.stop();
} catch (e) {
console.error("Failed to stop nodes: ", e);
}

View File

@ -19,49 +19,49 @@ import { makeLogFileName } from "../src/log_file.js";
import { NimGoNode } from "../src/node/node.js";
describe("Peer Exchange", () => {
let waku: LightNode;
describe("Auto Discovery", function () {
let waku: LightNode;
afterEach(async function () {
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Auto discovery", async function () {
// skipping in CI as this test demonstrates Peer Exchange working with the test fleet
// but not with locally run nwaku nodes
if (process.env.CI) {
this.skip();
}
this.timeout(50_000);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: getPredefinedBootstrapNodes(Fleet.Test, 3) }),
wakuPeerExchangeDiscovery(),
],
},
afterEach(async function () {
await waku?.stop();
});
await waku.start();
it("connection with fleet nodes", async function () {
// skipping in CI as this test demonstrates Peer Exchange working with the test fleet
// but not with locally run nwaku nodes
if (process.env.CI) {
this.skip();
}
const foundPxPeer = await new Promise<boolean>((resolve) => {
const testNodes = getPredefinedBootstrapNodes(Fleet.Test, 3);
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const { multiaddrs } = evt.detail;
multiaddrs.forEach((ma) => {
const isBootstrapNode = testNodes.find((n) => n === ma.toString());
this.timeout(50_000);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: getPredefinedBootstrapNodes(Fleet.Test, 3) }),
wakuPeerExchangeDiscovery(),
],
},
});
await waku.start();
const foundPxPeer = await new Promise<boolean>((resolve) => {
const testNodes = getPredefinedBootstrapNodes(Fleet.Test, 3);
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const peerId = evt.detail.id.toString();
const isBootstrapNode = testNodes.find((n) => n.includes(peerId));
if (!isBootstrapNode) {
resolve(true);
}
});
});
});
expect(foundPxPeer).to.be.true;
expect(foundPxPeer).to.be.true;
});
});
describe("Locally run nodes", () => {
describe("Locally Run Nodes", () => {
let waku: LightNode;
let nwaku1: NimGoNode;
let nwaku2: NimGoNode;
@ -72,9 +72,10 @@ describe("Peer Exchange", () => {
});
afterEach(async function () {
!!nwaku1 && nwaku1.stop();
!!nwaku2 && nwaku2.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
this.timeout(10_000);
await nwaku1?.stop();
await nwaku2?.stop();
await waku?.stop();
});
it("nwaku interop", async function () {
@ -102,23 +103,7 @@ describe("Peer Exchange", () => {
await waku.start();
await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
await new Promise<void>((resolve) => {
waku.libp2p.peerStore.addEventListener("change:protocols", (evt) => {
if (evt.detail.protocols.includes(PeerExchangeCodec)) {
resolve();
}
});
});
// the forced type casting is done in ref to https://github.com/libp2p/js-libp2p-interfaces/issues/338#issuecomment-1431643645
const { connectionManager, registrar, peerStore } =
waku.libp2p as unknown as Libp2pComponents;
const components = {
connectionManager: connectionManager,
registrar: registrar,
peerStore: peerStore,
};
const components = waku.libp2p.components as unknown as Libp2pComponents;
const peerExchange = new WakuPeerExchange(components);
const numPeersToRequest = 1;
@ -142,11 +127,12 @@ describe("Peer Exchange", () => {
expect(doesPeerIdExistInResponse).to.be.equal(true);
expect(waku.libp2p.peerStore.has(await nwaku2.getPeerId())).to.be.true;
expect(await waku.libp2p.peerStore.has(await nwaku2.getPeerId())).to.be
.true;
});
});
describe("compliance test", async function () {
describe("Compliance Test", function () {
this.timeout(55_000);
let waku: LightNode;
@ -175,35 +161,26 @@ describe("Peer Exchange", () => {
discv5BootstrapNode: enr,
});
waku = await createLightNode();
waku = await createLightNode({
libp2p: {
peerDiscovery: [wakuPeerExchangeDiscovery()],
},
});
const peerExchange = waku.libp2p.components["components"][
"peer-discovery-0"
] as PeerExchangeDiscovery;
await waku.start();
const nwaku2Ma = await nwaku2.getMultiaddrWithId();
await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
await new Promise<void>((resolve) => {
waku.libp2p.peerStore.addEventListener("change:protocols", (evt) => {
if (evt.detail.protocols.includes(PeerExchangeCodec)) {
resolve();
}
});
});
// the forced type casting is done in ref to https://github.com/libp2p/js-libp2p-interfaces/issues/338#issuecomment-1431643645
const { connectionManager, registrar, peerStore } =
waku.libp2p as unknown as Libp2pComponents;
const components = {
connectionManager: connectionManager,
registrar: registrar,
peerStore: peerStore,
};
return new PeerExchangeDiscovery(components);
return peerExchange;
},
teardown: async () => {
!!nwaku1 && (await nwaku1.stop());
!!nwaku2 && (await nwaku2.stop());
!!waku && (await waku.stop());
await nwaku1?.stop();
await nwaku2?.stop();
await waku?.stop();
},
});
});

View File

@ -67,10 +67,9 @@ describe("Waku Relay [node only]", () => {
}).then((waku) => waku.start().then(() => waku)),
]);
log("Instances started, adding waku2 to waku1's address book");
await waku1.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku1.libp2p.peerStore.merge(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await waku1.dial(waku2.libp2p.peerId);
log("Wait for mutual pubsub subscription");
@ -90,11 +89,11 @@ describe("Waku Relay [node only]", () => {
it("Subscribe", async function () {
log("Getting subscribers");
const subscribers1 = waku1.libp2p.pubsub
.getSubscribers(DefaultPubSubTopic)
const subscribers1 = waku1.libp2p.services
.pubsub!.getSubscribers(DefaultPubSubTopic)
.map((p) => p.toString());
const subscribers2 = waku2.libp2p.pubsub
.getSubscribers(DefaultPubSubTopic)
const subscribers2 = waku2.libp2p.services
.pubsub!.getSubscribers(DefaultPubSubTopic)
.map((p) => p.toString());
log("Asserting mutual subscription");
@ -121,7 +120,7 @@ describe("Waku Relay [node only]", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], resolve);
void waku2.relay.subscribe([TestDecoder], resolve);
}
);
@ -152,12 +151,12 @@ describe("Waku Relay [node only]", () => {
const barDecoder = createDecoder(barContentTopic);
const fooMessages: DecodedMessage[] = [];
waku2.relay.subscribe([fooDecoder], (msg) => {
void waku2.relay.subscribe([fooDecoder], (msg) => {
fooMessages.push(msg);
});
const barMessages: DecodedMessage[] = [];
waku2.relay.subscribe([barDecoder], (msg) => {
void waku2.relay.subscribe([barDecoder], (msg) => {
barMessages.push(msg);
});
@ -207,10 +206,10 @@ describe("Waku Relay [node only]", () => {
const symDecoder = createSymDecoder(symTopic, symKey);
const msgs: DecodedMessage[] = [];
waku2.relay.subscribe([eciesDecoder], (wakuMsg) => {
void waku2.relay.subscribe([eciesDecoder], (wakuMsg) => {
msgs.push(wakuMsg);
});
waku2.relay.subscribe([symDecoder], (wakuMsg) => {
void waku2.relay.subscribe([symDecoder], (wakuMsg) => {
msgs.push(wakuMsg);
});
@ -291,14 +290,12 @@ describe("Waku Relay [node only]", () => {
}).then((waku) => waku.start().then(() => waku)),
]);
await waku1.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku3.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku1.libp2p.peerStore.merge(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await waku3.libp2p.peerStore.merge(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await Promise.all([
waku1.dial(waku2.libp2p.peerId),
waku3.dial(waku2.libp2p.peerId),
@ -313,7 +310,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], resolve);
void waku2.relay.subscribe([TestDecoder], resolve);
}
);
@ -321,7 +318,7 @@ describe("Waku Relay [node only]", () => {
// pubsub topic.
const waku3NoMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve, reject) => {
waku3.relay.subscribe([TestDecoder], reject);
void waku3.relay.subscribe([TestDecoder], reject);
setTimeout(resolve, 1000);
}
);
@ -356,10 +353,9 @@ describe("Waku Relay [node only]", () => {
}).then((waku) => waku.start().then(() => waku)),
]);
await waku1.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku1.libp2p.peerStore.merge(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await Promise.all([waku1.dial(waku2.libp2p.peerId)]);
await Promise.all([
@ -369,7 +365,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([TestDecoder], () =>
void waku2.relay.subscribe([TestDecoder], () =>
resolve({
payload: new Uint8Array([]),
} as DecodedMessage)
@ -428,7 +424,8 @@ describe("Waku Relay [node only]", () => {
while (subscribers.length === 0) {
await delay(200);
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
subscribers =
waku.libp2p.services.pubsub!.getSubscribers(DefaultPubSubTopic);
}
const nimPeerId = await nwaku.getPeerId();
@ -463,7 +460,7 @@ describe("Waku Relay [node only]", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku.relay.subscribe<DecodedMessage>(TestDecoder, (msg) =>
void waku.relay.subscribe<DecodedMessage>(TestDecoder, (msg) =>
resolve(msg)
);
}
@ -535,7 +532,7 @@ describe("Waku Relay [node only]", () => {
const waku2ReceivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe(TestDecoder, resolve);
void waku2.relay.subscribe(TestDecoder, resolve);
}
);

View File

@ -90,7 +90,8 @@ describe("Wait for remote peer", function () {
done();
}
);
});
})
.catch((e) => done(e));
});
it("Store - dialed first", async function () {

View File

@ -63,7 +63,7 @@ describe("Waku Dial [node only]", function () {
let nwaku: NimGoNode;
afterEach(async function () {
!!nwaku && nwaku.stop();
!!nwaku && (await nwaku.stop());
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
@ -83,7 +83,7 @@ describe("Waku Dial [node only]", function () {
const connectedPeerID: PeerId = await new Promise((resolve) => {
waku.libp2p.addEventListener("peer:connect", (evt) => {
resolve(evt.detail.remotePeer);
resolve(evt.detail);
});
});
@ -108,7 +108,7 @@ describe("Waku Dial [node only]", function () {
const connectedPeerID: PeerId = await new Promise((resolve) => {
waku.libp2p.addEventListener("peer:connect", (evt) => {
resolve(evt.detail.remotePeer);
resolve(evt.detail);
});
});
@ -139,10 +139,9 @@ describe("Decryption Keys", () => {
}).then((waku) => waku.start().then(() => waku)),
]);
await waku1.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku1.libp2p.peerStore.merge(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await waku1.dial(waku2.libp2p.peerId);
await Promise.all([
@ -175,7 +174,7 @@ describe("Decryption Keys", () => {
const receivedMsgPromise: Promise<DecodedMessage> = new Promise(
(resolve) => {
waku2.relay.subscribe([decoder], resolve);
void waku2.relay.subscribe([decoder], resolve);
}
);
@ -214,22 +213,21 @@ describe("User Agent", () => {
}).then((waku) => waku.start().then(() => waku)),
]);
await waku1.libp2p.peerStore.addressBook.set(
waku2.libp2p.peerId,
waku2.libp2p.getMultiaddrs()
);
await waku1.libp2p.peerStore.save(waku2.libp2p.peerId, {
multiaddrs: waku2.libp2p.getMultiaddrs(),
});
await waku1.dial(waku2.libp2p.peerId);
await waitForRemotePeer(waku1);
const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([
waku2.libp2p.peerStore.metadataBook.get(waku1.libp2p.peerId),
waku1.libp2p.peerStore.metadataBook.get(waku2.libp2p.peerId),
waku2.libp2p.peerStore.get(waku1.libp2p.peerId),
waku1.libp2p.peerStore.get(waku2.libp2p.peerId),
]);
expect(bytesToUtf8(waku1PeerInfo.get("AgentVersion")!)).to.eq(
expect(bytesToUtf8(waku1PeerInfo.metadata.get("AgentVersion")!)).to.eq(
waku1UserAgent
);
expect(bytesToUtf8(waku2PeerInfo.get("AgentVersion")!)).to.eq(
expect(bytesToUtf8(waku2PeerInfo.metadata.get("AgentVersion")!)).to.eq(
DefaultUserAgent
);
});

View File

@ -69,23 +69,15 @@
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-connection": "^5.1.1",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-peer-store": "^2.0.4",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.59.8",
"@waku/build-utils": "*",
"@waku/interfaces": "0.0.15",
"cspell": "^6.31.1",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.4",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rollup": "^3.21.3",