Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add IPC support (#6348)
Browse files Browse the repository at this point in the history
This is useful for both security and performance reasons. IPC is faster
than TCP, and it is subject to OS access controls.
  • Loading branch information
Demi-Marie committed Jun 16, 2020
1 parent 4368fc6 commit 6907018
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 1 deletion.
80 changes: 79 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions client/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ macro_rules! substrate_cli_subcommands {
}
}

fn rpc_ipc(&self) -> $crate::Result<::std::option::Option<::std::string::String>> {
match self {
$($enum::$variant(cmd) => cmd.rpc_ipc()),*
}
}

fn rpc_http(&self) -> $crate::Result<::std::option::Option<::std::net::SocketAddr>> {
match self {
$($enum::$variant(cmd) => cmd.rpc_http()),*
Expand Down
8 changes: 8 additions & 0 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub struct RunCmd {
#[structopt(long = "prometheus-external")]
pub prometheus_external: bool,

/// Specify IPC RPC server path
#[structopt(long = "ipc-path", value_name = "PATH")]
pub ipc_path: Option<String>,

/// Specify HTTP RPC server TCP port.
#[structopt(long = "rpc-port", value_name = "PORT")]
pub rpc_port: Option<u16>,
Expand Down Expand Up @@ -434,6 +438,10 @@ impl CliConfiguration for RunCmd {
Ok(Some(SocketAddr::new(interface, self.rpc_port.unwrap_or(9933))))
}

fn rpc_ipc(&self) -> Result<Option<String>> {
Ok(self.ipc_path.clone())
}

fn rpc_ws(&self) -> Result<Option<SocketAddr>> {
let interface = rpc_interface(
self.ws_external,
Expand Down
8 changes: 8 additions & 0 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ pub trait CliConfiguration: Sized {
Ok(Default::default())
}

/// Get the RPC IPC path (`None` if disabled).
///
/// By default this is `None`.
fn rpc_ipc(&self) -> Result<Option<String>> {
Ok(Default::default())
}

/// Get the RPC websocket address (`None` if disabled).
///
/// By default this is `None`.
Expand Down Expand Up @@ -451,6 +458,7 @@ pub trait CliConfiguration: Sized {
execution_strategies: self.execution_strategies(is_dev, is_validator)?,
rpc_http: self.rpc_http()?,
rpc_ws: self.rpc_ws()?,
rpc_ipc: self.rpc_ipc()?,
rpc_methods: self.rpc_methods()?,
rpc_ws_max_connections: self.rpc_ws_max_connections()?,
rpc_cors: self.rpc_cors(is_dev)?,
Expand Down
1 change: 1 addition & 0 deletions client/rpc-servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ sp-runtime = { version = "2.0.0-rc3", path = "../../primitives/runtime" }
[target.'cfg(not(target_os = "unknown"))'.dependencies]
http = { package = "jsonrpc-http-server", version = "14.2.0" }
ws = { package = "jsonrpc-ws-server", version = "14.2.0" }
ipc = { version = "14.2.0", package = "jsonrpc-ipc-server" }
19 changes: 19 additions & 0 deletions client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn rpc_handler<M: PubSubMetadata>(
mod inner {
use super::*;

/// Type alias for ipc server
pub type IpcServer = ipc::Server;
/// Type alias for http server
pub type HttpServer = http::Server;
/// Type alias for ws server
Expand Down Expand Up @@ -89,6 +91,23 @@ mod inner {
.start_http(addr)
}

/// Start IPC server listening on given path.
///
/// **Note**: Only available if `not(target_os = "unknown")`.
pub fn start_ipc<M: pubsub::PubSubMetadata + Default>(
addr: &str,
io: RpcHandler<M>,
) -> io::Result<ipc::Server> {
let builder = ipc::ServerBuilder::new(io);
#[cfg(target_os = "unix")]
builder.set_security_attributes({
let security_attributes = ipc::SecurityAttributes::empty();
security_attributes.set_mode(0o600)?;
security_attributes
});
builder.start(addr)
}

/// Start WS server listening on given address.
///
/// **Note**: Only available if `not(target_os = "unknown")`.
Expand Down
2 changes: 2 additions & 0 deletions client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct Configuration {
pub rpc_http: Option<SocketAddr>,
/// RPC over Websockets binding address. `None` if disabled.
pub rpc_ws: Option<SocketAddr>,
/// RPC over IPC binding path. `None` if disabled.
pub rpc_ipc: Option<String>,
/// Maximum number of connections for WebSockets RPC server. `None` if default.
pub rpc_ws_max_connections: Option<usize>,
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed.
Expand Down
11 changes: 11 additions & 0 deletions client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ mod waiting {
}
}

pub struct IpcServer(pub Option<sc_rpc_server::IpcServer>);
impl Drop for IpcServer {
fn drop(&mut self) {
if let Some(server) = self.0.take() {
server.close_handle().close();
let _ = server.wait();
}
}
}

pub struct WsServer(pub Option<sc_rpc_server::WsServer>);
impl Drop for WsServer {
fn drop(&mut self) {
Expand Down Expand Up @@ -555,6 +565,7 @@ fn start_rpc_servers<H: FnMut(sc_rpc::DenyUnsafe) -> sc_rpc_server::RpcHandler<s
}

Ok(Box::new((
config.rpc_ipc.as_ref().map(|path| sc_rpc_server::start_ipc(&*path, gen_handler(sc_rpc::DenyUnsafe::No))),
maybe_start_server(
config.rpc_http,
|address| sc_rpc_server::start_http(
Expand Down
1 change: 1 addition & 0 deletions client/service/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
wasm_method: sc_service::config::WasmExecutionMethod::Interpreted,
execution_strategies: Default::default(),
rpc_http: None,
rpc_ipc: None,
rpc_ws: None,
rpc_ws_max_connections: None,
rpc_cors: None,
Expand Down
1 change: 1 addition & 0 deletions utils/browser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ where
pruning: Default::default(),
rpc_cors: Default::default(),
rpc_http: Default::default(),
rpc_ipc: Default::default(),
rpc_ws: Default::default(),
rpc_ws_max_connections: Default::default(),
rpc_methods: Default::default(),
Expand Down

0 comments on commit 6907018

Please sign in to comment.