#[cfg(test)]
mod tests;
use futures::channel::oneshot;
use jsonrpsee::{
core::{async_trait, JsonValue},
Extensions,
};
use sc_rpc_api::check_if_safe;
use sc_tracing::logging;
use sc_utils::mpsc::TracingUnboundedSender;
use sp_runtime::traits::{self, Header as HeaderT};
pub use self::helpers::{Health, NodeRole, PeerInfo, SyncState, SystemInfo};
pub use sc_rpc_api::system::*;
pub struct System<B: traits::Block> {
info: SystemInfo,
send_back: TracingUnboundedSender<Request<B>>,
}
pub enum Request<B: traits::Block> {
Health(oneshot::Sender<Health>),
LocalPeerId(oneshot::Sender<String>),
LocalListenAddresses(oneshot::Sender<Vec<String>>),
Peers(oneshot::Sender<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>>),
NetworkState(oneshot::Sender<serde_json::Value>),
NetworkAddReservedPeer(String, oneshot::Sender<error::Result<()>>),
NetworkRemoveReservedPeer(String, oneshot::Sender<error::Result<()>>),
NetworkReservedPeers(oneshot::Sender<Vec<String>>),
NodeRoles(oneshot::Sender<Vec<NodeRole>>),
SyncState(oneshot::Sender<SyncState<<B::Header as HeaderT>::Number>>),
}
impl<B: traits::Block> System<B> {
pub fn new(info: SystemInfo, send_back: TracingUnboundedSender<Request<B>>) -> Self {
System { info, send_back }
}
}
#[async_trait]
impl<B: traits::Block> SystemApiServer<B::Hash, <B::Header as HeaderT>::Number> for System<B> {
fn system_name(&self) -> Result<String, Error> {
Ok(self.info.impl_name.clone())
}
fn system_version(&self) -> Result<String, Error> {
Ok(self.info.impl_version.clone())
}
fn system_chain(&self) -> Result<String, Error> {
Ok(self.info.chain_name.clone())
}
fn system_type(&self) -> Result<sc_chain_spec::ChainType, Error> {
Ok(self.info.chain_type.clone())
}
fn system_properties(&self) -> Result<sc_chain_spec::Properties, Error> {
Ok(self.info.properties.clone())
}
async fn system_health(&self) -> Result<Health, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::Health(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_local_peer_id(&self) -> Result<String, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::LocalPeerId(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_local_listen_addresses(&self) -> Result<Vec<String>, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::LocalListenAddresses(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_peers(
&self,
ext: &Extensions,
) -> Result<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>, Error> {
check_if_safe(ext)?;
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::Peers(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_network_state(&self, ext: &Extensions) -> Result<JsonValue, Error> {
check_if_safe(ext)?;
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkState(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_add_reserved_peer(&self, ext: &Extensions, peer: String) -> Result<(), Error> {
check_if_safe(ext)?;
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkAddReservedPeer(peer, tx));
match rx.await {
Ok(Ok(())) => Ok(()),
Ok(Err(e)) => Err(e),
Err(e) => Err(Error::Internal(e.to_string())),
}
}
async fn system_remove_reserved_peer(
&self,
ext: &Extensions,
peer: String,
) -> Result<(), Error> {
check_if_safe(ext)?;
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkRemoveReservedPeer(peer, tx));
match rx.await {
Ok(Ok(())) => Ok(()),
Ok(Err(e)) => Err(e),
Err(e) => Err(Error::Internal(e.to_string())),
}
}
async fn system_reserved_peers(&self) -> Result<Vec<String>, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkReservedPeers(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_node_roles(&self) -> Result<Vec<NodeRole>, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NodeRoles(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
async fn system_sync_state(&self) -> Result<SyncState<<B::Header as HeaderT>::Number>, Error> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::SyncState(tx));
rx.await.map_err(|e| Error::Internal(e.to_string()))
}
fn system_add_log_filter(&self, ext: &Extensions, directives: String) -> Result<(), Error> {
check_if_safe(ext)?;
logging::add_directives(&directives);
logging::reload_filter().map_err(|e| Error::Internal(e))
}
fn system_reset_log_filter(&self, ext: &Extensions) -> Result<(), Error> {
check_if_safe(ext)?;
logging::reset_log_filter().map_err(|e| Error::Internal(e))
}
}