From e8c41fceb32aa51e64c9b5dec09a5d06028ab72a Mon Sep 17 00:00:00 2001 From: hinto-janaiyo Date: Sat, 19 Nov 2022 13:34:46 -0500 Subject: [PATCH] main: prevent overflowing p2pool manual node index --- src/disk.rs | 38 ++++++++++++++------------------------ src/main.rs | 23 ++++++++++++++++++++--- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/disk.rs b/src/disk.rs index b7b9666..76a07fc 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -325,12 +325,12 @@ impl Node { // Convert [Vec<(String, Self)>] into [String] // that can be written as a proper TOML file - pub fn into_string(vec: Vec<(String, Self)>) -> String { + pub fn to_string(vec: &Vec<(String, Self)>) -> String { let mut toml = String::new(); for (key, value) in vec.iter() { write!( toml, - "[\'{}\']\nip = {:#?}\nrpc = {:#?}\nzmq = {:#?}\n", + "[\'{}\']\nip = {:#?}\nrpc = {:#?}\nzmq = {:#?}\n\n", key, value.ip, value.rpc, @@ -367,33 +367,23 @@ impl Node { info!("Node | Creating new default..."); let new = Self::new_vec(); let path = get_file_path(File::Node)?; - let string = Self::into_string(Self::new_vec()); + let string = Self::to_string(&Self::new_vec()); fs::write(&path, &string)?; info!("Node | Write ... OK"); Ok(new) } -// // Save [State] onto disk file [gupax.toml] -// pub fn save(&mut self) -> Result<(), TomlError> { -// info!("Saving {:?} to disk...", self); -// let path = get_file_path(File::State)?; -// // Convert path to absolute -// self.gupax.absolute_p2pool_path = into_absolute_path(self.gupax.p2pool_path.clone())?; -// self.gupax.absolute_xmrig_path = into_absolute_path(self.gupax.xmrig_path.clone())?; -// let string = match toml::ser::to_string(&self) { -// Ok(string) => { -// info!("TOML parse ... OK"); -// print_toml(&string); -// string -// }, -// Err(err) => { error!("Couldn't parse TOML into string"); return Err(TomlError::Serialize(err)) }, -// }; -// match fs::write(path, string) { -// Ok(_) => { info!("TOML save ... OK"); Ok(()) }, -// Err(err) => { error!("Couldn't overwrite TOML file"); return Err(TomlError::Io(err)) }, -// } -// } -// + // Save [Node] onto disk file [node.toml] + pub fn save(vec: &Vec<(String, Self)>) -> Result<(), TomlError> { + info!("Node | Saving to disk..."); + let path = get_file_path(File::Node)?; + let string = Self::to_string(vec); + match fs::write(path, string) { + Ok(_) => { info!("TOML save ... OK"); Ok(()) }, + Err(err) => { error!("Couldn't overwrite TOML file"); return Err(TomlError::Io(err)) }, + } + } + // // Take [Self] as input, merge it with whatever the current [default] is, // // leaving behind old keys+values and updating [default] with old valid ones. // // Automatically overwrite current file. diff --git a/src/main.rs b/src/main.rs index 24ebce1..83e5e19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,7 +167,8 @@ impl App { let mut og = app.og.lock().unwrap(); // Lock [og] app.state = og.clone(); // Get node list - app.node_vec = Node::get().unwrap(); + app.og_node_vec = Node::get().unwrap(); + app.node_vec = app.og_node_vec.clone(); // Handle max threads og.xmrig.max_threads = num_cpus::get(); let current = og.xmrig.current_threads; @@ -175,6 +176,21 @@ impl App { if current > max { og.xmrig.current_threads = max; } + // Handle [node_vec] overflow + if og.p2pool.selected_index > app.og_node_vec.len() as u16 { + warn!("App | Overflowing manual node index [{} > {}], resetting to 1", og.p2pool.selected_index, app.og_node_vec.len()); + let (name, node) = app.og_node_vec[0].clone(); + og.p2pool.selected_index = 1; + og.p2pool.selected_name = name.clone(); + og.p2pool.selected_ip = node.ip.clone(); + og.p2pool.selected_rpc = node.rpc.clone(); + og.p2pool.selected_zmq = node.rpc.clone(); + app.state.p2pool.selected_index = 1; + app.state.p2pool.selected_name = name; + app.state.p2pool.selected_ip = node.ip; + app.state.p2pool.selected_rpc = node.rpc; + app.state.p2pool.selected_zmq = node.zmq; + } // Apply TOML values to [Update] let p2pool_path = og.gupax.absolute_p2pool_path.clone(); let xmrig_path = og.gupax.absolute_xmrig_path.clone(); @@ -516,7 +532,7 @@ fn print_disk_file(file: File) { Err(e) => { error!("{}", e); exit(1); }, }; match std::fs::read_to_string(&path) { - Ok(string) => { println!("{}", string); exit(0); }, + Ok(string) => { print!("{}", string); exit(0); }, Err(e) => { error!("{}", e); exit(1); }, } } @@ -765,8 +781,9 @@ impl eframe::App for App { og.gupax = self.state.gupax.clone(); og.p2pool = self.state.p2pool.clone(); og.xmrig = self.state.xmrig.clone(); - self.state.save(); self.og_node_vec = self.node_vec.clone(); + self.state.save(); + Node::save(&self.og_node_vec); } });