2023-02-10 09:43:16 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-04-12 07:26:26 +00:00
|
|
|
std/[options, times, json],
|
2023-02-10 09:43:16 +00:00
|
|
|
stew/shims/net as stewNet,
|
|
|
|
testutils/unittests,
|
|
|
|
eth/keys,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
json_rpc/[rpcserver, rpcclient]
|
|
|
|
import
|
2023-04-19 14:39:52 +00:00
|
|
|
../../../waku/v2/waku_core,
|
2023-04-24 14:37:54 +00:00
|
|
|
../../../waku/v2/node/peer_manager,
|
2023-04-05 09:58:59 +00:00
|
|
|
../../../waku/v2/waku_node,
|
2023-02-10 09:43:16 +00:00
|
|
|
../../../waku/v2/node/jsonrpc/store/handlers as store_api,
|
|
|
|
../../../waku/v2/node/jsonrpc/store/client as store_api_client,
|
2023-04-18 13:22:10 +00:00
|
|
|
../../../waku/v2/waku_archive,
|
|
|
|
../../../waku/v2/waku_archive/driver/queue_driver,
|
|
|
|
../../../waku/v2/waku_store,
|
|
|
|
../../../waku/v2/waku_store/rpc,
|
2023-02-13 10:43:49 +00:00
|
|
|
../../v2/testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
../../v2/testlib/wakucore,
|
|
|
|
../../v2/testlib/wakunode
|
2023-02-10 09:43:16 +00:00
|
|
|
|
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
proc put(store: ArchiveDriver, pubsubTopic: PubsubTopic, message: WakuMessage): Future[Result[void, string]] =
|
2023-02-10 09:43:16 +00:00
|
|
|
let
|
|
|
|
digest = waku_archive.computeDigest(message)
|
|
|
|
receivedTime = if message.timestamp > 0: message.timestamp
|
|
|
|
else: getNanosecondTime(getTime().toUnixFloat())
|
|
|
|
|
|
|
|
store.put(pubsubTopic, message, digest, receivedTime)
|
|
|
|
|
|
|
|
procSuite "Waku v2 JSON-RPC API - Store":
|
|
|
|
|
|
|
|
asyncTest "query a node and retrieve historical messages":
|
2023-04-12 07:26:26 +00:00
|
|
|
let
|
|
|
|
privkey = generateSecp256k1Key()
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
|
|
|
port = Port(0)
|
|
|
|
node = newTestWakuNode(privkey, bindIp, port, some(extIp), some(port))
|
2023-02-10 09:43:16 +00:00
|
|
|
|
2023-04-12 07:26:26 +00:00
|
|
|
await node.start()
|
2023-02-10 09:43:16 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
let
|
|
|
|
rpcPort = Port(8549)
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
|
|
installStoreApiHandlers(node, server)
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
# WakuStore setup
|
|
|
|
let
|
2023-02-13 10:43:49 +00:00
|
|
|
key = generateEcdsaKey()
|
2023-02-10 09:43:16 +00:00
|
|
|
peer = PeerInfo.new(key)
|
|
|
|
|
|
|
|
let driver: ArchiveDriver = QueueDriver.new()
|
2023-06-27 11:24:31 +00:00
|
|
|
let mountArchiveRes = node.mountArchive(driver)
|
|
|
|
assert mountArchiveRes.isOk(), mountArchiveRes.error
|
|
|
|
|
2023-02-10 09:43:16 +00:00
|
|
|
await node.mountStore()
|
|
|
|
node.mountStoreClient()
|
|
|
|
|
|
|
|
var listenSwitch = newStandardSwitch(some(key))
|
|
|
|
await listenSwitch.start()
|
|
|
|
|
2023-02-27 17:24:31 +00:00
|
|
|
node.peerManager.addServicePeer(listenSwitch.peerInfo.toRemotePeerInfo(), WakuStoreCodec)
|
2023-02-10 09:43:16 +00:00
|
|
|
|
|
|
|
listenSwitch.mount(node.wakuStore)
|
|
|
|
|
|
|
|
# Now prime it with some history before tests
|
|
|
|
let msgList = @[
|
|
|
|
fakeWakuMessage(@[byte 0], contentTopic=ContentTopic("2"), ts=0),
|
|
|
|
fakeWakuMessage(@[byte 1], ts=1),
|
|
|
|
fakeWakuMessage(@[byte 2], ts=2),
|
|
|
|
fakeWakuMessage(@[byte 3], ts=3),
|
|
|
|
fakeWakuMessage(@[byte 4], ts=4),
|
|
|
|
fakeWakuMessage(@[byte 5], ts=5),
|
|
|
|
fakeWakuMessage(@[byte 6], ts=6),
|
|
|
|
fakeWakuMessage(@[byte 7], ts=7),
|
|
|
|
fakeWakuMessage(@[byte 8], ts=8),
|
|
|
|
fakeWakuMessage(@[byte 9], contentTopic=ContentTopic("2"), ts=9)
|
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
2023-05-25 15:34:34 +00:00
|
|
|
require (waitFor driver.put(DefaultPubsubTopic, msg)).isOk()
|
2023-02-10 09:43:16 +00:00
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
|
|
|
|
2023-04-12 07:26:26 +00:00
|
|
|
let response = await client.get_waku_v2_store_v1_messages(
|
|
|
|
some(DefaultPubsubTopic),
|
|
|
|
some(@[HistoryContentFilterRPC(contentTopic: DefaultContentTopic)]),
|
|
|
|
some(Timestamp(0)),
|
|
|
|
some(Timestamp(9)),
|
|
|
|
some(StorePagingOptions()))
|
2023-02-10 09:43:16 +00:00
|
|
|
check:
|
2023-04-12 07:26:26 +00:00
|
|
|
response.messages.len == 8
|
2023-02-10 09:43:16 +00:00
|
|
|
response.pagingOptions.isNone()
|
|
|
|
|
|
|
|
await server.stop()
|
|
|
|
await server.closeWait()
|
2023-04-12 07:26:26 +00:00
|
|
|
await node.stop()
|
|
|
|
await listenSwitch.stop()
|
|
|
|
|
|
|
|
asyncTest "check error response when peer-store-node is not available":
|
|
|
|
let
|
|
|
|
privkey = generateSecp256k1Key()
|
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
|
|
|
port = Port(0)
|
|
|
|
node = newTestWakuNode(privkey, bindIp, port, some(extIp), some(port))
|
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
# RPC server setup
|
|
|
|
let
|
|
|
|
rpcPort = Port(8549)
|
|
|
|
ta = initTAddress(bindIp, rpcPort)
|
|
|
|
server = newRpcHttpServer([ta])
|
|
|
|
|
|
|
|
installStoreApiHandlers(node, server)
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let driver: ArchiveDriver = QueueDriver.new()
|
2023-06-27 11:24:31 +00:00
|
|
|
let mountArchiveRes = node.mountArchive(driver)
|
|
|
|
assert mountArchiveRes.isOk(), mountArchiveRes.error
|
|
|
|
|
2023-04-12 07:26:26 +00:00
|
|
|
await node.mountStore()
|
|
|
|
node.mountStoreClient()
|
|
|
|
|
|
|
|
# Now prime it with some history before tests
|
|
|
|
let msgList = @[
|
|
|
|
fakeWakuMessage(@[byte 0], ts=0),
|
|
|
|
fakeWakuMessage(@[byte 9], ts=9)
|
|
|
|
]
|
|
|
|
for msg in msgList:
|
2023-05-25 15:34:34 +00:00
|
|
|
require (waitFor driver.put(DefaultPubsubTopic, msg)).isOk()
|
2023-04-12 07:26:26 +00:00
|
|
|
|
|
|
|
let client = newRpcHttpClient()
|
|
|
|
await client.connect("127.0.0.1", rpcPort, false)
|
2023-02-10 09:43:16 +00:00
|
|
|
|
2023-04-12 07:26:26 +00:00
|
|
|
var response:StoreResponse
|
|
|
|
var jsonError:JsonNode
|
|
|
|
try:
|
|
|
|
response = await client.get_waku_v2_store_v1_messages(
|
|
|
|
some(DefaultPubsubTopic),
|
|
|
|
some(@[HistoryContentFilterRPC(contentTopic: DefaultContentTopic)]),
|
|
|
|
some(Timestamp(0)),
|
|
|
|
some(Timestamp(9)),
|
|
|
|
some(StorePagingOptions()))
|
|
|
|
except ValueError:
|
|
|
|
jsonError = parseJson(getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
check:
|
|
|
|
$jsonError["code"] == "-32000"
|
|
|
|
jsonError["message"].getStr() == "get_waku_v2_store_v1_messages raised an exception"
|
|
|
|
jsonError["data"].getStr() == "no suitable remote store peers"
|
|
|
|
|
|
|
|
# Now configure a store-peer
|
|
|
|
let
|
|
|
|
key = generateEcdsaKey()
|
|
|
|
peer = PeerInfo.new(key)
|
|
|
|
|
|
|
|
var listenSwitch = newStandardSwitch(some(key))
|
|
|
|
await listenSwitch.start()
|
|
|
|
|
|
|
|
listenSwitch.mount(node.wakuStore)
|
|
|
|
|
|
|
|
node.peerManager.addServicePeer(listenSwitch.peerInfo.toRemotePeerInfo(),
|
|
|
|
WakuStoreCodec)
|
|
|
|
|
|
|
|
response = await client.get_waku_v2_store_v1_messages(
|
|
|
|
some(DefaultPubsubTopic),
|
|
|
|
some(@[HistoryContentFilterRPC(contentTopic: DefaultContentTopic)]),
|
|
|
|
some(Timestamp(0)),
|
|
|
|
some(Timestamp(9)),
|
|
|
|
some(StorePagingOptions()))
|
|
|
|
check:
|
|
|
|
response.messages.len == 2
|
|
|
|
response.pagingOptions.isNone()
|
|
|
|
|
|
|
|
await server.stop()
|
|
|
|
await server.closeWait()
|
2023-02-10 09:43:16 +00:00
|
|
|
await node.stop()
|
2023-04-12 07:26:26 +00:00
|
|
|
await listenSwitch.stop()
|