2026-06-25 11:54:47 +02:00
|
|
|
## The query/response are complex types, so this keeps the JSON bridge: the
|
|
|
|
|
## request carries the query as a JSON string, the response is returned as JSON.
|
2026-06-25 00:40:02 +02:00
|
|
|
import std/[json, sugar, options]
|
2026-06-25 11:54:47 +02:00
|
|
|
import logos_delivery/waku/waku_core/message/digest
|
|
|
|
|
import logos_delivery/waku/waku_store/common
|
|
|
|
|
import logos_delivery/waku/common/paging
|
|
|
|
|
import library/utils
|
2025-12-19 17:00:43 +01:00
|
|
|
|
2026-06-25 11:54:47 +02:00
|
|
|
func storeQueryFromJson(jsonContent: JsonNode): Result[StoreQueryRequest, string] =
|
2025-02-12 18:35:50 +02:00
|
|
|
var contentTopics: seq[string]
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("contentTopics"):
|
2025-02-12 18:35:50 +02:00
|
|
|
contentTopics = collect(newSeq):
|
2025-04-11 12:07:35 +03:00
|
|
|
for cTopic in jsonContent["contentTopics"].getElems():
|
2025-02-12 18:35:50 +02:00
|
|
|
cTopic.getStr()
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-01-30 10:15:31 +02:00
|
|
|
var msgHashes: seq[WakuMessageHash]
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("messageHashes"):
|
|
|
|
|
for hashJsonObj in jsonContent["messageHashes"].getElems():
|
2025-01-30 10:15:31 +02:00
|
|
|
let hash = hashJsonObj.getStr().hexToHash().valueOr:
|
|
|
|
|
return err("Failed converting message hash hex string to bytes: " & error)
|
|
|
|
|
msgHashes.add(hash)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
|
|
|
|
let pubsubTopic =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("pubsubTopic"):
|
|
|
|
|
some(jsonContent["pubsubTopic"].getStr())
|
2024-08-29 14:29:02 +02:00
|
|
|
else:
|
|
|
|
|
none(string)
|
|
|
|
|
|
|
|
|
|
let paginationCursor =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("paginationCursor"):
|
|
|
|
|
let hash = jsonContent["paginationCursor"].getStr().hexToHash().valueOr:
|
|
|
|
|
return err("Failed converting paginationCursor hex string to bytes: " & error)
|
2024-08-29 14:29:02 +02:00
|
|
|
some(hash)
|
|
|
|
|
else:
|
|
|
|
|
none(WakuMessageHash)
|
|
|
|
|
|
2025-04-11 12:07:35 +03:00
|
|
|
let paginationForwardBool = jsonContent["paginationForward"].getBool()
|
2024-08-29 14:29:02 +02:00
|
|
|
let paginationForward =
|
|
|
|
|
if paginationForwardBool: PagingDirection.FORWARD else: PagingDirection.BACKWARD
|
|
|
|
|
|
|
|
|
|
let paginationLimit =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("paginationLimit"):
|
|
|
|
|
some(uint64(jsonContent["paginationLimit"].getInt()))
|
2024-08-29 14:29:02 +02:00
|
|
|
else:
|
|
|
|
|
none(uint64)
|
|
|
|
|
|
2025-04-11 12:07:35 +03:00
|
|
|
let startTime = ?jsonContent.getProtoInt64("timeStart")
|
|
|
|
|
let endTime = ?jsonContent.getProtoInt64("timeEnd")
|
2025-01-03 12:26:46 +01:00
|
|
|
|
2024-12-10 13:52:21 -04:00
|
|
|
return ok(
|
|
|
|
|
StoreQueryRequest(
|
2025-04-11 12:07:35 +03:00
|
|
|
requestId: jsonContent["requestId"].getStr(),
|
|
|
|
|
includeData: jsonContent["includeData"].getBool(),
|
2024-12-10 13:52:21 -04:00
|
|
|
pubsubTopic: pubsubTopic,
|
|
|
|
|
contentTopics: contentTopics,
|
2025-01-03 12:26:46 +01:00
|
|
|
startTime: startTime,
|
|
|
|
|
endTime: endTime,
|
2024-12-10 13:52:21 -04:00
|
|
|
messageHashes: msgHashes,
|
|
|
|
|
paginationCursor: paginationCursor,
|
|
|
|
|
paginationForward: paginationForward,
|
|
|
|
|
paginationLimit: paginationLimit,
|
|
|
|
|
)
|
2024-08-29 14:29:02 +02:00
|
|
|
)
|
2023-12-11 08:49:13 +01:00
|
|
|
|
2026-06-25 11:54:47 +02:00
|
|
|
proc store_query*(
|
|
|
|
|
self: LogosDelivery, queryJson: string, peer: string, timeoutMs: int
|
|
|
|
|
): Future[Result[string, string]] {.ffi.} =
|
|
|
|
|
let jsonContent =
|
|
|
|
|
try:
|
|
|
|
|
parseJson(queryJson)
|
|
|
|
|
except CatchableError as e:
|
|
|
|
|
return err("StoreRequest failed parsing store request: " & e.msg)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2026-06-25 11:54:47 +02:00
|
|
|
let storeQueryRequest = storeQueryFromJson(jsonContent).valueOr:
|
|
|
|
|
return err(error)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2026-06-25 11:54:47 +02:00
|
|
|
let queryResponse = (await self.waku.storeQuery(storeQueryRequest, peer, timeoutMs)).valueOr:
|
2026-06-25 00:40:02 +02:00
|
|
|
return err("StoreRequest failed store query: " & error)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2026-06-25 11:54:47 +02:00
|
|
|
return ok($(%*(queryResponse.toHex()))) ## response in json format
|