Wallet listener and server startup ambiguity fix (#774)

* unify code path for server run and create wallet seed if it doesn't exist

* rustfmt
This commit is contained in:
Yeastplume 2018-03-14 18:21:48 +00:00 committed by GitHub
parent 6a1a45e21f
commit c02309ce87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 91 deletions

View file

@ -83,29 +83,6 @@ fn start_server_tui(config: grin::ServerConfig) {
} }
} }
fn start_from_config_file(mut global_config: GlobalConfig) {
info!(
LOGGER,
"Starting the Grin server from configuration file at {}",
global_config.config_file_path.unwrap().to_str().unwrap()
);
global::set_mining_mode(
global_config
.members
.as_mut()
.unwrap()
.server
.clone()
.chain_type,
);
start_server(global_config.members.as_mut().unwrap().server.clone());
loop {
thread::sleep(Duration::from_secs(60));
}
}
fn main() { fn main() {
// First, load a global config object, // First, load a global config object,
// then modify that object with any switches // then modify that object with any switches
@ -135,16 +112,6 @@ fn main() {
log_conf.log_to_stdout = false; log_conf.log_to_stdout = false;
} }
init_logger(Some(log_conf)); init_logger(Some(log_conf));
info!(
LOGGER,
"Using configuration file at: {}",
global_config
.config_file_path
.clone()
.unwrap()
.to_str()
.unwrap()
);
global::set_mining_mode( global::set_mining_mode(
global_config global_config
.members .members
@ -332,7 +299,7 @@ fn main() {
match args.subcommand() { match args.subcommand() {
// server commands and options // server commands and options
("server", Some(server_args)) => { ("server", Some(server_args)) => {
server_command(server_args, global_config); server_command(Some(server_args), global_config);
} }
// client commands and options // client commands and options
@ -350,7 +317,7 @@ fn main() {
// with most command line options being phased out // with most command line options being phased out
_ => { _ => {
if global_config.using_config_file { if global_config.using_config_file {
start_from_config_file(global_config); server_command(None, global_config);
} else { } else {
// won't attempt to just start with defaults, // won't attempt to just start with defaults,
// and will reject // and will reject
@ -365,26 +332,47 @@ fn main() {
/// stopping the Grin blockchain server. Processes all the command line /// stopping the Grin blockchain server. Processes all the command line
/// arguments /// arguments
/// to build a proper configuration and runs Grin with that configuration. /// to build a proper configuration and runs Grin with that configuration.
fn server_command(server_args: &ArgMatches, global_config: GlobalConfig) { fn server_command(server_args: Option<&ArgMatches>, mut global_config: GlobalConfig) {
info!(LOGGER, "Starting the Grin server..."); if global_config.using_config_file {
info!(
LOGGER,
"Starting the Grin server from configuration file at {}",
global_config.config_file_path.unwrap().to_str().unwrap()
);
global::set_mining_mode(
global_config
.members
.as_mut()
.unwrap()
.server
.clone()
.chain_type,
);
} else {
info!(
LOGGER,
"Starting the Grin server (no configuration file) ..."
);
}
// just get defaults from the global config // just get defaults from the global config
let mut server_config = global_config.members.as_ref().unwrap().server.clone(); let mut server_config = global_config.members.as_ref().unwrap().server.clone();
if let Some(port) = server_args.value_of("port") { if let Some(a) = server_args {
if let Some(port) = a.value_of("port") {
server_config.p2p_config.port = port.parse().unwrap(); server_config.p2p_config.port = port.parse().unwrap();
} }
if let Some(api_port) = server_args.value_of("api_port") { if let Some(api_port) = a.value_of("api_port") {
let default_ip = "0.0.0.0"; let default_ip = "0.0.0.0";
server_config.api_http_addr = format!("{}:{}", default_ip, api_port); server_config.api_http_addr = format!("{}:{}", default_ip, api_port);
} }
if server_args.is_present("mine") { if a.is_present("mine") {
server_config.mining_config.as_mut().unwrap().enable_mining = true; server_config.mining_config.as_mut().unwrap().enable_mining = true;
} }
if let Some(wallet_url) = server_args.value_of("wallet_url") { if let Some(wallet_url) = a.value_of("wallet_url") {
server_config server_config
.mining_config .mining_config
.as_mut() .as_mut()
@ -392,15 +380,19 @@ fn server_command(server_args: &ArgMatches, global_config: GlobalConfig) {
.wallet_listener_url = wallet_url.to_string(); .wallet_listener_url = wallet_url.to_string();
} }
if let Some(seeds) = server_args.values_of("seed") { if let Some(seeds) = a.values_of("seed") {
server_config.seeding_type = grin::Seeding::List; server_config.seeding_type = grin::Seeding::List;
server_config.seeds = Some(seeds.map(|s| s.to_string()).collect()); server_config.seeds = Some(seeds.map(|s| s.to_string()).collect());
} }
}
if let Some(true) = server_config.run_wallet_listener { if let Some(true) = server_config.run_wallet_listener {
let mut wallet_config = global_config.members.unwrap().wallet; let mut wallet_config = global_config.members.unwrap().wallet;
let wallet_seed = wallet::WalletSeed::from_file(&wallet_config) let wallet_seed = match wallet::WalletSeed::from_file(&wallet_config) {
.expect("Failed to read wallet seed file."); Ok(ws) => ws,
Err(_) => wallet::WalletSeed::init_file(&wallet_config)
.expect("Failed to create wallet seed file."),
};
let mut keychain = wallet_seed let mut keychain = wallet_seed
.derive_keychain("") .derive_keychain("")
.expect("Failed to derive keychain from seed file and passphrase."); .expect("Failed to derive keychain from seed file and passphrase.");
@ -413,7 +405,8 @@ fn server_command(server_args: &ArgMatches, global_config: GlobalConfig) {
} }
// start the server in the different run modes (interactive or daemon) // start the server in the different run modes (interactive or daemon)
match server_args.subcommand() { if let Some(a) = server_args {
match a.subcommand() {
("run", _) => { ("run", _) => {
start_server(server_config); start_server(server_config);
} }
@ -442,6 +435,9 @@ fn server_command(server_args: &ArgMatches, global_config: GlobalConfig) {
); );
} }
} }
} else {
start_server(server_config);
}
} }
fn client_command(client_args: &ArgMatches, global_config: GlobalConfig) { fn client_command(client_args: &ArgMatches, global_config: GlobalConfig) {

View file

@ -16,7 +16,7 @@
extern crate grin_pow as pow; extern crate grin_pow as pow;
pub mod ui; pub mod ui;
mod table; pub mod table;
mod peers; mod peers;
mod constants; mod constants;
mod menu; mod menu;

View file

@ -125,6 +125,10 @@ pub enum ErrorKind {
#[fail(display = "Wallet seed exists error")] #[fail(display = "Wallet seed exists error")]
WalletSeedExists, WalletSeedExists,
/// Wallet seed doesn't exist
#[fail(display = "Wallet seed doesn't exist error")]
WalletSeedDoesntExist,
#[fail(display = "Generic error: {}", _0)] GenericError(&'static str), #[fail(display = "Generic error: {}", _0)] GenericError(&'static str),
} }
@ -471,12 +475,11 @@ impl WalletSeed {
} else { } else {
error!( error!(
LOGGER, LOGGER,
"Run: \"grin wallet init\" to initialize a new wallet.", "wallet seed file {} could not be opened (grin wallet init). \
); Run \"grin wallet init\" to initialize a new wallet.",
panic!(format!(
"wallet seed file {} could not be opened (grin wallet init)",
seed_file_path seed_file_path
)); );
Err(ErrorKind::WalletSeedDoesntExist)?
} }
} }
} }