From 0eb9c6200f2c0f32b244bbbe55daf7ba29ea0899 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:52:33 +0300 Subject: [PATCH] feat: add waku_get_connected_peers_info to libwaku (#3356) --- library/libwaku.h | 4 +++ library/libwaku.nim | 14 +++++++++++ .../requests/peer_manager_request.nim | 25 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/library/libwaku.h b/library/libwaku.h index d49a40076..3c15b36f9 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -168,6 +168,10 @@ int waku_get_peerids_from_peerstore(void* ctx, WakuCallBack callback, void* userData); +int waku_get_connected_peers_info(void* ctx, + WakuCallBack callback, + void* userData); + int waku_get_peerids_by_protocol(void* ctx, const char* protocol, WakuCallBack callback, diff --git a/library/libwaku.nim b/library/libwaku.nim index f7c14c061..ebe730da8 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -692,6 +692,20 @@ proc waku_get_peerids_from_peerstore( userData, ) +proc waku_get_connected_peers_info( + ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer +): cint {.dynlib, exportc.} = + initializeLibrary() + checkLibwakuParams(ctx, callback, userData) + + handleRequest( + ctx, + RequestType.PEER_MANAGER, + PeerManagementRequest.createShared(PeerManagementMsgType.GET_CONNECTED_PEERS_INFO), + callback, + userData, + ) + proc waku_get_connected_peers( ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ): cint {.dynlib, exportc.} = diff --git a/library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim b/library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim index 73b5a320d..d8a0a57af 100644 --- a/library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim +++ b/library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim @@ -1,5 +1,5 @@ import std/[sequtils, strutils] -import chronicles, chronos, results, options +import chronicles, chronos, results, options, json import ../../../../waku/factory/waku, ../../../../waku/node/waku_node, @@ -9,6 +9,7 @@ import type PeerManagementMsgType* {.pure.} = enum CONNECT_TO GET_ALL_PEER_IDS + GET_CONNECTED_PEERS_INFO GET_PEER_IDS_BY_PROTOCOL DISCONNECT_PEER_BY_ID DIAL_PEER @@ -22,6 +23,10 @@ type PeerManagementRequest* = object protocol: cstring peerId: cstring +type PeerInfo = object + protocols: seq[string] + addresses: seq[string] + proc createShared*( T: type PeerManagementRequest, op: PeerManagementMsgType, @@ -83,6 +88,24 @@ proc process*( let peerIDs = waku.node.peerManager.wakuPeerStore.peers().mapIt($it.peerId).join(",") return ok(peerIDs) + of GET_CONNECTED_PEERS_INFO: + ## returns a JSON string mapping peerIDs to objects with protocols and addresses + + var peersMap = initTable[string, PeerInfo]() + let peers = waku.node.peerManager.wakuPeerStore.peers().filterIt( + it.connectedness == Connected + ) + + # Build a map of peer IDs to peer info objects + for peer in peers: + let peerIdStr = $peer.peerId + peersMap[peerIdStr] = + PeerInfo(protocols: peer.protocols, addresses: peer.addrs.mapIt($it)) + + # Convert the map to JSON string + let jsonObj = %*peersMap + let jsonStr = $jsonObj + return ok(jsonStr) of GET_PEER_IDS_BY_PROTOCOL: ## returns a comma-separated string of peerIDs that mount the given protocol let connectedPeers = waku.node.peerManager.wakuPeerStore