mirror of
https://github.com/waku-org/nwaku.git
synced 2025-02-05 19:43:41 +00:00
Add post method to Admin API for ad-hoc peer dialing (#412)
* Add post method to Admin API * Updated CHANGELOG
This commit is contained in:
parent
55096cbd1d
commit
e6b26cc059
@ -7,6 +7,7 @@
|
|||||||
- PubSub topic `subscribe` and `unsubscribe` no longer returns a future (removed `async` designation)
|
- PubSub topic `subscribe` and `unsubscribe` no longer returns a future (removed `async` designation)
|
||||||
- Added a peer manager for `relay`, `filter`, `store` and `swap` peers.
|
- Added a peer manager for `relay`, `filter`, `store` and `swap` peers.
|
||||||
- `relay`, `filter`, `store` and `swap` peers are now stored in a common, shared peer store and no longer in separate sets.
|
- `relay`, `filter`, `store` and `swap` peers are now stored in a common, shared peer store and no longer in separate sets.
|
||||||
|
- Admin API now provides a `post` method to connect to peers on an ad-hoc basis
|
||||||
|
|
||||||
## 2021-01-05 v0.2
|
## 2021-01-05 v0.2
|
||||||
|
|
||||||
|
@ -367,6 +367,62 @@ procSuite "Waku v2 JSON-RPC API":
|
|||||||
server.close()
|
server.close()
|
||||||
waitfor node.stop()
|
waitfor node.stop()
|
||||||
|
|
||||||
|
asyncTest "Admin API: connect to ad-hoc peers":
|
||||||
|
# Create a couple of nodes
|
||||||
|
let
|
||||||
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
|
node1 = WakuNode.init(nodeKey1, ValidIpAddress.init("0.0.0.0"),
|
||||||
|
Port(60000))
|
||||||
|
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
|
node2 = WakuNode.init(nodeKey2, ValidIpAddress.init("0.0.0.0"),
|
||||||
|
Port(60002))
|
||||||
|
peerInfo2 = node2.peerInfo
|
||||||
|
nodeKey3 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
|
node3 = WakuNode.init(nodeKey3, ValidIpAddress.init("0.0.0.0"),
|
||||||
|
Port(60004))
|
||||||
|
peerInfo3 = node3.peerInfo
|
||||||
|
|
||||||
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
||||||
|
|
||||||
|
node1.mountRelay()
|
||||||
|
node2.mountRelay()
|
||||||
|
node3.mountRelay()
|
||||||
|
|
||||||
|
# RPC server setup
|
||||||
|
let
|
||||||
|
rpcPort = Port(8545)
|
||||||
|
ta = initTAddress(bindIp, rpcPort)
|
||||||
|
server = newRpcHttpServer([ta])
|
||||||
|
|
||||||
|
installAdminApiHandlers(node1, server)
|
||||||
|
server.start()
|
||||||
|
|
||||||
|
let client = newRpcHttpClient()
|
||||||
|
await client.connect("127.0.0.1", rpcPort)
|
||||||
|
|
||||||
|
# Connect to nodes 2 and 3 using the Admin API
|
||||||
|
let postRes = await client.post_waku_v2_admin_v1_peers(@[constructMultiaddrStr(peerInfo2),
|
||||||
|
constructMultiaddrStr(peerInfo3)])
|
||||||
|
|
||||||
|
check:
|
||||||
|
postRes
|
||||||
|
|
||||||
|
# Verify that newly connected peers are being managed
|
||||||
|
let getRes = await client.get_waku_v2_admin_v1_peers()
|
||||||
|
|
||||||
|
check:
|
||||||
|
getRes.len == 2
|
||||||
|
# Check peer 2
|
||||||
|
getRes.anyIt(it.protocol == WakuRelayCodec and
|
||||||
|
it.multiaddr == constructMultiaddrStr(peerInfo2))
|
||||||
|
# Check peer 3
|
||||||
|
getRes.anyIt(it.protocol == WakuRelayCodec and
|
||||||
|
it.multiaddr == constructMultiaddrStr(peerInfo3))
|
||||||
|
|
||||||
|
server.stop()
|
||||||
|
server.close()
|
||||||
|
await allFutures([node1.stop(), node2.stop(), node3.stop()])
|
||||||
|
|
||||||
asyncTest "Admin API: get managed peer information":
|
asyncTest "Admin API: get managed peer information":
|
||||||
# Create a couple of nodes
|
# Create a couple of nodes
|
||||||
let
|
let
|
||||||
|
@ -14,6 +14,8 @@ import
|
|||||||
|
|
||||||
export jsonrpc_types
|
export jsonrpc_types
|
||||||
|
|
||||||
|
const futTimeout* = 30.seconds # Max time to wait for futures
|
||||||
|
|
||||||
proc constructMultiaddrStr*(wireaddr: MultiAddress, peerId: PeerId): string =
|
proc constructMultiaddrStr*(wireaddr: MultiAddress, peerId: PeerId): string =
|
||||||
# Constructs a multiaddress with both wire address and p2p identity
|
# Constructs a multiaddress with both wire address and p2p identity
|
||||||
$wireaddr & "/p2p/" & $peerId
|
$wireaddr & "/p2p/" & $peerId
|
||||||
@ -26,6 +28,17 @@ proc installAdminApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
|
|||||||
|
|
||||||
## Admin API version 1 definitions
|
## Admin API version 1 definitions
|
||||||
|
|
||||||
|
rpcsrv.rpc("post_waku_v2_admin_v1_peers") do(peers: seq[string]) -> bool:
|
||||||
|
## Connect to a list of peers
|
||||||
|
debug "post_waku_v2_admin_v1_peers"
|
||||||
|
|
||||||
|
if (await node.connectToNodes(peers).withTimeout(futTimeout)):
|
||||||
|
# Successfully connected to peers
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
# Failed to connect to peers
|
||||||
|
raise newException(ValueError, "Failed to connect to peers: " & $peers)
|
||||||
|
|
||||||
rpcsrv.rpc("get_waku_v2_admin_v1_peers") do() -> seq[WakuPeer]:
|
rpcsrv.rpc("get_waku_v2_admin_v1_peers") do() -> seq[WakuPeer]:
|
||||||
## Returns history for a list of content topics with optional paging
|
## Returns history for a list of content topics with optional paging
|
||||||
debug "get_waku_v2_admin_v1_peers"
|
debug "get_waku_v2_admin_v1_peers"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# Admin API
|
# Admin API
|
||||||
|
|
||||||
proc get_waku_v2_admin_v1_peers(): seq[WakuPeer]
|
proc get_waku_v2_admin_v1_peers(): seq[WakuPeer]
|
||||||
|
proc post_waku_v2_admin_v1_peers(peers: seq[string]): bool
|
||||||
|
|
||||||
# Debug API
|
# Debug API
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user