diff --git a/parity/configuration.rs b/parity/configuration.rs index d6a50d398..8f7e93805 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -25,6 +25,7 @@ use docopt::Docopt; use die::*; use util::*; use util::keys::store::AccountService; +use util::network_settings::NetworkSettings; use ethcore::client::{append_path, get_db_path, ClientConfig}; use ethcore::ethereum; use ethcore::spec::Spec; @@ -236,5 +237,17 @@ impl Configuration { } account_service } + + pub fn network_settings(&self) -> NetworkSettings { + NetworkSettings { + name: self.args.flag_identity.clone(), + chain: self.args.flag_chain.clone(), + max_peers: self.args.flag_maxpeers.unwrap_or(self.args.flag_peers), + network_port: self.args.flag_port, + rpc_enabled: self.args.flag_rpc || self.args.flag_jsonrpc, + rpc_interface: self.args.flag_rpcaddr.clone().unwrap_or(self.args.flag_jsonrpc_interface.clone()), + rpc_port: self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port), + } + } } diff --git a/parity/main.rs b/parity/main.rs index 267ae3d54..1cc9fffd0 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -153,6 +153,8 @@ fn execute_client(conf: Configuration) { miner.set_minimal_gas_price(conf.gas_price()); miner.set_transactions_limit(conf.args.flag_tx_limit); + let network_settings = Arc::new(conf.network_settings()); + // Sync let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone()); @@ -168,7 +170,8 @@ fn execute_client(conf: Configuration) { sync: sync.clone(), secret_store: account_service.clone(), miner: miner.clone(), - logger: logger.clone() + logger: logger.clone(), + settings: network_settings.clone(), }); let webapp_server = webapp::new(webapp::Configuration { @@ -182,7 +185,8 @@ fn execute_client(conf: Configuration) { sync: sync.clone(), secret_store: account_service.clone(), miner: miner.clone(), - logger: logger.clone() + logger: logger.clone(), + settings: network_settings.clone(), }); // Register IO handler diff --git a/parity/rpc.rs b/parity/rpc.rs index b2e6391d8..8cb3b4d17 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -23,6 +23,7 @@ use ethsync::EthSync; use ethminer::Miner; use util::RotatingLogger; use util::keys::store::{AccountService}; +use util::network_settings::NetworkSettings; use die::*; #[cfg(feature = "rpc")] @@ -46,6 +47,7 @@ pub struct Dependencies { pub secret_store: Arc, pub miner: Arc, pub logger: Arc, + pub settings: Arc, } pub fn new(conf: Configuration, deps: Dependencies) -> Option { @@ -94,7 +96,7 @@ pub fn setup_rpc_server( server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate()); }, "personal" => server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()), - "ethcore" => server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone()).to_delegate()), + "ethcore" => server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate()), _ => { die!("{}: Invalid API name to be enabled.", api); }, diff --git a/parity/webapp.rs b/parity/webapp.rs index 74e8c2e51..a4598fb4a 100644 --- a/parity/webapp.rs +++ b/parity/webapp.rs @@ -22,6 +22,7 @@ use ethsync::EthSync; use ethminer::Miner; use util::RotatingLogger; use util::keys::store::{AccountService}; +use util::network_settings::NetworkSettings; use die::*; #[cfg(feature = "webapp")] @@ -43,6 +44,7 @@ pub struct Dependencies { pub secret_store: Arc, pub miner: Arc, pub logger: Arc, + pub settings: Arc, } pub fn new(configuration: Configuration, deps: Dependencies) -> Option { @@ -96,7 +98,7 @@ pub fn setup_webapp_server( server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner).to_delegate()); server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate()); server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()); - server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger).to_delegate()); + server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger, deps.settings).to_delegate()); let start_result = match auth { None => { diff --git a/rpc/src/v1/impls/ethcore.rs b/rpc/src/v1/impls/ethcore.rs index 533aad80e..492f5810c 100644 --- a/rpc/src/v1/impls/ethcore.rs +++ b/rpc/src/v1/impls/ethcore.rs @@ -16,8 +16,10 @@ //! Ethcore-specific rpc implementation. use util::{U256, Address, RotatingLogger}; +use util::network_settings::NetworkSettings; use std::sync::{Arc, Weak}; use std::ops::Deref; +use std::collections::BTreeMap; use jsonrpc_core::*; use ethminer::{MinerService}; use v1::traits::Ethcore; @@ -28,14 +30,16 @@ pub struct EthcoreClient where M: MinerService { miner: Weak, logger: Arc, + settings: Arc, } impl EthcoreClient where M: MinerService { /// Creates new `EthcoreClient`. - pub fn new(miner: &Arc, logger: Arc) -> Self { + pub fn new(miner: &Arc, logger: Arc, settings: Arc) -> Self { EthcoreClient { miner: Arc::downgrade(miner), logger: logger, + settings: settings, } } } @@ -102,4 +106,27 @@ impl Ethcore for EthcoreClient where M: MinerService + 'static { to_value(&self.logger.levels()) } + fn net_chain(&self, _params: Params) -> Result { + to_value(&self.settings.chain) + } + + fn net_max_peers(&self, _params: Params) -> Result { + to_value(&self.settings.max_peers) + } + + fn net_port(&self, _params: Params) -> Result { + to_value(&self.settings.network_port) + } + + fn node_name(&self, _params: Params) -> Result { + to_value(&self.settings.name) + } + + fn rpc_settings(&self, _params: Params) -> Result { + let mut map = BTreeMap::new(); + map.insert("enabled".to_owned(), Value::Bool(self.settings.rpc_enabled)); + map.insert("interface".to_owned(), Value::String(self.settings.rpc_interface.clone())); + map.insert("port".to_owned(), Value::U64(self.settings.rpc_port as u64)); + Ok(Value::Object(map)) + } } diff --git a/rpc/src/v1/tests/ethcore.rs b/rpc/src/v1/tests/ethcore.rs index 4544541dd..a2e06acc2 100644 --- a/rpc/src/v1/tests/ethcore.rs +++ b/rpc/src/v1/tests/ethcore.rs @@ -23,6 +23,7 @@ use v1::tests::helpers::TestMinerService; use util::numbers::*; use rustc_serialize::hex::FromHex; use util::log::RotatingLogger; +use util::network_settings::NetworkSettings; fn miner_service() -> Arc { @@ -32,10 +33,26 @@ fn logger() -> Arc { Arc::new(RotatingLogger::new("rpc=trace".to_owned())) } +fn settings() -> Arc { + Arc::new(NetworkSettings { + name: "mynode".to_owned(), + chain: "testchain".to_owned(), + max_peers: 25, + network_port: 30303, + rpc_enabled: true, + rpc_interface: "all".to_owned(), + rpc_port: 8545, + }) +} + +fn ethcore_client(miner: &Arc) -> EthcoreClient { + EthcoreClient::new(&miner, logger(), settings()) +} + #[test] fn rpc_ethcore_extra_data() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -49,7 +66,7 @@ fn rpc_ethcore_extra_data() { #[test] fn rpc_ethcore_gas_floor_target() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -62,7 +79,7 @@ fn rpc_ethcore_gas_floor_target() { #[test] fn rpc_ethcore_min_gas_price() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -75,7 +92,7 @@ fn rpc_ethcore_min_gas_price() { #[test] fn rpc_ethcore_set_min_gas_price() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -89,7 +106,7 @@ fn rpc_ethcore_set_min_gas_price() { #[test] fn rpc_ethcore_set_gas_floor_target() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -103,7 +120,7 @@ fn rpc_ethcore_set_gas_floor_target() { #[test] fn rpc_ethcore_set_extra_data() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -117,7 +134,7 @@ fn rpc_ethcore_set_extra_data() { #[test] fn rpc_ethcore_set_author() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -134,7 +151,7 @@ fn rpc_ethcore_dev_logs() { let logger = logger(); logger.append("a".to_owned()); logger.append("b".to_owned()); - let ethcore = EthcoreClient::new(&miner, logger.clone()).to_delegate(); + let ethcore = EthcoreClient::new(&miner, logger.clone(), settings()).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -147,7 +164,7 @@ fn rpc_ethcore_dev_logs() { #[test] fn rpc_ethcore_dev_logs_levels() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -159,7 +176,7 @@ fn rpc_ethcore_dev_logs_levels() { #[test] fn rpc_ethcore_set_transactions_limit() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -170,11 +187,10 @@ fn rpc_ethcore_set_transactions_limit() { assert_eq!(miner.transactions_limit(), 10_240_240); } - #[test] fn rpc_ethcore_transactions_limit() { let miner = miner_service(); - let ethcore = EthcoreClient::new(&miner, logger()).to_delegate(); + let ethcore = ethcore_client(&miner).to_delegate(); let io = IoHandler::new(); io.add_delegate(ethcore); @@ -183,3 +199,68 @@ fn rpc_ethcore_transactions_limit() { assert_eq!(io.handle_request(request), Some(response.to_owned())); } + +#[test] +fn rpc_ethcore_net_chain() { + let miner = miner_service(); + let ethcore = ethcore_client(&miner).to_delegate(); + let io = IoHandler::new(); + io.add_delegate(ethcore); + + let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netChain", "params":[], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"testchain","id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn rpc_ethcore_net_max_peers() { + let miner = miner_service(); + let ethcore = ethcore_client(&miner).to_delegate(); + let io = IoHandler::new(); + io.add_delegate(ethcore); + + let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netMaxPeers", "params":[], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":25,"id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn rpc_ethcore_net_port() { + let miner = miner_service(); + let ethcore = ethcore_client(&miner).to_delegate(); + let io = IoHandler::new(); + io.add_delegate(ethcore); + + let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netPort", "params":[], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":30303,"id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn rpc_ethcore_rpc_settings() { + let miner = miner_service(); + let ethcore = ethcore_client(&miner).to_delegate(); + let io = IoHandler::new(); + io.add_delegate(ethcore); + + let request = r#"{"jsonrpc": "2.0", "method": "ethcore_rpcSettings", "params":[], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":{"enabled":true,"interface":"all","port":8545},"id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn rpc_ethcore_node_name() { + let miner = miner_service(); + let ethcore = ethcore_client(&miner).to_delegate(); + let io = IoHandler::new(); + io.add_delegate(ethcore); + + let request = r#"{"jsonrpc": "2.0", "method": "ethcore_nodeName", "params":[], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"mynode","id":1}"#; + + assert_eq!(io.handle_request(request), Some(response.to_owned())); +} diff --git a/rpc/src/v1/traits/ethcore.rs b/rpc/src/v1/traits/ethcore.rs index f00d7bf9a..9aab17b4c 100644 --- a/rpc/src/v1/traits/ethcore.rs +++ b/rpc/src/v1/traits/ethcore.rs @@ -54,6 +54,21 @@ pub trait Ethcore: Sized + Send + Sync + 'static { /// Returns logs levels fn dev_logs_levels(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Returns chain name + fn net_chain(&self, _: Params) -> Result { rpc_unimplemented!() } + + /// Returns max peers + fn net_max_peers(&self, _: Params) -> Result { rpc_unimplemented!() } + + /// Returns network port + fn net_port(&self, _: Params) -> Result { rpc_unimplemented!() } + + /// Returns rpc settings + fn rpc_settings(&self, _: Params) -> Result { rpc_unimplemented!() } + + /// Returns node name + fn node_name(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Should be used to convert object to io delegate. fn to_delegate(self) -> IoDelegate { let mut delegate = IoDelegate::new(Arc::new(self)); @@ -69,6 +84,11 @@ pub trait Ethcore: Sized + Send + Sync + 'static { delegate.add_method("ethcore_transactionsLimit", Ethcore::transactions_limit); delegate.add_method("ethcore_devLogs", Ethcore::dev_logs); delegate.add_method("ethcore_devLogsLevels", Ethcore::dev_logs_levels); + delegate.add_method("ethcore_netChain", Ethcore::net_chain); + delegate.add_method("ethcore_netMaxPeers", Ethcore::net_max_peers); + delegate.add_method("ethcore_netPort", Ethcore::net_port); + delegate.add_method("ethcore_rpcSettings", Ethcore::rpc_settings); + delegate.add_method("ethcore_nodeName", Ethcore::node_name); delegate } diff --git a/util/src/lib.rs b/util/src/lib.rs index dfdb300f6..530f2b4c5 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -147,6 +147,7 @@ pub mod log; pub mod panics; pub mod keys; pub mod table; +pub mod network_settings; pub use common::*; pub use misc::*; diff --git a/util/src/network_settings.rs b/util/src/network_settings.rs new file mode 100644 index 000000000..723266ade --- /dev/null +++ b/util/src/network_settings.rs @@ -0,0 +1,35 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . +//! Structure to hold network settings configured from CLI + +/// Networking & RPC settings +pub struct NetworkSettings { + /// Node name + pub name: String, + /// Name of the chain we are connected to + pub chain: String, + /// Ideal number of peers + pub max_peers: usize, + /// Networking port + pub network_port: u16, + /// Is JSON-RPC server enabled? + pub rpc_enabled: bool, + /// Interface that JSON-RPC listens on + pub rpc_interface: String, + /// Port for JSON-RPC server + pub rpc_port: u16, +} +