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

fix(libwaku): support string and int64 for timestamps #3205

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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
25 changes: 15 additions & 10 deletions library/events/json_message_event.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import system, results, std/json
import system, results, std/json, std/strutils
import stew/byteutils
import
../../waku/common/base64,
../../waku/waku_core/message,
../../waku/waku_core/message/message,
../utils,
./json_base_event

type JsonMessage* = ref object # https://rfc.vac.dev/spec/36/#jsonmessage-type
Expand All @@ -15,16 +16,20 @@ type JsonMessage* = ref object # https://rfc.vac.dev/spec/36/#jsonmessage-type
meta*: Base64String
proof*: Base64String

func fromJsonNode*(T: type JsonMessage, jsonContent: JsonNode): JsonMessage =
func fromJsonNode*(
T: type JsonMessage, jsonContent: JsonNode
): Result[JsonMessage, string] =
# Visit https://rfc.vac.dev/spec/14/ for further details
JsonMessage(
payload: Base64String(jsonContent["payload"].getStr()),
contentTopic: jsonContent["contentTopic"].getStr(),
version: uint32(jsonContent{"version"}.getInt()),
timestamp: int64(jsonContent{"timestamp"}.getBiggestInt()),
ephemeral: jsonContent{"ephemeral"}.getBool(),
meta: Base64String(jsonContent{"meta"}.getStr()),
proof: Base64String(jsonContent{"proof"}.getStr()),
ok(
JsonMessage(
payload: Base64String(jsonContent["payload"].getStr()),
contentTopic: jsonContent["contentTopic"].getStr(),
version: uint32(jsonContent{"version"}.getInt()),
timestamp: (?jsonContent.getProtoInt64("timestamp")).get(0),
ephemeral: jsonContent{"ephemeral"}.getBool(),
meta: Base64String(jsonContent{"meta"}.getStr()),
proof: Base64String(jsonContent{"proof"}.getStr()),
)
)

proc toWakuMessage*(self: JsonMessage): Result[WakuMessage, string] =
Expand Down
6 changes: 4 additions & 2 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ proc waku_relay_publish(
var jsonMessage: JsonMessage
try:
let jsonContent = parseJson($jwm)
jsonMessage = JsonMessage.fromJsonNode(jsonContent)
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
raise newException(JsonParsingError, $error)
except JsonParsingError:
deallocShared(jwm)
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
Expand Down Expand Up @@ -495,7 +496,8 @@ proc waku_lightpush_publish(
var jsonMessage: JsonMessage
try:
let jsonContent = parseJson($jwm)
jsonMessage = JsonMessage.fromJsonNode(jsonContent)
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
raise newException(JsonParsingError, $error)
except JsonParsingError:
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
Expand Down
20 changes: 20 additions & 0 deletions library/utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import std/[json, options, strutils]
import results

proc getProtoInt64*(node: JsonNode, key: string): Result[Option[int64], string] =
try:
let (value, ok) =
if node.hasKey(key):
if node[key].kind == JString:
(parseBiggestInt(node[key].getStr()), true)
else:
(node[key].getBiggestInt(), true)
else:
(0, false)

if ok:
return ok(some(value))

return ok(none(int64))
except CatchableError:
return err("Invalid int64 value in `" & key & "`")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chronos, chronicles, results
import
../../../../../waku/factory/waku,
../../../../alloc,
../../../../utils,
../../../../../waku/waku_core/peers,
../../../../../waku/waku_core/time,
../../../../../waku/waku_core/message/digest,
Expand All @@ -24,7 +25,7 @@ type StoreRequest* = object

func fromJsonNode(
T: type JsonStoreQueryRequest, jsonContent: JsonNode
): StoreQueryRequest =
): Result[StoreQueryRequest, string] =
let contentTopics = collect(newSeq):
for cTopic in jsonContent["content_topics"].getElems():
cTopic.getStr()
Expand All @@ -45,18 +46,6 @@ func fromJsonNode(
else:
none(string)

let startTime =
if jsonContent.contains("time_start"):
some(Timestamp(jsonContent["time_start"].getInt()))
else:
none(Timestamp)

let endTime =
if jsonContent.contains("time_end"):
some(Timestamp(jsonContent["time_end"].getInt()))
else:
none(Timestamp)

let paginationCursor =
if jsonContent.contains("pagination_cursor"):
var hash: WakuMessageHash
Expand All @@ -79,17 +68,19 @@ func fromJsonNode(
else:
none(uint64)

return StoreQueryRequest(
requestId: jsonContent["request_id"].getStr(),
includeData: jsonContent["include_data"].getBool(),
pubsubTopic: pubsubTopic,
contentTopics: contentTopics,
startTime: startTime,
endTime: endTime,
messageHashes: msgHashes,
paginationCursor: paginationCursor,
paginationForward: paginationForward,
paginationLimit: paginationLimit,
return ok(
StoreQueryRequest(
requestId: jsonContent["request_id"].getStr(),
includeData: jsonContent["include_data"].getBool(),
pubsubTopic: pubsubTopic,
contentTopics: contentTopics,
startTime: ?jsonContent.getProtoInt64("time_start"),
endTime: ?jsonContent.getProtoInt64("time_end"),
messageHashes: msgHashes,
paginationCursor: paginationCursor,
paginationForward: paginationForward,
paginationLimit: paginationLimit,
)
)

proc createShared*(
Expand Down Expand Up @@ -128,7 +119,7 @@ proc process(
let peer = peers.parsePeerInfo(($self[].peerAddr).split(",")).valueOr:
return err("JsonStoreQueryRequest failed to parse peer addr: " & $error)

let queryResponse = (await waku.node.wakuStoreClient.query(storeQueryRequest, peer)).valueOr:
let queryResponse = (await waku.node.wakuStoreClient.query(?storeQueryRequest, peer)).valueOr:
return err("JsonStoreQueryRequest failed store query: " & $error)

return ok($(%*queryResponse)) ## returning the response in json format
Expand Down
Loading