Pagination: Pagination feature/compute index (#225)

* 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 <ot@oskarthoren.com>

* 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 <ot@oskarthoren.com>
This commit is contained in:
Sanaz Taheri Boshrooyeh 2020-10-19 19:20:44 -07:00 committed by GitHub
parent 5c25ed131a
commit d6c48da071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 6 deletions

View File

@ -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

View File

@ -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