108 lines
3.0 KiB
Rust
108 lines
3.0 KiB
Rust
extern crate ethcore_util as util;
|
|
extern crate ethcore;
|
|
extern crate rustc_serialize;
|
|
extern crate log;
|
|
extern crate env_logger;
|
|
|
|
use std::io::stdin;
|
|
use std::env;
|
|
use log::{LogLevelFilter};
|
|
use env_logger::LogBuilder;
|
|
use util::*;
|
|
use ethcore::client::*;
|
|
use ethcore::service::{ClientService, NetSyncMessage};
|
|
use ethcore::ethereum;
|
|
use ethcore::blockchain::CacheSize;
|
|
|
|
fn setup_log() {
|
|
let mut builder = LogBuilder::new();
|
|
builder.filter(None, LogLevelFilter::Info);
|
|
|
|
if env::var("RUST_LOG").is_ok() {
|
|
builder.parse(&env::var("RUST_LOG").unwrap());
|
|
}
|
|
|
|
builder.init().unwrap();
|
|
}
|
|
|
|
fn main() {
|
|
setup_log();
|
|
let spec = ethereum::new_frontier();
|
|
let mut service = ClientService::start(spec).unwrap();
|
|
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default() });
|
|
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
|
loop {
|
|
let mut cmd = String::new();
|
|
stdin().read_line(&mut cmd).unwrap();
|
|
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Informant {
|
|
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),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Informant {
|
|
pub fn tick(&self, client: &Client) {
|
|
// 5 seconds betwen calls. TODO: calculate this properly.
|
|
let dur = 5usize;
|
|
|
|
let chain_info = client.chain_info();
|
|
let cache_info = client.cache_info();
|
|
let report = client.report();
|
|
|
|
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()) {
|
|
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //···{}···// {} ({}) bl {} ({}) ex ]",
|
|
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),
|
|
0, // TODO: peers
|
|
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
|
|
);
|
|
}
|
|
|
|
*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);
|
|
}
|
|
}
|
|
|
|
const INFO_TIMER: TimerToken = 0;
|
|
|
|
struct ClientIoHandler {
|
|
client: Arc<Client>,
|
|
info: Informant,
|
|
}
|
|
|
|
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
|
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
|
io.register_timer(INFO_TIMER, 5000).expect("Error registering timer");
|
|
}
|
|
|
|
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
|
if INFO_TIMER == timer {
|
|
self.info.tick(&self.client);
|
|
}
|
|
}
|
|
}
|
|
|