From 7205f95cff9f49ca0bb762e8fd0bf56a6a7f3b3b Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:59:12 +0200 Subject: [PATCH] chore: adding origin to peers admin endpoint (#2848) --- tests/wakunode_rest/test_rest_admin.nim | 33 ++++++++++++++++++++++--- waku/waku_api/rest/admin/handlers.nim | 10 ++++++-- waku/waku_api/rest/admin/types.nim | 25 ++++++++++++++++--- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/tests/wakunode_rest/test_rest_admin.nim b/tests/wakunode_rest/test_rest_admin.nim index 5cece660d4..d53b949b65 100644 --- a/tests/wakunode_rest/test_rest_admin.nim +++ b/tests/wakunode_rest/test_rest_admin.nim @@ -37,17 +37,18 @@ suite "Waku v2 Rest API - Admin": asyncSetup: node1 = newTestWakuNode(generateSecp256k1Key(), parseIpAddress("127.0.0.1"), Port(60600)) - peerInfo1 = node1.switch.peerInfo node2 = newTestWakuNode(generateSecp256k1Key(), parseIpAddress("127.0.0.1"), Port(60602)) - peerInfo2 = node2.switch.peerInfo node3 = newTestWakuNode(generateSecp256k1Key(), parseIpAddress("127.0.0.1"), Port(60604)) - peerInfo3 = node3.switch.peerInfo await allFutures(node1.start(), node2.start(), node3.start()) await allFutures(node1.mountRelay(), node2.mountRelay(), node3.mountRelay()) + peerInfo1 = node1.switch.peerInfo + peerInfo2 = node2.switch.peerInfo + peerInfo3 = node3.switch.peerInfo + var restPort = Port(0) let restAddress = parseIpAddress("127.0.0.1") restServer = WakuRestServerRef.init(restAddress, restPort).tryGet() @@ -165,3 +166,29 @@ suite "Waku v2 Rest API - Admin": check: getRes.status == 400 getRes.data == "Error: Filter Protocol is not mounted to the node" + + asyncTest "Get peer origin": + # Adding peers to the Peer Store + node1.peerManager.addPeer(peerInfo2, Discv5) + node1.peerManager.addPeer(peerInfo3, PeerExchange) + + # Connecting to both peers + let conn2 = await node1.peerManager.connectRelay(peerInfo2) + let conn3 = await node1.peerManager.connectRelay(peerInfo3) + + # Check successful connections + check: + conn2 == true + conn3 == true + + # Query peers REST endpoint + let getRes = await client.getPeers() + + check: + getRes.status == 200 + $getRes.contentType == $MIMETYPE_JSON + getRes.data.len() == 2 + # Check peer 2 + getRes.data.anyIt(it.origin == Discv5) + # Check peer 3 + getRes.data.anyIt(it.origin == PeerExchange) diff --git a/waku/waku_api/rest/admin/handlers.nim b/waku/waku_api/rest/admin/handlers.nim index 70c0630dbf..fc523391c4 100644 --- a/waku/waku_api/rest/admin/handlers.nim +++ b/waku/waku_api/rest/admin/handlers.nim @@ -32,11 +32,12 @@ logScope: const ROUTE_ADMIN_V1_PEERS* = "/admin/v1/peers" const ROUTE_ADMIN_V1_FILTER_SUBS* = "/admin/v1/filter/subscriptions" -type PeerProtocolTuple = tuple[multiaddr: string, protocol: string, connected: bool] +type PeerProtocolTuple = + tuple[multiaddr: string, protocol: string, connected: bool, origin: PeerOrigin] proc tuplesToWakuPeers(peers: var WakuPeers, peersTup: seq[PeerProtocolTuple]) = for peer in peersTup: - peers.add(peer.multiaddr, peer.protocol, peer.connected) + peers.add(peer.multiaddr, peer.protocol, peer.connected, peer.origin) proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = router.api(MethodGet, ROUTE_ADMIN_V1_PEERS) do() -> RestApiResponse: @@ -48,6 +49,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = multiaddr: constructMultiaddrStr(it), protocol: WakuRelayCodec, connected: it.connectedness == Connectedness.Connected, + origin: it.origin, ) ) tuplesToWakuPeers(peers, relayPeers) @@ -60,6 +62,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = multiaddr: constructMultiaddrStr(it), protocol: WakuFilterSubscribeCodec, connected: it.connectedness == Connectedness.Connected, + origin: it.origin, ) ) tuplesToWakuPeers(peers, filterV2Peers) @@ -70,6 +73,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = multiaddr: constructMultiaddrStr(it), protocol: WakuStoreCodec, connected: it.connectedness == Connectedness.Connected, + origin: it.origin, ) ) tuplesToWakuPeers(peers, storePeers) @@ -82,6 +86,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = multiaddr: constructMultiaddrStr(it), protocol: WakuLegacyStoreCodec, connected: it.connectedness == Connectedness.Connected, + origin: it.origin, ) ) tuplesToWakuPeers(peers, legacyStorePeers) @@ -93,6 +98,7 @@ proc installAdminV1GetPeersHandler(router: var RestRouter, node: WakuNode) = multiaddr: constructMultiaddrStr(it), protocol: WakuLightPushCodec, connected: it.connectedness == Connectedness.Connected, + origin: it.origin, ) ) tuplesToWakuPeers(peers, lightpushPeers) diff --git a/waku/waku_api/rest/admin/types.nim b/waku/waku_api/rest/admin/types.nim index 6ee1ed86ae..a27d104148 100644 --- a/waku/waku_api/rest/admin/types.nim +++ b/waku/waku_api/rest/admin/types.nim @@ -8,7 +8,7 @@ import json_serialization, json_serialization/std/options, json_serialization/lexer -import ../serdes +import ../serdes, ../../../waku_core #### Types @@ -19,6 +19,7 @@ type ProtocolState* = object type WakuPeer* = object multiaddr*: string protocols*: seq[ProtocolState] + origin*: PeerOrigin type WakuPeers* = seq[WakuPeer] @@ -46,6 +47,7 @@ proc writeValue*( writer.beginRecord() writer.writeField("multiaddr", value.multiaddr) writer.writeField("protocols", value.protocols) + writer.writeField("origin", value.origin) writer.endRecord() proc writeValue*( @@ -100,6 +102,7 @@ proc readValue*( var multiaddr: Option[string] protocols: Option[seq[ProtocolState]] + origin: Option[PeerOrigin] for fieldName in readObjectFields(reader): case fieldName @@ -111,6 +114,10 @@ proc readValue*( if protocols.isSome(): reader.raiseUnexpectedField("Multiple `protocols` fields found", "WakuPeer") protocols = some(reader.readValue(seq[ProtocolState])) + of "origin": + if origin.isSome(): + reader.raiseUnexpectedField("Multiple `origin` fields found", "WakuPeer") + origin = some(reader.readValue(PeerOrigin)) else: unrecognizedFieldWarning() @@ -120,7 +127,12 @@ proc readValue*( if protocols.isNone(): reader.raiseUnexpectedValue("Field `protocols` are missing") - value = WakuPeer(multiaddr: multiaddr.get(), protocols: protocols.get()) + if origin.isNone(): + reader.raiseUnexpectedValue("Field `origin` is missing") + + value = WakuPeer( + multiaddr: multiaddr.get(), protocols: protocols.get(), origin: origin.get() + ) proc readValue*( reader: var JsonReader[RestJson], value: var FilterTopic @@ -196,10 +208,17 @@ func `==`*(a: ProtocolState, b: string): bool {.inline.} = func `==`*(a, b: WakuPeer): bool {.inline.} = return a.multiaddr == b.multiaddr -proc add*(peers: var WakuPeers, multiaddr: string, protocol: string, connected: bool) = +proc add*( + peers: var WakuPeers, + multiaddr: string, + protocol: string, + connected: bool, + origin: PeerOrigin, +) = var peer: WakuPeer = WakuPeer( multiaddr: multiaddr, protocols: @[ProtocolState(protocol: protocol, connected: connected)], + origin: origin, ) let idx = peers.find(peer)