Handle RPC requests in the substrate-service (#2866)

* Rework RPC queries

* Remove SyncProvider trait

* Fix RPC tests
This commit is contained in:
Pierre Krieger
2019-06-15 15:44:04 +02:00
committed by Gavin Wood
parent c48aebe897
commit f7bd56d2a8
10 changed files with 180 additions and 133 deletions
+4 -5
View File
@@ -34,6 +34,7 @@ use crate::config::Configuration;
use primitives::{Blake2Hasher, H256};
use rpc::{self, apis::system::SystemInfo};
use parking_lot::Mutex;
use futures::sync::mpsc;
// Type aliases.
// These exist mainly to avoid typing `<F as Factory>::Foo` all over the code.
@@ -139,8 +140,7 @@ pub trait StartRPC<C: Components> {
fn start_rpc(
client: Arc<ComponentClient<C>>,
network: Arc<dyn network::SyncProvider<ComponentBlock<C>>>,
should_have_peers: bool,
system_send_back: mpsc::UnboundedSender<rpc::apis::system::Request<ComponentBlock<C>>>,
system_info: SystemInfo,
rpc_http: Option<SocketAddr>,
rpc_ws: Option<SocketAddr>,
@@ -159,8 +159,7 @@ impl<C: Components> StartRPC<Self> for C where
fn start_rpc(
client: Arc<ComponentClient<C>>,
network: Arc<dyn network::SyncProvider<ComponentBlock<C>>>,
should_have_peers: bool,
system_send_back: mpsc::UnboundedSender<rpc::apis::system::Request<ComponentBlock<C>>>,
rpc_system_info: SystemInfo,
rpc_http: Option<SocketAddr>,
rpc_ws: Option<SocketAddr>,
@@ -178,7 +177,7 @@ impl<C: Components> StartRPC<Self> for C where
client.clone(), transaction_pool.clone(), subscriptions
);
let system = rpc::apis::system::System::new(
rpc_system_info.clone(), network.clone(), should_have_peers
rpc_system_info.clone(), system_send_back.clone()
);
rpc::rpc_handler::<ComponentBlock<C>, ComponentExHash<C>, _, _, _, _>(
state,
+40 -3
View File
@@ -41,7 +41,6 @@ use primitives::Pair;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Header, SaturatedConversion};
use substrate_executor::NativeExecutor;
use network::SyncProvider;
use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
use tel::{telemetry, SUBSTRATE_INFO};
@@ -384,10 +383,10 @@ impl<Components: components::Components> Service<Components> {
impl_version: config.impl_version.into(),
properties: config.chain_spec.properties(),
};
let (system_rpc_tx, system_rpc_rx) = mpsc::unbounded();
let rpc = Components::RuntimeServices::start_rpc(
client.clone(),
network.clone(),
has_bootnodes,
system_rpc_tx,
system_info,
config.rpc_http,
config.rpc_ws,
@@ -396,6 +395,11 @@ impl<Components: components::Components> Service<Components> {
task_executor.clone(),
transaction_pool.clone(),
)?;
task_executor.spawn(build_system_rpc_handler::<Components>(
network.clone(),
system_rpc_rx,
has_bootnodes
));
let telemetry_connection_sinks: Arc<Mutex<Vec<mpsc::UnboundedSender<()>>>> = Default::default();
@@ -611,6 +615,39 @@ impl<C: Components> network::TransactionPool<ComponentExHash<C>, ComponentBlock<
}
}
/// Builds a never-ending `Future` that answers the RPC requests coming on the receiver.
fn build_system_rpc_handler<Components: components::Components>(
network: Arc<NetworkService<Components::Factory>>,
rx: mpsc::UnboundedReceiver<rpc::apis::system::Request<ComponentBlock<Components>>>,
should_have_peers: bool,
) -> impl Future<Item = (), Error = ()> {
rx.for_each(move |request| {
match request {
rpc::apis::system::Request::Health(sender) => {
let _ = sender.send(rpc::apis::system::Health {
peers: network.peers_debug_info().len(),
is_syncing: network.is_major_syncing(),
should_have_peers,
});
},
rpc::apis::system::Request::Peers(sender) => {
let _ = sender.send(network.peers_debug_info().into_iter().map(|(peer_id, p)| rpc::apis::system::PeerInfo {
peer_id: peer_id.to_base58(),
roles: format!("{:?}", p.roles),
protocol_version: p.protocol_version,
best_hash: p.best_hash,
best_number: p.best_number,
}).collect());
}
rpc::apis::system::Request::NetworkState(sender) => {
let _ = sender.send(network.network_state());
}
};
Ok(())
})
}
/// Constructs a service factory with the given name that implements the `ServiceFactory` trait.
/// The required parameters are required to be given in the exact order. Some parameters are followed
/// by `{}` blocks. These blocks are required and used to initialize the given parameter.