mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 06:21:11 +00:00
Allow to expose a subset of unsafe RPCs (#5233)
* sc-cli: Use type-safe constructors for RPC/Prometheus interfaces * service: Simplify rpc handler creation Could probably be further simplifies once [this][commit] lands. [commit]: https://github.com/paritytech/jsonrpc/commit/20485387ed06a48f1a70bf4d609a7cde6cf0accf * service: Streamline some HTTP & WS server start logic * client: Introduce a simple RPC policy mechanism * rpc/system: Check unsafe RPCs * rpc/offchain: Check unsafe RPCs * rpc/author: Check unsafe RPCs
This commit is contained in:
@@ -21,7 +21,7 @@ mod tests;
|
||||
|
||||
use futures::{future::BoxFuture, FutureExt, TryFutureExt};
|
||||
use futures::{channel::oneshot, compat::Compat};
|
||||
use sc_rpc_api::Receiver;
|
||||
use sc_rpc_api::{DenyUnsafe, Receiver};
|
||||
use sp_utils::mpsc::TracingUnboundedSender;
|
||||
use sp_runtime::traits::{self, Header as HeaderT};
|
||||
|
||||
@@ -31,10 +31,19 @@ pub use sc_rpc_api::system::*;
|
||||
pub use self::helpers::{SystemInfo, Health, PeerInfo, NodeRole};
|
||||
pub use self::gen_client::Client as SystemClient;
|
||||
|
||||
macro_rules! bail_if_unsafe {
|
||||
($value: expr) => {
|
||||
if let Err(err) = $value.check_if_safe() {
|
||||
return async move { Err(err.into()) }.boxed().compat();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// System API implementation
|
||||
pub struct System<B: traits::Block> {
|
||||
info: SystemInfo,
|
||||
send_back: TracingUnboundedSender<Request<B>>,
|
||||
deny_unsafe: DenyUnsafe,
|
||||
}
|
||||
|
||||
/// Request to be processed.
|
||||
@@ -66,10 +75,12 @@ impl<B: traits::Block> System<B> {
|
||||
pub fn new(
|
||||
info: SystemInfo,
|
||||
send_back: TracingUnboundedSender<Request<B>>,
|
||||
deny_unsafe: DenyUnsafe,
|
||||
) -> Self {
|
||||
System {
|
||||
info,
|
||||
send_back,
|
||||
deny_unsafe,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,21 +124,37 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
|
||||
Receiver(Compat::new(rx))
|
||||
}
|
||||
|
||||
fn system_peers(&self) -> Receiver<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>> {
|
||||
fn system_peers(&self)
|
||||
-> Compat<BoxFuture<'static, rpc::Result<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>>>>
|
||||
{
|
||||
bail_if_unsafe!(self.deny_unsafe);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::Peers(tx));
|
||||
Receiver(Compat::new(rx))
|
||||
|
||||
async move {
|
||||
rx.await.map_err(|_| rpc::Error::internal_error())
|
||||
}.boxed().compat()
|
||||
}
|
||||
|
||||
fn system_network_state(&self) -> Receiver<rpc::Value> {
|
||||
fn system_network_state(&self)
|
||||
-> Compat<BoxFuture<'static, rpc::Result<rpc::Value>>>
|
||||
{
|
||||
bail_if_unsafe!(self.deny_unsafe);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NetworkState(tx));
|
||||
Receiver(Compat::new(rx))
|
||||
|
||||
async move {
|
||||
rx.await.map_err(|_| rpc::Error::internal_error())
|
||||
}.boxed().compat()
|
||||
}
|
||||
|
||||
fn system_add_reserved_peer(&self, peer: String)
|
||||
-> Compat<BoxFuture<'static, std::result::Result<(), rpc::Error>>>
|
||||
{
|
||||
bail_if_unsafe!(self.deny_unsafe);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NetworkAddReservedPeer(peer, tx));
|
||||
async move {
|
||||
@@ -142,6 +169,8 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
|
||||
fn system_remove_reserved_peer(&self, peer: String)
|
||||
-> Compat<BoxFuture<'static, std::result::Result<(), rpc::Error>>>
|
||||
{
|
||||
bail_if_unsafe!(self.deny_unsafe);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.send_back.unbounded_send(Request::NetworkRemoveReservedPeer(peer, tx));
|
||||
async move {
|
||||
|
||||
Reference in New Issue
Block a user