mirror of
https://code.gri.mw/GUI/grim.git
synced 2026-07-13 10:18:54 +00:00
config + ui: config refactoring, stratum server launch, mining ui (without worker list yet)
This commit is contained in:
+25
-51
@@ -12,11 +12,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{fs, thread};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use grin_config::{config, ConfigError, ConfigMembers, GlobalConfig};
|
||||
use grin_config::config::{API_SECRET_FILE_NAME, FOREIGN_API_SECRET_FILE_NAME, SERVER_CONFIG_FILE_NAME};
|
||||
use grin_core::global::ChainTypes;
|
||||
@@ -24,70 +19,49 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Settings;
|
||||
|
||||
/// Node config that contains [`GlobalConfig`] to be used by [`grin_servers::Server`].
|
||||
/// Wrapped node config to be used by [`grin_servers::Server`].
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct NodeConfig {
|
||||
pub global_config: GlobalConfig,
|
||||
update_needed: AtomicBool,
|
||||
updating: AtomicBool
|
||||
pub members: ConfigMembers
|
||||
}
|
||||
|
||||
impl NodeConfig {
|
||||
/// Initialize node config with provided chain type from the disk.
|
||||
pub fn init(chain_type: &ChainTypes) -> Self {
|
||||
/// Initialize integrated node config.
|
||||
pub fn init(chain_type: ChainTypes) -> Self {
|
||||
let _ = Self::check_api_secret_files(chain_type, API_SECRET_FILE_NAME);
|
||||
let _ = Self::check_api_secret_files(chain_type, FOREIGN_API_SECRET_FILE_NAME);
|
||||
|
||||
let config_path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
||||
|
||||
// Create default config if it doesn't exist or has wrong format.
|
||||
if !config_path.exists() || toml::from_str::<ConfigMembers>(
|
||||
fs::read_to_string(config_path.clone()).unwrap().as_str()
|
||||
).is_err() {
|
||||
let mut default_config = GlobalConfig::for_chain(chain_type);
|
||||
default_config.update_paths(&Settings::get_working_path(Some(chain_type)));
|
||||
let _ = default_config.write_to_file(config_path.to_str().unwrap());
|
||||
}
|
||||
|
||||
let config = GlobalConfig::new(config_path.to_str().unwrap());
|
||||
|
||||
let config_members = Self::for_chain_type(chain_type);
|
||||
Self {
|
||||
global_config: config.unwrap(),
|
||||
update_needed: AtomicBool::new(false),
|
||||
updating: AtomicBool::new(false)
|
||||
members: config_members
|
||||
}
|
||||
}
|
||||
|
||||
/// Write node config on disk.
|
||||
pub fn save_config(&self) {
|
||||
if self.updating.load(Ordering::Relaxed) {
|
||||
self.update_needed.store(true, Ordering::Relaxed);
|
||||
return;
|
||||
/// Initialize config with provided [`ChainTypes`].
|
||||
pub fn for_chain_type(chain_type: ChainTypes) -> ConfigMembers {
|
||||
let path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
||||
let parsed = Settings::read_from_file::<ConfigMembers>(path.clone());
|
||||
if !path.exists() || parsed.is_err() {
|
||||
let mut default_config = GlobalConfig::for_chain(&chain_type);
|
||||
default_config.update_paths(&Settings::get_working_path(Some(chain_type)));
|
||||
let config = default_config.members.unwrap();
|
||||
Settings::write_to_file(&config, path);
|
||||
config
|
||||
} else {
|
||||
parsed.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
thread::spawn(move || loop {
|
||||
let config = Settings::get_node_config();
|
||||
config.update_needed.store(false, Ordering::Relaxed);
|
||||
config.updating.store(true, Ordering::Relaxed);
|
||||
|
||||
let chain_type = &config.global_config.members.clone().unwrap().server.chain_type;
|
||||
let config_path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
||||
|
||||
// Write config to file.
|
||||
let conf_out = toml::to_string(&config.global_config.members).unwrap();
|
||||
let mut file = File::create(config_path.to_str().unwrap()).unwrap();
|
||||
file.write_all(conf_out.as_bytes()).unwrap();
|
||||
|
||||
if !config.update_needed.load(Ordering::Relaxed) {
|
||||
config.updating.store(false, Ordering::Relaxed);
|
||||
break;
|
||||
}
|
||||
});
|
||||
/// Save node config to disk.
|
||||
pub fn save(&mut self) {
|
||||
let chain_type = self.members.server.chain_type;
|
||||
let config_path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
||||
Settings::write_to_file(&self.members, config_path);
|
||||
}
|
||||
|
||||
/// Check that the api secret files exist and are valid.
|
||||
fn check_api_secret_files(
|
||||
chain_type: &ChainTypes,
|
||||
chain_type: ChainTypes,
|
||||
secret_file_name: &str,
|
||||
) -> Result<(), ConfigError> {
|
||||
let grin_path = Settings::get_working_path(Some(chain_type));
|
||||
|
||||
+35
-11
@@ -45,7 +45,9 @@ pub struct Node {
|
||||
/// Thread flag to stop the server.
|
||||
stop_needed: AtomicBool,
|
||||
/// Flag to check if app exit is needed after server stop.
|
||||
exit_after_stop: AtomicBool
|
||||
exit_after_stop: AtomicBool,
|
||||
/// Thread flag to start stratum server at separate.
|
||||
start_stratum_server: AtomicBool
|
||||
}
|
||||
|
||||
impl Default for Node {
|
||||
@@ -55,7 +57,8 @@ impl Default for Node {
|
||||
starting: AtomicBool::new(false),
|
||||
restart_needed: AtomicBool::new(false),
|
||||
stop_needed: AtomicBool::new(false),
|
||||
exit_after_stop: AtomicBool::new(false)
|
||||
exit_after_stop: AtomicBool::new(false),
|
||||
start_stratum_server: AtomicBool::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,6 +86,16 @@ impl Node {
|
||||
}
|
||||
}
|
||||
|
||||
/// Start stratum server.
|
||||
pub fn start_stratum_server() {
|
||||
NODE_STATE.start_stratum_server.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Check if stratum server is starting.
|
||||
pub fn is_stratum_server_starting() -> bool {
|
||||
NODE_STATE.start_stratum_server.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Check if node is starting.
|
||||
pub fn is_starting() -> bool {
|
||||
NODE_STATE.starting.load(Ordering::Relaxed)
|
||||
@@ -164,8 +177,20 @@ impl Node {
|
||||
|
||||
NODE_STATE.starting.store(false, Ordering::Relaxed);
|
||||
NODE_STATE.stop_needed.store(false, Ordering::Relaxed);
|
||||
NODE_STATE.start_stratum_server.store(false, Ordering::Relaxed);
|
||||
break;
|
||||
} else {
|
||||
if Self::is_stratum_server_starting() {
|
||||
// Start mining server.
|
||||
let stratum_config = server.config.stratum_mining_config.clone().unwrap();
|
||||
server.start_stratum_server(stratum_config);
|
||||
|
||||
// Wait for mining server to start and update status.
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
|
||||
NODE_STATE.start_stratum_server.store(false, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
let stats = server.get_server_stats();
|
||||
if stats.is_ok() {
|
||||
// Update server stats.
|
||||
@@ -308,8 +333,8 @@ impl Node {
|
||||
/// Start the node [`Server`].
|
||||
fn start_server() -> Server {
|
||||
// Get current global config
|
||||
let config = &Settings::get_node_config().global_config;
|
||||
let server_config = config.members.as_ref().unwrap().server.clone();
|
||||
let config = &Settings::node_config_to_read().members;
|
||||
let server_config = config.server.clone();
|
||||
|
||||
// Remove temporary file dir
|
||||
{
|
||||
@@ -328,7 +353,7 @@ fn start_server() -> Server {
|
||||
// accept_fee_base, and future_time_limit.
|
||||
// These are read via global and not read from config beyond this point.
|
||||
if !global::GLOBAL_CHAIN_TYPE.is_init() {
|
||||
global::init_global_chain_type(config.members.as_ref().unwrap().server.chain_type);
|
||||
global::init_global_chain_type(config.server.chain_type);
|
||||
}
|
||||
info!("Chain: {:?}", global::get_chain_type());
|
||||
|
||||
@@ -345,12 +370,12 @@ fn start_server() -> Server {
|
||||
}
|
||||
}
|
||||
if !global::GLOBAL_ACCEPT_FEE_BASE.is_init() {
|
||||
let afb = config.members.as_ref().unwrap().server.pool_config.accept_fee_base;
|
||||
let afb = config.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() {
|
||||
let future_time_limit = config.members.as_ref().unwrap().server.future_time_limit;
|
||||
let future_time_limit = config.server.future_time_limit;
|
||||
global::init_global_future_time_limit(future_time_limit);
|
||||
info!("Future Time Limit: {:?}", global::get_future_time_limit());
|
||||
}
|
||||
@@ -417,13 +442,12 @@ pub extern "C" fn Java_mw_gri_android_BackgroundService_getSyncTitle(
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
/// Check if app exit is needed after node stop.
|
||||
/// Check if app exit is needed after node stop to finish Android app at background.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_exitAppAfterNodeStop(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jboolean {
|
||||
let exit_after_stop = NODE_STATE.exit_after_stop.load(Ordering::Relaxed);
|
||||
let is_app_exit_needed = !Node::is_running() && exit_after_stop;
|
||||
return is_app_exit_needed as jboolean;
|
||||
let exit_needed = !Node::is_running() && NODE_STATE.exit_after_stop.load(Ordering::Relaxed);
|
||||
return exit_needed as jboolean;
|
||||
}
|
||||
Reference in New Issue
Block a user