Skip to content

Commit

Permalink
feat: add a protected RPC to disconnect from a peer
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed May 11, 2021
1 parent 0cdb09a commit 2a7553c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion network/src/peers/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<S: Storage + Send + Sync + 'static> Node<S> {
/// as disconnected from this node server.
///
#[inline]
pub(crate) fn disconnect_from_peer(&self, remote_address: SocketAddr) -> Result<(), NetworkError> {
pub fn disconnect_from_peer(&self, remote_address: SocketAddr) -> Result<(), NetworkError> {
// Set the peer as disconnected in the peer book.
let result = self.peer_book.set_disconnected(remote_address);

Expand Down
20 changes: 20 additions & 0 deletions rpc/documentation/private_endpoints/disconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Disconnects the node from the given address.

### Protected Endpoint

Yes

### Arguments

| Parameter | Type | Required | Description |
|:-------------------:|:------:|:--------:|:---------------------------------------------- |
| `address` | string | Yes | The address to disconnect in an IP:port format |

### Response

null

### Example
```ignore
curl --user username:password --data-binary '{"jsonrpc": "2.0", "id":"1", "method": "disconnect", "params": ["127.0.0.1:4141"] }' -H 'content-type: application/json' http://127.0.0.1:3030/
```
23 changes: 22 additions & 1 deletion rpc/src/rpc_impl_protected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use snarkvm_utilities::{
use itertools::Itertools;
use jsonrpc_http_server::jsonrpc_core::{IoDelegate, MetaIoHandler, Params, Value};
use rand::{thread_rng, Rng};
use std::{str::FromStr, sync::Arc};
use std::{net::SocketAddr, str::FromStr, sync::Arc};

type JsonRPCError = jsonrpc_core::Error;

Expand Down Expand Up @@ -245,6 +245,23 @@ impl<S: Storage + Send + Sync + 'static> RpcImpl<S> {
}
}

/// Disconnects from the given address
pub async fn disconnect_protected(self, params: Params, meta: Meta) -> Result<Value, JsonRPCError> {
self.validate_auth(meta)?;

let value = match params {
Params::Array(arr) => arr,
_ => return Err(JsonRPCError::invalid_request()),
};

let address: SocketAddr = serde_json::from_value(value[0].clone())
.map_err(|e| JsonRPCError::invalid_params(format!("Invalid params: {}.", e)))?;

let _ = self.node.disconnect_from_peer(address);

Ok(Value::Null)
}

/// Expose the protected functions as RPC enpoints
pub fn add_protected(&self, io: &mut MetaIoHandler<Meta>) {
let mut d = IoDelegate::<Self, Meta>::new(Arc::new(self.clone()));
Expand Down Expand Up @@ -285,6 +302,10 @@ impl<S: Storage + Send + Sync + 'static> RpcImpl<S> {
let rpc = rpc.clone();
rpc.create_account_protected(params, meta)
});
d.add_method_with_meta("disconnect", |rpc, params, meta| {
let rpc = rpc.clone();
rpc.disconnect_protected(params, meta)
});

io.extend_with(d)
}
Expand Down

0 comments on commit 2a7553c

Please sign in to comment.