# Nim-LibP2P # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) # * MIT license ([LICENSE-MIT](LICENSE-MIT)) # at your option. # This file may not be copied, modified, or distributed except according to # those terms. {.push raises: [].} import std/[sets, tables] import rpc/[messages] import results export sets, tables, messages, results type CacheEntry* = object msgId*: MessageId topic*: string MCache* = object of RootObj msgs*: Table[MessageId, Message] history*: seq[seq[CacheEntry]] pos*: int windowSize*: Natural func get*(c: MCache, msgId: MessageId): Opt[Message] = if msgId in c.msgs: try: Opt.some(c.msgs[msgId]) except KeyError: raiseAssert "checked" else: Opt.none(Message) func contains*(c: MCache, msgId: MessageId): bool = msgId in c.msgs func put*(c: var MCache, msgId: MessageId, msg: Message) = if not c.msgs.hasKeyOrPut(msgId, msg): # Only add cache entry if the message was not already in the cache c.history[c.pos].add(CacheEntry(msgId: msgId, topic: msg.topic)) func window*(c: MCache, topic: string): HashSet[MessageId] = let len = min(c.windowSize, c.history.len) for i in 0..