Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 100 additions & 27 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions codechain/codechain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ args:
takes_value: true
conflicts_with:
- no-ipc
- ws-interface:
long: ws-interface
value_name: INTERFACE
help: Specify the interface address for the WebSockets JSON-RPC server.
takes_value: true
conflicts_with:
- no-ws
- ws-port:
long: ws-port
value_name: PORT
help: Specify the port portion of the WebSockets JSON-RPC server.
takes_value: true
conflicts_with:
- no-ws
- ws-max-connections:
long: ws-max-connections
value_name: CONN
help: Maximum number of allowed concurrent WebSockets JSON-RPC connections.
takes_value: true
conflicts_with:
- no-ws
- no-ws:
long: no-ws
help: Do not run the WebSockets JSON-RPC server.
takes_value: false
- no-jsonrpc:
long: no-jsonrpc
help: Do not run jsonrpc.
Expand Down
43 changes: 42 additions & 1 deletion codechain/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ccore::{MinerOptions, ShardValidatorConfig, StratumConfig};
use ckey::PlatformAddress;
use clap;
use cnetwork::{NetworkConfig, SocketAddr};
use rpc::{RpcHttpConfig, RpcIpcConfig};
use rpc::{RpcHttpConfig, RpcIpcConfig, RpcWsConfig};
use toml;

pub use self::chain_type::ChainType;
Expand All @@ -39,6 +39,7 @@ pub struct Config {
pub mining: Mining,
pub network: Network,
pub rpc: Rpc,
pub ws: Ws,
pub snapshot: Snapshot,
pub stratum: Stratum,
pub shard_validator: ShardValidator,
Expand Down Expand Up @@ -109,6 +110,17 @@ impl Config {
}
}

pub fn rpc_ws_config(&self) -> RpcWsConfig {
debug_assert!(!self.ws.disable.unwrap());

// FIXME: Add hosts and origins options.
RpcWsConfig {
interface: self.ws.interface.clone().unwrap(),
port: self.ws.port.unwrap(),
max_connections: self.ws.max_connections.unwrap(),
}
}

pub fn network_config(&self) -> Result<NetworkConfig, String> {
debug_assert!(!self.network.disable.unwrap());

Expand Down Expand Up @@ -235,6 +247,15 @@ pub struct Rpc {
pub enable_devel_api: bool,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Ws {
pub disable: Option<bool>,
pub interface: Option<String>,
pub port: Option<u16>,
pub max_connections: Option<usize>,
}

fn default_enable_devel_api() -> bool {
cfg!(debug_assertions)
}
Expand Down Expand Up @@ -536,6 +557,25 @@ impl Rpc {
}
}

impl Ws {
pub fn overwrite_with(&mut self, matches: &clap::ArgMatches) -> Result<(), String> {
if matches.is_present("no-ws") {
self.disable = Some(true);
}

if let Some(interface) = matches.value_of("ws-interface") {
self.interface = Some(interface.to_string());
}
if let Some(port) = matches.value_of("ws-port") {
self.port = Some(port.parse().map_err(|_| "Invalid port")?);
}
if let Some(max_connections) = matches.value_of("ws-max-connections") {
self.max_connections = Some(max_connections.parse().map_err(|_| "Invalid max connections")?);
}
Ok(())
}
}

impl Snapshot {
pub fn merge(&mut self, other: &Snapshot) {
if other.disable.is_some() {
Expand Down Expand Up @@ -633,6 +673,7 @@ pub fn load_config(matches: &clap::ArgMatches) -> Result<Config, String> {
config.mining.overwrite_with(&matches)?;
config.network.overwrite_with(&matches)?;
config.rpc.overwrite_with(&matches)?;
config.ws.overwrite_with(&matches)?;
config.snapshot.overwrite_with(&matches)?;
config.stratum.overwrite_with(&matches)?;
config.shard_validator.overwrite_with(&matches)?;
Expand Down
6 changes: 6 additions & 0 deletions codechain/config/presets/config.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ port = 8080
disable = false
path = "/tmp/jsonrpc.ipc"

[ws]
disable = false
interface = "127.0.0.1"
port = 8081
max_connections = 100

[snapshot]
disable = false
path = "snapshot"
Expand Down
6 changes: 6 additions & 0 deletions codechain/config/presets/config.prod.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ port = 8080
disable = true
path = "/tmp/jsonrpc.ipc"

[ws]
disable = true
interface = "127.0.0.1"
port = 8081
max_connections = 100

[snapshot]
disable = true
path = "snapshot"
Expand Down
30 changes: 29 additions & 1 deletion codechain/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io;
use std::net::SocketAddr;
use std::sync::Arc;

use crpc::{start_http, start_ipc, HttpServer, IpcServer};
use crpc::{start_http, start_ipc, start_ws, HttpServer, IpcServer, WsError, WsErrorKind, WsServer};
use crpc::{Compatibility, MetaIoHandler};
use rpc_apis;

Expand Down Expand Up @@ -84,6 +84,34 @@ pub fn rpc_ipc_start(
}
}

#[derive(Debug, PartialEq)]
pub struct RpcWsConfig {
pub interface: String,
pub port: u16,
pub max_connections: usize,
}

pub fn rpc_ws_start(
cfg: RpcWsConfig,
enable_devel_api: bool,
deps: Arc<rpc_apis::ApiDependencies>,
) -> Result<WsServer, String> {
let server = setup_rpc_server(enable_devel_api, deps);
let url = format!("{}:{}", cfg.interface, cfg.port);
let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?;
let start_result = start_ws(&addr, server, cfg.max_connections);
match start_result {
Err(WsError(WsErrorKind::Io(ref err), _)) if err.kind() == io::ErrorKind::AddrInUse => {
Err(format!("WebSockets address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ws-port options.", addr))
},
Err(e) => Err(format!("WebSockets error: {:?}", e)),
Ok(server) => {
cinfo!(RPC, "WebSockets Listening on {}", addr);
Ok(server)
},
}
}

fn setup_rpc_server(enable_devel_api: bool, deps: Arc<rpc_apis::ApiDependencies>) -> MetaIoHandler<()> {
let mut handler = MetaIoHandler::with_compatibility(Compatibility::Both);
deps.extend_api(enable_devel_api, &mut handler);
Expand Down
10 changes: 9 additions & 1 deletion codechain/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use super::config::{self, load_config};
use super::constants::DEFAULT_KEYS_PATH;
use super::dummy_network_service::DummyNetworkService;
use super::json::PasswordFile;
use super::rpc::{rpc_http_start, rpc_ipc_start};
use super::rpc::{rpc_http_start, rpc_ipc_start, rpc_ws_start};
use super::rpc_apis::ApiDependencies;

fn network_start(cfg: &NetworkConfig) -> Result<Arc<NetworkService>, String> {
Expand Down Expand Up @@ -296,6 +296,14 @@ pub fn run_node(matches: ArgMatches) -> Result<(), String> {
}
};

let _ws_server = {
if !config.ws.disable.unwrap() {
Some(rpc_ws_start(config.rpc_ws_config(), config.rpc.enable_devel_api, Arc::clone(&rpc_apis_deps))?)
} else {
None
}
};

if (!config.stratum.disable.unwrap()) && (miner.engine_type() == EngineType::PoW) {
stratum_start(config.stratum_config(), Arc::clone(&miner), client.client())?
}
Expand Down
1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "pa
jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-ipc-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
4 changes: 4 additions & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern crate codechain_types as ctypes;
extern crate jsonrpc_core;
extern crate jsonrpc_http_server;
extern crate jsonrpc_ipc_server;
extern crate jsonrpc_ws_server;
extern crate kvdb;
extern crate kvdb_rocksdb as rocksdb;
#[macro_use]
Expand Down Expand Up @@ -55,3 +56,6 @@ pub use rpc_server::start_http;

pub use jsonrpc_ipc_server::Server as IpcServer;
pub use rpc_server::start_ipc;

pub use jsonrpc_ws_server::{Error as WsError, ErrorKind as WsErrorKind, Server as WsServer};
pub use rpc_server::start_ws;
13 changes: 13 additions & 0 deletions rpc/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use jsonrpc_core;
use jsonrpc_http_server::{self, Host, Server as HttpServer, ServerBuilder as HttpServerBuilder};
use jsonrpc_ipc_server::{Server as IpcServer, ServerBuilder as IpcServerBuilder};
use jsonrpc_ws_server::{Error as WsError, Server as WsServer, ServerBuilder as WsServerBuilder};
use std::default::Default;
use std::io;
use std::net::SocketAddr;
Expand Down Expand Up @@ -57,3 +58,15 @@ where
M: Default, {
IpcServerBuilder::new(handler).start(addr)
}

/// Start WS server and return `Server` handle.
pub fn start_ws<M: jsonrpc_core::Metadata>(
addr: &SocketAddr,
handler: jsonrpc_core::MetaIoHandler<M>,
max_connections: usize,
) -> Result<WsServer, WsError>
where
M: Default, {
// FIXME: Add Hosts, Origins and Session States
WsServerBuilder::new(handler).max_connections(max_connections).start(addr)
}