2022-02-17 10:00:45 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-08-01 10:37:45 +00:00
|
|
|
std/times,
|
2022-02-17 10:00:45 +00:00
|
|
|
stew/byteutils,
|
2022-08-01 10:37:45 +00:00
|
|
|
testutils/unittests,
|
|
|
|
nimcrypto
|
|
|
|
import
|
|
|
|
../../waku/v2/protocol/waku_message,
|
|
|
|
../../waku/v2/utils/time,
|
2022-10-21 13:01:39 +00:00
|
|
|
../../waku/v2/node/storage/message/queue_store/index,
|
|
|
|
./testlib/common
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Helpers
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
proc getTestTimestamp(offset=0): Timestamp =
|
|
|
|
let now = getNanosecondTime(epochTime() + float(offset))
|
|
|
|
Timestamp(now)
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
proc hashFromStr(input: string): MDigest[256] =
|
|
|
|
var ctx: sha256
|
|
|
|
|
|
|
|
ctx.init()
|
|
|
|
ctx.update(input.toBytes())
|
|
|
|
let hashed = ctx.finish()
|
|
|
|
ctx.clear()
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
return hashed
|
|
|
|
|
|
|
|
|
|
|
|
suite "Pagination - Index":
|
2022-02-17 10:00:45 +00:00
|
|
|
|
|
|
|
## Test vars
|
|
|
|
let
|
|
|
|
smallIndex1 = Index(digest: hashFromStr("1234"),
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(1000))
|
2022-02-17 10:00:45 +00:00
|
|
|
smallIndex2 = Index(digest: hashFromStr("1234567"), # digest is less significant than senderTime
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(1000))
|
2022-02-17 10:00:45 +00:00
|
|
|
largeIndex1 = Index(digest: hashFromStr("1234"),
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(9000)) # only senderTime differ from smallIndex1
|
2022-02-17 10:00:45 +00:00
|
|
|
largeIndex2 = Index(digest: hashFromStr("12345"), # only digest differs from smallIndex1
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(1000))
|
2022-02-17 10:00:45 +00:00
|
|
|
eqIndex1 = Index(digest: hashFromStr("0003"),
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(54321))
|
2022-02-17 10:00:45 +00:00
|
|
|
eqIndex2 = Index(digest: hashFromStr("0003"),
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(54321))
|
2022-02-17 10:00:45 +00:00
|
|
|
eqIndex3 = Index(digest: hashFromStr("0003"),
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime: getNanosecondTime(9999), # receiverTime difference should have no effect on comparisons
|
|
|
|
senderTime: getNanosecondTime(54321))
|
2022-02-28 16:29:01 +00:00
|
|
|
diffPsTopic = Index(digest: hashFromStr("1234"),
|
|
|
|
receiverTime: getNanosecondTime(0),
|
|
|
|
senderTime: getNanosecondTime(1000),
|
|
|
|
pubsubTopic: "zzzz")
|
|
|
|
noSenderTime1 = Index(digest: hashFromStr("1234"),
|
|
|
|
receiverTime: getNanosecondTime(1100),
|
|
|
|
senderTime: getNanosecondTime(0),
|
|
|
|
pubsubTopic: "zzzz")
|
|
|
|
noSenderTime2 = Index(digest: hashFromStr("1234"),
|
|
|
|
receiverTime: getNanosecondTime(10000),
|
|
|
|
senderTime: getNanosecondTime(0),
|
|
|
|
pubsubTopic: "zzzz")
|
|
|
|
noSenderTime3 = Index(digest: hashFromStr("1234"),
|
|
|
|
receiverTime: getNanosecondTime(1200),
|
|
|
|
senderTime: getNanosecondTime(0),
|
|
|
|
pubsubTopic: "aaaa")
|
|
|
|
noSenderTime4 = Index(digest: hashFromStr("0"),
|
|
|
|
receiverTime: getNanosecondTime(1200),
|
|
|
|
senderTime: getNanosecondTime(0),
|
|
|
|
pubsubTopic: "zzzz")
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
test "Index comparison":
|
|
|
|
# Index comparison with senderTime diff
|
2022-02-17 10:00:45 +00:00
|
|
|
check:
|
|
|
|
cmp(smallIndex1, largeIndex1) < 0
|
|
|
|
cmp(smallIndex2, largeIndex1) < 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Index comparison with digest diff
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
cmp(smallIndex1, smallIndex2) < 0
|
|
|
|
cmp(smallIndex1, largeIndex2) < 0
|
|
|
|
cmp(smallIndex2, largeIndex2) > 0
|
|
|
|
cmp(largeIndex1, largeIndex2) > 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Index comparison when equal
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
cmp(eqIndex1, eqIndex2) == 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# pubsubTopic difference
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
cmp(smallIndex1, diffPsTopic) < 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# receiverTime diff plays no role when senderTime set
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
cmp(eqIndex1, eqIndex3) == 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# receiverTime diff plays no role when digest/pubsubTopic equal
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
cmp(noSenderTime1, noSenderTime2) == 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# sort on receiverTime with no senderTimestamp and unequal pubsubTopic
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
cmp(noSenderTime1, noSenderTime3) < 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# sort on receiverTime with no senderTimestamp and unequal digest
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
cmp(noSenderTime1, noSenderTime4) < 0
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# sort on receiverTime if no senderTimestamp on only one side
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
cmp(smallIndex1, noSenderTime1) < 0
|
|
|
|
cmp(noSenderTime1, smallIndex1) > 0 # Test symmetry
|
|
|
|
cmp(noSenderTime2, eqIndex3) < 0
|
|
|
|
cmp(eqIndex3, noSenderTime2) > 0 # Test symmetry
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
test "Index equality":
|
|
|
|
# Exactly equal
|
2022-02-17 10:00:45 +00:00
|
|
|
check:
|
|
|
|
eqIndex1 == eqIndex2
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Receiver time plays no role, even without sender time
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
eqIndex1 == eqIndex3
|
2022-02-28 16:29:01 +00:00
|
|
|
noSenderTime1 == noSenderTime2 # only receiver time differs, indices are equal
|
|
|
|
noSenderTime1 != noSenderTime3 # pubsubTopics differ
|
|
|
|
noSenderTime1 != noSenderTime4 # digests differ
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Unequal sender time
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
smallIndex1 != largeIndex1
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Unequal digest
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
smallIndex1 != smallIndex2
|
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Unequal hash and digest
|
|
|
|
check:
|
2022-02-17 10:00:45 +00:00
|
|
|
smallIndex1 != eqIndex1
|
2022-02-28 16:29:01 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
# Unequal pubsubTopic
|
|
|
|
check:
|
2022-02-28 16:29:01 +00:00
|
|
|
smallIndex1 != diffPsTopic
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
test "Index computation should not be empty":
|
|
|
|
## Given
|
|
|
|
let ts = getTestTimestamp()
|
|
|
|
let wm = WakuMessage(payload: @[byte 1, 2, 3], timestamp: ts)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let ts2 = getTestTimestamp() + 10
|
2022-08-01 16:21:11 +00:00
|
|
|
let index = Index.compute(wm, ts2, DefaultContentTopic)
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
index.digest.data.len != 0
|
|
|
|
index.digest.data.len == 32 # sha2 output length in bytes
|
|
|
|
index.receiverTime == ts2 # the receiver timestamp should be a non-zero value
|
|
|
|
index.senderTime == ts
|
2022-08-01 16:21:11 +00:00
|
|
|
index.pubsubTopic == DefaultContentTopic
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
test "Index digest of two identical messsage should be the same":
|
|
|
|
## Given
|
|
|
|
let topic = ContentTopic("test-content-topic")
|
|
|
|
let
|
|
|
|
wm1 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic)
|
|
|
|
wm2 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let ts = getTestTimestamp()
|
|
|
|
let
|
2022-08-01 16:21:11 +00:00
|
|
|
index1 = Index.compute(wm1, ts, DefaultPubsubTopic)
|
|
|
|
index2 = Index.compute(wm2, ts, DefaultPubsubTopic)
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
index1.digest == index2.digest
|
|
|
|
|