2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-08-30 13:57:45 +00:00
|
|
|
|
|
|
|
import
|
2023-11-28 12:21:41 +00:00
|
|
|
std/[sequtils, sugar, algorithm, options],
|
2022-08-30 13:57:45 +00:00
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
libp2p/protocols/pubsub
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../waku_core
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-04-18 13:22:10 +00:00
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku node message_cache"
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
const DefaultMessageCacheCapacity: int = 50
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
type MessageCache* = ref object
|
|
|
|
pubsubTopics: seq[PubsubTopic]
|
|
|
|
contentTopics: seq[ContentTopic]
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
pubsubIndex: seq[tuple[pubsubIdx: int, msgIdx: int]]
|
|
|
|
contentIndex: seq[tuple[contentIdx: int, msgIdx: int]]
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
messages: seq[WakuMessage]
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
capacity: int
|
|
|
|
|
|
|
|
func `$`*(self: MessageCache): string =
|
2024-03-15 23:08:47 +00:00
|
|
|
"Messages: " & $self.messages.len & " \nPubsubTopics: " & $self.pubsubTopics &
|
|
|
|
" \nContentTopics: " & $self.contentTopics & " \nPubsubIndex: " & $self.pubsubIndex &
|
|
|
|
" \nContentIndex: " & $self.contentIndex
|
2023-11-28 12:21:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func init*(T: type MessageCache, capacity = DefaultMessageCacheCapacity): T =
|
|
|
|
MessageCache(capacity: capacity)
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc messagesCount*(self: MessageCache): int =
|
|
|
|
self.messages.len
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc pubsubTopicCount*(self: MessageCache): int =
|
|
|
|
self.pubsubTopics.len
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc contentTopicCount*(self: MessageCache): int =
|
|
|
|
self.contentTopics.len
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc pubsubSearch(self: MessageCache, pubsubTopic: PubsubTopic): Option[int] =
|
|
|
|
# Return some with the index if found none otherwise.
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
for i, topic in self.pubsubTopics:
|
|
|
|
if topic == pubsubTopic:
|
|
|
|
return some(i)
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
return none(int)
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc contentSearch(self: MessageCache, contentTopic: ContentTopic): Option[int] =
|
|
|
|
# Return some with the index if found none otherwise.
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
for i, topic in self.contentTopics:
|
|
|
|
if topic == contentTopic:
|
|
|
|
return some(i)
|
|
|
|
|
|
|
|
return none(int)
|
|
|
|
|
|
|
|
proc isPubsubSubscribed*(self: MessageCache, pubsubTopic: PubsubTopic): bool =
|
|
|
|
self.pubsubSearch(pubsubTopic).isSome()
|
|
|
|
|
|
|
|
proc isContentSubscribed*(self: MessageCache, contentTopic: ContentTopic): bool =
|
|
|
|
self.contentSearch(contentTopic).isSome()
|
|
|
|
|
|
|
|
proc pubsubSubscribe*(self: MessageCache, pubsubTopic: PubsubTopic) =
|
|
|
|
if self.pubsubSearch(pubsubTopic).isNone():
|
|
|
|
self.pubsubTopics.add(pubsubTopic)
|
|
|
|
|
|
|
|
proc contentSubscribe*(self: MessageCache, contentTopic: ContentTopic) =
|
|
|
|
if self.contentSearch(contentTopic).isNone():
|
|
|
|
self.contentTopics.add(contentTopic)
|
|
|
|
|
|
|
|
proc removeMessage(self: MessageCache, idx: int) =
|
|
|
|
# get last index because del() is a swap
|
|
|
|
let lastIndex = self.messages.high
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
self.messages.del(idx)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
# update indices
|
|
|
|
var j = self.pubsubIndex.high
|
|
|
|
while j > -1:
|
|
|
|
let (pId, mId) = self.pubsubIndex[j]
|
|
|
|
|
|
|
|
if mId == idx:
|
|
|
|
self.pubsubIndex.del(j)
|
|
|
|
elif mId == lastIndex:
|
|
|
|
self.pubsubIndex[j] = (pId, idx)
|
|
|
|
|
|
|
|
dec(j)
|
|
|
|
|
|
|
|
j = self.contentIndex.high
|
|
|
|
while j > -1:
|
|
|
|
let (cId, mId) = self.contentIndex[j]
|
|
|
|
|
|
|
|
if mId == idx:
|
|
|
|
self.contentIndex.del(j)
|
|
|
|
elif mId == lastIndex:
|
|
|
|
self.contentIndex[j] = (cId, idx)
|
|
|
|
|
|
|
|
dec(j)
|
|
|
|
|
|
|
|
proc pubsubUnsubscribe*(self: MessageCache, pubsubTopic: PubsubTopic) =
|
|
|
|
let pubsubIdxOp = self.pubsubSearch(pubsubTopic)
|
|
|
|
|
|
|
|
let pubsubIdx =
|
2024-03-15 23:08:47 +00:00
|
|
|
if pubsubIdxOp.isSome():
|
|
|
|
pubsubIdxOp.get()
|
|
|
|
else:
|
|
|
|
return
|
2023-11-28 12:21:41 +00:00
|
|
|
|
|
|
|
let lastIndex = self.pubsubTopics.high
|
|
|
|
self.pubsubTopics.del(pubsubIdx)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
var msgIndices = newSeq[int](0)
|
|
|
|
|
|
|
|
var j = self.pubsubIndex.high
|
|
|
|
while j > -1:
|
|
|
|
let (pId, mId) = self.pubsubIndex[j]
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
if pId == pubsubIdx:
|
|
|
|
# remove index for this topic
|
|
|
|
self.pubsubIndex.del(j)
|
|
|
|
msgIndices.add(mId)
|
|
|
|
elif pId == lastIndex:
|
|
|
|
# swap the index because pubsubTopics.del() is a swap
|
|
|
|
self.pubsubIndex[j] = (pubsubIdx, mId)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
dec(j)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
# check if messages on this pubsub topic are indexed by any content topic, if not remove them.
|
2024-05-09 14:38:55 +00:00
|
|
|
for mId in msgIndices.sorted(SortOrder.Descending):
|
2023-11-28 12:21:41 +00:00
|
|
|
if not self.contentIndex.anyIt(it.msgIdx == mId):
|
|
|
|
self.removeMessage(mId)
|
|
|
|
|
|
|
|
proc contentUnsubscribe*(self: MessageCache, contentTopic: ContentTopic) =
|
|
|
|
let contentIdxOP = self.contentSearch(contentTopic)
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let contentIdx =
|
|
|
|
if contentIdxOP.isSome():
|
|
|
|
contentIdxOP.get()
|
|
|
|
else:
|
|
|
|
return
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let lastIndex = self.contentTopics.high
|
|
|
|
self.contentTopics.del(contentIdx)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
var msgIndices = newSeq[int](0)
|
2023-04-18 13:22:10 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
var j = self.contentIndex.high
|
|
|
|
while j > -1:
|
|
|
|
let (cId, mId) = self.contentIndex[j]
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
if cId == contentIdx:
|
|
|
|
# remove indices for this topic
|
|
|
|
self.contentIndex.del(j)
|
|
|
|
msgIndices.add(mId)
|
|
|
|
elif cId == lastIndex:
|
|
|
|
# swap the indices because contentTopics.del() is a swap
|
|
|
|
self.contentIndex[j] = (contentIdx, mId)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
dec(j)
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
# check if messages on this content topic are indexed by any pubsub topic, if not remove them.
|
2024-05-09 14:38:55 +00:00
|
|
|
for mId in msgIndices.sorted(SortOrder.Descending):
|
2023-11-28 12:21:41 +00:00
|
|
|
if not self.pubsubIndex.anyIt(it.msgIdx == mId):
|
|
|
|
self.removeMessage(mId)
|
|
|
|
|
|
|
|
proc reset*(self: MessageCache) =
|
|
|
|
self.messages.setLen(0)
|
|
|
|
self.pubsubTopics.setLen(0)
|
|
|
|
self.contentTopics.setLen(0)
|
|
|
|
self.pubsubIndex.setLen(0)
|
|
|
|
self.contentIndex.setLen(0)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc addMessage*(self: MessageCache, pubsubTopic: PubsubTopic, msg: WakuMessage) =
|
2023-11-28 12:21:41 +00:00
|
|
|
## Idempotent message addition.
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
var oldestTime = int64.high
|
|
|
|
var oldestMsg = int.high
|
|
|
|
for i, message in self.messages.reversed:
|
|
|
|
if message == msg:
|
|
|
|
return
|
|
|
|
|
|
|
|
if message.timestamp < oldestTime:
|
2024-03-15 23:08:47 +00:00
|
|
|
oldestTime = message.timestamp
|
|
|
|
oldestMsg = i
|
2023-11-28 12:21:41 +00:00
|
|
|
|
|
|
|
# reverse index
|
|
|
|
oldestMsg = self.messages.high - oldestMsg
|
|
|
|
|
|
|
|
var pubsubIdxOp = self.pubsubSearch(pubsubTopic)
|
|
|
|
var contentIdxOp = self.contentSearch(msg.contentTopic)
|
|
|
|
|
|
|
|
if pubsubIdxOp.isNone() and contentIdxOp.isNone():
|
2022-08-30 13:57:45 +00:00
|
|
|
return
|
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let pubsubIdx =
|
|
|
|
if pubsubIdxOp.isNone():
|
|
|
|
self.pubsubTopics.add(pubsubTopic)
|
|
|
|
self.pubsubTopics.high
|
|
|
|
else:
|
|
|
|
pubsubIdxOp.get()
|
|
|
|
|
|
|
|
let contentIdx =
|
|
|
|
if contentIdxOp.isNone():
|
|
|
|
self.contentTopics.add(msg.contentTopic)
|
|
|
|
self.contentTopics.high
|
|
|
|
else:
|
|
|
|
contentIdxOp.get()
|
|
|
|
|
|
|
|
# add the message, make space if needed
|
|
|
|
if self.messages.len >= self.capacity:
|
|
|
|
self.removeMessage(oldestMsg)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
let msgIdx = self.messages.len
|
|
|
|
self.messages.add(msg)
|
|
|
|
|
|
|
|
self.pubsubIndex.add((pubsubIdx, msgIdx))
|
|
|
|
self.contentIndex.add((contentIdx, msgIdx))
|
|
|
|
|
|
|
|
proc getMessages*(
|
2024-03-15 23:08:47 +00:00
|
|
|
self: MessageCache, pubsubTopic: PubsubTopic, clear = false
|
|
|
|
): Result[seq[WakuMessage], string] =
|
2023-11-28 12:21:41 +00:00
|
|
|
## Return all messages on this pubsub topic
|
|
|
|
|
|
|
|
if self.pubsubTopics.len == 0:
|
|
|
|
return err("not subscribed to any pubsub topics")
|
|
|
|
|
|
|
|
let pubsubIdxOp = self.pubsubSearch(pubsubTopic)
|
|
|
|
let pubsubIdx =
|
|
|
|
if pubsubIdxOp.isNone:
|
|
|
|
return err("not subscribed to this pubsub topic")
|
2024-03-15 23:08:47 +00:00
|
|
|
else:
|
|
|
|
pubsubIdxOp.get()
|
2023-11-28 12:21:41 +00:00
|
|
|
|
|
|
|
let msgIndices = collect:
|
|
|
|
for (pId, mId) in self.pubsubIndex:
|
|
|
|
if pId == pubsubIdx:
|
|
|
|
mId
|
|
|
|
|
|
|
|
let messages = msgIndices.mapIt(self.messages[it])
|
2022-08-30 13:57:45 +00:00
|
|
|
|
|
|
|
if clear:
|
2023-11-28 12:21:41 +00:00
|
|
|
for idx in msgIndices.reversed:
|
|
|
|
self.removeMessage(idx)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
return ok(messages)
|
|
|
|
|
|
|
|
proc getAutoMessages*(
|
2024-03-15 23:08:47 +00:00
|
|
|
self: MessageCache, contentTopic: ContentTopic, clear = false
|
|
|
|
): Result[seq[WakuMessage], string] =
|
2023-11-28 12:21:41 +00:00
|
|
|
## Return all messages on this content topic
|
2022-08-30 13:57:45 +00:00
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
if self.contentTopics.len == 0:
|
|
|
|
return err("not subscribed to any content topics")
|
|
|
|
|
|
|
|
let contentIdxOp = self.contentSearch(contentTopic)
|
|
|
|
let contentIdx =
|
|
|
|
if contentIdxOp.isNone():
|
|
|
|
return err("not subscribed to this content topic")
|
2024-03-15 23:08:47 +00:00
|
|
|
else:
|
|
|
|
contentIdxOp.get()
|
2023-11-28 12:21:41 +00:00
|
|
|
|
|
|
|
let msgIndices = collect:
|
|
|
|
for (cId, mId) in self.contentIndex:
|
|
|
|
if cId == contentIdx:
|
|
|
|
mId
|
|
|
|
|
|
|
|
let messages = msgIndices.mapIt(self.messages[it])
|
|
|
|
|
|
|
|
if clear:
|
2024-05-09 14:38:55 +00:00
|
|
|
for idx in msgIndices.sorted(SortOrder.Descending):
|
2023-11-28 12:21:41 +00:00
|
|
|
self.removeMessage(idx)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
|
|
|
return ok(messages)
|