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 stew/byteutils
|
||||||
import
|
import
|
||||||
../../waku/common/base64,
|
../../waku/common/base64,
|
||||||
../../waku/waku_core/message,
|
../../waku/waku_core/message,
|
||||||
../../waku/waku_core/message/message,
|
../../waku/waku_core/message/message,
|
||||||
|
../utils,
|
||||||
./json_base_event
|
./json_base_event
|
||||||
|
|
||||||
type JsonMessage* = ref object # https://rfc.vac.dev/spec/36/#jsonmessage-type
|
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
|
meta*: Base64String
|
||||||
proof*: 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
|
# Visit https://rfc.vac.dev/spec/14/ for further details
|
||||||
JsonMessage(
|
ok(
|
||||||
payload: Base64String(jsonContent["payload"].getStr()),
|
JsonMessage(
|
||||||
contentTopic: jsonContent["contentTopic"].getStr(),
|
payload: Base64String(jsonContent["payload"].getStr()),
|
||||||
version: uint32(jsonContent{"version"}.getInt()),
|
contentTopic: jsonContent["contentTopic"].getStr(),
|
||||||
timestamp: int64(jsonContent{"timestamp"}.getBiggestInt()),
|
version: uint32(jsonContent{"version"}.getInt()),
|
||||||
ephemeral: jsonContent{"ephemeral"}.getBool(),
|
timestamp: (?jsonContent.getProtoInt64("timestamp")).get(0),
|
||||||
meta: Base64String(jsonContent{"meta"}.getStr()),
|
ephemeral: jsonContent{"ephemeral"}.getBool(),
|
||||||
proof: Base64String(jsonContent{"proof"}.getStr()),
|
meta: Base64String(jsonContent{"meta"}.getStr()),
|
||||||
|
proof: Base64String(jsonContent{"proof"}.getStr()),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
proc toWakuMessage*(self: JsonMessage): Result[WakuMessage, string] =
|
proc toWakuMessage*(self: JsonMessage): Result[WakuMessage, string] =
|
||||||
|
|
|
@ -267,7 +267,8 @@ proc waku_relay_publish(
|
||||||
var jsonMessage: JsonMessage
|
var jsonMessage: JsonMessage
|
||||||
try:
|
try:
|
||||||
let jsonContent = parseJson($jwm)
|
let jsonContent = parseJson($jwm)
|
||||||
jsonMessage = JsonMessage.fromJsonNode(jsonContent)
|
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
|
||||||
|
raise newException(JsonParsingError, $error)
|
||||||
except JsonParsingError:
|
except JsonParsingError:
|
||||||
deallocShared(jwm)
|
deallocShared(jwm)
|
||||||
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
|
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
|
||||||
|
@ -495,7 +496,8 @@ proc waku_lightpush_publish(
|
||||||
var jsonMessage: JsonMessage
|
var jsonMessage: JsonMessage
|
||||||
try:
|
try:
|
||||||
let jsonContent = parseJson($jwm)
|
let jsonContent = parseJson($jwm)
|
||||||
jsonMessage = JsonMessage.fromJsonNode(jsonContent)
|
jsonMessage = JsonMessage.fromJsonNode(jsonContent).valueOr:
|
||||||
|
raise newException(JsonParsingError, $error)
|
||||||
except JsonParsingError:
|
except JsonParsingError:
|
||||||
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
|
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
|
||||||
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
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
|
import
|
||||||
../../../../../waku/factory/waku,
|
../../../../../waku/factory/waku,
|
||||||
../../../../alloc,
|
../../../../alloc,
|
||||||
|
../../../../utils,
|
||||||
../../../../../waku/waku_core/peers,
|
../../../../../waku/waku_core/peers,
|
||||||
../../../../../waku/waku_core/time,
|
../../../../../waku/waku_core/time,
|
||||||
../../../../../waku/waku_core/message/digest,
|
../../../../../waku/waku_core/message/digest,
|
||||||
|
@ -24,7 +25,7 @@ type StoreRequest* = object
|
||||||
|
|
||||||
func fromJsonNode(
|
func fromJsonNode(
|
||||||
T: type JsonStoreQueryRequest, jsonContent: JsonNode
|
T: type JsonStoreQueryRequest, jsonContent: JsonNode
|
||||||
): StoreQueryRequest =
|
): Result[StoreQueryRequest, string] =
|
||||||
let contentTopics = collect(newSeq):
|
let contentTopics = collect(newSeq):
|
||||||
for cTopic in jsonContent["content_topics"].getElems():
|
for cTopic in jsonContent["content_topics"].getElems():
|
||||||
cTopic.getStr()
|
cTopic.getStr()
|
||||||
|
@ -45,18 +46,6 @@ func fromJsonNode(
|
||||||
else:
|
else:
|
||||||
none(string)
|
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 =
|
let paginationCursor =
|
||||||
if jsonContent.contains("pagination_cursor"):
|
if jsonContent.contains("pagination_cursor"):
|
||||||
var hash: WakuMessageHash
|
var hash: WakuMessageHash
|
||||||
|
@ -79,17 +68,19 @@ func fromJsonNode(
|
||||||
else:
|
else:
|
||||||
none(uint64)
|
none(uint64)
|
||||||
|
|
||||||
return StoreQueryRequest(
|
return ok(
|
||||||
requestId: jsonContent["request_id"].getStr(),
|
StoreQueryRequest(
|
||||||
includeData: jsonContent["include_data"].getBool(),
|
requestId: jsonContent["request_id"].getStr(),
|
||||||
pubsubTopic: pubsubTopic,
|
includeData: jsonContent["include_data"].getBool(),
|
||||||
contentTopics: contentTopics,
|
pubsubTopic: pubsubTopic,
|
||||||
startTime: startTime,
|
contentTopics: contentTopics,
|
||||||
endTime: endTime,
|
startTime: ?jsonContent.getProtoInt64("time_start"),
|
||||||
messageHashes: msgHashes,
|
endTime: ?jsonContent.getProtoInt64("time_end"),
|
||||||
paginationCursor: paginationCursor,
|
messageHashes: msgHashes,
|
||||||
paginationForward: paginationForward,
|
paginationCursor: paginationCursor,
|
||||||
paginationLimit: paginationLimit,
|
paginationForward: paginationForward,
|
||||||
|
paginationLimit: paginationLimit,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
proc createShared*(
|
proc createShared*(
|
||||||
|
@ -128,7 +119,7 @@ proc process(
|
||||||
let peer = peers.parsePeerInfo(($self[].peerAddr).split(",")).valueOr:
|
let peer = peers.parsePeerInfo(($self[].peerAddr).split(",")).valueOr:
|
||||||
return err("JsonStoreQueryRequest failed to parse peer addr: " & $error)
|
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 err("JsonStoreQueryRequest failed store query: " & $error)
|
||||||
|
|
||||||
return ok($(%*queryResponse)) ## returning the response in json format
|
return ok($(%*queryResponse)) ## returning the response in json format
|
||||||
|
|
Loading…
Reference in New Issue