mirror of https://github.com/waku-org/nwaku.git
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:
parent
5c25ed131a
commit
d6c48da071
|
@ -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
|
||||||
|
|
|
@ -3,14 +3,16 @@
|
||||||
## TODO Move more common data types here
|
## TODO Move more common data types here
|
||||||
|
|
||||||
import
|
import
|
||||||
std/tables,
|
std/[tables, times],
|
||||||
chronos, bearssl, stew/byteutils,
|
chronos, bearssl, stew/byteutils,
|
||||||
libp2p/[switch, peerinfo, multiaddress, crypto/crypto],
|
libp2p/[switch, peerinfo, multiaddress, crypto/crypto],
|
||||||
libp2p/protobuf/minprotobuf,
|
libp2p/protobuf/minprotobuf,
|
||||||
libp2p/protocols/protocol,
|
libp2p/protocols/protocol,
|
||||||
libp2p/switch,
|
libp2p/switch,
|
||||||
libp2p/stream/connection,
|
libp2p/stream/connection,
|
||||||
libp2p/protocols/pubsub/[pubsub, gossipsub]
|
libp2p/protocols/pubsub/[pubsub, gossipsub],
|
||||||
|
nimcrypto/sha2,
|
||||||
|
stew/byteutils
|
||||||
|
|
||||||
# Common data types -----------------------------------------------------------
|
# Common data types -----------------------------------------------------------
|
||||||
|
|
||||||
|
@ -32,11 +34,29 @@ type
|
||||||
|
|
||||||
QueryHandlerFunc* = proc(response: HistoryResponse) {.gcsafe, closure.}
|
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
|
HistoryQuery* = object
|
||||||
topics*: seq[string]
|
topics*: seq[string]
|
||||||
|
pagingInfo*: PagingInfo
|
||||||
|
|
||||||
HistoryResponse* = object
|
HistoryResponse* = object
|
||||||
messages*: seq[WakuMessage]
|
messages*: seq[WakuMessage]
|
||||||
|
pagingInfo*: PagingInfo
|
||||||
|
|
||||||
HistoryRPC* = object
|
HistoryRPC* = object
|
||||||
requestId*: string
|
requestId*: string
|
||||||
|
@ -111,11 +131,11 @@ type
|
||||||
gossipEnabled*: bool
|
gossipEnabled*: bool
|
||||||
|
|
||||||
WakuInfo* = object
|
WakuInfo* = object
|
||||||
# NOTE One for simplicity, can extend later as needed
|
# NOTE One for simplicity, can extend later as needed
|
||||||
listenStr*: string
|
listenStr*: string
|
||||||
#multiaddrStrings*: seq[string]
|
#multiaddrStrings*: seq[string]
|
||||||
|
|
||||||
# Encoding and decoding -------------------------------------------------------
|
# Encoding and decoding -------------------------------------------------------
|
||||||
|
|
||||||
proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
||||||
var msg = WakuMessage()
|
var msg = WakuMessage()
|
||||||
|
@ -154,3 +174,17 @@ proc generateRequestId*(rng: ref BrHmacDrbgContext): string =
|
||||||
var bytes: array[10, byte]
|
var bytes: array[10, byte]
|
||||||
brHmacDrbgGenerate(rng[], bytes)
|
brHmacDrbgGenerate(rng[], bytes)
|
||||||
toHex(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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue