From fa26d05f8e0517de742950871cfbb15276b6c462 Mon Sep 17 00:00:00 2001 From: Simon-Pierre Vivier Date: Thu, 9 May 2024 10:38:55 -0400 Subject: [PATCH] fix: message cache removal crash (#2682) --- tests/test_message_cache.nim | 40 ++++++++++++++++++++++++++++++++- waku/waku_api/message_cache.nim | 6 ++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/tests/test_message_cache.nim b/tests/test_message_cache.nim index ae877f4214..63dbed6adf 100644 --- a/tests/test_message_cache.nim +++ b/tests/test_message_cache.nim @@ -1,8 +1,10 @@ {.used.} -import std/sets, stew/[results, byteutils], testutils/unittests +import std/[sets, random], stew/[results, byteutils], testutils/unittests import ../../waku/waku_core, ../../waku/waku_api/message_cache, ./testlib/wakucore +randomize() + suite "MessageCache": setup: ## Given @@ -209,3 +211,39 @@ suite "MessageCache": check: cache.messagesCount() == 2 + + test "fuzzing": + let testContentTopic1 = "contentTopic1" + let testContentTopic2 = "contentTopic2" + + let cache = MessageCache.init(50) + + cache.contentSubscribe(testContentTopic1) + cache.contentSubscribe(testContentTopic2) + + for _ in 0 .. 10000: + let numb = rand(1.0) + + if numb > 0.4: + let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2 + + let testMessage = fakeWakuMessage(contentTopic = topic) + + cache.addMessage(DefaultPubsubTopic, testMessage) + elif numb > 0.1: + let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2 + + let clear = rand(1.0) > 0.5 + discard cache.getAutoMessages(topic, clear) + elif numb > 0.05: + if rand(1.0) > 0.5: + cache.pubsubUnsubscribe(DefaultPubsubTopic) + else: + cache.pubsubSubscribe(DefaultPubsubTopic) + else: + let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2 + + if rand(1.0) > 0.5: + cache.contentUnsubscribe(topic) + else: + cache.contentSubscribe(topic) diff --git a/waku/waku_api/message_cache.nim b/waku/waku_api/message_cache.nim index 7f97cbf8cc..66b9c62d91 100644 --- a/waku/waku_api/message_cache.nim +++ b/waku/waku_api/message_cache.nim @@ -134,7 +134,7 @@ proc pubsubUnsubscribe*(self: MessageCache, pubsubTopic: PubsubTopic) = dec(j) # check if messages on this pubsub topic are indexed by any content topic, if not remove them. - for mId in msgIndices: + for mId in msgIndices.sorted(SortOrder.Descending): if not self.contentIndex.anyIt(it.msgIdx == mId): self.removeMessage(mId) @@ -167,7 +167,7 @@ proc contentUnsubscribe*(self: MessageCache, contentTopic: ContentTopic) = dec(j) # check if messages on this content topic are indexed by any pubsub topic, if not remove them. - for mId in msgIndices: + for mId in msgIndices.sorted(SortOrder.Descending): if not self.pubsubIndex.anyIt(it.msgIdx == mId): self.removeMessage(mId) @@ -275,7 +275,7 @@ proc getAutoMessages*( let messages = msgIndices.mapIt(self.messages[it]) if clear: - for idx in msgIndices.reversed: + for idx in msgIndices.sorted(SortOrder.Descending): self.removeMessage(idx) return ok(messages)