From 6f5f8b835cd65381988658642bdbbd7b7281e367 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Sun, 1 Dec 2024 18:09:40 +0100 Subject: [PATCH 1/2] libwaku store enhancements --- library/libwaku.nim | 2 +- .../requests/protocols/store_request.nim | 48 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/library/libwaku.nim b/library/libwaku.nim index 0dac652d0c..cc361e5ef5 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -647,7 +647,7 @@ proc waku_store_query( .sendRequestToWakuThread( ctx, RequestType.STORE, - JsonStoreQueryRequest.createShared(jsonQuery, peerAddr, timeoutMs), + StoreRequest.createShared(StoreReqType.REMOTE_QUERY, jsonQuery, peerAddr, timeoutMs), ) .handleRes(callback, userData) diff --git a/library/waku_thread/inter_thread_communication/requests/protocols/store_request.nim b/library/waku_thread/inter_thread_communication/requests/protocols/store_request.nim index ee2b608c37..3c48d6af6c 100644 --- a/library/waku_thread/inter_thread_communication/requests/protocols/store_request.nim +++ b/library/waku_thread/inter_thread_communication/requests/protocols/store_request.nim @@ -13,31 +13,27 @@ import type StoreReqType* = enum REMOTE_QUERY ## to perform a query to another Store node -type JsonStoreQueryRequest* = object +type StoreRequest* = object + operation: StoreReqType jsonQuery: cstring peerAddr: cstring timeoutMs: cint -type StoreRequest* = object - operation: StoreReqType - storeReq: pointer - -func fromJsonNode( - T: type JsonStoreQueryRequest, jsonContent: JsonNode -): StoreQueryRequest = +func fromJsonNode(T: type StoreRequest, jsonContent: JsonNode): StoreQueryRequest = let contentTopics = collect(newSeq): for cTopic in jsonContent["content_topics"].getElems(): cTopic.getStr() let msgHashes = collect(newSeq): - for hashJsonObj in jsonContent["message_hashes"].getElems(): - var hash: WakuMessageHash - var count: int = 0 - for byteValue in hashJsonObj.getElems(): - hash[count] = byteValue.getInt().byte - count.inc() + if jsonContent.contains("message_hashes"): + for hashJsonObj in jsonContent["message_hashes"].getElems(): + var hash: WakuMessageHash + var count: int = 0 + for byteValue in hashJsonObj.getElems(): + hash[count] = byteValue.getInt().byte + count.inc() - hash + hash let pubsubTopic = if jsonContent.contains("pubsub_topic"): @@ -93,24 +89,26 @@ func fromJsonNode( ) proc createShared*( - T: type JsonStoreQueryRequest, + T: type StoreRequest, + op: StoreReqType, jsonQuery: cstring, peerAddr: cstring, timeoutMs: cint, ): ptr type T = var ret = createShared(T) + ret[].operation = op ret[].timeoutMs = timeoutMs ret[].jsonQuery = jsonQuery.alloc() ret[].peerAddr = peerAddr.alloc() return ret -proc destroyShared(self: ptr JsonStoreQueryRequest) = +proc destroyShared(self: ptr StoreRequest) = deallocShared(self[].jsonQuery) deallocShared(self[].peerAddr) deallocShared(self) -proc process( - self: ptr JsonStoreQueryRequest, waku: ptr Waku +proc process_remote_query( + self: ptr StoreRequest, waku: ptr Waku ): Future[Result[string, string]] {.async.} = defer: destroyShared(self) @@ -119,17 +117,15 @@ proc process( parseJson($self[].jsonQuery) if jsonContentRes.isErr(): - return err( - "JsonStoreQueryRequest failed parsing store request: " & jsonContentRes.error.msg - ) + return err("StoreRequest failed parsing store request: " & jsonContentRes.error.msg) - let storeQueryRequest = JsonStoreQueryRequest.fromJsonNode(jsonContentRes.get()) + let storeQueryRequest = StoreRequest.fromJsonNode(jsonContentRes.get()) let peer = peers.parsePeerInfo(($self[].peerAddr).split(",")).valueOr: - return err("JsonStoreQueryRequest failed to parse peer addr: " & $error) + return err("StoreRequest failed to parse peer addr: " & $error) let queryResponse = (await waku.node.wakuStoreClient.query(storeQueryRequest, peer)).valueOr: - return err("JsonStoreQueryRequest failed store query: " & $error) + return err("StoreRequest failed store query: " & $error) return ok($(%*queryResponse)) ## returning the response in json format @@ -141,7 +137,7 @@ proc process*( case self.operation of REMOTE_QUERY: - return await cast[ptr JsonStoreQueryRequest](self[].storeReq).process(waku) + return await self.process_remote_query(waku) error "store request not handled at all" return err("store request not handled at all") From 9d957f2daa96c9861bd89c8a73f026a2b92fcec8 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Fri, 13 Dec 2024 15:49:27 +0100 Subject: [PATCH 2/2] json_message_event: avoid converting a WakuMessageHash into 0x... --- library/events/json_message_event.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/events/json_message_event.nim b/library/events/json_message_event.nim index 04dc0b8b4a..22d3a67e24 100644 --- a/library/events/json_message_event.nim +++ b/library/events/json_message_event.nim @@ -52,9 +52,6 @@ proc toWakuMessage*(self: JsonMessage): Result[WakuMessage, string] = proc `%`*(value: Base64String): JsonNode = %(value.string) -proc `%`*(value: WakuMessageHash): JsonNode = - %(to0xHex(value)) - type JsonMessageEvent* = ref object of JsonEvent pubsubTopic*: string messageHash*: WakuMessageHash