Additional RPC methods for settings

This commit is contained in:
Tomasz Drwięga 2016-04-21 19:19:42 +02:00
parent ffc5d726bd
commit 0458a3378c
9 changed files with 202 additions and 17 deletions

View File

@ -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),
}
}
}

View File

@ -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

View File

@ -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<AccountService>,
pub miner: Arc<Miner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
}
pub fn new(conf: Configuration, deps: Dependencies) -> Option<RpcServer> {
@ -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);
},

View File

@ -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<AccountService>,
pub miner: Arc<Miner>,
pub logger: Arc<RotatingLogger>,
pub settings: Arc<NetworkSettings>,
}
pub fn new(configuration: Configuration, deps: Dependencies) -> Option<WebappServer> {
@ -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 => {

View File

@ -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<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,
}
}
}
@ -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))
}
}

View File

@ -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> {
@ -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);
@ -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()));
}

View File

@ -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));
@ -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
}

View File

@ -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::*;

View File

@ -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 <http://www.gnu.org/licenses/>.
//! 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,
}