Skip to content

Fix crashes on SIGINT #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 18, 2019
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
814 changes: 513 additions & 301 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions codechain/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use std::net::SocketAddr;

use crate::rpc_apis;
use crpc::{
jsonrpc_core, start_http, start_ipc, start_ws, HttpServer, IpcServer, MetaIoHandler, Middleware, WsError,
WsErrorKind, WsServer,
jsonrpc_core, start_http, start_ipc, start_ws, HttpServer, IpcServer, MetaIoHandler, Middleware, WsError, WsServer,
};
use futures::future::Either;
use serde_json;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -109,7 +109,7 @@ pub fn rpc_ws_start(
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(WsError::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)),
Expand All @@ -133,8 +133,9 @@ struct LogMiddleware {}

impl<M: jsonrpc_core::Metadata> jsonrpc_core::Middleware<M> for LogMiddleware {
type Future = jsonrpc_core::FutureResponse;
type CallFuture = jsonrpc_core::FutureOutput;

fn on_request<F, X>(&self, request: jsonrpc_core::Request, meta: M, next: F) -> Self::Future
fn on_request<F, X>(&self, request: jsonrpc_core::Request, meta: M, next: F) -> Either<Self::Future, X>
where
F: FnOnce(jsonrpc_core::Request, M) -> X + Send,
X: futures::Future<Item = Option<jsonrpc_core::Response>, Error = ()> + Send + 'static, {
Expand All @@ -146,7 +147,15 @@ impl<M: jsonrpc_core::Metadata> jsonrpc_core::Middleware<M> for LogMiddleware {
}
}
}
Box::new(next(request, meta))
Either::B(next(request, meta))
}

fn on_call<F, X>(&self, call: jsonrpc_core::Call, meta: M, next: F) -> Either<Self::CallFuture, X>
where
F: FnOnce(jsonrpc_core::Call, M) -> X + Send,
X: futures::Future<Item = Option<jsonrpc_core::Output>, Error = ()> + Send + 'static, {
Self::print_call(&call);
Either::B(next(call, meta))
}
}

Expand All @@ -166,7 +175,9 @@ impl LogMiddleware {
);
}
jsonrpc_core::Call::Notification(_) => {}
jsonrpc_core::Call::Invalid(_) => {}
jsonrpc_core::Call::Invalid {
..
} => {}
}
}
}
19 changes: 16 additions & 3 deletions codechain/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,23 +328,23 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
block_sync: maybe_sync_sender,
});

let _rpc_server = {
let rpc_server = {
if !config.rpc.disable.unwrap() {
Some(rpc_http_start(config.rpc_http_config(), config.rpc.enable_devel_api, &*rpc_apis_deps)?)
} else {
None
}
};

let _ipc_server = {
let ipc_server = {
if !config.ipc.disable.unwrap() {
Some(rpc_ipc_start(&config.rpc_ipc_config(), config.rpc.enable_devel_api, &*rpc_apis_deps)?)
} else {
None
}
};

let _ws_server = {
let ws_server = {
if !config.ws.disable.unwrap() {
Some(rpc_ws_start(&config.rpc_ws_config(), config.rpc.enable_devel_api, &*rpc_apis_deps)?)
} else {
Expand Down Expand Up @@ -376,5 +376,18 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {

wait_for_exit();

if let Some(server) = rpc_server {
server.close_handle().close();
server.wait();
}
if let Some(server) = ipc_server {
server.close_handle().close();
server.wait();
}
if let Some(server) = ws_server {
server.close_handle().close();
server.wait().map_err(|err| format!("Error while closing jsonrpc ws server: {}", err))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use map_erro only for ws_server?
rpc_server and ipc_server don't need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't touch the interface. jsonrpc returns Result<()> only for ws_

}

Ok(())
}
10 changes: 5 additions & 5 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ rustc-hex = "1.0"
rustc-serialize = "0.3"
time = "0.1"
tokio-core = "0.1.17"
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
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" }
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", rev = "d1993a8" }
jsonrpc-derive = { git = "https://github.com/paritytech/jsonrpc.git", rev = "d1993a8" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", rev = "d1993a8" }
jsonrpc-ipc-server = { git = "https://github.com/paritytech/jsonrpc.git", rev = "d1993a8" }
jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", rev = "d1993a8" }
5 changes: 2 additions & 3 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ extern crate time;
extern crate tokio_core;

#[macro_use]
extern crate jsonrpc_macros;
extern crate jsonrpc_derive;

pub mod rpc_server;
pub mod v1;

pub use rustc_serialize::hex;

pub use jsonrpc_core::{Compatibility, Error, MetaIoHandler, Middleware, Params, Value};
pub use jsonrpc_http_server::tokio_core::reactor::Remote;

pub use jsonrpc_http_server::Server as HttpServer;
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 jsonrpc_ws_server::{Error as WsError, Server as WsServer};
pub use rpc_server::start_ws;
64 changes: 34 additions & 30 deletions rpc/src/v1/traits/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,38 @@ use primitives::H256;

use super::super::types::{SendTransactionResult, UnsignedTransaction};

build_rpc_trait! {
pub trait Account {
/// Gets a list of accounts
# [rpc(name = "account_getList")]
fn get_account_list(&self) -> Result<Vec<PlatformAddress>>;

/// Creates a new account
# [rpc(name = "account_create")]
fn create_account(&self, Option<Password>) -> Result<PlatformAddress>;

/// Imports a private key
# [rpc(name = "account_importRaw")]
fn create_account_from_secret(&self, H256, Option<Password>) -> Result<PlatformAddress>;

/// Unlocks the specified account for use.
# [rpc(name = "account_unlock")]
fn unlock(&self, PlatformAddress, Password, Option<u64>) -> Result<()>;

/// Calculates the account's signature for a given message
# [rpc(name = "account_sign")]
fn sign(&self, H256, PlatformAddress, Option<Password>) -> Result<Signature>;

/// Sends a transaction with a signature of the account
# [rpc(name = "account_sendTransaction")]
fn send_transaction(&self, UnsignedTransaction, PlatformAddress, Option<Password>) -> Result<SendTransactionResult>;

/// Changes the account's password
# [rpc(name = "account_changePassword")]
fn change_password(&self, PlatformAddress, Password, Password) -> Result<()>;
}
#[rpc(server)]
pub trait Account {
/// Gets a list of accounts
#[rpc(name = "account_getList")]
fn get_account_list(&self) -> Result<Vec<PlatformAddress>>;

/// Creates a new account
#[rpc(name = "account_create")]
fn create_account(&self, passphrase: Option<Password>) -> Result<PlatformAddress>;

/// Imports a private key
#[rpc(name = "account_importRaw")]
fn create_account_from_secret(&self, secret: H256, passphrase: Option<Password>) -> Result<PlatformAddress>;

/// Unlocks the specified account for use.
#[rpc(name = "account_unlock")]
fn unlock(&self, address: PlatformAddress, password: Password, duration: Option<u64>) -> Result<()>;

/// Calculates the account's signature for a given message
#[rpc(name = "account_sign")]
fn sign(&self, message_digest: H256, address: PlatformAddress, passphrase: Option<Password>) -> Result<Signature>;

/// Sends a transaction with a signature of the account
#[rpc(name = "account_sendTransaction")]
fn send_transaction(
&self,
tx: UnsignedTransaction,
platform_address: PlatformAddress,
passphrase: Option<Password>,
) -> Result<SendTransactionResult>;

/// Changes the account's password
#[rpc(name = "account_changePassword")]
fn change_password(&self, address: PlatformAddress, old_password: Password, new_password: Password) -> Result<()>;
}
Loading