Delta-based logging.

Closes #181
This commit is contained in:
Gav Wood 2016-01-18 23:23:32 +01:00
parent a2860eb115
commit 40d9cbdeaf
2 changed files with 65 additions and 4 deletions

View File

@ -12,6 +12,7 @@ use util::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::service::ClientService; use ethcore::service::ClientService;
use ethcore::ethereum; use ethcore::ethereum;
use ethcore::blockchain::CacheSize;
use ethcore::sync::*; use ethcore::sync::*;
fn setup_log() { fn setup_log() {
@ -29,7 +30,7 @@ fn main() {
setup_log(); setup_log();
let spec = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut service = ClientService::start(spec).unwrap(); let mut service = ClientService::start(spec).unwrap();
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0 }); let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() });
service.io().register_handler(io_handler).expect("Error registering IO handler"); service.io().register_handler(io_handler).expect("Error registering IO handler");
loop { loop {
let mut cmd = String::new(); let mut cmd = String::new();
@ -40,10 +41,47 @@ fn main() {
} }
} }
#[derive(Default, Debug)]
struct Informant {
chain_info: Option<BlockChainInfo>,
cache_info: Option<CacheSize>,
report: Option<ClientReport>,
}
impl Informant {
pub fn tick(&mut 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, &self.cache_info, &self.report) {
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 = Some(chain_info);
self.cache_info = Some(cache_info);
self.report = Some(report);
}
}
struct ClientIoHandler { struct ClientIoHandler {
client: Arc<RwLock<Client>>, client: Arc<RwLock<Client>>,
timer: TimerToken, timer: TimerToken,
info: Informant,
} }
impl IoHandler<NetSyncMessage> for ClientIoHandler { impl IoHandler<NetSyncMessage> for ClientIoHandler {
@ -55,8 +93,7 @@ impl IoHandler<NetSyncMessage> for ClientIoHandler {
if self.timer == timer { if self.timer == timer {
let client = self.client.read().unwrap(); let client = self.client.read().unwrap();
client.tick(); client.tick();
println!("Chain info: {}", client.chain_info()); self.info.tick(client.deref());
println!("Cache info: {:?}", client.cache_info());
} }
} }
} }

View File

@ -105,12 +105,28 @@ pub trait BlockChainClient : Sync + Send {
fn chain_info(&self) -> BlockChainInfo; fn chain_info(&self) -> BlockChainInfo;
} }
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct ClientReport {
pub blocks_imported: usize,
pub transactions_applied: usize,
pub gas_processed: U256,
}
impl ClientReport {
pub fn accrue_block(&mut self, block: &PreVerifiedBlock) {
self.blocks_imported += 1;
self.transactions_applied += block.transactions.len();
self.gas_processed += block.header.gas_used;
}
}
/// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue. /// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue.
pub struct Client { pub struct Client {
chain: Arc<RwLock<BlockChain>>, chain: Arc<RwLock<BlockChain>>,
engine: Arc<Box<Engine>>, engine: Arc<Box<Engine>>,
state_db: JournalDB, state_db: JournalDB,
queue: BlockQueue, queue: BlockQueue,
report: ClientReport,
} }
const HISTORY: u64 = 1000; const HISTORY: u64 = 1000;
@ -156,6 +172,7 @@ impl Client {
engine: engine.clone(), engine: engine.clone(),
state_db: state_db, state_db: state_db,
queue: BlockQueue::new(engine, message_channel), queue: BlockQueue::new(engine, message_channel),
report: Default::default(),
}) })
} }
@ -228,7 +245,9 @@ impl Client {
return; return;
} }
} }
info!(target: "client", "Imported #{} ({})", header.number(), header.hash()); self.report.accrue_block(&block);
trace!(target: "client", "Imported #{} ({})", header.number(), header.hash());
} }
} }
@ -237,6 +256,11 @@ impl Client {
self.chain.read().unwrap().cache_size() self.chain.read().unwrap().cache_size()
} }
/// Get the report.
pub fn report(&self) -> ClientReport {
self.report.clone()
}
/// Tick the client. /// Tick the client.
pub fn tick(&self) { pub fn tick(&self) {
self.chain.read().unwrap().collect_garbage(false); self.chain.read().unwrap().collect_garbage(false);