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

Commit

Permalink
Show imported messages for light client (#8517)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored and rphmeier committed May 1, 2018
1 parent 44c6822 commit 2a829b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 6 additions & 1 deletion ethcore/light/src/client/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use kvdb::KeyValueDB;
use cache::Cache;
use parking_lot::Mutex;

use super::{ChainDataFetcher, Client, Config as ClientConfig};
use super::{ChainDataFetcher, LightChainNotify, Client, Config as ClientConfig};

/// Errors on service initialization.
#[derive(Debug)]
Expand Down Expand Up @@ -86,6 +86,11 @@ impl<T: ChainDataFetcher> Service<T> {
})
}

/// Set the actor to be notified on certain chain events
pub fn add_notify(&self, notify: Arc<LightChainNotify>) {
self.client.add_listener(Arc::downgrade(&notify));
}

/// Register an I/O handler on the service.
pub fn register_handler(&self, handler: Arc<IoHandler<ClientIoMessage> + Send>) -> Result<(), IoError> {
self.io_service.register_handler(handler)
Expand Down
29 changes: 28 additions & 1 deletion parity/informant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use ethcore::snapshot::service::Service as SnapshotService;
use sync::{LightSyncProvider, LightSync, SyncProvider, ManageNetwork};
use io::{TimerToken, IoContext, IoHandler};
use light::Cache as LightDataCache;
use light::client::LightChainClient;
use light::client::{LightChainClient, LightChainNotify};
use number_prefix::{binary_prefix, Standalone, Prefixed};
use parity_rpc::{is_major_importing};
use parity_rpc::informant::RpcStats;
Expand Down Expand Up @@ -395,6 +395,33 @@ impl ChainNotify for Informant<FullNodeInformantData> {
}
}

impl LightChainNotify for Informant<LightNodeInformantData> {
fn new_headers(&self, good: &[H256]) {
let mut last_import = self.last_import.lock();
let client = &self.target.client;

let importing = self.target.is_major_importing();
let ripe = Instant::now() > *last_import + Duration::from_secs(1) && !importing;

if ripe {
if let Some(header) = good.last().and_then(|h| client.block_header(BlockId::Hash(*h))) {
info!(target: "import", "Imported {} {} ({} Mgas){}",
Colour::White.bold().paint(format!("#{}", header.number())),
Colour::White.bold().paint(format!("{}", header.hash())),
Colour::Yellow.bold().paint(format!("{:.2}", header.gas_used().low_u64() as f32 / 1000000f32)),
if good.len() > 1 {
format!(" + another {} header(s)",
Colour::Red.bold().paint(format!("{}", good.len() - 1)))
} else {
String::new()
}
);
*last_import = Instant::now();
}
}
}
}

const INFO_TIMER: TimerToken = 0;

impl<T: InformantData> IoHandler<ClientIoMessage> for Informant<T> {
Expand Down
2 changes: 1 addition & 1 deletion parity/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ fn execute_light_impl(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<Runnin
Some(rpc_stats),
cmd.logger_config.color,
));

service.add_notify(informant.clone());
service.register_handler(informant.clone()).map_err(|_| "Unable to register informant handler".to_owned())?;

Ok(RunningClient::Light {
Expand Down

0 comments on commit 2a829b1

Please sign in to comment.