mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-27 23:27:27 +00:00
chore: adding origin to peers admin endpoint (#2848)
This commit is contained in:
parent
974b8a39ac
commit
7205f95cff
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user