From b7f3db0b0af4be9fb979e509be3a54c47f9d5da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Thu, 29 Feb 2024 20:58:35 -0400 Subject: [PATCH] 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 --- library/alloc.nim | 5 +++-- library/events/json_message_event.nim | 5 ++--- library/libwaku.nim | 2 ++ .../requests/protocols/relay_request.nim | 13 +++++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/library/alloc.nim b/library/alloc.nim index a9aee0a8b..a08ea5776 100644 --- a/library/alloc.nim +++ b/library/alloc.nim @@ -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]) = diff --git a/library/events/json_message_event.nim b/library/events/json_message_event.nim index 459a44213..bfb5bd7d9 100644 --- a/library/events/json_message_event.nim +++ b/library/events/json_message_event.nim @@ -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, diff --git a/library/libwaku.nim b/library/libwaku.nim index b1c43a648..41a1c316c 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -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, diff --git a/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim b/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim index be0684bd1..9453aeb06 100644 --- a/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim +++ b/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim @@ -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("")