2020-08-31 03:32:41 +00:00
|
|
|
{.used.}
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-08-31 03:32:41 +00:00
|
|
|
import
|
|
|
|
std/[unittest, options, tables, sets],
|
|
|
|
chronos, chronicles,
|
|
|
|
libp2p/switch,
|
|
|
|
libp2p/protobuf/minprotobuf,
|
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/pubsub/rpc/message,
|
|
|
|
libp2p/multistream,
|
|
|
|
libp2p/transports/transport,
|
|
|
|
libp2p/transports/tcptransport,
|
2020-09-07 11:26:32 +00:00
|
|
|
../../waku/protocol/v2/[waku_store, message_notifier],
|
2020-09-16 02:59:10 +00:00
|
|
|
../../waku/node/v2/waku_types,
|
2020-08-31 03:32:41 +00:00
|
|
|
../test_helpers, ./utils
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
procSuite "Waku Store":
|
|
|
|
asyncTest "handle query":
|
|
|
|
let
|
2020-09-17 20:10:41 +00:00
|
|
|
key = PrivateKey.random(ECDSA, rng[]).get()
|
|
|
|
peer = PeerInfo.init(key)
|
2020-10-21 08:55:06 +00:00
|
|
|
topic = ContentTopic(1)
|
|
|
|
msg = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic)
|
|
|
|
msg2 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: ContentTopic(2))
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-17 20:10:41 +00:00
|
|
|
var dialSwitch = newStandardSwitch()
|
|
|
|
discard await dialSwitch.start()
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-17 20:10:41 +00:00
|
|
|
var listenSwitch = newStandardSwitch(some(key))
|
|
|
|
discard await listenSwitch.start()
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-24 02:16:25 +00:00
|
|
|
let
|
2020-09-28 21:44:14 +00:00
|
|
|
proto = WakuStore.init(dialSwitch, crypto.newRng())
|
2020-09-24 02:16:25 +00:00
|
|
|
subscription = proto.subscription()
|
2020-10-21 08:55:06 +00:00
|
|
|
rpc = HistoryQuery(topics: @[topic])
|
2020-09-24 02:16:25 +00:00
|
|
|
|
|
|
|
proto.setPeer(listenSwitch.peerInfo)
|
|
|
|
|
|
|
|
var subscriptions = newTable[string, MessageNotificationSubscription]()
|
|
|
|
subscriptions["test"] = subscription
|
|
|
|
|
|
|
|
listenSwitch.mount(proto)
|
|
|
|
|
2020-09-17 20:10:41 +00:00
|
|
|
await subscriptions.notify("foo", msg)
|
|
|
|
await subscriptions.notify("foo", msg2)
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-24 02:16:25 +00:00
|
|
|
var completionFut = newFuture[bool]()
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-24 02:16:25 +00:00
|
|
|
proc handler(response: HistoryResponse) {.gcsafe, closure.} =
|
|
|
|
check:
|
|
|
|
response.messages.len() == 1
|
|
|
|
response.messages[0] == msg
|
|
|
|
completionFut.complete(true)
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2020-09-24 02:16:25 +00:00
|
|
|
await proto.query(rpc, handler)
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
check:
|
2020-09-24 02:16:25 +00:00
|
|
|
(await completionFut.withTimeout(5.seconds)) == true
|
2020-10-30 01:55:31 +00:00
|
|
|
|
|
|
|
test "Index Protobuf encoder/decoder test":
|
|
|
|
let
|
|
|
|
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
|
|
|
|
pb = index.encode()
|
|
|
|
decodedIndex = Index.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# the fields of decodedIndex must be the same as the original index
|
|
|
|
decodedIndex.isErr == false
|
|
|
|
decodedIndex.value == index
|
|
|
|
|
|
|
|
let
|
|
|
|
emptyIndex = Index()
|
|
|
|
epb = emptyIndex.encode()
|
|
|
|
decodedEmptyIndex = Index.init(epb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# check the correctness of init and encode for an empty Index
|
|
|
|
decodedEmptyIndex.isErr == false
|
|
|
|
decodedEmptyIndex.value == emptyIndex
|
|
|
|
|
|
|
|
|
|
|
|
test "PagingDirection Protobuf encod/init test":
|
|
|
|
let
|
|
|
|
pagingDirection = PagingDirection.BACKWARD
|
|
|
|
pb = pagingDirection.encode()
|
|
|
|
decodedPagingDirection = PagingDirection.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# the decodedPagingDirection must be the same as the original pagingDirection
|
|
|
|
decodedPagingDirection.isErr == false
|
|
|
|
decodedPagingDirection.value == pagingDirection
|
|
|
|
|
|
|
|
test "PagingInfo Protobuf encod/init test":
|
|
|
|
let
|
|
|
|
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
|
|
|
|
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
|
|
|
|
pb = pagingInfo.encode()
|
|
|
|
decodedPagingInfo = PagingInfo.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# the fields of decodedPagingInfo must be the same as the original pagingInfo
|
|
|
|
decodedPagingInfo.isErr == false
|
|
|
|
decodedPagingInfo.value == pagingInfo
|
|
|
|
|
|
|
|
let
|
|
|
|
emptyPagingInfo = PagingInfo()
|
|
|
|
epb = emptyPagingInfo.encode()
|
|
|
|
decodedEmptyPagingInfo = PagingInfo.init(epb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# check the correctness of init and encode for an empty PagingInfo
|
|
|
|
decodedEmptyPagingInfo.isErr == false
|
|
|
|
decodedEmptyPagingInfo.value == emptyPagingInfo
|
|
|
|
|
|
|
|
test "HistoryQuery Protobuf encod/init test":
|
|
|
|
let
|
|
|
|
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1)))
|
|
|
|
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
|
|
|
|
query=HistoryQuery(topics: @[ContentTopic(1)], pagingInfo: pagingInfo)
|
|
|
|
pb = query.encode()
|
|
|
|
decodedQuery = HistoryQuery.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# the fields of decoded query decodedQuery must be the same as the original query query
|
|
|
|
decodedQuery.isErr == false
|
|
|
|
decodedQuery.value == query
|
|
|
|
|
|
|
|
let
|
|
|
|
emptyQuery=HistoryQuery()
|
|
|
|
epb = emptyQuery.encode()
|
|
|
|
decodedEmptyQuery = HistoryQuery.init(epb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# check the correctness of init and encode for an empty HistoryQuery
|
|
|
|
decodedEmptyQuery.isErr == false
|
|
|
|
decodedEmptyQuery.value == emptyQuery
|
|
|
|
|
|
|
|
test "HistoryResponse Protobuf encod/init test":
|
|
|
|
let
|
|
|
|
wm = WakuMessage(payload: @[byte 1], contentTopic: ContentTopic(1))
|
|
|
|
index = computeIndex(wm)
|
|
|
|
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
|
|
|
|
res = HistoryResponse(messages: @[wm], pagingInfo:pagingInfo)
|
|
|
|
pb = res.encode()
|
|
|
|
decodedRes = HistoryResponse.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# the fields of decoded response decodedRes must be the same as the original response res
|
|
|
|
decodedRes.isErr == false
|
|
|
|
decodedRes.value == res
|
|
|
|
|
|
|
|
let
|
|
|
|
emptyRes=HistoryResponse()
|
|
|
|
epb = emptyRes.encode()
|
|
|
|
decodedEmptyRes = HistoryResponse.init(epb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# check the correctness of init and encode for an empty HistoryResponse
|
|
|
|
decodedEmptyRes.isErr == false
|
|
|
|
decodedEmptyRes.value == emptyRes
|
|
|
|
|