2016-01-27 17:18:38 +01:00
|
|
|
//! Ethcore client application.
|
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
2016-01-23 23:53:20 +01:00
|
|
|
#![feature(plugin)]
|
2016-01-25 11:54:15 +01:00
|
|
|
#![plugin(docopt_macros)]
|
2016-01-27 17:18:38 +01:00
|
|
|
#![plugin(clippy)]
|
2016-01-23 23:53:20 +01:00
|
|
|
extern crate docopt;
|
|
|
|
extern crate rustc_serialize;
|
2015-12-22 22:19:50 +01:00
|
|
|
extern crate ethcore_util as util;
|
2016-01-07 16:08:12 +01:00
|
|
|
extern crate ethcore;
|
2016-01-29 15:56:06 +01:00
|
|
|
extern crate ethsync;
|
2016-01-31 11:08:04 +01:00
|
|
|
extern crate log as rlog;
|
2016-01-09 23:21:57 +01:00
|
|
|
extern crate env_logger;
|
2016-01-22 00:11:19 +01:00
|
|
|
extern crate ctrlc;
|
2015-12-22 22:19:50 +01:00
|
|
|
|
2016-01-20 04:19:38 +01:00
|
|
|
#[cfg(feature = "rpc")]
|
2016-01-26 13:14:22 +01:00
|
|
|
extern crate ethcore_rpc as rpc;
|
2016-01-20 04:19:38 +01:00
|
|
|
|
2016-01-14 22:38:49 +01:00
|
|
|
use std::env;
|
2016-01-31 11:08:04 +01:00
|
|
|
use rlog::{LogLevelFilter};
|
2016-01-14 22:38:49 +01:00
|
|
|
use env_logger::LogBuilder;
|
2016-01-22 00:11:19 +01:00
|
|
|
use ctrlc::CtrlC;
|
2016-01-16 13:30:27 +01:00
|
|
|
use util::*;
|
|
|
|
use ethcore::client::*;
|
2016-01-21 23:33:52 +01:00
|
|
|
use ethcore::service::{ClientService, NetSyncMessage};
|
2016-01-09 18:50:45 +01:00
|
|
|
use ethcore::ethereum;
|
2016-01-18 23:23:32 +01:00
|
|
|
use ethcore::blockchain::CacheSize;
|
2016-01-29 15:56:06 +01:00
|
|
|
use ethsync::EthSync;
|
2016-01-23 23:53:20 +01:00
|
|
|
|
2016-01-25 11:54:15 +01:00
|
|
|
docopt!(Args derive Debug, "
|
2016-01-23 23:53:20 +01:00
|
|
|
Parity. Ethereum Client.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
parity [options]
|
|
|
|
parity [options] <enode>...
|
|
|
|
|
|
|
|
Options:
|
2016-02-02 01:59:14 +01:00
|
|
|
-l --logging LOGGING Specify the logging level.
|
|
|
|
-j --jsonrpc Enable the JSON-RPC API sever.
|
|
|
|
--jsonrpc-url URL Specify URL for JSON-RPC API server (default: 127.0.0.1:8545).
|
|
|
|
|
|
|
|
--cache-pref-size BYTES Specify the prefered size of the blockchain cache in bytes (default: 16384).
|
|
|
|
--cache-max-size BYTES Specify the maximum size of the blockchain cache in bytes (default: 262144).
|
|
|
|
|
|
|
|
-h --help Show this screen.
|
|
|
|
", flag_cache_pref_size: usize, flag_cache_max_size: usize);
|
2015-12-22 22:19:50 +01:00
|
|
|
|
2016-01-27 17:18:38 +01:00
|
|
|
fn setup_log(init: &str) {
|
2016-01-14 22:38:49 +01:00
|
|
|
let mut builder = LogBuilder::new();
|
2016-01-15 01:44:57 +01:00
|
|
|
builder.filter(None, LogLevelFilter::Info);
|
2016-01-14 22:38:49 +01:00
|
|
|
|
|
|
|
if env::var("RUST_LOG").is_ok() {
|
|
|
|
builder.parse(&env::var("RUST_LOG").unwrap());
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:54:15 +01:00
|
|
|
builder.parse(init);
|
2016-01-23 23:53:20 +01:00
|
|
|
|
2016-01-14 22:38:49 +01:00
|
|
|
builder.init().unwrap();
|
|
|
|
}
|
|
|
|
|
2016-01-20 04:19:38 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "rpc")]
|
2016-02-02 01:59:14 +01:00
|
|
|
fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str) {
|
2016-01-27 17:14:41 +01:00
|
|
|
use rpc::v1::*;
|
2016-01-21 00:54:19 +01:00
|
|
|
|
2016-01-27 17:18:38 +01:00
|
|
|
let mut server = rpc::HttpServer::new(1);
|
2016-01-21 00:54:19 +01:00
|
|
|
server.add_delegate(Web3Client::new().to_delegate());
|
2016-01-21 11:25:39 +01:00
|
|
|
server.add_delegate(EthClient::new(client.clone()).to_delegate());
|
|
|
|
server.add_delegate(EthFilterClient::new(client).to_delegate());
|
2016-01-28 10:54:49 +01:00
|
|
|
server.add_delegate(NetClient::new(sync).to_delegate());
|
2016-02-02 01:59:14 +01:00
|
|
|
server.start_async(url);
|
2016-01-20 04:19:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "rpc"))]
|
2016-02-02 01:59:14 +01:00
|
|
|
fn setup_rpc_server(_client: Arc<Client>, _sync: Arc<EthSync>, _url: &str) {
|
2016-01-20 04:19:38 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 22:19:50 +01:00
|
|
|
fn main() {
|
2016-01-25 11:54:15 +01:00
|
|
|
let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());
|
2016-01-23 23:53:20 +01:00
|
|
|
|
|
|
|
setup_log(&args.flag_logging);
|
|
|
|
|
2016-01-11 12:28:59 +01:00
|
|
|
let spec = ethereum::new_frontier();
|
2016-01-25 11:54:15 +01:00
|
|
|
let init_nodes = match args.arg_enode.len() {
|
|
|
|
0 => spec.nodes().clone(),
|
|
|
|
_ => args.arg_enode.clone(),
|
2016-01-23 23:53:20 +01:00
|
|
|
};
|
2016-01-23 02:36:58 +01:00
|
|
|
let mut net_settings = NetworkConfiguration::new();
|
2016-01-24 19:21:31 +01:00
|
|
|
net_settings.boot_nodes = init_nodes;
|
2016-01-23 02:36:58 +01:00
|
|
|
let mut service = ClientService::start(spec, net_settings).unwrap();
|
2016-01-29 15:56:06 +01:00
|
|
|
let client = service.client().clone();
|
2016-02-02 01:59:14 +01:00
|
|
|
client.configure_cache(args.flag_cache_pref_size, args.flag_cache_max_size);
|
2016-01-29 15:56:06 +01:00
|
|
|
let sync = EthSync::register(service.network(), client);
|
2016-02-02 01:59:14 +01:00
|
|
|
if args.flag_jsonrpc {
|
|
|
|
setup_rpc_server(service.client(), sync.clone(), &args.flag_jsonrpc_url);
|
|
|
|
}
|
2016-01-29 15:56:06 +01:00
|
|
|
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: sync });
|
2016-01-16 13:30:27 +01:00
|
|
|
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
2016-01-22 00:11:19 +01:00
|
|
|
|
|
|
|
let exit = Arc::new(Condvar::new());
|
|
|
|
let e = exit.clone();
|
2016-01-22 14:03:42 +01:00
|
|
|
CtrlC::set_handler(move || { e.notify_all(); });
|
2016-01-22 00:11:19 +01:00
|
|
|
let mutex = Mutex::new(());
|
|
|
|
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 23:23:32 +01:00
|
|
|
struct Informant {
|
2016-01-21 17:21:51 +01:00
|
|
|
chain_info: RwLock<Option<BlockChainInfo>>,
|
|
|
|
cache_info: RwLock<Option<CacheSize>>,
|
|
|
|
report: RwLock<Option<ClientReport>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Informant {
|
|
|
|
fn default() -> Self {
|
|
|
|
Informant {
|
|
|
|
chain_info: RwLock::new(None),
|
|
|
|
cache_info: RwLock::new(None),
|
|
|
|
report: RwLock::new(None),
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 23:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Informant {
|
2016-01-22 04:54:38 +01:00
|
|
|
pub fn tick(&self, client: &Client, sync: &EthSync) {
|
2016-01-18 23:23:32 +01:00
|
|
|
// 5 seconds betwen calls. TODO: calculate this properly.
|
|
|
|
let dur = 5usize;
|
|
|
|
|
|
|
|
let chain_info = client.chain_info();
|
2016-01-22 04:54:38 +01:00
|
|
|
let queue_info = client.queue_info();
|
2016-01-18 23:23:32 +01:00
|
|
|
let cache_info = client.cache_info();
|
|
|
|
let report = client.report();
|
2016-01-22 04:54:38 +01:00
|
|
|
let sync_info = sync.status();
|
2016-01-18 23:23:32 +01:00
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) {
|
2016-01-22 15:58:52 +01:00
|
|
|
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //··· {}/{} peers, {} downloaded, {}+{} queued ···// {} ({}) bl {} ({}) ex ]",
|
2016-01-18 23:23:32 +01:00
|
|
|
chain_info.best_block_number,
|
|
|
|
chain_info.best_block_hash,
|
|
|
|
(report.blocks_imported - last_report.blocks_imported) / dur,
|
|
|
|
(report.transactions_applied - last_report.transactions_applied) / dur,
|
|
|
|
(report.gas_processed - last_report.gas_processed) / From::from(dur),
|
2016-01-22 04:54:38 +01:00
|
|
|
|
|
|
|
sync_info.num_active_peers,
|
|
|
|
sync_info.num_peers,
|
|
|
|
sync_info.blocks_received,
|
2016-01-22 15:58:52 +01:00
|
|
|
queue_info.unverified_queue_size,
|
|
|
|
queue_info.verified_queue_size,
|
2016-01-22 04:54:38 +01:00
|
|
|
|
2016-01-18 23:23:32 +01:00
|
|
|
cache_info.blocks,
|
|
|
|
cache_info.blocks as isize - last_cache_info.blocks as isize,
|
|
|
|
cache_info.block_details,
|
|
|
|
cache_info.block_details as isize - last_cache_info.block_details as isize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
*self.chain_info.write().unwrap().deref_mut() = Some(chain_info);
|
|
|
|
*self.cache_info.write().unwrap().deref_mut() = Some(cache_info);
|
|
|
|
*self.report.write().unwrap().deref_mut() = Some(report);
|
2016-01-18 23:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 13:30:27 +01:00
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
const INFO_TIMER: TimerToken = 0;
|
|
|
|
|
2016-01-16 13:30:27 +01:00
|
|
|
struct ClientIoHandler {
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>,
|
2016-01-22 04:54:38 +01:00
|
|
|
sync: Arc<EthSync>,
|
2016-01-18 23:23:32 +01:00
|
|
|
info: Informant,
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
2016-01-21 17:21:51 +01:00
|
|
|
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
|
|
|
io.register_timer(INFO_TIMER, 5000).expect("Error registering timer");
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
|
|
|
if INFO_TIMER == timer {
|
2016-01-22 04:54:38 +01:00
|
|
|
self.info.tick(&self.client, &self.sync);
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 02:04:03 +01:00
|
|
|
/// Parity needs at least 1 test to generate coverage reports correctly.
|
|
|
|
#[test]
|
|
|
|
fn if_works() {
|
|
|
|
}
|