mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
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:
parent
6a1a45e21f
commit
c02309ce87
3 changed files with 90 additions and 91 deletions
166
src/bin/grin.rs
166
src/bin/grin.rs
|
@ -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,42 +332,67 @@ 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 {
|
||||||
server_config.p2p_config.port = port.parse().unwrap();
|
if let Some(port) = a.value_of("port") {
|
||||||
}
|
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()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.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,34 +405,38 @@ 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 {
|
||||||
("run", _) => {
|
match a.subcommand() {
|
||||||
start_server(server_config);
|
("run", _) => {
|
||||||
}
|
start_server(server_config);
|
||||||
("start", _) => {
|
}
|
||||||
let daemonize = Daemonize::new()
|
("start", _) => {
|
||||||
.pid_file("/tmp/grin.pid")
|
let daemonize = Daemonize::new()
|
||||||
.chown_pid_file(true)
|
.pid_file("/tmp/grin.pid")
|
||||||
.working_directory(current_dir().unwrap())
|
.chown_pid_file(true)
|
||||||
.privileged_action(move || {
|
.working_directory(current_dir().unwrap())
|
||||||
start_server(server_config.clone());
|
.privileged_action(move || {
|
||||||
loop {
|
start_server(server_config.clone());
|
||||||
thread::sleep(Duration::from_secs(60));
|
loop {
|
||||||
}
|
thread::sleep(Duration::from_secs(60));
|
||||||
});
|
}
|
||||||
match daemonize.start() {
|
});
|
||||||
Ok(_) => info!(LOGGER, "Grin server successfully started."),
|
match daemonize.start() {
|
||||||
Err(e) => error!(LOGGER, "Error starting: {}", e),
|
Ok(_) => info!(LOGGER, "Grin server successfully started."),
|
||||||
|
Err(e) => error!(LOGGER, "Error starting: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
("stop", _) => println!("TODO. Just 'kill $pid' for now. Maybe /tmp/grin.pid is $pid"),
|
||||||
|
(cmd, _) => {
|
||||||
|
println!(":: {:?}", server_args);
|
||||||
|
panic!(
|
||||||
|
"Unknown server command '{}', use 'grin help server' for details",
|
||||||
|
cmd
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
("stop", _) => println!("TODO. Just 'kill $pid' for now. Maybe /tmp/grin.pid is $pid"),
|
} else {
|
||||||
(cmd, _) => {
|
start_server(server_config);
|
||||||
println!(":: {:?}", server_args);
|
|
||||||
panic!(
|
|
||||||
"Unknown server command '{}', use 'grin help server' for details",
|
|
||||||
cmd
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue