fix: return message id on `waku_relay_publish` (#2485)

* fix: return message id on `waku_relay_publish`
* fix: remove unneeded cast and handle 0 len seqs
* chore: rename messageId to messageHash
This commit is contained in:
richΛrd 2024-02-29 20:58:35 -04:00 committed by GitHub
parent c994ee049b
commit 045091a9f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 9 deletions

View File

@ -19,8 +19,9 @@ proc alloc*(str: string): cstring =
return ret
proc allocSharedSeq*[T](s: seq[T]): SharedSeq[T] =
let data = cast[ptr T](allocShared(s.len))
copyMem(data, unsafeAddr s, s.len)
let data = allocShared(sizeof(T) * s.len)
if s.len != 0:
copyMem(data, unsafeAddr s[0], s.len)
return (cast[ptr UncheckedArray[T]](data), s.len)
proc deallocSharedSeq*[T](s: var SharedSeq[T]) =

View File

@ -61,7 +61,7 @@ proc `%`*(value: WakuMessageHash): JsonNode =
type JsonMessageEvent* = ref object of JsonEvent
pubsubTopic*: string
messageId*: string
messageHash*: WakuMessageHash
wakuMessage*: JsonMessage
proc new*(T: type JsonMessageEvent,
@ -83,12 +83,11 @@ proc new*(T: type JsonMessageEvent,
copyMem(addr proof[0], unsafeAddr msg.proof[0], len(msg.proof))
let msgHash = computeMessageHash(pubSubTopic, msg)
let msgHashHex = to0xHex(msgHash)
return JsonMessageEvent(
eventType: "message",
pubSubTopic: pubSubTopic,
messageId: msgHashHex,
messageHash: msgHash,
wakuMessage: JsonMessage(
payload: base64.encode(payload),
contentTopic: msg.contentTopic,

View File

@ -235,6 +235,8 @@ proc waku_relay_publish(ctx: ptr Context,
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msgHash = $sendReqRes.value
callback(RET_OK, unsafeAddr msgHash[0], cast[csize_t](len(msgHash)), userData)
return RET_OK
proc waku_start(ctx: ptr Context,

View File

@ -4,11 +4,13 @@ import
import
chronicles,
chronos,
stew/byteutils,
stew/results,
stew/shims/net
import
../../../../../waku/waku_core/message/message,
../../../../../waku/node/waku_node,
../../../../../waku/waku_core/message,
../../../../../waku/waku_core/time, # Timestamp
../../../../../waku/waku_core/topics/pubsub_topic,
../../../../../waku/waku_relay/protocol,
@ -104,13 +106,16 @@ proc process*(self: ptr RelayRequest,
node.wakuRelay.unsubscribeAll($self.pubsubTopic)
of PUBLISH:
let numPeers = await node.wakuRelay.publish($self.pubsubTopic,
self.message.toWakuMessage())
let msg = self.message.toWakuMessage()
let pubsubTopic = $self.pubsubTopic
let numPeers = await node.wakuRelay.publish(pubsubTopic,
msg)
if numPeers == 0:
return err("Message not sent because no peers found.")
elif numPeers > 0:
# TODO: pending to return a valid message Id
return ok("hard-coded-message-id")
let msgHash = computeMessageHash(pubSubTopic, msg).to0xHex
return ok(msgHash)
return ok("")