From 491366bd4f96d5b72f83ca47dea5a93389ec5a27 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:17:02 +0530 Subject: [PATCH] fix: filter subscription with `pubsubTopic1` and decoder with `pubsubTopic2` (#1675) * add a test for a failing case * add error handling & update test * remove only * rename test --- packages/core/src/lib/filter/index.ts | 10 ++++++++++ .../tests/tests/filter/multiple_pubsub.node.spec.ts | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index 1f66cb0d33..63202bd3eb 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -74,6 +74,16 @@ class Subscription { callback: Callback ): Promise { const decodersArray = Array.isArray(decoders) ? decoders : [decoders]; + + // check that all decoders are configured for the same pubsub topic as this subscription + decodersArray.forEach((decoder) => { + if (decoder.pubsubTopic !== this.pubsubTopic) { + throw new Error( + `Pubsub topic not configured: decoder is configured for pubsub topic ${decoder.pubsubTopic} but this subscription is for pubsub topic ${this.pubsubTopic}. Please create a new Subscription for the different pubsub topic.` + ); + } + }); + const decodersGroupedByCT = groupByContentTopic(decodersArray); const contentTopics = Array.from(decodersGroupedByCT.keys()); diff --git a/packages/tests/tests/filter/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/multiple_pubsub.node.spec.ts index efd787dc99..020eb3a590 100644 --- a/packages/tests/tests/filter/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/multiple_pubsub.node.spec.ts @@ -146,4 +146,15 @@ describe("Waku Filter V2: Multiple PubSubtopics", function () { expectedMessageText: "M2" }); }); + + it("Should fail to subscribe with decoder with wrong pubsubTopic", async function () { + // this subscription object is set up with the `customPubsubTopic` but we're passing it a Decoder with the `DefaultPubsubTopic` + try { + await subscription.subscribe([TestDecoder], messageCollector.callback); + } catch (error) { + expect((error as Error).message).to.include( + "Pubsub topic not configured" + ); + } + }); });