main: prevent overflowing p2pool manual node index

This commit is contained in:
hinto-janaiyo 2022-11-19 13:34:46 -05:00
parent 2f6bb53027
commit e8c41fceb3
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
2 changed files with 34 additions and 27 deletions

View file

@ -325,12 +325,12 @@ impl Node {
// Convert [Vec<(String, Self)>] into [String] // Convert [Vec<(String, Self)>] into [String]
// that can be written as a proper TOML file // 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(); let mut toml = String::new();
for (key, value) in vec.iter() { for (key, value) in vec.iter() {
write!( write!(
toml, toml,
"[\'{}\']\nip = {:#?}\nrpc = {:#?}\nzmq = {:#?}\n", "[\'{}\']\nip = {:#?}\nrpc = {:#?}\nzmq = {:#?}\n\n",
key, key,
value.ip, value.ip,
value.rpc, value.rpc,
@ -367,33 +367,23 @@ impl Node {
info!("Node | Creating new default..."); info!("Node | Creating new default...");
let new = Self::new_vec(); let new = Self::new_vec();
let path = get_file_path(File::Node)?; 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)?; fs::write(&path, &string)?;
info!("Node | Write ... OK"); info!("Node | Write ... OK");
Ok(new) Ok(new)
} }
// // Save [State] onto disk file [gupax.toml] // Save [Node] onto disk file [node.toml]
// pub fn save(&mut self) -> Result<(), TomlError> { pub fn save(vec: &Vec<(String, Self)>) -> Result<(), TomlError> {
// info!("Saving {:?} to disk...", self); info!("Node | Saving to disk...");
// let path = get_file_path(File::State)?; let path = get_file_path(File::Node)?;
// // Convert path to absolute let string = Self::to_string(vec);
// self.gupax.absolute_p2pool_path = into_absolute_path(self.gupax.p2pool_path.clone())?; match fs::write(path, string) {
// self.gupax.absolute_xmrig_path = into_absolute_path(self.gupax.xmrig_path.clone())?; Ok(_) => { info!("TOML save ... OK"); Ok(()) },
// let string = match toml::ser::to_string(&self) { Err(err) => { error!("Couldn't overwrite TOML file"); return Err(TomlError::Io(err)) },
// 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)) },
// }
// }
//
// // Take [Self] as input, merge it with whatever the current [default] is, // // Take [Self] as input, merge it with whatever the current [default] is,
// // leaving behind old keys+values and updating [default] with old valid ones. // // leaving behind old keys+values and updating [default] with old valid ones.
// // Automatically overwrite current file. // // Automatically overwrite current file.

View file

@ -167,7 +167,8 @@ impl App {
let mut og = app.og.lock().unwrap(); // Lock [og] let mut og = app.og.lock().unwrap(); // Lock [og]
app.state = og.clone(); app.state = og.clone();
// Get node list // 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 // Handle max threads
og.xmrig.max_threads = num_cpus::get(); og.xmrig.max_threads = num_cpus::get();
let current = og.xmrig.current_threads; let current = og.xmrig.current_threads;
@ -175,6 +176,21 @@ impl App {
if current > max { if current > max {
og.xmrig.current_threads = 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] // Apply TOML values to [Update]
let p2pool_path = og.gupax.absolute_p2pool_path.clone(); let p2pool_path = og.gupax.absolute_p2pool_path.clone();
let xmrig_path = og.gupax.absolute_xmrig_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); }, Err(e) => { error!("{}", e); exit(1); },
}; };
match std::fs::read_to_string(&path) { 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); }, Err(e) => { error!("{}", e); exit(1); },
} }
} }
@ -765,8 +781,9 @@ impl eframe::App for App {
og.gupax = self.state.gupax.clone(); og.gupax = self.state.gupax.clone();
og.p2pool = self.state.p2pool.clone(); og.p2pool = self.state.p2pool.clone();
og.xmrig = self.state.xmrig.clone(); og.xmrig = self.state.xmrig.clone();
self.state.save();
self.og_node_vec = self.node_vec.clone(); self.og_node_vec = self.node_vec.clone();
self.state.save();
Node::save(&self.og_node_vec);
} }
}); });