diff --git a/tests/v2/test_waku_pagination.nim b/tests/v2/test_waku_pagination.nim new file mode 100644 index 000000000..940520039 --- /dev/null +++ b/tests/v2/test_waku_pagination.nim @@ -0,0 +1,27 @@ +import + std/unittest, + ../../waku/node/v2/waku_types, + ../test_helpers + +procSuite "pagination": + test "computeIndex: empty contentTopic test": + let + wm = WakuMessage(payload: @[byte 1, 2, 3]) + index = wm.computeIndex() + check: + # the fields of the index should be non-empty + len(index.digest.data) != 0 + len(index.digest.data) == 32 # sha2 output length in bytes + index.receivedTime != 0 # the timestamp should be a non-zero value + + test "computeIndex: identical WakuMessages test": + let + wm = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: "topic2") + index1 = wm.computeIndex() + wm2 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: "topic2") + index2 = wm2.computeIndex() + + check: + # the digests of two identical WakuMessages must be the same + index1.digest == index2.digest + diff --git a/waku/node/v2/waku_types.nim b/waku/node/v2/waku_types.nim index a60190437..47e0a7722 100644 --- a/waku/node/v2/waku_types.nim +++ b/waku/node/v2/waku_types.nim @@ -3,14 +3,16 @@ ## TODO Move more common data types here import - std/tables, + std/[tables, times], chronos, bearssl, stew/byteutils, libp2p/[switch, peerinfo, multiaddress, crypto/crypto], libp2p/protobuf/minprotobuf, libp2p/protocols/protocol, libp2p/switch, libp2p/stream/connection, - libp2p/protocols/pubsub/[pubsub, gossipsub] + libp2p/protocols/pubsub/[pubsub, gossipsub], + nimcrypto/sha2, + stew/byteutils # Common data types ----------------------------------------------------------- @@ -32,11 +34,29 @@ type QueryHandlerFunc* = proc(response: HistoryResponse) {.gcsafe, closure.} + + Index* = object + ## This type contains the description of an index used in the pagination of waku messages + digest*: MDigest[256] + receivedTime*: float + + IndexedWakuMessage* = object + msg*: WakuMessage + index*: Index + + PagingInfo* = object + ## This type holds the information needed for the pagination + pageSize*: int + cursor*: Index + direction*: bool + HistoryQuery* = object topics*: seq[string] + pagingInfo*: PagingInfo HistoryResponse* = object messages*: seq[WakuMessage] + pagingInfo*: PagingInfo HistoryRPC* = object requestId*: string @@ -111,11 +131,11 @@ type gossipEnabled*: bool WakuInfo* = object - # NOTE One for simplicity, can extend later as needed - listenStr*: string - #multiaddrStrings*: seq[string] + # NOTE One for simplicity, can extend later as needed + listenStr*: string + #multiaddrStrings*: seq[string] -# Encoding and decoding ------------------------------------------------------- + # Encoding and decoding ------------------------------------------------------- proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] = var msg = WakuMessage() @@ -154,3 +174,17 @@ proc generateRequestId*(rng: ref BrHmacDrbgContext): string = var bytes: array[10, byte] brHmacDrbgGenerate(rng[], bytes) toHex(bytes) + +proc computeIndex*(msg: WakuMessage): Index = + ## Takes a WakuMessage and returns its index + var ctx: sha256 + ctx.init() + if msg.contentTopic.len != 0: # checks for non-empty contentTopic + ctx.update(msg.contentTopic.toBytes()) # converts the topic to bytes + ctx.update(msg.payload) + let digest = ctx.finish() # computes the hash + ctx.clear() + + result.digest = digest + result.receivedTime = epochTime() # gets the unix timestamp +