2022-11-04 10:52:27 +01:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2020-12-01 11:57:54 +02:00
|
|
|
|
2020-11-24 05:44:37 +02:00
|
|
|
import
|
2022-11-09 18:50:18 +01:00
|
|
|
std/[options, sequtils],
|
2021-06-09 16:37:08 +02:00
|
|
|
chronicles,
|
2022-07-25 13:01:37 +02:00
|
|
|
json_rpc/rpcserver
|
|
|
|
import
|
|
|
|
../../protocol/waku_store,
|
2022-11-09 18:50:18 +01:00
|
|
|
../../protocol/waku_store/rpc,
|
2022-02-17 16:00:15 +01:00
|
|
|
../../utils/time,
|
2022-11-09 18:50:18 +01:00
|
|
|
../waku_node,
|
2023-02-06 10:03:30 +01:00
|
|
|
../peer_manager,
|
2023-01-26 10:20:20 +01:00
|
|
|
./jsonrpc_types,
|
2022-07-25 13:01:37 +02:00
|
|
|
./jsonrpc_utils
|
2020-11-24 05:44:37 +02:00
|
|
|
|
2020-12-23 11:33:28 +02:00
|
|
|
export jsonrpc_types
|
|
|
|
|
2021-03-23 10:04:51 +02:00
|
|
|
logScope:
|
2022-11-03 16:36:24 +01:00
|
|
|
topics = "waku node jsonrpc store_api"
|
2021-03-23 10:04:51 +02:00
|
|
|
|
2020-11-27 09:18:48 +02:00
|
|
|
proc installStoreApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
|
2020-11-24 05:44:37 +02:00
|
|
|
const futTimeout = 5.seconds
|
|
|
|
|
|
|
|
## Store API version 1 definitions
|
|
|
|
|
2022-11-09 18:50:18 +01:00
|
|
|
rpcsrv.rpc("get_waku_v2_store_v1_messages") do (pubsubTopicOption: Option[string], contentFiltersOption: Option[seq[HistoryContentFilterRPC]], startTime: Option[Timestamp], endTime: Option[Timestamp], pagingOptions: Option[StorePagingOptions]) -> StoreResponse:
|
2020-11-24 05:44:37 +02:00
|
|
|
## Returns history for a list of content topics with optional paging
|
|
|
|
debug "get_waku_v2_store_v1_messages"
|
|
|
|
|
2023-01-26 10:20:20 +01:00
|
|
|
let peerOpt = node.peerManager.selectPeer(WakuStoreCodec)
|
2022-10-28 20:11:28 +02:00
|
|
|
if peerOpt.isNone():
|
|
|
|
raise newException(ValueError, "no suitable remote store peers")
|
|
|
|
|
2022-11-09 18:50:18 +01:00
|
|
|
let req = HistoryQuery(
|
|
|
|
pubsubTopic: pubsubTopicOption,
|
|
|
|
contentTopics: if contentFiltersOption.isNone(): @[]
|
|
|
|
else: contentFiltersOption.get().mapIt(it.contentTopic),
|
|
|
|
startTime: startTime,
|
|
|
|
endTime: endTime,
|
|
|
|
ascending: if pagingOptions.isNone(): true
|
|
|
|
else: pagingOptions.get().forward,
|
|
|
|
pageSize: if pagingOptions.isNone(): DefaultPageSize
|
|
|
|
else: min(pagingOptions.get().pageSize, MaxPageSize),
|
|
|
|
cursor: if pagingOptions.isNone(): none(HistoryCursor)
|
|
|
|
else: pagingOptions.get().cursor.map(toAPI)
|
|
|
|
)
|
|
|
|
|
|
|
|
let queryFut = node.query(req, peerOpt.get())
|
2020-11-24 05:44:37 +02:00
|
|
|
|
2022-10-28 20:11:28 +02:00
|
|
|
if not await queryFut.withTimeout(futTimeout):
|
2022-09-21 18:27:40 +02:00
|
|
|
raise newException(ValueError, "No history response received (timeout)")
|
2023-01-26 10:20:20 +01:00
|
|
|
|
2022-10-28 20:11:28 +02:00
|
|
|
let res = queryFut.read()
|
2022-09-21 18:27:40 +02:00
|
|
|
if res.isErr():
|
2022-10-28 20:11:28 +02:00
|
|
|
raise newException(ValueError, $res.error)
|
2022-09-21 18:27:40 +02:00
|
|
|
|
|
|
|
debug "get_waku_v2_store_v1_messages response"
|
2022-11-09 18:50:18 +01:00
|
|
|
return res.value.toJsonRPCStoreResponse()
|