diff --git a/packages/interfaces/src/filter.ts b/packages/interfaces/src/filter.ts index 2446ce08a9..4c56a3d680 100644 --- a/packages/interfaces/src/filter.ts +++ b/packages/interfaces/src/filter.ts @@ -1,3 +1,5 @@ +import type { PeerId } from "@libp2p/interface"; + import type { IDecodedMessage, IDecoder } from "./message.js"; import type { ContentTopic, PubsubTopic, ThisOrThat } from "./misc.js"; import type { @@ -27,7 +29,7 @@ export interface ISubscriptionSDK { unsubscribe(contentTopics: ContentTopic[]): Promise; - ping(): Promise; + ping(peerId?: PeerId): Promise; unsubscribeAll(): Promise; } diff --git a/packages/tests/tests/filter/peer_management.spec.ts b/packages/tests/tests/filter/peer_management.spec.ts index dd3fd739b6..f5e9c47cb2 100644 --- a/packages/tests/tests/filter/peer_management.spec.ts +++ b/packages/tests/tests/filter/peer_management.spec.ts @@ -131,6 +131,35 @@ describe.only("Waku Filter: Peer Management: E2E", function () { ).to.eq(false); }); + it("Tracks peer failures correctly", async function () { + const maxPingFailures = 3; + await subscription.subscribe([decoder], () => {}, { + pingsBeforePeerRenewed: maxPingFailures + }); + + const targetPeer = waku.filter.connectedPeers[0]; + await waku.connectionManager.dropConnection(targetPeer.id); + + for (let i = 0; i < maxPingFailures; i++) { + await subscription.ping(targetPeer.id); + } + + // At this point, the peer should not be renewed yet + expect( + waku.filter.connectedPeers.some((peer) => peer.id.equals(targetPeer.id)) + ).to.be.true; + + // One more failure should trigger renewal + await subscription.ping(targetPeer.id); + + expect( + waku.filter.connectedPeers.some((peer) => peer.id.equals(targetPeer.id)) + ).to.be.false; + expect(waku.filter.connectedPeers.length).to.equal( + waku.filter.numPeersToUse + ); + }); + it("Maintains correct number of peers after multiple subscribe/unsubscribe cycles", async function () { for (let i = 0; i < 3; i++) { await subscription.subscribe([decoder], () => {});