2021-03-10 18:14:48 +03:00
|
|
|
// Copyright 2021 The Grin Developers
|
2018-08-04 01:39:54 +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.
|
|
|
|
|
|
|
|
/// Grin server commands processing
|
|
|
|
use std::process::exit;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::config::GlobalConfig;
|
2020-02-14 17:58:57 +03:00
|
|
|
use crate::p2p::Seeding;
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::servers;
|
|
|
|
use crate::tui::ui;
|
2022-01-07 16:24:54 +03:00
|
|
|
use futures::channel::oneshot;
|
2020-02-14 17:58:57 +03:00
|
|
|
use grin_p2p::msg::PeerAddrs;
|
|
|
|
use grin_p2p::PeerAddr;
|
2019-11-13 17:45:59 +03:00
|
|
|
use grin_util::logger::LogEntry;
|
|
|
|
use std::sync::mpsc;
|
2018-08-04 01:39:54 +03:00
|
|
|
|
|
|
|
/// wrap below to allow UI to clean up on stop
|
2022-01-07 16:24:54 +03:00
|
|
|
pub fn start_server(
|
|
|
|
config: servers::ServerConfig,
|
|
|
|
logs_rx: Option<mpsc::Receiver<LogEntry>>,
|
|
|
|
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
|
|
|
|
) {
|
|
|
|
start_server_tui(config, logs_rx, api_chan);
|
2018-08-04 01:39:54 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:54 +03:00
|
|
|
fn start_server_tui(
|
|
|
|
config: servers::ServerConfig,
|
|
|
|
logs_rx: Option<mpsc::Receiver<LogEntry>>,
|
|
|
|
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
|
|
|
|
) {
|
2018-08-04 01:39:54 +03:00
|
|
|
// Run the UI controller.. here for now for simplicity to access
|
|
|
|
// everything it might need
|
2019-01-20 12:45:16 +03:00
|
|
|
if config.run_tui.unwrap_or(false) {
|
2018-10-21 23:30:56 +03:00
|
|
|
warn!("Starting GRIN in UI mode...");
|
2019-11-13 17:45:59 +03:00
|
|
|
servers::Server::start(
|
|
|
|
config,
|
|
|
|
logs_rx,
|
|
|
|
|serv: servers::Server, logs_rx: Option<mpsc::Receiver<LogEntry>>| {
|
|
|
|
let mut controller = ui::Controller::new(logs_rx.unwrap()).unwrap_or_else(|e| {
|
|
|
|
panic!("Error loading UI controller: {}", e);
|
|
|
|
});
|
|
|
|
controller.run(serv);
|
|
|
|
},
|
2022-01-07 16:24:54 +03:00
|
|
|
None,
|
|
|
|
api_chan,
|
2019-11-13 17:45:59 +03:00
|
|
|
)
|
2018-12-08 02:59:40 +03:00
|
|
|
.unwrap();
|
2018-08-04 01:39:54 +03:00
|
|
|
} else {
|
2018-10-21 23:30:56 +03:00
|
|
|
warn!("Starting GRIN w/o UI...");
|
2019-11-13 17:45:59 +03:00
|
|
|
servers::Server::start(
|
|
|
|
config,
|
|
|
|
logs_rx,
|
|
|
|
|serv: servers::Server, _: Option<mpsc::Receiver<LogEntry>>| {
|
|
|
|
let running = Arc::new(AtomicBool::new(true));
|
|
|
|
let r = running.clone();
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
r.store(false, Ordering::SeqCst);
|
|
|
|
})
|
|
|
|
.expect("Error setting handler for both SIGINT (Ctrl+C) and SIGTERM (kill)");
|
|
|
|
while running.load(Ordering::SeqCst) {
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
|
|
|
}
|
|
|
|
warn!("Received SIGINT (Ctrl+C) or SIGTERM (kill).");
|
|
|
|
serv.stop();
|
|
|
|
},
|
2022-01-07 16:24:54 +03:00
|
|
|
None,
|
|
|
|
api_chan,
|
2019-11-13 17:45:59 +03:00
|
|
|
)
|
2018-12-08 02:59:40 +03:00
|
|
|
.unwrap();
|
2018-08-04 01:39:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles the server part of the command line, mostly running, starting and
|
|
|
|
/// stopping the Grin blockchain server. Processes all the command line
|
|
|
|
/// arguments to build a proper configuration and runs Grin with that
|
|
|
|
/// configuration.
|
2018-12-08 02:59:40 +03:00
|
|
|
pub fn server_command(
|
|
|
|
server_args: Option<&ArgMatches<'_>>,
|
2020-05-22 14:51:58 +03:00
|
|
|
global_config: GlobalConfig,
|
2019-11-13 17:45:59 +03:00
|
|
|
logs_rx: Option<mpsc::Receiver<LogEntry>>,
|
2022-01-07 16:24:54 +03:00
|
|
|
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
|
2018-12-08 02:59:40 +03:00
|
|
|
) -> i32 {
|
2018-08-04 01:39:54 +03:00
|
|
|
// just get defaults from the global config
|
|
|
|
let mut server_config = global_config.members.as_ref().unwrap().server.clone();
|
|
|
|
|
|
|
|
if let Some(a) = server_args {
|
|
|
|
if let Some(port) = a.value_of("port") {
|
|
|
|
server_config.p2p_config.port = port.parse().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(api_port) = a.value_of("api_port") {
|
|
|
|
let default_ip = "0.0.0.0";
|
|
|
|
server_config.api_http_addr = format!("{}:{}", default_ip, api_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(wallet_url) = a.value_of("wallet_url") {
|
|
|
|
server_config
|
|
|
|
.stratum_mining_config
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.wallet_listener_url = wallet_url.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(seeds) = a.values_of("seed") {
|
2020-04-24 12:19:34 +03:00
|
|
|
let peers = seeds.filter_map(|s| s.parse().ok()).map(PeerAddr).collect();
|
2018-08-23 22:16:04 +03:00
|
|
|
server_config.p2p_config.seeding_type = Seeding::List;
|
2020-02-14 17:58:57 +03:00
|
|
|
server_config.p2p_config.seeds = Some(PeerAddrs { peers });
|
2018-08-04 01:39:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(a) = server_args {
|
|
|
|
match a.subcommand() {
|
|
|
|
("run", _) => {
|
2022-01-07 16:24:54 +03:00
|
|
|
start_server(server_config, logs_rx, api_chan);
|
2018-08-04 01:39:54 +03:00
|
|
|
}
|
2018-11-26 02:47:47 +03:00
|
|
|
("", _) => {
|
|
|
|
println!("Subcommand required, use 'grin help server' for details");
|
|
|
|
}
|
2018-08-04 01:39:54 +03:00
|
|
|
(cmd, _) => {
|
|
|
|
println!(":: {:?}", server_args);
|
|
|
|
panic!(
|
|
|
|
"Unknown server command '{}', use 'grin help server' for details",
|
|
|
|
cmd
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-07 16:24:54 +03:00
|
|
|
start_server(server_config, logs_rx, api_chan);
|
2018-08-04 01:39:54 +03:00
|
|
|
}
|
2018-11-04 23:26:46 +03:00
|
|
|
0
|
2018-08-04 01:39:54 +03:00
|
|
|
}
|