2022-09-09 13:54:16 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
stew/byteutils,
|
|
|
|
stew/shims/net as stewNet,
|
|
|
|
testutils/unittests,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/peerid,
|
|
|
|
libp2p/multiaddress,
|
|
|
|
libp2p/switch,
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
libp2p/protocols/pubsub/pubsub,
|
2022-09-20 09:39:52 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub
|
2022-09-09 13:54:16 +00:00
|
|
|
import
|
|
|
|
../../waku/v2/node/storage/sqlite,
|
2022-09-13 11:36:04 +00:00
|
|
|
../../waku/v2/node/storage/message/sqlite_store,
|
2022-09-09 13:54:16 +00:00
|
|
|
../../waku/v2/node/storage/message/waku_store_queue,
|
2022-09-20 09:39:52 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
2022-09-09 13:54:16 +00:00
|
|
|
../../waku/v2/protocol/waku_store,
|
|
|
|
../../waku/v2/protocol/waku_filter,
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
|
|
|
../../waku/v2/utils/peers,
|
|
|
|
../../waku/v2/utils/time,
|
2022-10-18 14:05:53 +00:00
|
|
|
../../waku/v2/node/waku_node
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
from std/times import getTime, toUnixFloat
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
DefaultPubsubTopic = "/waku/2/default-waku/proto"
|
|
|
|
DefaultContentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
|
|
|
|
proc now(): Timestamp =
|
|
|
|
getNanosecondTime(getTime().toUnixFloat())
|
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
proc newTestMessageStore(): MessageStore =
|
|
|
|
let database = SqliteDatabase.init("", inMemory = true)[]
|
|
|
|
SqliteStore.init(database).tryGet()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
proc fakeWakuMessage(
|
|
|
|
payload = "TEST-PAYLOAD",
|
|
|
|
contentTopic = DefaultContentTopic,
|
|
|
|
ts = now()
|
|
|
|
): WakuMessage =
|
|
|
|
WakuMessage(
|
|
|
|
payload: toBytes(payload),
|
|
|
|
contentTopic: contentTopic,
|
|
|
|
version: 1,
|
|
|
|
timestamp: ts
|
|
|
|
)
|
|
|
|
|
2022-09-09 13:54:16 +00:00
|
|
|
|
|
|
|
procSuite "WakuNode - Store":
|
2022-09-20 09:39:52 +00:00
|
|
|
let rng = crypto.newRng()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
|
|
|
asyncTest "Store protocol returns expected message":
|
2022-09-21 16:27:40 +00:00
|
|
|
## Setup
|
2022-09-09 13:54:16 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60000))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(client.start(), server.start())
|
|
|
|
await server.mountStore(store=newTestMessageStore())
|
|
|
|
await client.mountStore()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuStore.setPeer(server.peerInfo.toRemotePeerInfo())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-09-26 09:50:15 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, message).isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
|
|
|
let req = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)])
|
|
|
|
let queryRes = await client.query(req)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check queryRes.isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
let response = queryRes.get()
|
2022-09-09 13:54:16 +00:00
|
|
|
check:
|
2022-09-21 16:27:40 +00:00
|
|
|
response.messages == @[message]
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
# Cleanup
|
|
|
|
await allFutures(client.stop(), server.stop())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
asyncTest "Store protocol returns expected message when relay is disabled and filter enabled":
|
|
|
|
## See nwaku issue #937: 'Store: ability to decouple store from relay'
|
|
|
|
## Setup
|
2022-09-09 13:54:16 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
filterSourceKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
filterSource = WakuNode.new(filterSourceKey, ValidIpAddress.init("0.0.0.0"), Port(60004))
|
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60000))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(client.start(), server.start(), filterSource.start())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await filterSource.mountFilter()
|
|
|
|
await server.mountStore(store=newTestMessageStore())
|
|
|
|
await server.mountFilter()
|
|
|
|
await client.mountStore()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
server.wakuFilter.setPeer(filterSource.peerInfo.toRemotePeerInfo())
|
|
|
|
client.wakuStore.setPeer(server.peerInfo.toRemotePeerInfo())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Then
|
|
|
|
let filterFut = newFuture[bool]()
|
2022-09-09 13:54:16 +00:00
|
|
|
proc filterReqHandler(msg: WakuMessage) {.gcsafe, closure.} =
|
|
|
|
check:
|
|
|
|
msg == message
|
2022-09-21 16:27:40 +00:00
|
|
|
filterFut.complete(true)
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
let filterReq = FilterRequest(pubSubTopic: DefaultPubsubTopic, contentFilters: @[ContentFilter(contentTopic: DefaultContentTopic)], subscribe: true)
|
|
|
|
await server.subscribe(filterReq, filterReqHandler)
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await sleepAsync(100.millis)
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
# Send filter push message to server from source node
|
|
|
|
await filterSource.wakuFilter.handleMessage(DefaultPubsubTopic, message)
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
# Wait for the server filter to receive the push message
|
|
|
|
require (await filterFut.withTimeout(5.seconds))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
let res = await client.query(HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)]))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Then
|
|
|
|
check res.isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
let response = res.get()
|
2022-09-09 13:54:16 +00:00
|
|
|
check:
|
2022-09-21 16:27:40 +00:00
|
|
|
response.messages.len == 1
|
|
|
|
response.messages[0] == message
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(client.stop(), server.stop(), filterSource.stop())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
asyncTest "Resume proc fetches the history":
|
2022-09-21 16:27:40 +00:00
|
|
|
## Setup
|
2022-09-09 13:54:16 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60000))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(client.start(), server.start())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await server.mountStore(store=newTestMessageStore())
|
|
|
|
await client.mountStore(store=StoreQueueRef.new())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuStore.setPeer(server.peerInfo.toRemotePeerInfo())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-09-26 09:50:15 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, message).isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
|
|
|
await client.resume()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
# Then
|
2022-09-09 13:54:16 +00:00
|
|
|
check:
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuStore.store.getMessagesCount().tryGet() == 1
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(client.stop(), server.stop())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
asyncTest "Resume proc discards duplicate messages":
|
2022-09-21 16:27:40 +00:00
|
|
|
## Setup
|
2022-09-09 13:54:16 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60002))
|
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60000))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(server.start(), client.start())
|
2022-09-20 09:39:52 +00:00
|
|
|
await client.mountStore(store=StoreQueueRef.new())
|
|
|
|
await server.mountStore(store=StoreQueueRef.new())
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuStore.setPeer(server.peerInfo.toRemotePeerInfo())
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let timeOrigin = now()
|
|
|
|
let
|
|
|
|
msg1 = fakeWakuMessage(payload="hello world1", ts=(timeOrigin + getNanoSecondTime(1)))
|
|
|
|
msg2 = fakeWakuMessage(payload="hello world2", ts=(timeOrigin + getNanoSecondTime(2)))
|
|
|
|
msg3 = fakeWakuMessage(payload="hello world3", ts=(timeOrigin + getNanoSecondTime(3)))
|
|
|
|
|
2022-09-26 09:50:15 +00:00
|
|
|
require server.wakuStore.store.put(DefaultTopic, msg1).isOk()
|
|
|
|
require server.wakuStore.store.put(DefaultTopic, msg2).isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
# Insert the same message in both node's store
|
2022-09-26 09:50:15 +00:00
|
|
|
let
|
|
|
|
receivedTime3 = getNanosecondTime(getTime().toUnixFloat() + 10.float)
|
|
|
|
digest3 = computeDigest(msg3)
|
|
|
|
require server.wakuStore.store.put(DefaultTopic, msg3, digest3, receivedTime3).isOk()
|
|
|
|
require client.wakuStore.store.put(DefaultTopic, msg3, digest3, receivedTime3).isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
2022-09-20 09:39:52 +00:00
|
|
|
await client.resume()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Then
|
2022-09-09 13:54:16 +00:00
|
|
|
check:
|
2022-09-20 09:39:52 +00:00
|
|
|
# If the duplicates are discarded properly, then the total number of messages after resume should be 3
|
|
|
|
client.wakuStore.store.getMessagesCount().tryGet() == 3
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
await allFutures(client.stop(), server.stop())
|