Skip to content

Commit

Permalink
chore: return all connected peers from REST API (#2923)
Browse files Browse the repository at this point in the history
* Remove the condition of gathering connected peers with relay and user req/resp protocols.
* Return PeerExchange protocol support of connected nodes with /admin/peers
* Added test for checking return of PeerExchange mounted protocol of connected peer by GET /admin/peers

---------

Co-authored-by: NagyZoltanPeter <[email protected]>
  • Loading branch information
weboko and NagyZoltanPeter authored Jul 23, 2024
1 parent 086cc8e commit a29eca7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 56 deletions.
16 changes: 14 additions & 2 deletions tests/wakunode_rest/test_rest_admin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
109 changes: 55 additions & 54 deletions waku/waku_api/rest/admin/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import
../../../waku_filter_v2,
../../../waku_lightpush/common,
../../../waku_relay,
../../../waku_peer_exchange,
../../../waku_node,
../../../node/peer_manager,
../responses,
Expand All @@ -40,65 +41,65 @@ 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,
)
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, 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)

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, 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)

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, 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)

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, legacyStorePeers)

let lightpushPeers = node.peerManager.peerStore.peers(WakuLightPushCodec).mapIt(
(
multiaddr: constructMultiaddrStr(it),
protocol: WakuLightPushCodec,
connected: it.connectedness == Connectedness.Connected,
origin: it.origin,
)
tuplesToWakuPeers(peers, lightpushPeers)
)
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():
Expand Down

0 comments on commit a29eca7

Please sign in to comment.