Skip to content

Commit 03bcaf1

Browse files
committed
api changes to integrate with logos-chat-ui app
1 parent 45da95f commit 03bcaf1

File tree

7 files changed

+86
-25
lines changed

7 files changed

+86
-25
lines changed

library/kernel_api/debug_node_api.nim

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import
99
metrics,
1010
ffi
1111
import
12-
waku/factory/waku, waku/node/waku_node, waku/node/health_monitor, library/declare_lib
12+
waku/factory/waku,
13+
waku/node/waku_node,
14+
waku/node/health_monitor,
15+
library/declare_lib,
16+
waku/waku_core/codecs
1317

1418
proc getMultiaddresses(node: WakuNode): seq[string] =
1519
return node.info().listenAddresses
@@ -48,3 +52,19 @@ proc waku_is_online(
4852
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
4953
) {.ffi.} =
5054
return ok($ctx.myLib[].healthMonitor.onlineMonitor.amIOnline())
55+
56+
proc waku_get_mixnode_pool_size(
57+
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
58+
) {.ffi.} =
59+
## Returns the number of mix nodes in the pool
60+
if ctx.myLib[].node.wakuMix.isNil():
61+
return ok("0")
62+
return ok($ctx.myLib[].node.getMixNodePoolSize())
63+
64+
proc waku_get_lightpush_peers_count(
65+
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
66+
) {.ffi.} =
67+
## Returns the count of all peers in peerstore supporting lightpush protocol
68+
let peers =
69+
ctx.myLib[].node.peerManager.switch.peerStore.getPeersByProtocol(WakuLightPushCodec)
70+
return ok($peers.len)

library/kernel_api/protocols/filter_api.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ proc waku_filter_subscribe(
4747
error "fail filter subscribe", error = errorMsg
4848
return err(errorMsg)
4949

50+
let pubsubTopicOpt =
51+
if ($pubsubTopic).len > 0:
52+
some(PubsubTopic($pubsubTopic))
53+
else:
54+
none(PubsubTopic)
5055
let subFut = ctx.myLib[].node.filterSubscribe(
51-
some(PubsubTopic($pubsubTopic)),
52-
($contentTopics).split(",").mapIt(ContentTopic(it)),
53-
peer,
56+
pubsubTopicOpt, ($contentTopics).split(",").mapIt(ContentTopic(it)), peer
5457
)
5558
if not await subFut.withTimeout(FilterOpTimeout):
5659
let errorMsg = "filter subscription timed out"

library/kernel_api/protocols/lightpush_api.nim

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ proc waku_lightpush_publish(
3939
let errorMsg = "failed to lightpublish message, no suitable remote peers"
4040
error "PUBLISH failed", error = errorMsg
4141
return err(errorMsg)
42+
let topic =
43+
if ($pubsubTopic).len == 0:
44+
none(PubsubTopic)
45+
else:
46+
some(PubsubTopic($pubsubTopic))
4247

43-
let msgHashHex = (
44-
await ctx.myLib[].node.wakuLegacyLightpushClient.publish(
45-
$pubsubTopic, msg, peer = peerOpt.get()
46-
)
47-
).valueOr:
48-
error "PUBLISH failed", error = error
49-
return err($error)
48+
let messageHash = (await ctx.myLib[].node.lightpushPublish(topic, msg, peerOpt)).valueOr:
49+
let errorMsg = error.desc.get($error.code.int)
50+
error "PUBLISH failed", error = errorMsg
51+
return err(errorMsg)
5052

51-
return ok(msgHashHex)
53+
return ok($messageHash)

library/kernel_api/protocols/relay_api.nim

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,47 @@ proc waku_relay_subscribe(
8585
callback: FFICallBack,
8686
userData: pointer,
8787
pubSubTopic: cstring,
88+
contentTopic: cstring,
8889
) {.ffi.} =
89-
echo "Subscribing to topic: " & $pubSubTopic & " ..."
9090
proc onReceivedMessage(ctx: ptr FFIContext[Waku]): WakuRelayHandler =
9191
return proc(pubsubTopic: PubsubTopic, msg: WakuMessage) {.async.} =
9292
callEventCallback(ctx, "onReceivedMessage"):
9393
$JsonMessageEvent.new(pubsubTopic, msg)
9494

9595
var cb = onReceivedMessage(ctx)
9696

97-
ctx.myLib[].node.subscribe(
98-
(kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic),
99-
handler = WakuRelayHandler(cb),
100-
).isOkOr:
97+
# If contentTopic is provided and non-empty, use ContentSub, otherwise use PubsubSub
98+
let subscription =
99+
if contentTopic != nil and len($contentTopic) > 0:
100+
echo "Subscribing to content topic: " & $contentTopic & " ..."
101+
(kind: SubscriptionKind.ContentSub, topic: $contentTopic)
102+
else:
103+
echo "Subscribing to pubsub topic: " & $pubSubTopic & " ..."
104+
(kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)
105+
106+
ctx.myLib[].node.subscribe(subscription, handler = WakuRelayHandler(cb)).isOkOr:
101107
error "SUBSCRIBE failed", error = error
102108
return err($error)
103109
return ok("")
104110

111+
# NOTE: When unsubscribing via contentTopic, this will unsubscribe from the entire
112+
# underlying pubsub topic/shard that the content topic maps to. This affects ALL
113+
# content topics on the same shard, not just the specified content topic.
105114
proc waku_relay_unsubscribe(
106115
ctx: ptr FFIContext[Waku],
107116
callback: FFICallBack,
108117
userData: pointer,
109118
pubSubTopic: cstring,
119+
contentTopic: cstring,
110120
) {.ffi.} =
111-
ctx.myLib[].node.unsubscribe((kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)).isOkOr:
121+
# If contentTopic is provided and non-empty, use ContentUnsub, otherwise use PubsubUnsub
122+
let subscription =
123+
if contentTopic != nil and len($contentTopic) > 0:
124+
(kind: SubscriptionKind.ContentUnsub, topic: $contentTopic)
125+
else:
126+
(kind: SubscriptionKind.PubsubUnsub, topic: $pubsubTopic)
127+
128+
ctx.myLib[].node.unsubscribe(subscription).isOkOr:
112129
error "UNSUBSCRIBE failed", error = error
113130
return err($error)
114131

library/kernel_api/protocols/store_api.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import
88
waku/waku_store/common,
99
waku/waku_store/client,
1010
waku/common/paging,
11+
waku/common/base64,
1112
library/declare_lib
1213

14+
# Custom JSON serialization for seq[byte] to avoid ambiguity
15+
proc `%`*(data: seq[byte]): JsonNode =
16+
%base64.encode(data)
17+
1318
func fromJsonNode(jsonContent: JsonNode): Result[StoreQueryRequest, string] =
1419
var contentTopics: seq[string]
1520
if jsonContent.contains("contentTopics"):
@@ -90,5 +95,6 @@ proc waku_store_query(
9095
).valueOr:
9196
return err("StoreRequest failed store query: " & $error)
9297

93-
let res = $(%*(queryResponse.toHex()))
98+
let hexResponse = queryResponse.toHex()
99+
let res = $(%*hexResponse)
94100
return ok(res) ## returning the response in json format

library/libwaku.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ extern "C"
8585
int waku_relay_subscribe(void *ctx,
8686
FFICallBack callback,
8787
void *userData,
88-
const char *pubSubTopic);
88+
const char *pubSubTopic,
89+
const char *contentTopic);
8990

9091
int waku_relay_add_protected_shard(void *ctx,
9192
FFICallBack callback,
@@ -97,7 +98,8 @@ extern "C"
9798
int waku_relay_unsubscribe(void *ctx,
9899
FFICallBack callback,
99100
void *userData,
100-
const char *pubSubTopic);
101+
const char *pubSubTopic,
102+
const char *contentTopic);
101103

102104
int waku_filter_subscribe(void *ctx,
103105
FFICallBack callback,
@@ -247,6 +249,14 @@ extern "C"
247249
FFICallBack callback,
248250
void *userData);
249251

252+
int waku_get_mixnode_pool_size(void *ctx,
253+
FFICallBack callback,
254+
void *userData);
255+
256+
int waku_get_lightpush_peers_count(void *ctx,
257+
FFICallBack callback,
258+
void *userData);
259+
250260
#ifdef __cplusplus
251261
}
252262
#endif

waku/node/kernel_api/lightpush.nim

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,14 @@ proc lightpushPublish*(
247247
return lighpushErrorResult(
248248
LightPushErrorCode.SERVICE_NOT_AVAILABLE, "Waku lightpush not available"
249249
)
250-
if mixify and node.wakuMix.isNil():
251-
error "failed to publish message using mix as mix protocol is not mounted"
250+
var lmixify = mixify
251+
if not node.wakuMix.isNil():
252+
lmixify = true
253+
254+
#[ error "failed to publish message using mix as mix protocol is not mounted"
252255
return lighpushErrorResult(
253256
LightPushErrorCode.SERVICE_NOT_AVAILABLE, "Waku lightpush with mix not available"
254-
)
257+
) ]#
255258
let toPeer: RemotePeerInfo = peerOpt.valueOr:
256259
if not node.wakuLightPush.isNil():
257260
RemotePeerInfo.init(node.peerId())
@@ -281,4 +284,4 @@ proc lightpushPublish*(
281284
error "lightpush publish error", error = msg
282285
return lighpushErrorResult(LightPushErrorCode.INTERNAL_SERVER_ERROR, msg)
283286

284-
return await lightpushPublishHandler(node, pubsubForPublish, message, toPeer, mixify)
287+
return await lightpushPublishHandler(node, pubsubForPublish, message, toPeer, lmixify)

0 commit comments

Comments
 (0)