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

Simple signing queue, confirmation APIs exposed in signer WebSockets. #1182

Merged
merged 18 commits into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from 16 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
5 changes: 0 additions & 5 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions dapps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern crate serde_json;
extern crate jsonrpc_core;
extern crate jsonrpc_http_server;
extern crate parity_dapps;
extern crate ethcore_rpc;

mod endpoint;
mod apps;
Expand All @@ -66,6 +67,7 @@ use std::net::SocketAddr;
use std::collections::HashMap;
use jsonrpc_core::{IoHandler, IoDelegate};
use router::auth::{Authorization, NoAuth, HttpBasicAuth};
use ethcore_rpc::Extendable;

static DAPPS_DOMAIN : &'static str = ".parity";

Expand All @@ -74,6 +76,12 @@ pub struct ServerBuilder {
handler: Arc<IoHandler>,
}

impl Extendable for ServerBuilder {
fn add_delegate<D: Send + Sync + 'static>(&self, delegate: IoDelegate<D>) {
self.handler.add_delegate(delegate);
}
}

impl ServerBuilder {
/// Construct new dapps server
pub fn new() -> Self {
Expand All @@ -82,11 +90,6 @@ impl ServerBuilder {
}
}

/// Add io delegate.
pub fn add_delegate<D>(&self, delegate: IoDelegate<D>) where D: Send + Sync + 'static {
self.handler.add_delegate(delegate);
}

/// Asynchronously start server with no authentication,
/// returns result with `Server` handle on success or an error.
pub fn start_unsecure_http(&self, addr: &SocketAddr) -> Result<Server, ServerError> {
Expand Down
24 changes: 3 additions & 21 deletions parity/dapps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
use std::sync::Arc;
use std::str::FromStr;
use std::net::SocketAddr;
use ethcore::client::Client;
use ethsync::EthSync;
use ethminer::{Miner, ExternalMiner};
use util::RotatingLogger;
use util::panics::PanicHandler;
use util::keys::store::AccountService;
use util::network_settings::NetworkSettings;
use die::*;
use rpc_apis;

#[cfg(feature = "dapps")]
pub use ethcore_dapps::Server as WebappServer;
Expand All @@ -41,13 +36,7 @@ pub struct Configuration {

pub struct Dependencies {
pub panic_handler: Arc<PanicHandler>,
pub client: Arc<Client>,
pub sync: Arc<EthSync>,
pub secret_store: Arc<AccountService>,
pub miner: Arc<Miner>,
pub external_miner: Arc<ExternalMiner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
pub apis: Arc<rpc_apis::Dependencies>,
}

pub fn new(configuration: Configuration, deps: Dependencies) -> Option<WebappServer> {
Expand Down Expand Up @@ -92,17 +81,10 @@ pub fn setup_dapps_server(
url: &SocketAddr,
auth: Option<(String, String)>
) -> WebappServer {
use ethcore_rpc::v1::*;
use ethcore_dapps as dapps;

let server = dapps::ServerBuilder::new();
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(NetClient::new(&deps.sync).to_delegate());
server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner, &deps.external_miner).to_delegate());
server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate());
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate());
server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate());

let server = rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::UnsafeContext);
let start_result = match auth {
None => {
server.start_unsecure_http(url)
Expand Down
27 changes: 12 additions & 15 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mod cli;
mod configuration;
mod migration;
mod signer;
mod rpc_apis;

use std::io::{Write, Read, BufReader, BufRead};
use std::ops::Deref;
Expand Down Expand Up @@ -195,8 +196,9 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
// Sync
let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone());

let dependencies = Arc::new(rpc::Dependencies {
panic_handler: panic_handler.clone(),
let deps_for_rpc_apis = Arc::new(rpc_apis::Dependencies {
signer_enabled: conf.args.flag_signer,
signer_queue: Arc::new(rpc_apis::ConfirmationsQueue::default()),
client: client.clone(),
sync: sync.clone(),
secret_store: account_service.clone(),
Expand All @@ -206,6 +208,11 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
settings: network_settings.clone(),
});

let dependencies = rpc::Dependencies {
panic_handler: panic_handler.clone(),
apis: deps_for_rpc_apis.clone(),
};

// Setup http rpc
let rpc_server = rpc::new_http(rpc::HttpConfiguration {
enabled: network_settings.rpc_enabled,
Expand All @@ -227,26 +234,16 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
pass: conf.args.flag_dapps_pass.clone(),
}, dapps::Dependencies {
panic_handler: panic_handler.clone(),
client: client.clone(),
sync: sync.clone(),
secret_store: account_service.clone(),
miner: miner.clone(),
external_miner: external_miner.clone(),
logger: logger.clone(),
settings: network_settings.clone(),
apis: deps_for_rpc_apis.clone(),
});

// Set up a signer
let signer_server = signer::start(signer::Configuration {
enabled: conf.args.flag_signer,
enabled: deps_for_rpc_apis.signer_enabled,
port: conf.args.flag_signer_port,
}, signer::Dependencies {
panic_handler: panic_handler.clone(),
client: client.clone(),
sync: sync.clone(),
secret_store: account_service.clone(),
miner: miner.clone(),
external_miner: external_miner.clone(),
apis: deps_for_rpc_apis.clone(),
});

// Register IO handler
Expand Down
79 changes: 18 additions & 61 deletions parity/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.


use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::Arc;
use std::net::SocketAddr;
use ethcore::client::Client;
use ethsync::EthSync;
use ethminer::{Miner, ExternalMiner};
use util::RotatingLogger;
use util::panics::PanicHandler;
use util::keys::store::AccountService;
use util::network_settings::NetworkSettings;
use die::*;
use jsonipc;
use rpc_apis;

#[cfg(feature = "rpc")]
pub use ethcore_rpc::Server as RpcServer;
Expand All @@ -52,16 +46,10 @@ pub struct IpcConfiguration {

pub struct Dependencies {
pub panic_handler: Arc<PanicHandler>,
pub client: Arc<Client>,
pub sync: Arc<EthSync>,
pub secret_store: Arc<AccountService>,
pub miner: Arc<Miner>,
pub external_miner: Arc<ExternalMiner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
pub apis: Arc<rpc_apis::Dependencies>,
}

pub fn new_http(conf: HttpConfiguration, deps: &Arc<Dependencies>) -> Option<RpcServer> {
pub fn new_http(conf: HttpConfiguration, deps: &Dependencies) -> Option<RpcServer> {
if !conf.enabled {
return None;
}
Expand All @@ -78,86 +66,55 @@ pub fn new_http(conf: HttpConfiguration, deps: &Arc<Dependencies>) -> Option<Rpc
Some(setup_http_rpc_server(deps, &addr, conf.cors, apis))
}

pub fn new_ipc(conf: IpcConfiguration, deps: &Arc<Dependencies>) -> Option<jsonipc::Server> {
pub fn new_ipc(conf: IpcConfiguration, deps: &Dependencies) -> Option<jsonipc::Server> {
if !conf.enabled { return None; }
let apis = conf.apis.split(',').collect();
Some(setup_ipc_rpc_server(deps, &conf.socket_addr, apis))
}

fn setup_rpc_server(apis: Vec<&str>, deps: &Arc<Dependencies>) -> Server {
use ethcore_rpc::v1::*;

fn setup_rpc_server(apis: Vec<&str>, deps: &Dependencies) -> Server {
let apis = rpc_apis::from_str(apis);
let server = Server::new();
let mut modules = BTreeMap::new();
for api in apis.into_iter() {
match api {
"web3" => {
modules.insert("web3".to_owned(), "1.0".to_owned());
server.add_delegate(Web3Client::new().to_delegate());
},
"net" => {
modules.insert("net".to_owned(), "1.0".to_owned());
server.add_delegate(NetClient::new(&deps.sync).to_delegate());
},
"eth" => {
modules.insert("eth".to_owned(), "1.0".to_owned());
server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner, &deps.external_miner).to_delegate());
server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate());
},
"personal" => {
modules.insert("personal".to_owned(), "1.0".to_owned());
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate())
},
"ethcore" => {
modules.insert("ethcore".to_owned(), "1.0".to_owned());
server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate())
},
"traces" => {
modules.insert("traces".to_owned(), "1.0".to_owned());
server.add_delegate(TracesClient::new(&deps.client).to_delegate())
},
_ => {
die!("{}: Invalid API name to be enabled.", api);
},
}
}
server.add_delegate(RpcClient::new(modules).to_delegate());
server
rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::List(apis))
}

#[cfg(not(feature = "rpc"))]
pub fn setup_http_rpc_server(
_deps: Dependencies,
_deps: &Dependencies,
_url: &SocketAddr,
_cors_domain: Option<String>,
_cors_domain: Vec<String>,
_apis: Vec<&str>,
) -> ! {
die!("Your Parity version has been compiled without JSON-RPC support.")
}

#[cfg(feature = "rpc")]
pub fn setup_http_rpc_server(
dependencies: &Arc<Dependencies>,
dependencies: &Dependencies,
url: &SocketAddr,
cors_domains: Vec<String>,
apis: Vec<&str>,
) -> RpcServer {
let server = setup_rpc_server(apis, dependencies);
let start_result = server.start_http(url, cors_domains);
let deps = dependencies.clone();
let ph = dependencies.panic_handler.clone();
match start_result {
Err(RpcServerError::IoError(err)) => die_with_io_error("RPC", err),
Err(e) => die!("RPC: {:?}", e),
Ok(server) => {
server.set_panic_handler(move || {
deps.panic_handler.notify_all("Panic in RPC thread.".to_owned());
ph.notify_all("Panic in RPC thread.".to_owned());
});
server
},
}
}

pub fn setup_ipc_rpc_server(dependencies: &Arc<Dependencies>, addr: &str, apis: Vec<&str>) -> jsonipc::Server {
#[cfg(not(feature = "rpc"))]
pub fn setup_ipc_rpc_server(_dependencies: &Dependencies, _addr: &str, _apis: Vec<&str>) -> ! {
die!("Your Parity version has been compiled without JSON-RPC support.")
}
#[cfg(feature = "rpc")]
pub fn setup_ipc_rpc_server(dependencies: &Dependencies, addr: &str, apis: Vec<&str>) -> jsonipc::Server {
let server = setup_rpc_server(apis, dependencies);
match server.start_ipc(addr) {
Err(jsonipc::Error::Io(io_error)) => die_with_io_error("RPC", io_error),
Expand Down
Loading