Skip to content

Commit

Permalink
chore: adding origin to peers admin endpoint (#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Jun 26, 2024
1 parent 974b8a3 commit 7205f95
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
33 changes: 30 additions & 3 deletions tests/wakunode_rest/test_rest_admin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
10 changes: 8 additions & 2 deletions waku/waku_api/rest/admin/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions waku/waku_api/rest/admin/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import
json_serialization,
json_serialization/std/options,
json_serialization/lexer
import ../serdes
import ../serdes, ../../../waku_core

#### Types

Expand All @@ -19,6 +19,7 @@ type ProtocolState* = object
type WakuPeer* = object
multiaddr*: string
protocols*: seq[ProtocolState]
origin*: PeerOrigin

type WakuPeers* = seq[WakuPeer]

Expand Down Expand Up @@ -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*(
Expand Down Expand Up @@ -100,6 +102,7 @@ proc readValue*(
var
multiaddr: Option[string]
protocols: Option[seq[ProtocolState]]
origin: Option[PeerOrigin]

for fieldName in readObjectFields(reader):
case fieldName
Expand All @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 7205f95

Please sign in to comment.