Skip to content

Commit fa26d05

Browse files
authored
fix: message cache removal crash (#2682)
1 parent b643f4c commit fa26d05

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tests/test_message_cache.nim

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{.used.}
22

3-
import std/sets, stew/[results, byteutils], testutils/unittests
3+
import std/[sets, random], stew/[results, byteutils], testutils/unittests
44
import ../../waku/waku_core, ../../waku/waku_api/message_cache, ./testlib/wakucore
55

6+
randomize()
7+
68
suite "MessageCache":
79
setup:
810
## Given
@@ -209,3 +211,39 @@ suite "MessageCache":
209211

210212
check:
211213
cache.messagesCount() == 2
214+
215+
test "fuzzing":
216+
let testContentTopic1 = "contentTopic1"
217+
let testContentTopic2 = "contentTopic2"
218+
219+
let cache = MessageCache.init(50)
220+
221+
cache.contentSubscribe(testContentTopic1)
222+
cache.contentSubscribe(testContentTopic2)
223+
224+
for _ in 0 .. 10000:
225+
let numb = rand(1.0)
226+
227+
if numb > 0.4:
228+
let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2
229+
230+
let testMessage = fakeWakuMessage(contentTopic = topic)
231+
232+
cache.addMessage(DefaultPubsubTopic, testMessage)
233+
elif numb > 0.1:
234+
let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2
235+
236+
let clear = rand(1.0) > 0.5
237+
discard cache.getAutoMessages(topic, clear)
238+
elif numb > 0.05:
239+
if rand(1.0) > 0.5:
240+
cache.pubsubUnsubscribe(DefaultPubsubTopic)
241+
else:
242+
cache.pubsubSubscribe(DefaultPubsubTopic)
243+
else:
244+
let topic = if rand(1.0) > 0.5: testContentTopic1 else: testContentTopic2
245+
246+
if rand(1.0) > 0.5:
247+
cache.contentUnsubscribe(topic)
248+
else:
249+
cache.contentSubscribe(topic)

waku/waku_api/message_cache.nim

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ proc pubsubUnsubscribe*(self: MessageCache, pubsubTopic: PubsubTopic) =
134134
dec(j)
135135

136136
# check if messages on this pubsub topic are indexed by any content topic, if not remove them.
137-
for mId in msgIndices:
137+
for mId in msgIndices.sorted(SortOrder.Descending):
138138
if not self.contentIndex.anyIt(it.msgIdx == mId):
139139
self.removeMessage(mId)
140140

@@ -167,7 +167,7 @@ proc contentUnsubscribe*(self: MessageCache, contentTopic: ContentTopic) =
167167
dec(j)
168168

169169
# check if messages on this content topic are indexed by any pubsub topic, if not remove them.
170-
for mId in msgIndices:
170+
for mId in msgIndices.sorted(SortOrder.Descending):
171171
if not self.pubsubIndex.anyIt(it.msgIdx == mId):
172172
self.removeMessage(mId)
173173

@@ -275,7 +275,7 @@ proc getAutoMessages*(
275275
let messages = msgIndices.mapIt(self.messages[it])
276276

277277
if clear:
278-
for idx in msgIndices.reversed:
278+
for idx in msgIndices.sorted(SortOrder.Descending):
279279
self.removeMessage(idx)
280280

281281
return ok(messages)

0 commit comments

Comments
 (0)