## Nim-LibP2P ## Copyright (c) 2019 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. import std/[sets, tables, options] import rpc/[messages] export sets, tables, messages, options type CacheEntry* = object mid*: MessageID topicIDs*: seq[string] MCache* = object of RootObj msgs*: Table[MessageID, Message] history*: seq[seq[CacheEntry]] windowSize*: Natural func get*(c: MCache, mid: MessageID): Option[Message] = if mid in c.msgs: some(c.msgs[mid]) else: none(Message) func contains*(c: MCache, mid: MessageID): bool = c.get(mid).isSome 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[0].add(CacheEntry(mid: msgId, topicIDs: msg.topicIDs)) func window*(c: MCache, topic: string): HashSet[MessageID] = result = initHashSet[MessageID]() let len = min(c.windowSize, c.history.len) for i in 0..