2023-04-21 03:45:59 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// 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.
|
2023-04-10 16:02:53 +03:00
|
|
|
|
2023-05-04 20:09:26 +03:00
|
|
|
use std::{fs, thread};
|
|
|
|
use std::fmt::format;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::{Path, PathBuf};
|
2023-05-11 15:34:04 +03:00
|
|
|
use std::sync::{Arc, LockResult, RwLock, RwLockReadGuard};
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
|
2023-05-04 20:09:26 +03:00
|
|
|
use std::thread::JoinHandle;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use futures::channel::oneshot;
|
|
|
|
use grin_chain::SyncStatus;
|
|
|
|
use grin_config::config;
|
2023-04-10 16:02:53 +03:00
|
|
|
use grin_core::global;
|
|
|
|
use grin_core::global::ChainTypes;
|
2023-05-04 20:09:26 +03:00
|
|
|
use grin_servers::{Server, ServerStats};
|
|
|
|
use grin_servers::common::types::Error;
|
2023-04-10 16:02:53 +03:00
|
|
|
use grin_util::logger::LogEntry;
|
2023-05-04 20:09:26 +03:00
|
|
|
use grin_util::StopState;
|
2023-04-10 16:02:53 +03:00
|
|
|
use log::info;
|
|
|
|
|
2023-05-04 20:09:26 +03:00
|
|
|
pub struct Node {
|
|
|
|
/// Node state updated from the separate thread
|
2023-05-11 15:34:04 +03:00
|
|
|
pub(crate) state: Arc<NodeState>,
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
/// Instantiate new node with provided chain type, start server if needed
|
|
|
|
pub fn new(chain_type: ChainTypes, start: bool) -> Self {
|
2023-05-11 15:34:04 +03:00
|
|
|
let state = Arc::new(NodeState::new(chain_type));
|
2023-05-04 20:09:26 +03:00
|
|
|
if start {
|
2023-05-11 20:21:48 +03:00
|
|
|
start_server_thread(state.clone(), chain_type);
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
2023-05-11 15:34:04 +03:00
|
|
|
Self { state }
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop server
|
|
|
|
pub fn stop(&self) {
|
2023-05-11 15:34:04 +03:00
|
|
|
self.state.stop_needed.store(true, Ordering::Relaxed);
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Start server with provided chain type
|
|
|
|
pub fn start(&self, chain_type: ChainTypes) {
|
2023-05-11 20:21:48 +03:00
|
|
|
if !self.state.is_running() {
|
|
|
|
start_server_thread(self.state.clone(), chain_type);
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 20:21:48 +03:00
|
|
|
/// Restart server or start when not running
|
|
|
|
pub fn restart(&mut self) {
|
2023-05-11 15:34:04 +03:00
|
|
|
if self.state.is_running() {
|
|
|
|
self.state.restart_needed.store(true, Ordering::Relaxed);
|
2023-05-04 20:09:26 +03:00
|
|
|
} else {
|
2023-05-11 20:21:48 +03:00
|
|
|
self.start(*self.state.chain_type);
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NodeState {
|
2023-05-11 15:34:04 +03:00
|
|
|
/// Data for UI, None means server is not running
|
|
|
|
stats: Arc<RwLock<Option<ServerStats>>>,
|
2023-05-04 20:09:26 +03:00
|
|
|
/// Chain type of launched server
|
2023-05-11 15:34:04 +03:00
|
|
|
chain_type: Arc<ChainTypes>,
|
2023-05-04 20:09:26 +03:00
|
|
|
/// Thread flag to stop the server and start it again
|
2023-05-11 15:34:04 +03:00
|
|
|
restart_needed: AtomicBool,
|
2023-05-04 20:09:26 +03:00
|
|
|
/// Thread flag to stop the server
|
2023-05-11 15:34:04 +03:00
|
|
|
stop_needed: AtomicBool,
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeState {
|
|
|
|
/// Instantiate new node state with provided chain type and server state
|
2023-05-11 15:34:04 +03:00
|
|
|
pub fn new(chain_type: ChainTypes) -> Self {
|
2023-05-04 20:09:26 +03:00
|
|
|
Self {
|
2023-05-11 15:34:04 +03:00
|
|
|
stats: Arc::new(RwLock::new(None)),
|
|
|
|
chain_type: Arc::new(chain_type),
|
|
|
|
restart_needed: AtomicBool::new(false),
|
|
|
|
stop_needed: AtomicBool::new(false),
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 20:21:48 +03:00
|
|
|
/// Check if server is running when stats are not empty
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
self.get_stats().is_some()
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:34:04 +03:00
|
|
|
/// Check if server is stopping
|
2023-05-04 20:09:26 +03:00
|
|
|
pub fn is_stopping(&self) -> bool {
|
2023-05-11 20:21:48 +03:00
|
|
|
self.stop_needed.load(Ordering::Relaxed)
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
2023-05-11 15:34:04 +03:00
|
|
|
/// Check if server is restarting
|
2023-05-04 20:09:26 +03:00
|
|
|
pub fn is_restarting(&self) -> bool {
|
2023-05-11 20:21:48 +03:00
|
|
|
self.restart_needed.load(Ordering::Relaxed)
|
2023-05-11 15:34:04 +03:00
|
|
|
}
|
|
|
|
|
2023-05-11 20:21:48 +03:00
|
|
|
/// Get server stats
|
|
|
|
pub fn get_stats(&self) -> RwLockReadGuard<'_, Option<ServerStats>> {
|
|
|
|
self.stats.read().unwrap()
|
2023-05-11 15:34:04 +03:00
|
|
|
}
|
|
|
|
|
2023-05-11 20:21:48 +03:00
|
|
|
/// Get server sync status, empty when server is not running
|
|
|
|
pub fn get_sync_status(&self) -> Option<SyncStatus> {
|
|
|
|
// return shutdown status when node is stopping
|
|
|
|
if self.is_stopping() {
|
|
|
|
return Some(SyncStatus::Shutdown)
|
|
|
|
}
|
|
|
|
|
|
|
|
let stats = self.get_stats();
|
|
|
|
// return sync status when server is running (stats are not empty)
|
|
|
|
if stats.is_some() {
|
|
|
|
return Some(stats.as_ref().unwrap().sync_status)
|
|
|
|
}
|
|
|
|
None
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
2023-05-11 20:21:48 +03:00
|
|
|
|
|
|
|
/// Check if server is syncing based on sync status
|
|
|
|
pub fn is_syncing(&self) -> bool {
|
|
|
|
let sync_status = self.get_sync_status();
|
|
|
|
match sync_status {
|
|
|
|
None => { self.is_restarting() }
|
|
|
|
Some(s) => { s!= SyncStatus::NoSync }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a thread to launch server and update node state with server stats
|
|
|
|
fn start_server_thread(state: Arc<NodeState>, chain_type: ChainTypes) -> JoinHandle<()> {
|
|
|
|
thread::spawn(move || {
|
|
|
|
let mut server = start_server(&chain_type);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
|
|
|
|
|
|
|
if state.is_restarting() {
|
|
|
|
server.stop();
|
|
|
|
|
|
|
|
// Create new server with current chain type
|
|
|
|
server = start_server(&state.chain_type);
|
|
|
|
|
|
|
|
state.restart_needed.store(false, Ordering::Relaxed);
|
|
|
|
} else if state.is_stopping() {
|
|
|
|
server.stop();
|
|
|
|
|
|
|
|
let mut w_stats = state.stats.write().unwrap();
|
|
|
|
*w_stats = None;
|
|
|
|
|
|
|
|
state.stop_needed.store(false, Ordering::Relaxed);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
let stats = server.get_server_stats();
|
|
|
|
if stats.is_ok() {
|
|
|
|
let mut w_stats = state.stats.write().unwrap();
|
|
|
|
*w_stats = Some(stats.as_ref().ok().unwrap().clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
2023-05-11 15:34:04 +03:00
|
|
|
/// Start server with provided chain type
|
|
|
|
fn start_server(chain_type: &ChainTypes) -> Server {
|
2023-05-04 20:09:26 +03:00
|
|
|
let mut node_config_result = config::initial_setup_server(chain_type);
|
|
|
|
if node_config_result.is_err() {
|
|
|
|
// Remove config file on init error
|
|
|
|
let mut grin_path = dirs::home_dir().unwrap();
|
|
|
|
grin_path.push(".grin");
|
|
|
|
grin_path.push(chain_type.shortname());
|
|
|
|
grin_path.push(config::SERVER_CONFIG_FILE_NAME);
|
|
|
|
fs::remove_file(grin_path).unwrap();
|
|
|
|
|
|
|
|
// Reinit config
|
|
|
|
node_config_result = config::initial_setup_server(chain_type);
|
|
|
|
}
|
2023-04-10 16:02:53 +03:00
|
|
|
|
2023-05-04 20:09:26 +03:00
|
|
|
let node_config = node_config_result.ok();
|
2023-04-10 16:02:53 +03:00
|
|
|
let config = node_config.clone().unwrap();
|
2023-04-27 01:28:55 +03:00
|
|
|
let server_config = config.members.as_ref().unwrap().server.clone();
|
2023-04-10 16:02:53 +03:00
|
|
|
|
2023-05-11 15:34:04 +03:00
|
|
|
// Remove lock file (in case if we have running node from another app)
|
2023-05-11 20:21:48 +03:00
|
|
|
{
|
|
|
|
let mut db_path = PathBuf::from(&server_config.db_root);
|
|
|
|
db_path.push("grin.lock");
|
|
|
|
fs::remove_file(db_path).unwrap();
|
|
|
|
}
|
2023-05-04 20:09:26 +03:00
|
|
|
|
|
|
|
// Initialize our global chain_type, feature flags (NRD kernel support currently),
|
|
|
|
// accept_fee_base, and future_time_limit.
|
2023-04-10 16:02:53 +03:00
|
|
|
// These are read via global and not read from config beyond this point.
|
2023-05-04 20:09:26 +03:00
|
|
|
if !global::GLOBAL_CHAIN_TYPE.is_init() {
|
|
|
|
global::init_global_chain_type(config.members.as_ref().unwrap().server.chain_type);
|
|
|
|
}
|
2023-04-10 16:02:53 +03:00
|
|
|
info!("Chain: {:?}", global::get_chain_type());
|
2023-05-04 20:09:26 +03:00
|
|
|
|
|
|
|
if !global::GLOBAL_NRD_FEATURE_ENABLED.is_init() {
|
|
|
|
match global::get_chain_type() {
|
|
|
|
ChainTypes::Mainnet => {
|
|
|
|
// Set various mainnet specific feature flags.
|
|
|
|
global::init_global_nrd_enabled(false);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Set various non-mainnet feature flags.
|
|
|
|
global::init_global_nrd_enabled(true);
|
|
|
|
}
|
2023-04-10 16:02:53 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-04 20:09:26 +03:00
|
|
|
if !global::GLOBAL_ACCEPT_FEE_BASE.is_init() {
|
|
|
|
let afb = config
|
|
|
|
.members
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.server
|
|
|
|
.pool_config
|
|
|
|
.accept_fee_base;
|
|
|
|
global::init_global_accept_fee_base(afb);
|
|
|
|
info!("Accept Fee Base: {:?}", global::get_accept_fee_base());
|
|
|
|
}
|
|
|
|
if !global::GLOBAL_FUTURE_TIME_LIMIT.is_init() {
|
|
|
|
global::init_global_future_time_limit(config.members.unwrap().server.future_time_limit);
|
|
|
|
info!("Future Time Limit: {:?}", global::get_future_time_limit());
|
|
|
|
}
|
2023-04-10 16:02:53 +03:00
|
|
|
|
|
|
|
let api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>) =
|
|
|
|
Box::leak(Box::new(oneshot::channel::<()>()));
|
2023-05-11 15:34:04 +03:00
|
|
|
let mut server_result = Server::new(server_config.clone(), None, api_chan);
|
2023-05-04 20:09:26 +03:00
|
|
|
if server_result.is_err() {
|
|
|
|
let mut db_path = PathBuf::from(&server_config.db_root);
|
|
|
|
db_path.push("grin.lock");
|
|
|
|
fs::remove_file(db_path).unwrap();
|
|
|
|
|
|
|
|
// Remove chain data on server start error
|
2023-05-11 20:21:48 +03:00
|
|
|
let dirs_to_remove: Vec<&str> = vec!["header", "lmdb", "txhashset"];
|
2023-05-04 20:09:26 +03:00
|
|
|
for dir in dirs_to_remove {
|
|
|
|
let mut path = PathBuf::from(&server_config.db_root);
|
|
|
|
path.push(dir);
|
|
|
|
fs::remove_dir_all(path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate server
|
|
|
|
let config = node_config.clone().unwrap();
|
|
|
|
let server_config = config.members.as_ref().unwrap().server.clone();
|
|
|
|
let api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>) =
|
|
|
|
Box::leak(Box::new(oneshot::channel::<()>()));
|
2023-05-11 15:34:04 +03:00
|
|
|
server_result = Server::new(server_config.clone(), None, api_chan);
|
2023-05-04 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
server_result.unwrap()
|
2023-04-10 16:02:53 +03:00
|
|
|
}
|