2019-10-02 11:40:20 +03:00
|
|
|
// Copyright 2019 The Grin Developers
|
2017-04-06 09:41:49 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Main for building the binary of a Grin peer-to-peer node.
|
|
|
|
|
2018-03-27 00:21:03 +03:00
|
|
|
#[macro_use]
|
2017-04-28 08:07:25 +03:00
|
|
|
extern crate clap;
|
2018-12-08 02:59:40 +03:00
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
#[macro_use]
|
2018-10-21 23:30:56 +03:00
|
|
|
extern crate log;
|
2019-02-21 14:57:45 +03:00
|
|
|
use crate::config::config::SERVER_CONFIG_FILE_NAME;
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::global;
|
|
|
|
use crate::util::init_logger;
|
|
|
|
use clap::App;
|
|
|
|
use grin_api as api;
|
2019-07-23 17:47:59 +03:00
|
|
|
use grin_chain as chain;
|
2018-12-08 02:59:40 +03:00
|
|
|
use grin_config as config;
|
|
|
|
use grin_core as core;
|
|
|
|
use grin_p2p as p2p;
|
|
|
|
use grin_servers as servers;
|
|
|
|
use grin_util as util;
|
2017-04-25 04:55:01 +03:00
|
|
|
|
2018-08-04 01:39:54 +03:00
|
|
|
mod cmd;
|
2018-03-13 21:10:45 +03:00
|
|
|
pub mod tui;
|
2017-12-22 21:46:28 +03:00
|
|
|
|
2018-03-23 13:28:15 +03:00
|
|
|
// include build information
|
|
|
|
pub mod built_info {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/built.rs"));
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:44:50 +03:00
|
|
|
pub fn info_strings() -> (String, String) {
|
2018-03-23 13:28:15 +03:00
|
|
|
(
|
|
|
|
format!(
|
|
|
|
"This is Grin version {}{}, built for {} by {}.",
|
|
|
|
built_info::PKG_VERSION,
|
|
|
|
built_info::GIT_VERSION.map_or_else(|| "".to_owned(), |v| format!(" (git {})", v)),
|
|
|
|
built_info::TARGET,
|
2018-10-26 03:44:50 +03:00
|
|
|
built_info::RUSTC_VERSION,
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.to_string(),
|
2018-03-23 13:28:15 +03:00
|
|
|
format!(
|
2018-10-26 03:44:50 +03:00
|
|
|
"Built with profile \"{}\", features \"{}\".",
|
2018-03-23 13:28:15 +03:00
|
|
|
built_info::PROFILE,
|
|
|
|
built_info::FEATURES_STR,
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.to_string(),
|
2018-03-23 13:28:15 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_build_info() {
|
2018-10-26 03:44:50 +03:00
|
|
|
let (basic_info, detailed_info) = info_strings();
|
2018-10-21 23:30:56 +03:00
|
|
|
info!("{}", basic_info);
|
|
|
|
debug!("{}", detailed_info);
|
2018-03-23 13:28:15 +03:00
|
|
|
}
|
|
|
|
|
2017-04-06 09:41:49 +03:00
|
|
|
fn main() {
|
2018-11-04 23:26:46 +03:00
|
|
|
let exit_code = real_main();
|
|
|
|
std::process::exit(exit_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn real_main() -> i32 {
|
2018-12-06 18:25:05 +03:00
|
|
|
let yml = load_yaml!("grin.yml");
|
2019-07-02 18:02:57 +03:00
|
|
|
let args = App::from_yaml(yml)
|
|
|
|
.version(built_info::PKG_VERSION)
|
|
|
|
.get_matches();
|
2019-02-21 14:57:45 +03:00
|
|
|
let node_config;
|
2017-04-28 08:07:25 +03:00
|
|
|
|
2019-04-18 12:37:19 +03:00
|
|
|
// Temporary wallet warning message
|
|
|
|
match args.subcommand() {
|
|
|
|
("wallet", _) => {
|
|
|
|
println!();
|
2019-07-25 12:07:56 +03:00
|
|
|
println!("As of v1.1.0, the wallet has been split into a separate executable.");
|
2019-04-18 12:37:19 +03:00
|
|
|
println!(
|
2019-05-03 14:07:39 +03:00
|
|
|
"Please visit https://github.com/mimblewimble/grin-wallet/releases to download"
|
2019-04-18 12:37:19 +03:00
|
|
|
);
|
|
|
|
println!();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:46:21 +03:00
|
|
|
let chain_type = if args.is_present("floonet") {
|
|
|
|
global::ChainTypes::Floonet
|
|
|
|
} else if args.is_present("usernet") {
|
|
|
|
global::ChainTypes::UserTesting
|
|
|
|
} else {
|
|
|
|
global::ChainTypes::Mainnet
|
|
|
|
};
|
|
|
|
|
2018-08-30 12:10:40 +03:00
|
|
|
// Deal with configuration file creation
|
|
|
|
match args.subcommand() {
|
|
|
|
("server", Some(server_args)) => {
|
|
|
|
// If it's just a server config command, do it and exit
|
|
|
|
if let ("config", Some(_)) = server_args.subcommand() {
|
2018-12-29 01:46:21 +03:00
|
|
|
cmd::config_command_server(&chain_type, SERVER_CONFIG_FILE_NAME);
|
2018-11-04 23:26:46 +03:00
|
|
|
return 0;
|
2018-08-30 12:10:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-07-30 03:48:24 +03:00
|
|
|
}
|
2018-03-21 23:48:56 +03:00
|
|
|
|
2019-01-12 02:54:24 +03:00
|
|
|
// Load relevant config
|
2018-08-30 12:10:40 +03:00
|
|
|
match args.subcommand() {
|
2019-01-12 02:54:24 +03:00
|
|
|
// When the subscommand is 'server' take into account the 'config_file' flag
|
|
|
|
("server", Some(server_args)) => {
|
|
|
|
if let Some(_path) = server_args.value_of("config_file") {
|
|
|
|
node_config = Some(config::GlobalConfig::new(_path).unwrap_or_else(|e| {
|
|
|
|
panic!("Error loading server configuration: {}", e);
|
|
|
|
}));
|
2018-08-30 12:10:40 +03:00
|
|
|
} else {
|
2019-01-12 02:54:24 +03:00
|
|
|
node_config = Some(
|
|
|
|
config::initial_setup_server(&chain_type).unwrap_or_else(|e| {
|
|
|
|
panic!("Error loading server configuration: {}", e);
|
|
|
|
}),
|
|
|
|
);
|
2018-08-30 12:10:40 +03:00
|
|
|
}
|
2019-01-12 02:54:24 +03:00
|
|
|
}
|
|
|
|
// Otherwise load up the node config as usual
|
|
|
|
_ => {
|
|
|
|
node_config = Some(
|
|
|
|
config::initial_setup_server(&chain_type).unwrap_or_else(|e| {
|
|
|
|
panic!("Error loading server configuration: {}", e);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut config) = node_config.clone() {
|
|
|
|
let mut l = config.members.as_mut().unwrap().logging.clone().unwrap();
|
|
|
|
let run_tui = config.members.as_mut().unwrap().server.run_tui;
|
|
|
|
if let Some(true) = run_tui {
|
|
|
|
l.log_to_stdout = false;
|
|
|
|
l.tui_running = Some(true);
|
|
|
|
}
|
|
|
|
init_logger(Some(l));
|
|
|
|
|
|
|
|
global::set_mining_mode(config.members.unwrap().server.clone().chain_type);
|
|
|
|
|
|
|
|
if let Some(file_path) = &config.config_file_path {
|
|
|
|
info!(
|
|
|
|
"Using configuration file at {}",
|
|
|
|
file_path.to_str().unwrap()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!("Node configuration file not found, using default");
|
2018-08-30 12:10:40 +03:00
|
|
|
}
|
2018-07-31 13:14:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:10:40 +03:00
|
|
|
log_build_info();
|
|
|
|
|
2019-01-12 02:54:24 +03:00
|
|
|
// Execute subcommand
|
2017-06-01 00:44:44 +03:00
|
|
|
match args.subcommand() {
|
|
|
|
// server commands and options
|
|
|
|
("server", Some(server_args)) => {
|
2018-11-04 23:26:46 +03:00
|
|
|
cmd::server_command(Some(server_args), node_config.unwrap())
|
2017-06-01 00:44:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// client commands and options
|
2018-11-04 23:26:46 +03:00
|
|
|
("client", Some(client_args)) => cmd::client_command(client_args, node_config.unwrap()),
|
2017-06-01 00:44:44 +03:00
|
|
|
|
2019-07-25 12:13:56 +03:00
|
|
|
// clean command
|
|
|
|
("clean", _) => {
|
|
|
|
let db_root_path = node_config.unwrap().members.unwrap().server.db_root;
|
|
|
|
println!("Cleaning chain data directory: {}", db_root_path);
|
|
|
|
match std::fs::remove_dir_all(db_root_path) {
|
|
|
|
Ok(_) => 0,
|
|
|
|
Err(_) => 1,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-05 04:34:24 +03:00
|
|
|
// If nothing is specified, try to just use the config file instead
|
|
|
|
// this could possibly become the way to configure most things
|
2017-07-26 04:43:17 +03:00
|
|
|
// with most command line options being phased out
|
2018-11-04 23:26:46 +03:00
|
|
|
_ => cmd::server_command(None, node_config.unwrap()),
|
2017-12-22 21:46:28 +03:00
|
|
|
}
|
|
|
|
}
|