From 8970946b746130aaa5d9272fafa740a93cd225d1 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 16 Dec 2016 21:45:51 +0100 Subject: [PATCH] basic --light parameter --- parity/cli/config.full.toml | 1 + parity/cli/mod.rs | 5 ++- parity/cli/usage.txt | 14 +++--- parity/configuration.rs | 6 ++- parity/run.rs | 89 +++++++++++++++++++++++++++++-------- 5 files changed, 88 insertions(+), 27 deletions(-) diff --git a/parity/cli/config.full.toml b/parity/cli/config.full.toml index fc55e5294..01bec556c 100644 --- a/parity/cli/config.full.toml +++ b/parity/cli/config.full.toml @@ -12,6 +12,7 @@ base_path = "$HOME/.parity" db_path = "$HOME/.parity/chains" keys_path = "$HOME/.parity/keys" identity = "" +light = false [account] unlock = ["0xdeadbeefcafe0000000000000000000000000000"] diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index cac137316..29445056b 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -94,6 +94,7 @@ usage! { flag_db_path: String = "$BASE/chains", or |c: &Config| otry!(c.parity).db_path.clone(), flag_keys_path: String = "$BASE/keys", or |c: &Config| otry!(c.parity).keys_path.clone(), flag_identity: String = "", or |c: &Config| otry!(c.parity).identity.clone(), + flag_light: bool = false, or |c: &Config| otry!(c.parity).light.clone(), // -- Account Options flag_unlock: Option = None, @@ -259,7 +260,7 @@ usage! { or |c: &Config| otry!(c.footprint).fat_db.clone(), flag_scale_verifiers: bool = false, or |c: &Config| otry!(c.footprint).scale_verifiers.clone(), - flag_num_verifiers: Option = None, + flag_num_verifiers: Option = None, or |c: &Config| otry!(c.footprint).num_verifiers.clone().map(Some), // -- Import/Export Options @@ -323,6 +324,7 @@ struct Operating { db_path: Option, keys_path: Option, identity: Option, + light: Option, } #[derive(Default, Debug, PartialEq, RustcDecodable)] @@ -552,6 +554,7 @@ mod tests { flag_db_path: "$HOME/.parity/chains".into(), flag_keys_path: "$HOME/.parity/keys".into(), flag_identity: "".into(), + flag_light: false, // -- Account Options flag_unlock: Some("0xdeadbeefcafe0000000000000000000000000000".into()), diff --git a/parity/cli/usage.txt b/parity/cli/usage.txt index 1e4032bf5..b78941564 100644 --- a/parity/cli/usage.txt +++ b/parity/cli/usage.txt @@ -36,15 +36,15 @@ Operating Options: (default: {flag_mode_alarm}). --auto-update SET Set a releases set to automatically update and install. - all - All updates in the our release track. + all - All updates in the our release track. critical - Only consensus/security updates. - none - No updates will be auto-installed. + none - No updates will be auto-installed. (default: {flag_auto_update}). --release-track TRACK Set which release track we should use for updates. - stable - Stable releases. - beta - Beta releases. + stable - Stable releases. + beta - Beta releases. nightly - Nightly releases (unstable). - testing - Testing releases (do not use). + testing - Testing releases (do not use). current - Whatever track this executable was released on (default: {flag_release_track}). --no-download Normally new releases will be downloaded ready for @@ -66,6 +66,8 @@ Operating Options: --keys-path PATH Specify the path for JSON key files to be found (default: {flag_keys_path}). --identity NAME Specify your node's name. (default: {flag_identity}) + --light Run in light client mode. Very experimental. + (default: {flag_light}) Account Options: --unlock ACCOUNTS Unlock ACCOUNTS for the duration of the execution. @@ -363,7 +365,7 @@ Legacy Options: --cache MB Equivalent to --cache-size MB. Internal Options: - --can-restart Executable will auto-restart if exiting with 69. + --can-restart Executable will auto-restart if exiting with 69. Miscellaneous Options: -c --config CONFIG Specify a filename containing a configuration file. diff --git a/parity/configuration.rs b/parity/configuration.rs index ad8c8e30c..4bed93420 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -342,6 +342,7 @@ impl Configuration { check_seal: !self.args.flag_no_seal_check, download_old_blocks: !self.args.flag_no_ancient_blocks, serve_light: self.args.flag_serve_light, + light: self.args.flag_light, verifier_settings: verifier_settings, }; Cmd::Run(run_cmd) @@ -691,7 +692,7 @@ impl Configuration { "none" => UpdateFilter::None, "critical" => UpdateFilter::Critical, "all" => UpdateFilter::All, - _ => return Err("Invalid value for `--auto-update`. See `--help` for more information.".into()), + _ => return Err("Invalid value for `--auto-update`. See `--help` for more information.".into()), }, track: match self.args.flag_release_track.as_ref() { "stable" => ReleaseTrack::Stable, @@ -699,7 +700,7 @@ impl Configuration { "nightly" => ReleaseTrack::Nightly, "testing" => ReleaseTrack::Testing, "current" => ReleaseTrack::Unknown, - _ => return Err("Invalid value for `--releases-track`. See `--help` for more information.".into()), + _ => return Err("Invalid value for `--releases-track`. See `--help` for more information.".into()), }, path: default_hypervisor_path(), }) @@ -1034,6 +1035,7 @@ mod tests { download_old_blocks: true, serve_light: false, verifier_settings: Default::default(), + light: false, })); } diff --git a/parity/run.rs b/parity/run.rs index da534ee3b..4e88fe458 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -97,6 +97,7 @@ pub struct RunCmd { pub download_old_blocks: bool, pub serve_light: bool, pub verifier_settings: VerifierSettings, + pub light: bool, } pub fn open_ui(dapps_conf: &dapps::Configuration, signer_conf: &signer::Configuration) -> Result<(), String> { @@ -116,6 +117,56 @@ pub fn open_ui(dapps_conf: &dapps::Configuration, signer_conf: &signer::Configur Ok(()) } +// Execute in light client mode. +pub fn execute_light(cmd: RunCmd, logger: Arc) -> Result { + use light::client::{Config as ClientConfig, Service as LightClientService}; + use ethsync::{LightSync, LightSyncParams, ManageNetwork}; + + let panic_handler = PanicHandler::new_in_arc(); + + info!( + "Configured in {} mode. Note that this feature is {}.", + Colour::White.bold().paint("Light Client"), + Colour::Red.bold().paint("experimental"), + ); + + let mut client_config = ClientConfig::default(); + let queue_size = cmd.cache_config.queue(); + + client_config.queue.max_queue_size = queue_size as usize; + client_config.queue.verifier_settings = cmd.verifier_settings; + + let spec = try!(cmd.spec.spec()); + let service = try!(LightClientService::start(client_config, &spec) + .map_err(|e| format!("Error starting light client service: {}", e))); + + let net_conf = try!(cmd.net_conf.into_basic() + .map_err(|e| format!("Failed to create network config: {}", e))); + + let sync_params = LightSyncParams { + network_config: net_conf, + client: service.client().clone(), + network_id: cmd.network_id.unwrap_or(spec.network_id()), + subprotocol_name: *b"les", + }; + + let sync = try!(LightSync::new(sync_params) + .map_err(|e| format!("Failed to initialize sync service: {}", e))); + + sync.start_network(); + + let log_client = service.client().clone(); + ::std::thread::spawn(move || { + loop { + ::std::thread::sleep(::std::time::Duration::from_secs(5)); + println!("Best block: #{}", log_client.chain_info().best_block_number); + } + }); + + wait_for_exit(panic_handler, None, false); + Ok(false) +} + pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> Result { if cmd.ui && cmd.dapps_conf.enabled { // Check if Parity is already running @@ -125,6 +176,10 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R } } + if cmd.light { + return execute_light(cmd, logger); + } + // set up panic handler let panic_handler = PanicHandler::new_in_arc(); @@ -295,12 +350,12 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R // create sync object let (sync_provider, manage_network, chain_notify) = try!(modules::sync( - &mut hypervisor, - sync_config, - net_conf.into(), - client.clone(), - snapshot_service.clone(), - client.clone(), + &mut hypervisor, + sync_config, + net_conf.into(), + client.clone(), + snapshot_service.clone(), + client.clone(), &cmd.logger_config, ).map_err(|e| format!("Sync error: {}", e))); @@ -416,7 +471,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R } // Handle exit - let restart = wait_for_exit(panic_handler, http_server, ipc_server, dapps_server, signer_server, updater, can_restart); + let restart = wait_for_exit(panic_handler, Some(updater), can_restart); info!("Finishing work, please wait..."); @@ -471,11 +526,7 @@ fn prepare_account_provider(dirs: &Directories, data_dir: &str, cfg: AccountsCon fn wait_for_exit( panic_handler: Arc, - _http_server: Option, - _ipc_server: Option, - _dapps_server: Option, - _signer_server: Option, - updater: Arc, + updater: Option>, can_restart: bool ) -> bool { let exit = Arc::new((Mutex::new(false), Condvar::new())); @@ -488,12 +539,14 @@ fn wait_for_exit( let e = exit.clone(); panic_handler.on_panic(move |_reason| { e.1.notify_all(); }); - // Handle updater wanting to restart us - if can_restart { - let e = exit.clone(); - updater.set_exit_handler(move || { *e.0.lock() = true; e.1.notify_all(); }); - } else { - updater.set_exit_handler(|| info!("Update installed; ready for restart.")); + if let Some(updater) = updater { + // Handle updater wanting to restart us + if can_restart { + let e = exit.clone(); + updater.set_exit_handler(move || { *e.0.lock() = true; e.1.notify_all(); }); + } else { + updater.set_exit_handler(|| info!("Update installed; ready for restart.")); + } } // Wait for signal