Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: return all connected peers from REST API #2923

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading