From 507b1fc4d97a01ee5695a205f7f981bd4accc694 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:26:33 +0200 Subject: [PATCH] feat: adding waku_dial_peer and get_connected_peers to libwaku (#3149) --- library/libwaku.h | 11 ++++++ library/libwaku.nim | 37 ++++++++++++++++++- .../requests/peer_manager_request.nim | 19 +++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/library/libwaku.h b/library/libwaku.h index a0ccede92..6a5800d80 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -120,6 +120,13 @@ int waku_disconnect_peer_by_id(void* ctx, WakuCallBack callback, void* userData); +int waku_dial_peer(void* ctx, + const char* peerMultiAddr, + const char* protocol, + int timeoutMs, + WakuCallBack callback, + void* userData); + int waku_dial_peer_by_id(void* ctx, const char* peerId, const char* protocol, @@ -140,6 +147,10 @@ int waku_listen_addresses(void* ctx, WakuCallBack callback, void* userData); +int waku_get_connected_peers(void* ctx, + WakuCallBack callback, + void* userData); + // Returns a list of multiaddress given a url to a DNS discoverable ENR tree // Parameters // char* entTreeUrl: URL containing a discoverable ENR tree diff --git a/library/libwaku.nim b/library/libwaku.nim index 3d591b712..a5828f5d8 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -498,6 +498,28 @@ proc waku_disconnect_peer_by_id( ) .handleRes(callback, userData) +proc waku_dial_peer( + ctx: ptr WakuContext, + peerMultiAddr: cstring, + protocol: cstring, + timeoutMs: cuint, + callback: WakuCallBack, + userData: pointer, +): cint {.dynlib, exportc.} = + checkLibwakuParams(ctx, callback, userData) + + waku_thread + .sendRequestToWakuThread( + ctx, + RequestType.PEER_MANAGER, + PeerManagementRequest.createShared( + op = PeerManagementMsgType.DIAL_PEER, + peerMultiAddr = $peerMultiAddr, + protocol = $protocol, + ), + ) + .handleRes(callback, userData) + proc waku_dial_peer_by_id( ctx: ptr WakuContext, peerId: cstring, @@ -513,7 +535,7 @@ proc waku_dial_peer_by_id( ctx, RequestType.PEER_MANAGER, PeerManagementRequest.createShared( - op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId + op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId, protocol = $protocol ), ) .handleRes(callback, userData) @@ -531,6 +553,19 @@ proc waku_get_peerids_from_peerstore( ) .handleRes(callback, userData) +proc waku_get_connected_peers( + ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer +): cint {.dynlib, exportc.} = + checkLibwakuParams(ctx, callback, userData) + + waku_thread + .sendRequestToWakuThread( + ctx, + RequestType.PEER_MANAGER, + PeerManagementRequest.createShared(PeerManagementMsgType.GET_CONNECTED_PEERS), + ) + .handleRes(callback, userData) + proc waku_get_peerids_by_protocol( ctx: ptr WakuContext, protocol: cstring, 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 e92c3d4ed..73b5a320d 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 @@ -11,7 +11,9 @@ type PeerManagementMsgType* {.pure.} = enum GET_ALL_PEER_IDS GET_PEER_IDS_BY_PROTOCOL DISCONNECT_PEER_BY_ID + DIAL_PEER DIAL_PEER_BY_ID + GET_CONNECTED_PEERS type PeerManagementRequest* = object operation: PeerManagementMsgType @@ -95,6 +97,15 @@ proc process*( return err($error) await waku.node.peerManager.disconnectNode(peerId) return ok("") + of DIAL_PEER: + let remotePeerInfo = parsePeerInfo($self[].peerMultiAddr).valueOr: + error "DIAL_PEER failed", error = $error + return err($error) + let conn = await waku.node.peerManager.dialPeer(remotePeerInfo, $self[].protocol) + if conn.isNone(): + let msg = "failed dialing peer" + error "DIAL_PEER failed", error = msg, peerId = $remotePeerInfo.peerId + return err(msg) of DIAL_PEER_BY_ID: let peerId = PeerId.init($self[].peerId).valueOr: error "DIAL_PEER_BY_ID failed", error = $error @@ -102,7 +113,13 @@ proc process*( let conn = await waku.node.peerManager.dialPeer(peerId, $self[].protocol) if conn.isNone(): let msg = "failed dialing peer" - error "DIAL_PEER_BY_ID failed", error = msg + error "DIAL_PEER_BY_ID failed", error = msg, peerId = $peerId return err(msg) + of GET_CONNECTED_PEERS: + ## returns a comma-separated string of peerIDs + let + (inPeerIds, outPeerIds) = waku.node.peerManager.connectedPeers() + connectedPeerids = concat(inPeerIds, outPeerIds) + return ok(connectedPeerids.mapIt($it).join(",")) return ok("")