2022-09-09 13:54:16 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
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
|
2022-10-28 18:13:41 +00:00
|
|
|
../../waku/common/sqlite,
|
2022-11-04 18:48:22 +00:00
|
|
|
../../waku/v2/node/message_store/sqlite_store,
|
2022-11-08 15:10:23 +00:00
|
|
|
../../waku/v2/node/message_store/queue_store,
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
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/utils/peers,
|
|
|
|
../../waku/v2/utils/time,
|
2022-10-21 13:01:39 +00:00
|
|
|
../../waku/v2/node/waku_node,
|
|
|
|
./testlib/common
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
proc newTestMessageStore(): MessageStore =
|
2022-10-27 22:05:02 +00:00
|
|
|
let database = SqliteDatabase.new(":memory:").tryGet()
|
2022-09-20 09:39:52 +00:00
|
|
|
SqliteStore.init(database).tryGet()
|
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[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60432))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60430))
|
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-10-28 18:11:28 +00:00
|
|
|
client.mountStoreClient()
|
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-10-28 18:11:28 +00:00
|
|
|
let serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
2022-11-09 17:50:18 +00:00
|
|
|
let req = HistoryQuery(contentTopics: @[DefaultContentTopic])
|
2022-10-28 18:11:28 +00:00
|
|
|
let queryRes = await client.query(req, peer=serverPeer)
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
## 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[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
filterSource = WakuNode.new(filterSourceKey, ValidIpAddress.init("0.0.0.0"), Port(60404))
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60402))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60400))
|
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())
|
2022-11-02 10:59:58 +00:00
|
|
|
await server.mountFilterClient()
|
2022-10-28 18:11:28 +00:00
|
|
|
client.mountStoreClient()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-11-02 10:59:58 +00:00
|
|
|
let
|
|
|
|
serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
filterSourcePeer = filterSource.peerInfo.toRemotePeerInfo()
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Then
|
2022-11-02 10:59:58 +00:00
|
|
|
let filterFut = newFuture[(PubsubTopic, WakuMessage)]()
|
|
|
|
proc filterHandler(pubsubTopic: PubsubTopic, msg: WakuMessage) {.gcsafe, closure.} =
|
|
|
|
filterFut.complete((pubsubTopic, msg))
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
await server.filterSubscribe(DefaultPubsubTopic, DefaultContentTopic, filterHandler, peer=filterSourcePeer)
|
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
|
2022-11-02 10:59:58 +00:00
|
|
|
require await filterFut.withTimeout(5.seconds)
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-11-09 17:50:18 +00:00
|
|
|
let res = await client.query(HistoryQuery(contentTopics: @[DefaultContentTopic]), peer=serverPeer)
|
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
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
let (handledPubsubTopic, handledMsg) = filterFut.read()
|
|
|
|
check:
|
|
|
|
handledPubsubTopic == DefaultPubsubTopic
|
|
|
|
handledMsg == message
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## 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[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60412))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60410))
|
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())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
|
|
|
let clientStore = StoreQueueRef.new()
|
|
|
|
await client.mountStore(store=clientStore)
|
|
|
|
client.mountStoreClient(store=clientStore)
|
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-10-28 18:11:28 +00:00
|
|
|
let serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
2022-10-28 18:11:28 +00:00
|
|
|
await client.resume(some(@[serverPeer]))
|
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[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60422))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-02 10:59:58 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60420))
|
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 server.mountStore(store=StoreQueueRef.new())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
|
|
|
let clientStore = StoreQueueRef.new()
|
|
|
|
await client.mountStore(store=clientStore)
|
|
|
|
client.mountStoreClient(store=clientStore)
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
## 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-11-09 08:55:47 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, msg1).isOk()
|
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, 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
|
2022-10-21 13:01:39 +00:00
|
|
|
receivedTime3 = now() + getNanosecondTime(10)
|
2022-09-26 09:50:15 +00:00
|
|
|
digest3 = computeDigest(msg3)
|
2022-11-09 08:55:47 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, msg3, digest3, receivedTime3).isOk()
|
|
|
|
require client.wakuStore.store.put(DefaultPubsubTopic, msg3, digest3, receivedTime3).isOk()
|
2022-09-09 13:54:16 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
let serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
2022-10-28 18:11:28 +00:00
|
|
|
await client.resume(some(@[serverPeer]))
|
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())
|