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

Commit

Permalink
Merge pull request #983 from ethcore/rpc-settings
Browse files Browse the repository at this point in the history
Additional RPC methods for settings
  • Loading branch information
arkpar committed Apr 22, 2016
2 parents 08a0d42 + 0458a33 commit 11b0daf
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 17 deletions.
13 changes: 13 additions & 0 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
}
}
}

8 changes: 6 additions & 2 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion parity/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -46,6 +47,7 @@ pub struct Dependencies {
pub secret_store: Arc<AccountService>,
pub miner: Arc<Miner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
}

pub fn new(conf: Configuration, deps: Dependencies) -> Option<RpcServer> {
Expand Down Expand Up @@ -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);
},
Expand Down
4 changes: 3 additions & 1 deletion parity/webapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -43,6 +44,7 @@ pub struct Dependencies {
pub secret_store: Arc<AccountService>,
pub miner: Arc<Miner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
}

pub fn new(configuration: Configuration, deps: Dependencies) -> Option<WebappServer> {
Expand Down Expand Up @@ -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 => {
Expand Down
29 changes: 28 additions & 1 deletion rpc/src/v1/impls/ethcore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,14 +30,16 @@ pub struct EthcoreClient<M>
where M: MinerService {
miner: Weak<M>,
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
}

impl<M> EthcoreClient<M> where M: MinerService {
/// Creates new `EthcoreClient`.
pub fn new(miner: &Arc<M>, logger: Arc<RotatingLogger>) -> Self {
pub fn new(miner: &Arc<M>, logger: Arc<RotatingLogger>, settings: Arc<NetworkSettings>) -> Self {
EthcoreClient {
miner: Arc::downgrade(miner),
logger: logger,
settings: settings,
}
}
}
Expand Down Expand Up @@ -102,4 +106,27 @@ impl<M> Ethcore for EthcoreClient<M> where M: MinerService + 'static {
to_value(&self.logger.levels())
}

fn net_chain(&self, _params: Params) -> Result<Value, Error> {
to_value(&self.settings.chain)
}

fn net_max_peers(&self, _params: Params) -> Result<Value, Error> {
to_value(&self.settings.max_peers)
}

fn net_port(&self, _params: Params) -> Result<Value, Error> {
to_value(&self.settings.network_port)
}

fn node_name(&self, _params: Params) -> Result<Value, Error> {
to_value(&self.settings.name)
}

fn rpc_settings(&self, _params: Params) -> Result<Value, Error> {
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))
}
}
105 changes: 93 additions & 12 deletions rpc/src/v1/tests/ethcore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestMinerService> {
Expand All @@ -32,10 +33,26 @@ fn logger() -> Arc<RotatingLogger> {
Arc::new(RotatingLogger::new("rpc=trace".to_owned()))
}

fn settings() -> Arc<NetworkSettings> {
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<TestMinerService>) -> EthcoreClient<TestMinerService> {
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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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()));
}
20 changes: 20 additions & 0 deletions rpc/src/v1/traits/ethcore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ pub trait Ethcore: Sized + Send + Sync + 'static {
/// Returns logs levels
fn dev_logs_levels(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Returns chain name
fn net_chain(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Returns max peers
fn net_max_peers(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Returns network port
fn net_port(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Returns rpc settings
fn rpc_settings(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Returns node name
fn node_name(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 11b0daf

Please sign in to comment.