From fc9cea422fd40b0eda8e41ca80aeec6fe1f044a3 Mon Sep 17 00:00:00 2001 From: Sasha Date: Sat, 20 Jul 2024 00:37:59 +0200 Subject: [PATCH 1/4] chore: return all connected peers from REST API --- waku/waku_api/rest/admin/handlers.nim | 62 +-------------------------- 1 file changed, 1 insertion(+), 61 deletions(-) diff --git a/waku/waku_api/rest/admin/handlers.nim b/waku/waku_api/rest/admin/handlers.nim index 1ac7a79d59..c66b421aae 100644 --- a/waku/waku_api/rest/admin/handlers.nim +++ b/waku/waku_api/rest/admin/handlers.nim @@ -38,67 +38,7 @@ proc tuplesToWakuPeers(peers: var WakuPeers, peersTup: seq[PeerProtocolTuple]) = proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = router.api(MethodGet, ROUTE_ADMIN_V1_PEERS) do() -> RestApiResponse: - var peers: WakuPeers = @[] - - if not node.wakuRelay.isNil(): - let relayPeers = node.peerManager.peerStore.peers(WakuRelayCodec).mapIt( - ( - multiaddr: constructMultiaddrStr(it), - protocol: WakuRelayCodec, - connected: it.connectedness == Connectedness.Connected, - origin: it.origin, - ) - ) - tuplesToWakuPeers(peers, relayPeers) - - if not node.wakuFilter.isNil(): - let filterV2Peers = node.peerManager.peerStore - .peers(WakuFilterSubscribeCodec) - .mapIt( - ( - multiaddr: constructMultiaddrStr(it), - protocol: WakuFilterSubscribeCodec, - connected: it.connectedness == Connectedness.Connected, - origin: it.origin, - ) - ) - tuplesToWakuPeers(peers, filterV2Peers) - - if not node.wakuStore.isNil(): - let storePeers = node.peerManager.peerStore.peers(WakuStoreCodec).mapIt( - ( - multiaddr: constructMultiaddrStr(it), - protocol: WakuStoreCodec, - connected: it.connectedness == Connectedness.Connected, - origin: it.origin, - ) - ) - tuplesToWakuPeers(peers, storePeers) - - if not node.wakuLegacyStore.isNil(): - let legacyStorePeers = node.peerManager.peerStore - .peers(WakuLegacyStoreCodec) - .mapIt( - ( - multiaddr: constructMultiaddrStr(it), - protocol: WakuLegacyStoreCodec, - connected: it.connectedness == Connectedness.Connected, - origin: it.origin, - ) - ) - tuplesToWakuPeers(peers, legacyStorePeers) - - if not node.wakuLightPush.isNil(): - # Map WakuStore peers to WakuPeers and add to return list - let lightpushPeers = node.peerManager.peerStore.peers(WakuLightPushCodec).mapIt( - ( - multiaddr: constructMultiaddrStr(it), - protocol: WakuLightPushCodec, - connected: it.connectedness == Connectedness.Connected, - origin: it.origin, - ) - ) - tuplesToWakuPeers(peers, lightpushPeers) + var peers: WakuPeers = node.peerManager.peerStore.getConnectedPeers() let resp = RestApiResponse.jsonResponse(peers, status = Http200) if resp.isErr(): From 0f1b382c1eb142e415178f2d0f538a2e3a654b12 Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:38:00 +0200 Subject: [PATCH 2/4] Remove the condition of gathering connected peers with relay and user req/resp protocols. --- waku/waku_api/rest/admin/handlers.nim | 57 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/waku/waku_api/rest/admin/handlers.nim b/waku/waku_api/rest/admin/handlers.nim index c66b421aae..9320e5f97e 100644 --- a/waku/waku_api/rest/admin/handlers.nim +++ b/waku/waku_api/rest/admin/handlers.nim @@ -38,7 +38,62 @@ proc tuplesToWakuPeers(peers: var WakuPeers, peersTup: seq[PeerProtocolTuple]) = proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = router.api(MethodGet, ROUTE_ADMIN_V1_PEERS) do() -> RestApiResponse: - var peers: WakuPeers = node.peerManager.peerStore.getConnectedPeers() + var peers: WakuPeers = @[] + + let relayPeers = node.peerManager.peerStore.peers(WakuRelayCodec).mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuRelayCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, relayPeers) + + let filterV2Peers = node.peerManager.peerStore + .peers(WakuFilterSubscribeCodec) + .mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuFilterSubscribeCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, filterV2Peers) + + let storePeers = node.peerManager.peerStore.peers(WakuStoreCodec).mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuStoreCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, storePeers) + + let legacyStorePeers = node.peerManager.peerStore + .peers(WakuLegacyStoreCodec) + .mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuLegacyStoreCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, legacyStorePeers) + + # Map WakuStore peers to WakuPeers and add to return list + let lightpushPeers = node.peerManager.peerStore.peers(WakuLightPushCodec).mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuLightPushCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, lightpushPeers) let resp = RestApiResponse.jsonResponse(peers, status = Http200) if resp.isErr(): From a9a27d3396e7d5f442fdbb8d30a8a023e56afbef Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:30:18 +0200 Subject: [PATCH 3/4] Return PeerExchange protocol support of connected nodes with /admin/peers --- waku/waku_api/rest/admin/handlers.nim | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/waku/waku_api/rest/admin/handlers.nim b/waku/waku_api/rest/admin/handlers.nim index 9320e5f97e..435d19de25 100644 --- a/waku/waku_api/rest/admin/handlers.nim +++ b/waku/waku_api/rest/admin/handlers.nim @@ -14,6 +14,7 @@ import ../../../waku_filter_v2, ../../../waku_lightpush/common, ../../../waku_relay, + ../../../waku_peer_exchange, ../../../waku_node, ../../../node/peer_manager, ../responses, @@ -50,9 +51,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = ) tuplesToWakuPeers(peers, relayPeers) - let filterV2Peers = node.peerManager.peerStore - .peers(WakuFilterSubscribeCodec) - .mapIt( + let filterV2Peers = node.peerManager.peerStore.peers(WakuFilterSubscribeCodec).mapIt( ( multiaddr: constructMultiaddrStr(it), protocol: WakuFilterSubscribeCodec, @@ -72,9 +71,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = ) tuplesToWakuPeers(peers, storePeers) - let legacyStorePeers = node.peerManager.peerStore - .peers(WakuLegacyStoreCodec) - .mapIt( + let legacyStorePeers = node.peerManager.peerStore.peers(WakuLegacyStoreCodec).mapIt( ( multiaddr: constructMultiaddrStr(it), protocol: WakuLegacyStoreCodec, @@ -84,7 +81,6 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = ) tuplesToWakuPeers(peers, legacyStorePeers) - # Map WakuStore peers to WakuPeers and add to return list let lightpushPeers = node.peerManager.peerStore.peers(WakuLightPushCodec).mapIt( ( multiaddr: constructMultiaddrStr(it), @@ -95,6 +91,16 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = ) tuplesToWakuPeers(peers, lightpushPeers) + let pxPeers = node.peerManager.peerStore.peers(WakuPeerExchangeCodec).mapIt( + ( + multiaddr: constructMultiaddrStr(it), + protocol: WakuPeerExchangeCodec, + connected: it.connectedness == Connectedness.Connected, + origin: it.origin, + ) + ) + tuplesToWakuPeers(peers, pxPeers) + let resp = RestApiResponse.jsonResponse(peers, status = Http200) if resp.isErr(): error "An error ocurred while building the json respose: ", error = resp.error From 01f9d393f403d5494abc518976e92360ab1be1f1 Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:22:58 +0200 Subject: [PATCH 4/4] Added test for checking return of PeerExchange mounted protocol of connected peer by GET /admin/peers --- tests/wakunode_rest/test_rest_admin.nim | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/wakunode_rest/test_rest_admin.nim b/tests/wakunode_rest/test_rest_admin.nim index d7b83d23bf..b6c9336d08 100644 --- a/tests/wakunode_rest/test_rest_admin.nim +++ b/tests/wakunode_rest/test_rest_admin.nim @@ -21,6 +21,7 @@ import waku_api/rest/admin/handlers as admin_api, waku_api/rest/admin/client as admin_api_client, waku_relay, + waku_peer_exchange, ], ../testlib/wakucore, ../testlib/wakunode, @@ -45,7 +46,12 @@ suite "Waku v2 Rest API - Admin": newTestWakuNode(generateSecp256k1Key(), parseIpAddress("127.0.0.1"), Port(60604)) await allFutures(node1.start(), node2.start(), node3.start()) - await allFutures(node1.mountRelay(), node2.mountRelay(), node3.mountRelay()) + await allFutures( + node1.mountRelay(), + node2.mountRelay(), + node3.mountRelay(), + node3.mountPeerExchange(), + ) peerInfo1 = node1.switch.peerInfo peerInfo2 = node2.switch.peerInfo @@ -94,6 +100,12 @@ suite "Waku v2 Rest API - Admin": it.multiaddr == constructMultiaddrStr(peerInfo3) ) + # Check peer 3 + getRes.data.anyIt( + it.protocols.find(WakuPeerExchangeCodec) >= 0 and + it.multiaddr == constructMultiaddrStr(peerInfo3) + ) + asyncTest "Set wrong peer": let nonExistentPeer = "/ip4/0.0.0.0/tcp/10000/p2p/16Uiu2HAm6HZZr7aToTvEBPpiys4UxajCTU97zj5v7RNR2gbniy1D" @@ -170,7 +182,7 @@ suite "Waku v2 Rest API - Admin": getRes.data == "Error: Filter Protocol is not mounted to the node" asyncTest "Get peer origin": - # Adding peers to the Peer Store + # Adding peers to the Peer Store node1.peerManager.addPeer(peerInfo2, Discv5) node1.peerManager.addPeer(peerInfo3, PeerExchange)