diff --git a/library/libwaku.h b/library/libwaku.h index bb6fa20cf..d49a40076 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -127,6 +127,11 @@ int waku_relay_get_num_peers_in_mesh(void* ctx, WakuCallBack callback, void* userData); +int waku_relay_get_peers_in_mesh(void* ctx, + const char* pubSubTopic, + WakuCallBack callback, + void* userData); + int waku_store_query(void* ctx, const char* jsonQuery, const char* peerAddr, diff --git a/library/libwaku.nim b/library/libwaku.nim index d1ea0d082..f7c14c061 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -471,6 +471,27 @@ proc waku_relay_get_num_peers_in_mesh( initializeLibrary() checkLibwakuParams(ctx, callback, userData) + let pst = pubSubTopic.alloc() + defer: + deallocShared(pst) + + handleRequest( + ctx, + RequestType.RELAY, + RelayRequest.createShared(RelayMsgType.NUM_MESH_PEERS, pst), + callback, + userData, + ) + +proc waku_relay_get_peers_in_mesh( + ctx: ptr WakuContext, + pubSubTopic: cstring, + callback: WakuCallBack, + userData: pointer, +): cint {.dynlib, exportc.} = + initializeLibrary() + checkLibwakuParams(ctx, callback, userData) + let pst = pubSubTopic.alloc() defer: deallocShared(pst) 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 3b5059972..97f01488a 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 @@ -18,6 +18,7 @@ type RelayMsgType* = enum NUM_CONNECTED_PEERS LIST_CONNECTED_PEERS ## to return the list of all connected peers to an specific pubsub topic + NUM_MESH_PEERS LIST_MESH_PEERS ## to return the list of only the peers that conform the mesh for a particular pubsub topic ADD_PROTECTED_SHARD ## Protects a shard with a public key @@ -135,11 +136,17 @@ proc process*( return err($error) ## returns a comma-separated string of peerIDs return ok(connPeers.mapIt($it).join(",")) - of LIST_MESH_PEERS: + of NUM_MESH_PEERS: let numPeersInMesh = waku.node.wakuRelay.getNumPeersInMesh($self.pubsubTopic).valueOr: - error "LIST_MESH_PEERS failed", error = error + error "NUM_MESH_PEERS failed", error = error return err($error) return ok($numPeersInMesh) + of LIST_MESH_PEERS: + let meshPeers = waku.node.wakuRelay.getPeersInMesh($self.pubsubTopic).valueOr: + error "LIST_MESH_PEERS failed", error = error + return err($error) + ## returns a comma-separated string of peerIDs + return ok(meshPeers.mapIt($it).join(",")) of ADD_PROTECTED_SHARD: try: let relayShard = diff --git a/waku/waku_relay/protocol.nim b/waku/waku_relay/protocol.nim index 1af5f0d4f..126ff608c 100644 --- a/waku/waku_relay/protocol.nim +++ b/waku/waku_relay/protocol.nim @@ -323,21 +323,35 @@ proc addObserver*(w: WakuRelay, observer: PubSubObserver) {.gcsafe.} = proc getDHigh*(T: type WakuRelay): int = return GossipsubParameters.dHigh -proc getNumPeersInMesh*(w: WakuRelay, pubsubTopic: PubsubTopic): Result[int, string] = - ## Returns the number of peers in a mesh defined by the passed pubsub topic. +proc getPeersInMesh*( + w: WakuRelay, pubsubTopic: PubsubTopic +): Result[seq[PeerId], string] = + ## Returns the list of peerIds in a mesh defined by the passed pubsub topic. ## The 'mesh' atribute is defined in the GossipSub ref object. if not w.mesh.hasKey(pubsubTopic): - debug "getNumPeersInMesh - there is no mesh peer for the given pubsub topic", + debug "getPeersInMesh - there is no mesh peer for the given pubsub topic", pubsubTopic = pubsubTopic - return ok(0) + return ok(newSeq[PeerId]()) let peersRes = catch: w.mesh[pubsubTopic] let peers: HashSet[PubSubPeer] = peersRes.valueOr: - return - err("getNumPeersInMesh - exception accessing " & pubsubTopic & ": " & error.msg) + return err("getPeersInMesh - exception accessing " & pubsubTopic & ": " & error.msg) + + let peerIds = toSeq(peers).mapIt(it.peerId) + + return ok(peerIds) + +proc getNumPeersInMesh*(w: WakuRelay, pubsubTopic: PubsubTopic): Result[int, string] = + ## Returns the number of peers in a mesh defined by the passed pubsub topic. + + let peers = w.getPeersInMesh(pubsubTopic).valueOr: + return err( + "getNumPeersInMesh - failed retrieving peers in mesh: " & pubsubTopic & ": " & + error + ) return ok(peers.len)