mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 05:11:09 +00:00
RPCs for reserved peers (#4423)
* RPC forwarders for adding reserved peers * implement service side of reserved-peer RPCs * Clean up unnecessary format! invocation Co-Authored-By: Niklas Adolfsson <niklasadolfsson1@gmail.com> * add some tests for the new RPC methods * remove redundant `data` field Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
637d95e5ea
commit
da87c3c1e3
@@ -19,9 +19,11 @@
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use futures::{future::BoxFuture, FutureExt, TryFutureExt};
|
||||
use futures::{channel::{mpsc, oneshot}, compat::Compat};
|
||||
use sc_rpc_api::Receiver;
|
||||
use sp_runtime::traits::{self, Header as HeaderT};
|
||||
|
||||
use self::error::Result;
|
||||
|
||||
pub use sc_rpc_api::system::*;
|
||||
@@ -42,6 +44,10 @@ pub enum Request<B: traits::Block> {
|
||||
Peers(oneshot::Sender<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>>),
|
||||
/// Must return the state of the network.
|
||||
NetworkState(oneshot::Sender<rpc::Value>),
|
||||
/// Must return any potential parse error.
|
||||
NetworkAddReservedPeer(String, oneshot::Sender<Result<()>>),
|
||||
/// Must return any potential parse error.
|
||||
NetworkRemoveReservedPeer(String, oneshot::Sender<Result<()>>),
|
||||
/// Must return the node role.
|
||||
NodeRoles(oneshot::Sender<Vec<NodeRole>>)
|
||||
}
|
||||
@@ -53,7 +59,7 @@ impl<B: traits::Block> System<B> {
|
||||
/// reading from that channel and answering the requests.
|
||||
pub fn new(
|
||||
info: SystemInfo,
|
||||
send_back: mpsc::UnboundedSender<Request<B>>
|
||||
send_back: mpsc::UnboundedSender<Request<B>>,
|
||||
) -> Self {
|
||||
System {
|
||||
info,
|
||||
@@ -97,6 +103,34 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
|
||||
Receiver(Compat::new(rx))
|
||||
}
|
||||
|
||||
fn system_add_reserved_peer(&self, peer: String)
|
||||
-> Compat<BoxFuture<'static, std::result::Result<(), rpc::Error>>>
|
||||
{
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NetworkAddReservedPeer(peer, tx));
|
||||
async move {
|
||||
match rx.await {
|
||||
Ok(Ok(())) => Ok(()),
|
||||
Ok(Err(e)) => Err(rpc::Error::from(e)),
|
||||
Err(_) => Err(rpc::Error::internal_error()),
|
||||
}
|
||||
}.boxed().compat()
|
||||
}
|
||||
|
||||
fn system_remove_reserved_peer(&self, peer: String)
|
||||
-> Compat<BoxFuture<'static, std::result::Result<(), rpc::Error>>>
|
||||
{
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NetworkRemoveReservedPeer(peer, tx));
|
||||
async move {
|
||||
match rx.await {
|
||||
Ok(Ok(())) => Ok(()),
|
||||
Ok(Err(e)) => Err(rpc::Error::from(e)),
|
||||
Err(_) => Err(rpc::Error::internal_error()),
|
||||
}
|
||||
}.boxed().compat()
|
||||
}
|
||||
|
||||
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NodeRoles(tx));
|
||||
|
||||
Reference in New Issue
Block a user