Adjust the RPC service for the WASM-browser use case (#3013)

* Use SpawnTaskHandle to pass to the RPC

* Create the RPC server in lib.rs

* Create the RPC servers in a separate function

* Keep a local version of the RPC handlers

* Make rpc-servers compile for WASM

* Add RpcSesssion

* Clean up

* Address review

* Address pull request review
This commit is contained in:
Pierre Krieger
2019-07-05 09:55:50 +02:00
committed by Gavin Wood
parent 13164304b3
commit 8bca52128f
5 changed files with 199 additions and 160 deletions
+15 -51
View File
@@ -16,12 +16,12 @@
//! Substrate service components.
use std::{sync::Arc, net::SocketAddr, ops::Deref, ops::DerefMut};
use std::{sync::Arc, ops::Deref, ops::DerefMut};
use serde::{Serialize, de::DeserializeOwned};
use crate::chain_spec::ChainSpec;
use client_db;
use client::{self, Client, runtime_api};
use crate::{error, Service, maybe_start_server};
use crate::{error, Service};
use consensus_common::{import_queue::ImportQueue, SelectChain};
use network::{self, OnDemand, FinalityProofProvider};
use substrate_executor::{NativeExecutor, NativeExecutionDispatch};
@@ -32,7 +32,6 @@ use runtime_primitives::{
use crate::config::Configuration;
use primitives::{Blake2Hasher, H256};
use rpc::{self, apis::system::SystemInfo};
use parking_lot::Mutex;
use futures::{prelude::*, future::Executor, sync::mpsc};
// Type aliases.
@@ -144,72 +143,37 @@ impl<T: Serialize + DeserializeOwned + BuildStorage> RuntimeGenesis for T {}
/// Something that can start the RPC service.
pub trait StartRPC<C: Components> {
type ServersHandle: Send + Sync;
fn start_rpc(
client: Arc<ComponentClient<C>>,
system_send_back: mpsc::UnboundedSender<rpc::apis::system::Request<ComponentBlock<C>>>,
system_info: SystemInfo,
rpc_http: Option<SocketAddr>,
rpc_ws: Option<SocketAddr>,
rpc_ws_max_connections: Option<usize>,
rpc_cors: Option<Vec<String>>,
task_executor: TaskExecutor,
transaction_pool: Arc<TransactionPool<C::TransactionPoolApi>>,
) -> error::Result<Self::ServersHandle>;
) -> rpc::RpcHandler;
}
impl<C: Components> StartRPC<Self> for C where
ComponentClient<C>: ProvideRuntimeApi,
<ComponentClient<C> as ProvideRuntimeApi>::Api: runtime_api::Metadata<ComponentBlock<C>>,
{
type ServersHandle = (Option<rpc::HttpServer>, Option<Mutex<rpc::WsServer>>);
fn start_rpc(
client: Arc<ComponentClient<C>>,
system_send_back: mpsc::UnboundedSender<rpc::apis::system::Request<ComponentBlock<C>>>,
rpc_system_info: SystemInfo,
rpc_http: Option<SocketAddr>,
rpc_ws: Option<SocketAddr>,
rpc_ws_max_connections: Option<usize>,
rpc_cors: Option<Vec<String>>,
task_executor: TaskExecutor,
transaction_pool: Arc<TransactionPool<C::TransactionPoolApi>>,
) -> error::Result<Self::ServersHandle> {
let handler = || {
let client = client.clone();
let subscriptions = rpc::apis::Subscriptions::new(task_executor.clone());
let chain = rpc::apis::chain::Chain::new(client.clone(), subscriptions.clone());
let state = rpc::apis::state::State::new(client.clone(), subscriptions.clone());
let author = rpc::apis::author::Author::new(
client.clone(), transaction_pool.clone(), subscriptions
);
let system = rpc::apis::system::System::new(
rpc_system_info.clone(), system_send_back.clone()
);
rpc::rpc_handler::<ComponentBlock<C>, ComponentExHash<C>, _, _, _, _>(
state,
chain,
author,
system,
)
};
Ok((
maybe_start_server(
rpc_http,
|address| rpc::start_http(address, rpc_cors.as_ref(), handler()),
)?,
maybe_start_server(
rpc_ws,
|address| rpc::start_ws(
address,
rpc_ws_max_connections,
rpc_cors.as_ref(),
handler(),
),
)?.map(Mutex::new),
))
) -> rpc::RpcHandler {
let subscriptions = rpc::apis::Subscriptions::new(task_executor.clone());
let chain = rpc::apis::chain::Chain::new(client.clone(), subscriptions.clone());
let state = rpc::apis::state::State::new(client.clone(), subscriptions.clone());
let author = rpc::apis::author::Author::new(client, transaction_pool, subscriptions);
let system = rpc::apis::system::System::new(rpc_system_info, system_send_back);
rpc::rpc_handler::<ComponentBlock<C>, ComponentExHash<C>, _, _, _, _>(
state,
chain,
author,
system,
)
}
}