From e6949da9181585e7e5f5c021dc912e191d1ce851 Mon Sep 17 00:00:00 2001 From: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com> Date: Mon, 19 Oct 2020 19:20:44 -0700 Subject: [PATCH] Pagination: Pagination feature/compute index (#225) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adds the index object * adds the indexedWakuMessage * adds the PagingInfo * Adds PagingInfo to HistoryResponse and HistoryQuery * adds the computeIndex procedure * Update waku/node/v2/waku_types.nim Fixes spacing issues Co-authored-by: Oskar Thorén * Changes timestamp to receivedTime and checks for the empty contentTopic * adds a test file for pagination with test scenarios for computeIndex * changes receivedTimestamp to the unix timestamp * updates a test case * replaces std/sha1 with nimcrypto/sha2 * changes the tests titles * minor comments * Some clean up * fixes some formatting issue * edits a test-case * adds comments * changes the digest type to MDigest[256] and modifies the computeIndex * fixes formatting issue * edits indentations and fixes a bug * minor edits * changes suite to procSuite and adds a new text case * fixes all the indentations * cleanup of the imports Co-authored-by: Oskar Thorén --- tests/v2/test_waku_pagination.nim | 27 ++++++++++++++++++ waku/node/v2/waku_types.nim | 46 +++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/v2/test_waku_pagination.nim 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 +