mirror of https://github.com/waku-org/nwaku.git
fix(libwaku): support string and int64 for timestamps (#3205)
This commit is contained in:
parent
e2b7149f82
commit
2022f54f5c
|
@ -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
|
||||
|
@ -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] =
|
||||
|
|
|
@ -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()}"
|
||||
|
@ -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)
|
||||
|
|
|
@ -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 & "`")
|
|
@ -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,
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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*(
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue