      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! Ethcore client application.

    #![warn(missing_docs)]
    #![cfg_attr(feature="dev", feature(plugin))]
    #![cfg_attr(feature="dev", plugin(clippy))]
    #![cfg_attr(feature="dev", allow(useless_format))]
    #![cfg_attr(feature="dev", allow(match_bool))]

    extern crate docopt;
    extern crate num_cpus;
    extern crate rustc_serialize;
    extern crate ethcore_devtools as devtools;
    extern crate ethcore;
    extern crate ethsync;
    extern crate env_logger;
    extern crate ethcore_logger;
    extern crate ctrlc;
    extern crate fdlimit;
    extern crate time;
    extern crate number_prefix;
    extern crate rpassword;
    extern crate semver;
    extern crate ethcore_io as io;
    extern crate ethcore_ipc as ipc;
    extern crate ethcore_ipc_nano as nanoipc;
    extern crate serde;
    extern crate serde_json;
    extern crate jsonrpc_core;
    extern crate rlp;
    extern crate ethcore_light as light;
    extern crate parity_hash_fetch as hash_fetch;

    extern crate ethcore_ipc_hypervisor as hypervisor;
    extern crate ethcore_rpc;

    extern crate ethcore_signer;
    extern crate parity_updater as updater;
    extern crate ansi_term;

    extern crate regex;
    extern crate isatty;
    extern crate toml;
    extern crate app_dirs;
    extern crate parity_reactor;

    #[macro_use]
    extern crate ethcore_util as util;
    #[macro_use]
    extern crate log as rlog;
    #[macro_use]
    extern crate hyper; // for price_info.rs
    #[macro_use]
    extern crate lazy_static;

    #[cfg(feature="stratum")]
    extern crate ethcore_stratum;

    #[cfg(feature = "dapps")]
    extern crate ethcore_dapps;

    extern crate rpc_cli;

    macro_rules! dependency {
    	($dep_ty:ident, $url:expr) => {
    		{
    			let dep = boot::dependency::<$dep_ty<_>>($url)
    				.unwrap_or_else(|e| panic!("Fatal: error connecting service ({:?})", e));
    			dep.handshake()
    				.unwrap_or_else(|e| panic!("Fatal: error in connected service ({:?})", e));
    			dep
    		}
    	}
    }

    mod cache;
    mod upgrade;
    mod rpc;
    mod dapps;
    mod informant;
    mod cli;
    mod configuration;
    mod migration;
    mod signer;
    mod rpc_apis;
    mod url;
    mod helpers;
    mod params;
    mod deprecated;
    mod dir;
    mod modules;
    mod account;
    mod blockchain;
    mod presale;
    mod snapshot;
    mod run;
    #[cfg(feature="ipc")]
    mod sync;
    #[cfg(feature="ipc")]
    mod boot;
    mod user_defaults;

    #[cfg(feature="stratum")]
    mod stratum;

    use std::{process, env};
    use std::collections::HashMap;
    use std::io::{self as stdio, BufReader, Read, Write};
    use std::fs::File;
    use std::path::PathBuf;
    use util::sha3::sha3;
    use cli::Args;
    use configuration::{Cmd, Execute, Configuration};
    use deprecated::find_deprecated;
    use ethcore_logger::setup_log;
    use dir::default_hypervisor_path;

    fn print_hash_of(maybe_file: Option<String>) -> Result<String, String> {
    	if let Some(file) = maybe_file {
    		let mut f = BufReader::new(File::open(&file).map_err(|_| "Unable to open file".to_owned())?);
    		let hash = sha3(&mut f).map_err(|_| "Unable to read from file".to_owned())?;
    		Ok(hash.hex())
    	} else {
    		Err("Streaming from standard input not yet supported. Specify a file.".to_owned())
    	}
    }

    enum PostExecutionAction {
    	Print(String),
    	Restart,
    	Quit,
    }

    fn execute(command: Execute, can_restart: bool) -> Result<PostExecutionAction, String> {
    	let logger = setup_log(&command.logger).expect("Logger is initialized only once; qed");

    	match command.cmd {
    		Cmd::Run(run_cmd) => {
    			let restart = run::execute(run_cmd, can_restart, logger)?;
    			Ok(if restart { PostExecutionAction::Restart } else { PostExecutionAction::Quit })
    		},
    		Cmd::Version => Ok(PostExecutionAction::Print(Args::print_version())),
    		Cmd::Hash(maybe_file) => print_hash_of(maybe_file).map(|s| PostExecutionAction::Print(s)),
    		Cmd::Account(account_cmd) => account::execute(account_cmd).map(|s| PostExecutionAction::Print(s)),
    		Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd).map(|s| PostExecutionAction::Print(s)),
    		Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd).map(|_| PostExecutionAction::Quit),
    		Cmd::SignerToken(signer_cmd) => signer::execute(signer_cmd).map(|s| PostExecutionAction::Print(s)),
    		Cmd::SignerSign { id, pwfile, port, authfile } => rpc_cli::signer_sign(id, pwfile, port, authfile).map(|s| PostExecutionAction::Print(s)),
    		Cmd::SignerList { port, authfile } => rpc_cli::signer_list(port, authfile).map(|s| PostExecutionAction::Print(s)),
    		Cmd::SignerReject { id, port, authfile } => rpc_cli::signer_reject(id, port, authfile).map(|s| PostExecutionAction::Print(s)),
    		Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd).map(|s| PostExecutionAction::Print(s)),
    	}
    }

    fn start(can_restart: bool) -> Result<PostExecutionAction, String> {
    	let args: Vec<String> = env::args().collect();
    	let conf = Configuration::parse(&args).unwrap_or_else(|e| e.exit());

    	let deprecated = find_deprecated(&conf.args);
    	for d in deprecated {
    		println!("{}", d);
    	}

    	let cmd = conf.into_command()?;
    	execute(cmd, can_restart)
    }

    #[cfg(not(feature="stratum"))]
    fn stratum_main(_: &mut HashMap<String, fn()>) {}

    #[cfg(feature="stratum")]
    fn stratum_main(alt_mains: &mut HashMap<String, fn()>) {
    	alt_mains.insert("stratum".to_owned(), stratum::main);
    }

    #[cfg(not(feature="ipc"))]
    fn sync_main(_: &mut HashMap<String, fn()>) {}

    #[cfg(feature="ipc")]
    fn sync_main(alt_mains: &mut HashMap<String, fn()>) {
    	alt_mains.insert("sync".to_owned(), sync::main);
    }

    fn updates_path(name: &str) -> PathBuf {
    	let mut dest = PathBuf::from(default_hypervisor_path());
    	dest.push(name);
    	dest
    }

    fn latest_exe_path() -> Option<PathBuf> {
    	File::open(updates_path("latest")).ok()
    		.and_then(|mut f| { let mut exe = String::new(); f.read_to_string(&mut exe).ok().map(|_| updates_path(&exe)) })
    }

    #[cfg(windows)]
    fn global_cleanup() {
    	extern "system" { pub fn WSACleanup() -> i32; }
    	// We need to cleanup all sockets before spawning another Parity process. This makes shure everything is cleaned up.
    	// The loop is required because of internal refernce counter for winsock dll. We don't know how many crates we use do
    	// initialize it. There's at least 2 now.
    	for _ in 0.. 10 {
    		unsafe { WSACleanup(); }
    	}
    }

    #[cfg(not(windows))]
    fn global_cleanup() {}

    // Starts ~/.parity-updates/parity and returns the code it exits with.
    fn run_parity() -> Option<i32> {
    	global_cleanup();
    	use ::std::ffi::OsString;
    	let prefix = vec![OsString::from("--can-restart"), OsString::from("--force-direct")];
    	latest_exe_path().and_then(|exe| process::Command::new(exe)
    		.args(&(env::args_os().skip(1).chain(prefix.into_iter()).collect::<Vec<_>>()))
    		.status()
    		.map(|es| es.code().unwrap_or(128))
    		.ok()
    	)
    }

    const PLEASE_RESTART_EXIT_CODE: i32 = 69;

    // Run our version of parity.
    // Returns the exit error code.
    fn main_direct(can_restart: bool) -> i32 {
    	let mut alt_mains = HashMap::new();
    	sync_main(&mut alt_mains);
    	stratum_main(&mut alt_mains);
    	if let Some(f) = std::env::args().nth(1).and_then(|arg| alt_mains.get(&arg.to_string())) {
    		f();
    		0
    	} else {
    		match start(can_restart) {
    			Ok(result) => match result {
    				PostExecutionAction::Print(s) => { println!("{}", s); 0 },
    				PostExecutionAction::Restart => PLEASE_RESTART_EXIT_CODE,
    				PostExecutionAction::Quit => 0,
    			},
    			Err(err) => {
    				writeln!(&mut stdio::stderr(), "{}", err).expect("StdErr available; qed");
    				1
    			},
    		}
    	}
    }

    fn println_trace_main(s: String) {
    	if env::var("RUST_LOG").ok().and_then(|s| s.find("main=trace")).is_some() {
    		println!("{}", s);
    	}
    }

    #[macro_export]
    macro_rules! trace_main {
    	($arg:expr) => (println_trace_main($arg.into()));
    	($($arg:tt)*) => (println_trace_main(format!("{}", format_args!($($arg)*))));
    }

    fn main() {
    	// Always print backtrace on panic.
    	env::set_var("RUST_BACKTRACE", "1");

    	// assuming the user is not running with `--force-direct`, then:
    	// if argv[0] == "parity" and this executable != ~/.parity-updates/parity, run that instead.
    	let force_direct = std::env::args().any(|arg| arg == "--force-direct");
    	let exe = std::env::current_exe().ok();
    	let development = exe.as_ref().and_then(|p| p.parent().and_then(|p| p.parent()).and_then(|p| p.file_name()).map(|n| n == "target")).unwrap_or(false);
    	let same_name = exe.as_ref().map(|p| p.file_stem().map_or(false, |s| s == "parity") && p.extension().map_or(true, |x| x == "exe")).unwrap_or(false);
    	trace_main!("Starting up {} (force-direct: {}, development: {}, same-name: {})", std::env::current_exe().map(|x| format!("{}", x.display())).unwrap_or("<unknown>".to_owned()), force_direct, development, same_name);
    	if !force_direct && !development && same_name {
    		// looks like we're not running ~/.parity-updates/parity when the user is expecting otherwise.
    		// Everything run inside a loop, so we'll be able to restart from the child into a new version seamlessly.
    		loop {
    			// If we fail to run the updated parity then fallback to local version.
    			let latest_exe = latest_exe_path();
    			let have_update = latest_exe.as_ref().map_or(false, |p| p.exists());
    			let is_non_updated_current = exe.as_ref().map_or(false, |exe| latest_exe.as_ref().map_or(false, |lexe| exe.canonicalize().ok() != lexe.canonicalize().ok()));
    			trace_main!("Starting... (have-update: {}, non-updated-current: {})", have_update, is_non_updated_current);
    			let exit_code = if have_update && is_non_updated_current {
    				trace_main!("Attempting to run latest update ({})...", latest_exe.as_ref().expect("guarded by have_update; latest_exe must exist for have_update; qed").display());
    				run_parity().unwrap_or_else(|| { trace_main!("Falling back to local..."); main_direct(true) })
    			} else {
    				trace_main!("No latest update. Attempting to direct...");
    				main_direct(true)
    			};
    			trace_main!("Latest exited with {}", exit_code);
    			if exit_code != PLEASE_RESTART_EXIT_CODE {
    				trace_main!("Quitting...");
    				process::exit(exit_code);
    			}
    			trace_main!("Rerunning...");
    		}
    	} else {
    		trace_main!("Running direct");
    		// Otherwise, we're presumably running the version we want. Just run and fall-through.
    		let can_restart = std::env::args().any(|arg| arg == "--can-restart");
    		process::exit(main_direct(can_restart));
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)