Skip to content

Commit

Permalink
remove allPeers and connectedPeers from BaseProtocolCore, update test…
Browse files Browse the repository at this point in the history
…s, add getPeers for IWaku
  • Loading branch information
weboko committed Oct 20, 2024
1 parent e1813bc commit 9bdc2af
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 87 deletions.
20 changes: 0 additions & 20 deletions packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
Libp2pComponents,
PubsubTopic
} from "@waku/interfaces";
import { getPeersForProtocol } from "@waku/utils/libp2p";

import { StreamManager } from "./stream_manager/index.js";

Expand Down Expand Up @@ -42,23 +41,4 @@ export class BaseProtocol implements IBaseProtocolCore {
protected async getStream(peer: Peer): Promise<Stream> {
return this.streamManager.getStream(peer);
}

/**
* 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.
*/
public async allPeers(): Promise<Peer[]> {
return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
}

public async connectedPeers(): Promise<Peer[]> {
const peers = await this.allPeers();
return peers.filter((peer) => {
const connections = this.components.connectionManager.getConnections(
peer.id
);
return connections.length > 0;
});
}
}
2 changes: 0 additions & 2 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export enum Protocols {

export type IBaseProtocolCore = {
multicodec: string;
allPeers: () => Promise<Peer[]>;
connectedPeers: () => Promise<Peer[]>;
addLibp2pEventListener: Libp2p["addEventListener"];
removeLibp2pEventListener: Libp2p["removeEventListener"];
};
Expand Down
7 changes: 6 additions & 1 deletion packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PeerId, Stream } from "@libp2p/interface";
import type { Peer, PeerId, Stream } from "@libp2p/interface";
import type { MultiaddrInput } from "@multiformats/multiaddr";

import { IConnectionManager } from "./connection_manager.js";
Expand Down Expand Up @@ -121,6 +121,11 @@ export interface IWaku {
* @returns {boolean} `true` if the node has working connection and `false` otherwise
*/
isConnected(): boolean;

/**
* @returns {Peer[]} an array of all connected peers
*/
getPeers(): Promise<Peer[]>;
}

export interface LightNode extends IWaku {
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Stream } from "@libp2p/interface";
import { isPeerId, PeerId } from "@libp2p/interface";
import { isPeerId, Peer, PeerId } from "@libp2p/interface";
import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
import { ConnectionManager, getHealthManager } from "@waku/core";
import type {
Expand Down Expand Up @@ -186,6 +186,10 @@ export class WakuNode implements IWaku {
await this.libp2p.stop();
}

public async getPeers(): Promise<Peer[]> {
return this.connectionManager.getConnectedPeers();
}

public async waitForPeers(
protocols?: Protocols[],
timeoutMs?: number
Expand Down
4 changes: 3 additions & 1 deletion packages/tests/tests/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ describe("Metadata Protocol", function () {

waku = await createLightNode({
networkConfig: shardInfo,
pingKeepAlive: 1
connectionManager: {
pingKeepAlive: 1
}
});
await waku.start();
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
Expand Down
37 changes: 10 additions & 27 deletions packages/tests/tests/wait_for_remote_peer.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ describe("Wait for remote peer", function () {
await delay(1000);
await waku2.waitForPeers([Protocols.Store]);

const peers = (await waku2.store.protocol.connectedPeers()).map((peer) =>
peer.id.toString()
);
const peers = (await waku2.getPeers()).map((peer) => peer.id.toString());
const nimPeerId = multiAddrWithId.getPeerId();

expect(nimPeerId).to.not.be.undefined;
Expand Down Expand Up @@ -145,9 +143,7 @@ describe("Wait for remote peer", function () {
await waku2.dial(multiAddrWithId);
await waitPromise;

const peers = (await waku2.store.protocol.connectedPeers()).map((peer) =>
peer.id.toString()
);
const peers = (await waku2.getPeers()).map((peer) => peer.id.toString());

const nimPeerId = multiAddrWithId.getPeerId();

Expand All @@ -174,9 +170,7 @@ describe("Wait for remote peer", function () {
await waku2.dial(multiAddrWithId);
await waku2.waitForPeers([Protocols.LightPush]);

const peers = (await waku2.lightPush.protocol.connectedPeers()).map(
(peer) => peer.id.toString()
);
const peers = (await waku2.getPeers()).map((peer) => peer.id.toString());

const nimPeerId = multiAddrWithId.getPeerId();

Expand All @@ -203,24 +197,23 @@ describe("Wait for remote peer", function () {
await waku2.dial(multiAddrWithId);
await waku2.waitForPeers([Protocols.Filter]);

const peers = (await waku2.filter.protocol.connectedPeers()).map((peer) =>
peer.id.toString()
);
const peers = (await waku2.getPeers()).map((peer) => peer.id.toString());

const nimPeerId = multiAddrWithId.getPeerId();

expect(nimPeerId).to.not.be.undefined;
expect(peers.includes(nimPeerId as string)).to.be.true;
});

// TODO: re-enable store once https://github.com/waku-org/js-waku/issues/2162 is fixed
it("Light Node - default protocols", async function () {
this.timeout(20_000);
nwaku = new ServiceNode(makeLogFileName(this));
await nwaku.start({
filter: true,
lightpush: true,
relay: false,
store: true
relay: false
// store: true
});
const multiAddrWithId = await nwaku.getMultiaddrWithId();

Expand All @@ -232,26 +225,16 @@ describe("Wait for remote peer", function () {
await waku2.dial(multiAddrWithId);
await waku2.waitForPeers([
Protocols.Filter,
Protocols.Store,
// Protocols.Store,
Protocols.LightPush
]);

const filterPeers = (await waku2.filter.protocol.connectedPeers()).map(
(peer) => peer.id.toString()
);
const storePeers = (await waku2.store.protocol.connectedPeers()).map(
(peer) => peer.id.toString()
);
const lightPushPeers = (
await waku2.lightPush.protocol.connectedPeers()
).map((peer) => peer.id.toString());
const peers = (await waku2.getPeers()).map((peer) => peer.id.toString());

const nimPeerId = multiAddrWithId.getPeerId();

expect(nimPeerId).to.not.be.undefined;
expect(filterPeers.includes(nimPeerId as string)).to.be.true;
expect(storePeers.includes(nimPeerId as string)).to.be.true;
expect(lightPushPeers.includes(nimPeerId as string)).to.be.true;
expect(peers.includes(nimPeerId as string)).to.be.true;
});

it("Privacy Node - default protocol", async function () {
Expand Down
4 changes: 0 additions & 4 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./libp2p": {
"types": "./dist/libp2p/index.d.ts",
"import": "./dist/libp2p/index.js"
},
"./bytes": {
"types": "./dist/bytes/index.d.ts",
"import": "./dist/bytes/index.js"
Expand Down
31 changes: 0 additions & 31 deletions packages/utils/src/libp2p/index.ts

This file was deleted.

0 comments on commit 9bdc2af

Please sign in to comment.