Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enhance libwaku store #3209

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions library/events/json_message_event.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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")