Skip to content

Commit

Permalink
chore: use connected peers instead of discovered peers for protocols (#…
Browse files Browse the repository at this point in the history
…1758)

* switch to use connected peers instead of discovered peers for protocols

* switch to use connected peers instead of discovered peers for protocols
  • Loading branch information
danisharora099 authored Jan 2, 2024
1 parent 0d534e3 commit 9983549
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * as waku_filter from "./lib/filter/index.js";
export { wakuFilter, FilterCodecs } from "./lib/filter/index.js";

export * as waku_light_push from "./lib/light_push/index.js";
export { LightPushCodec } from "./lib/light_push/index.js";
export { wakuLightPush } from "./lib/light_push/index.js";

export * as waku_store from "./lib/store/index.js";
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import type {
} from "@waku/interfaces";
import { DefaultPubsubTopic } from "@waku/interfaces";
import { shardInfoToPubsubTopics } from "@waku/utils";
import { getPeersForProtocol, selectPeerForProtocol } from "@waku/utils/libp2p";
import {
getConnectedPeersForProtocol,
getPeersForProtocol,
selectPeerForProtocol
} from "@waku/utils/libp2p";

import { filterPeers } from "./filterPeers.js";
import { StreamManager } from "./stream_manager.js";
Expand Down Expand Up @@ -70,7 +74,7 @@ export class BaseProtocol implements IBaseProtocol {
}

/**
* Retrieves a list of peers based on the specified criteria.
* Retrieves a list of connected peers based on the specified criteria.
*
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
Expand All @@ -88,10 +92,12 @@ export class BaseProtocol implements IBaseProtocol {
numPeers: 0
}
): Promise<Peer[]> {
// Retrieve all peers that support the protocol
const allPeersForProtocol = await getPeersForProtocol(this.peerStore, [
this.multicodec
]);
// Retrieve all connected peers that support the protocol
const allPeersForProtocol = await getConnectedPeersForProtocol(
this.components.connectionManager.getConnections(),
this.peerStore,
[this.multicodec]
);

// Filter the peers based on the specified criteria
return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
Expand Down
43 changes: 41 additions & 2 deletions packages/tests/tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import type { PeerStore } from "@libp2p/interface/peer-store";
import type { Peer } from "@libp2p/interface/peer-store";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core";
import { DefaultPubsubTopic, LightNode, Protocols } from "@waku/interfaces";
import { LightPushCodec } from "@waku/core";
import { DefaultPubsubTopic, LightNode } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { toAsyncIterator } from "@waku/utils";
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
import { selectPeerForProtocol } from "@waku/utils/libp2p";
import {
getConnectedPeersForProtocol,
selectPeerForProtocol
} from "@waku/utils/libp2p";
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
Expand Down Expand Up @@ -260,3 +265,37 @@ describe("selectPeerForProtocol", () => {
);
});
});

describe("getConnectedPeersForProtocol", function () {
let waku: LightNode;
let nwaku: NimGoNode;

beforeEach(async function () {
this.timeout(15000);
nwaku = new NimGoNode(makeLogFileName(this));
await nwaku.start({
filter: true,
lightpush: true,
relay: true
});
waku = await createLightNode();
await waku.start();
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Filter]);
});

afterEach(async function () {
this.timeout(10000);
await tearDownNodes(nwaku, waku);
});

it("returns all connected peers that support the protocol", async function () {
const peers = await getConnectedPeersForProtocol(
waku.libp2p.getConnections(),
waku.libp2p.peerStore,
[LightPushCodec]
);

expect(peers.length).to.eq(1);
});
});
21 changes: 21 additions & 0 deletions packages/utils/src/libp2p/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ export async function getPeersForProtocol(
return peers;
}

export async function getConnectedPeersForProtocol(
connections: Connection[],
peerStore: PeerStore,
protocols: string[]
): Promise<Peer[]> {
const openConnections = connections.filter(
(connection) => connection.status === "open"
);

const peerPromises = openConnections.map(async (connection) => {
const peer = await peerStore.get(connection.remotePeer);
const supportsProtocol = peer.protocols.some((protocol) =>
protocols.includes(protocol)
);
return supportsProtocol ? peer : null;
});

const peersWithNulls = await Promise.all(peerPromises);
return peersWithNulls.filter((peer): peer is Peer => peer !== null);
}

/**
* Returns a peer that supports the given protocol.
* If peerId is provided, the peer with that id is returned.
Expand Down

0 comments on commit 9983549

Please sign in to comment.