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
|
2022-08-02 11:14:13 +00:00
|
|
|
std/[options, tables, sets, sequtils, times],
|
|
|
|
stew/byteutils,
|
|
|
|
testutils/unittests,
|
2022-07-25 11:01:37 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2020-08-31 03:32:41 +00:00
|
|
|
libp2p/switch,
|
2022-08-02 11:14:13 +00:00
|
|
|
libp2p/crypto/crypto
|
2022-07-25 11:01:37 +00:00
|
|
|
import
|
2021-07-13 07:18:51 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
2022-07-25 11:01:37 +00:00
|
|
|
../../waku/v2/protocol/waku_store,
|
|
|
|
../../waku/v2/node/storage/message/waku_store_queue,
|
2022-08-02 11:14:13 +00:00
|
|
|
../../waku/v2/node/storage/message/waku_message_store,
|
2021-03-26 08:49:51 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-07-25 11:01:37 +00:00
|
|
|
../../waku/v2/utils/pagination,
|
2022-02-17 15:00:15 +00:00
|
|
|
../../waku/v2/utils/time,
|
2022-08-02 11:14:13 +00:00
|
|
|
../test_helpers
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
const
|
|
|
|
DefaultPubsubTopic = "/waku/2/default-waku/proto"
|
|
|
|
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
proc newTestDatabase(): SqliteDatabase =
|
|
|
|
SqliteDatabase.init("", inMemory = true).tryGet()
|
2020-09-24 02:16:25 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
proc fakeWakuMessage(
|
|
|
|
payload = "TEST-PAYLOAD",
|
|
|
|
contentTopic = DefaultContentTopic,
|
|
|
|
ts = getNanosecondTime(epochTime())
|
|
|
|
): WakuMessage =
|
|
|
|
WakuMessage(
|
|
|
|
payload: toBytes(payload),
|
|
|
|
contentTopic: contentTopic,
|
|
|
|
version: 1,
|
|
|
|
timestamp: ts
|
|
|
|
)
|
2020-09-24 02:16:25 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
proc newTestSwitch(key=none(PrivateKey), address=none(MultiAddress)): Switch =
|
|
|
|
let peerKey = key.get(PrivateKey.random(ECDSA, rng[]).get())
|
|
|
|
let peerAddr = address.get(MultiAddress.init("/ip4/127.0.0.1/tcp/0").get())
|
|
|
|
return newStandardSwitch(some(peerKey), addrs=peerAddr)
|
2020-09-24 02:16:25 +00:00
|
|
|
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
proc newTestWakuStore(switch: Switch): WakuStore =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
rng = crypto.newRng()
|
|
|
|
database = newTestDatabase()
|
|
|
|
store = WakuMessageStore.init(database).tryGet()
|
|
|
|
proto = WakuStore.init(peerManager, rng, store)
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
switch.mount(proto)
|
2020-08-27 02:44:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
return proto
|
2022-01-25 02:31:14 +00:00
|
|
|
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
suite "Waku Store":
|
|
|
|
|
|
|
|
asyncTest "handle query":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
2021-04-19 17:38:30 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
2021-04-19 17:38:30 +00:00
|
|
|
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
|
|
|
let topic = ContentTopic("1")
|
2021-04-19 17:38:30 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
msg1 = fakeWakuMessage(contentTopic=topic)
|
|
|
|
msg2 = fakeWakuMessage()
|
2021-04-19 17:38:30 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await serverProto.handleMessage("foo", msg1)
|
|
|
|
await serverProto.handleMessage("foo", msg2)
|
2021-04-19 17:38:30 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: topic)])
|
|
|
|
let resQuery = await clientProto.query(rpc)
|
2021-04-19 17:38:30 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
resQuery.isOk()
|
2021-04-19 17:38:30 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = resQuery.tryGet()
|
2020-08-27 02:44:09 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len == 1
|
|
|
|
response.messages[0] == msg1
|
2022-01-25 02:31:14 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(serverSwitch.stop(), clientSwitch.stop())
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "handle query with multiple content filters":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
2021-04-27 23:52:24 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
topic1 = ContentTopic("1")
|
|
|
|
topic2 = ContentTopic("2")
|
|
|
|
topic3 = ContentTopic("3")
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let
|
|
|
|
msg1 = fakeWakuMessage(contentTopic=topic1)
|
|
|
|
msg2 = fakeWakuMessage(contentTopic=topic2)
|
|
|
|
msg3 = fakeWakuMessage(contentTopic=topic3)
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await serverProto.handleMessage("foo", msg1)
|
|
|
|
await serverProto.handleMessage("foo", msg2)
|
|
|
|
await serverProto.handleMessage("foo", msg3)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(contentFilters: @[
|
|
|
|
HistoryContentFilter(contentTopic: topic1),
|
|
|
|
HistoryContentFilter(contentTopic: topic3)
|
|
|
|
])
|
|
|
|
let resQuery = await clientProto.query(rpc)
|
|
|
|
|
|
|
|
## Then
|
2021-04-27 23:52:24 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
resQuery.isOk()
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = resQuery.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 2
|
|
|
|
response.messages.anyIt(it == msg1)
|
|
|
|
response.messages.anyIt(it == msg3)
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "handle query with pubsub topic filter":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
2021-04-27 23:52:24 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
pubsubTopic1 = "queried-topic"
|
|
|
|
pubsubTopic2 = "non-queried-topic"
|
|
|
|
|
2021-04-27 23:52:24 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
contentTopic1 = ContentTopic("1")
|
|
|
|
contentTopic2 = ContentTopic("2")
|
|
|
|
contentTopic3 = ContentTopic("3")
|
2021-04-27 23:52:24 +00:00
|
|
|
|
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
msg1 = fakeWakuMessage(contentTopic=contentTopic1)
|
|
|
|
msg2 = fakeWakuMessage(contentTopic=contentTopic2)
|
|
|
|
msg3 = fakeWakuMessage(contentTopic=contentTopic3)
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await serverProto.handleMessage(pubsubtopic1, msg1)
|
|
|
|
await serverProto.handleMessage(pubsubtopic2, msg2)
|
|
|
|
await serverProto.handleMessage(pubsubtopic2, msg3)
|
|
|
|
|
|
|
|
## When
|
|
|
|
# this query targets: pubsubtopic1 AND (contentTopic1 OR contentTopic3)
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: contentTopic1),
|
|
|
|
HistoryContentFilter(contentTopic: contentTopic3)],
|
|
|
|
pubsubTopic: pubsubTopic1
|
|
|
|
)
|
|
|
|
let resQuery = await clientProto.query(rpc)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
resQuery.isOk()
|
2021-04-27 23:52:24 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = resQuery.tryGet()
|
2021-04-27 23:52:24 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len() == 1
|
|
|
|
response.messages.anyIt(it == msg1)
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2022-01-25 02:31:14 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "handle query with pubsub topic filter - no match":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
2020-11-16 08:38:52 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
pubsubtopic1 = "queried-topic"
|
|
|
|
pubsubtopic2 = "non-queried-topic"
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let
|
|
|
|
msg1 = fakeWakuMessage()
|
|
|
|
msg2 = fakeWakuMessage()
|
|
|
|
msg3 = fakeWakuMessage()
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await serverProto.handleMessage(pubsubtopic2, msg1)
|
|
|
|
await serverProto.handleMessage(pubsubtopic2, msg2)
|
|
|
|
await serverProto.handleMessage(pubsubtopic2, msg3)
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(pubsubTopic: pubsubTopic1)
|
|
|
|
let res = await clientProto.query(rpc)
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
2020-11-16 08:38:52 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
res.isOk()
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 0
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2020-11-16 08:38:52 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "handle query with pubsub topic filter - match the entire stored messages":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
2022-01-25 02:31:14 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
|
|
|
let pubsubTopic = "queried-topic"
|
|
|
|
|
2020-11-09 04:48:09 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
msg1 = fakeWakuMessage(payload="TEST-1")
|
|
|
|
msg2 = fakeWakuMessage(payload="TEST-2")
|
|
|
|
msg3 = fakeWakuMessage(payload="TEST-3")
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
await serverProto.handleMessage(pubsubTopic, msg1)
|
|
|
|
await serverProto.handleMessage(pubsubTopic, msg2)
|
|
|
|
await serverProto.handleMessage(pubsubTopic, msg3)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(pubsubTopic: pubsubTopic)
|
|
|
|
let res = await clientProto.query(rpc)
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 3
|
|
|
|
response.messages.anyIt(it == msg1)
|
|
|
|
response.messages.anyIt(it == msg2)
|
|
|
|
response.messages.anyIt(it == msg3)
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "handle query with forward pagination":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
|
|
|
|
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let msgList = @[
|
|
|
|
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2")),
|
|
|
|
WakuMessage(payload: @[byte 1], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 2], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 3], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 4], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 5], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 6], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 7], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 8], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 9], contentTopic: ContentTopic("2"))
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
|
|
|
await serverProto.handleMessage("foo", msg)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)],
|
|
|
|
pagingInfo: PagingInfo(pageSize: 2, direction: PagingDirection.FORWARD)
|
|
|
|
)
|
|
|
|
let res = await clientProto.query(rpc)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
2020-11-09 04:48:09 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len() == 2
|
|
|
|
response.pagingInfo.pageSize == 2
|
|
|
|
response.pagingInfo.direction == PagingDirection.FORWARD
|
|
|
|
response.pagingInfo.cursor != Index()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2022-01-25 02:31:14 +00:00
|
|
|
|
2020-11-09 04:48:09 +00:00
|
|
|
asyncTest "handle query with backward pagination":
|
2022-08-02 11:14:13 +00:00
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
|
|
|
|
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let msgList = @[
|
|
|
|
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2")),
|
|
|
|
WakuMessage(payload: @[byte 1], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 2], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 3], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 4], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 5], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 6], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 7], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 8], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 9], contentTopic: ContentTopic("2"))
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
|
|
|
await serverProto.handleMessage("foo", msg)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)],
|
|
|
|
pagingInfo: PagingInfo(pageSize: 2, direction: PagingDirection.BACKWARD)
|
|
|
|
)
|
|
|
|
let res = await clientProto.query(rpc)
|
|
|
|
|
|
|
|
## Then
|
2020-11-09 04:48:09 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
res.isOk()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 2
|
|
|
|
response.pagingInfo.pageSize == 2
|
|
|
|
response.pagingInfo.direction == PagingDirection.BACKWARD
|
|
|
|
response.pagingInfo.cursor != Index()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "handle query with no paging info - auto-pagination":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
|
|
|
|
|
|
|
let
|
|
|
|
serverProto = newTestWakuStore(serverSwitch)
|
|
|
|
clientProto = newTestWakuStore(clientSwitch)
|
|
|
|
|
|
|
|
clientProto.setPeer(serverSwitch.peerInfo.toRemotePeerInfo())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let msgList = @[
|
|
|
|
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2")),
|
|
|
|
WakuMessage(payload: @[byte 1], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 2], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 3], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 4], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 5], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 6], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 7], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 8], contentTopic: DefaultContentTopic),
|
|
|
|
WakuMessage(payload: @[byte 9], contentTopic: ContentTopic("2"))
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
|
|
|
await serverProto.handleMessage("foo", msg)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let rpc = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)])
|
|
|
|
let res = await clientProto.query(rpc)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
## No pagination specified. Response will be auto-paginated with
|
|
|
|
## up to MaxPageSize messages per page.
|
|
|
|
response.messages.len() == 8
|
|
|
|
response.pagingInfo.pageSize == 8
|
|
|
|
response.pagingInfo.direction == PagingDirection.BACKWARD
|
|
|
|
response.pagingInfo.cursor != Index()
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
2020-11-09 04:48:09 +00:00
|
|
|
|
2020-10-30 01:55:31 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
# TODO: Review this test suite test cases
|
|
|
|
procSuite "Waku Store - fault tolerant store":
|
2022-01-25 02:31:14 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
proc newTestWakuStore(peer=none(RemotePeerInfo)): Future[(Switch, Switch, WakuStore)] {.async.} =
|
2021-04-09 23:40:34 +00:00
|
|
|
let
|
|
|
|
key = PrivateKey.random(ECDSA, rng[]).get()
|
2022-08-02 11:14:13 +00:00
|
|
|
listenSwitch = newStandardSwitch(some(key))
|
2022-01-14 09:25:01 +00:00
|
|
|
await listenSwitch.start()
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let dialSwitch = newStandardSwitch()
|
|
|
|
await dialSwitch.start()
|
|
|
|
|
2022-06-13 17:59:53 +00:00
|
|
|
let
|
2022-08-02 11:14:13 +00:00
|
|
|
peerManager = PeerManager.new(dialsWitch)
|
|
|
|
rng = crypto.newRng()
|
|
|
|
database = newTestDatabase()
|
|
|
|
store = WakuMessageStore.init(database).tryGet()
|
|
|
|
proto = WakuStore.init(peerManager, rng, store)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let storePeer = peer.get(listenSwitch.peerInfo.toRemotePeerInfo())
|
|
|
|
proto.setPeer(storePeer)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
|
|
|
listenSwitch.mount(proto)
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
return (listenSwitch, dialSwitch, proto)
|
2022-01-18 22:05:41 +00:00
|
|
|
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "temporal history queries":
|
|
|
|
## Setup
|
|
|
|
let (listenSwitch, dialSwitch, proto) = await newTestWakuStore()
|
|
|
|
let msgList = @[
|
|
|
|
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2"), timestamp: Timestamp(0)),
|
|
|
|
WakuMessage(payload: @[byte 1], contentTopic: ContentTopic("1"), timestamp: Timestamp(1)),
|
|
|
|
WakuMessage(payload: @[byte 2], contentTopic: ContentTopic("2"), timestamp: Timestamp(2)),
|
|
|
|
WakuMessage(payload: @[byte 3], contentTopic: ContentTopic("1"), timestamp: Timestamp(3)),
|
|
|
|
WakuMessage(payload: @[byte 4], contentTopic: ContentTopic("2"), timestamp: Timestamp(4)),
|
|
|
|
WakuMessage(payload: @[byte 5], contentTopic: ContentTopic("1"), timestamp: Timestamp(5)),
|
|
|
|
WakuMessage(payload: @[byte 6], contentTopic: ContentTopic("2"), timestamp: Timestamp(6)),
|
|
|
|
WakuMessage(payload: @[byte 7], contentTopic: ContentTopic("1"), timestamp: Timestamp(7)),
|
|
|
|
WakuMessage(payload: @[byte 8], contentTopic: ContentTopic("2"), timestamp: Timestamp(8)),
|
|
|
|
WakuMessage(payload: @[byte 9], contentTopic: ContentTopic("1"), timestamp: Timestamp(9))
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
2022-08-12 10:15:51 +00:00
|
|
|
await proto.handleMessage(DefaultPubsubTopic, msg)
|
2022-08-02 11:14:13 +00:00
|
|
|
|
|
|
|
let (listenSwitch2, dialSwitch2, proto2) = await newTestWakuStore()
|
|
|
|
let msgList2 = @[
|
|
|
|
WakuMessage(payload: @[byte 0], contentTopic: ContentTopic("2"), timestamp: Timestamp(0)),
|
|
|
|
WakuMessage(payload: @[byte 11], contentTopic: ContentTopic("1"), timestamp: Timestamp(1)),
|
|
|
|
WakuMessage(payload: @[byte 12], contentTopic: ContentTopic("2"), timestamp: Timestamp(2)),
|
|
|
|
WakuMessage(payload: @[byte 3], contentTopic: ContentTopic("1"), timestamp: Timestamp(3)),
|
|
|
|
WakuMessage(payload: @[byte 4], contentTopic: ContentTopic("2"), timestamp: Timestamp(4)),
|
|
|
|
WakuMessage(payload: @[byte 5], contentTopic: ContentTopic("1"), timestamp: Timestamp(5)),
|
|
|
|
WakuMessage(payload: @[byte 13], contentTopic: ContentTopic("2"), timestamp: Timestamp(6)),
|
|
|
|
WakuMessage(payload: @[byte 14], contentTopic: ContentTopic("1"), timestamp: Timestamp(7))
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList2:
|
2022-08-12 10:15:51 +00:00
|
|
|
await proto2.handleMessage(DefaultPubsubTopic, msg)
|
2022-01-18 22:05:41 +00:00
|
|
|
|
2021-04-09 23:40:34 +00:00
|
|
|
|
|
|
|
asyncTest "handle temporal history query with a valid time window":
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: ContentTopic("1"))],
|
|
|
|
startTime: Timestamp(2),
|
|
|
|
endTime: Timestamp(5)
|
|
|
|
)
|
|
|
|
|
|
|
|
## When
|
|
|
|
let res = await proto.query(rpc)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check res.isOk()
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
2021-04-09 23:40:34 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len() == 2
|
|
|
|
response.messages.anyIt(it.timestamp == Timestamp(3))
|
|
|
|
response.messages.anyIt(it.timestamp == Timestamp(5))
|
|
|
|
|
2021-04-09 23:40:34 +00:00
|
|
|
asyncTest "handle temporal history query with a zero-size time window":
|
|
|
|
# a zero-size window results in an empty list of history messages
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: ContentTopic("1"))],
|
|
|
|
startTime: Timestamp(2),
|
|
|
|
endTime: Timestamp(2)
|
|
|
|
)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let res = await proto.query(rpc)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check res.isOk()
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
2021-04-09 23:40:34 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len == 0
|
2021-04-09 23:40:34 +00:00
|
|
|
|
|
|
|
asyncTest "handle temporal history query with an invalid time window":
|
2022-08-02 11:14:13 +00:00
|
|
|
# A history query with an invalid time range results in an empty list of history messages
|
|
|
|
## Given
|
|
|
|
let rpc = HistoryQuery(
|
|
|
|
contentFilters: @[HistoryContentFilter(contentTopic: ContentTopic("1"))],
|
|
|
|
startTime: Timestamp(5),
|
|
|
|
endTime: Timestamp(2)
|
|
|
|
)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let res = await proto.query(rpc)
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check res.isOk()
|
2021-04-09 23:40:34 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
2021-04-09 23:40:34 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.messages.len == 0
|
2021-05-13 21:21:46 +00:00
|
|
|
|
|
|
|
asyncTest "resume message history":
|
2022-08-02 11:14:13 +00:00
|
|
|
## Given
|
|
|
|
# Start a new node
|
|
|
|
let (listenSwitch3, dialSwitch3, proto3) = await newTestWakuStore(peer=some(listenSwitch.peerInfo.toRemotePeerInfo()))
|
2021-05-19 19:28:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
2022-01-25 02:31:14 +00:00
|
|
|
let successResult = await proto3.resume()
|
2022-08-02 11:14:13 +00:00
|
|
|
|
|
|
|
## Then
|
2021-05-19 19:28:09 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
successResult.isOk()
|
2021-05-19 19:28:09 +00:00
|
|
|
successResult.value == 10
|
2022-01-25 02:31:14 +00:00
|
|
|
proto3.messages.len == 10
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(dialSwitch3.stop(), listenSwitch3.stop())
|
2021-07-02 18:37:58 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "queryFromWithPaging - no pagingInfo":
|
|
|
|
## Given
|
2022-02-17 15:00:15 +00:00
|
|
|
let rpc = HistoryQuery(startTime: Timestamp(2), endTime: Timestamp(5))
|
2021-07-02 18:37:58 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let res = await proto.queryFromWithPaging(rpc, listenSwitch.peerInfo.toRemotePeerInfo())
|
2021-07-02 18:37:58 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
check res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
2021-07-02 18:37:58 +00:00
|
|
|
check:
|
2022-08-02 11:14:13 +00:00
|
|
|
response.len == 4
|
2021-07-02 18:37:58 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
asyncTest "queryFromWithPaging - with pagination":
|
2021-07-02 18:37:58 +00:00
|
|
|
var pinfo = PagingInfo(direction:PagingDirection.FORWARD, pageSize: 1)
|
2022-02-17 15:00:15 +00:00
|
|
|
let rpc = HistoryQuery(startTime: Timestamp(2), endTime: Timestamp(5), pagingInfo: pinfo)
|
2021-07-02 18:37:58 +00:00
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
let messagesResult = await proto.queryFromWithPaging(rpc, listenSwitch.peerInfo.toRemotePeerInfo())
|
2021-07-02 18:37:58 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
messagesResult.isOk
|
|
|
|
messagesResult.value.len == 4
|
2021-05-19 19:28:09 +00:00
|
|
|
|
2022-01-18 22:05:41 +00:00
|
|
|
asyncTest "resume history from a list of offline peers":
|
|
|
|
var offListenSwitch = newStandardSwitch(some(PrivateKey.random(ECDSA, rng[]).get()))
|
|
|
|
var dialSwitch3 = newStandardSwitch()
|
|
|
|
await dialSwitch3.start()
|
|
|
|
let proto3 = WakuStore.init(PeerManager.new(dialSwitch3), crypto.newRng())
|
|
|
|
let successResult = await proto3.resume(some(@[offListenSwitch.peerInfo.toRemotePeerInfo()]))
|
|
|
|
check:
|
|
|
|
successResult.isErr
|
2022-01-25 02:31:14 +00:00
|
|
|
|
|
|
|
#free resources
|
|
|
|
await allFutures(dialSwitch3.stop(),
|
|
|
|
offListenSwitch.stop())
|
2022-01-18 22:05:41 +00:00
|
|
|
|
2021-05-19 19:28:09 +00:00
|
|
|
asyncTest "resume history from a list of candidate peers":
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let offListenSwitch = newStandardSwitch(some(PrivateKey.random(ECDSA, rng[]).get()))
|
|
|
|
let (listenSwitch3, dialSwitch3, proto3) = await newTestWakuStore()
|
2021-05-19 19:28:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## When
|
|
|
|
let res = await proto3.resume(some(@[
|
|
|
|
offListenSwitch.peerInfo.toRemotePeerInfo(),
|
|
|
|
listenSwitch.peerInfo.toRemotePeerInfo(),
|
|
|
|
listenSwitch2.peerInfo.toRemotePeerInfo()
|
|
|
|
]))
|
2022-06-13 17:59:53 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Then
|
|
|
|
# `proto3` is expected to retrieve 14 messages because:
|
|
|
|
# - the store mounted on `listenSwitch` holds 10 messages (`msgList`)
|
|
|
|
# - the store mounted on `listenSwitch2` holds 7 messages (see `msgList2`)
|
|
|
|
# - both stores share 3 messages, resulting in 14 unique messages in total
|
|
|
|
check res.isOk()
|
2021-05-19 19:28:09 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response == 14
|
|
|
|
|
2021-05-19 19:28:09 +00:00
|
|
|
check:
|
2022-01-18 22:05:41 +00:00
|
|
|
proto3.messages.len == 14
|
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(listenSwitch3.stop(), dialSwitch3.stop(), offListenSwitch.stop())
|
2021-11-03 10:59:51 +00:00
|
|
|
|
2022-08-02 11:14:13 +00:00
|
|
|
## Cleanup
|
|
|
|
await allFutures(dialSwitch.stop(), dialSwitch2.stop(), listenSwitch.stop(), listenSwitch2.stop())
|