From 201e1b129e83a01d6da55a46c54016369a2f124b Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Fri, 22 Sep 2023 15:15:26 +0300 Subject: [PATCH] Fix deleting pubsub topics; only allow adding unique topics --- waku/v2/peerstore/waku_peer_store.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/waku/v2/peerstore/waku_peer_store.go b/waku/v2/peerstore/waku_peer_store.go index 00c812c59..124126827 100644 --- a/waku/v2/peerstore/waku_peer_store.go +++ b/waku/v2/peerstore/waku_peer_store.go @@ -155,6 +155,11 @@ func (ps *WakuPeerstoreImpl) AddPubSubTopic(p peer.ID, topic string) error { if err != nil { return err } + for _, t := range existingTopics { + if t == topic { + return errors.New("Pubsub topic already exists") + } + } existingTopics = append(existingTopics, topic) return ps.peerStore.Put(p, peerPubSubTopics, existingTopics) } @@ -170,8 +175,11 @@ func (ps *WakuPeerstoreImpl) RemovePubSubTopic(p peer.ID, topic string) error { return nil } - for i := range existingTopics { - existingTopics = append(existingTopics[:i], existingTopics[i+1:]...) + for i, t := range existingTopics { + if t == topic { + existingTopics = append(existingTopics[:i], existingTopics[i+1:]...) + break + } } err = ps.SetPubSubTopics(p, existingTopics) if err != nil {