From 23fc9f3a39255a1e90d5f2684b39742a5fe35aab Mon Sep 17 00:00:00 2001 From: Cyrix126 Date: Tue, 26 Mar 2024 07:45:57 +0100 Subject: [PATCH] fix: getting current shares fix: get the good pattern fix: use regex (perf improvements) fix: use error message in log instead of panic feat: add debug messages --- src/helper/p2pool.rs | 25 +++++++++++++++++-------- src/utils/constants.rs | 8 ++------ src/utils/regex.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/helper/p2pool.rs b/src/helper/p2pool.rs index 137d0e1..2d8eb67 100644 --- a/src/helper/p2pool.rs +++ b/src/helper/p2pool.rs @@ -5,6 +5,10 @@ use crate::disk::state::P2pool; use crate::helper::ProcessName; use crate::helper::ProcessSignal; use crate::helper::ProcessState; +use crate::regex::contains_end_status; +use crate::regex::contains_statuscommand; +use crate::regex::contains_yourshare; +use crate::regex::nb_current_shares; use crate::regex::P2POOL_REGEX; use crate::{ constants::*, @@ -58,19 +62,24 @@ impl Helper { while let Some(Ok(line)) = stdout.next() { // if command status is sent by gupaxx process and not the user, forward it only to update_from_status method. // 25 lines after the command are the result of status, with last line finishing by update. - if line.contains("statusfromgupaxx") { + if contains_statuscommand(&line) { status_output = true; continue; } if status_output { - if line.contains("Your shares") { + if contains_yourshare(&line) { // update sidechain shares - // todo optimization: replace with regex - // todo safety: do not panic but show error message to user - let shares = line.split_once("=").expect("should be = at Your Share, maybe new version of p2pool has different output for status command ?").1.split_once("blocks").expect("should be a 'blocks' at Your Share, maybe new version of p2pool has different output for status command ?").0.trim().parse::().expect("this should be the number of share"); - lock!(pub_api).sidechain_shares = shares; + if let Some(shares) = nb_current_shares(&line) { + debug!( + "P2pool | PTY getting current shares data from status: {} share", + shares + ); + lock!(pub_api).sidechain_shares = shares; + } else { + error!("P2pool | PTY Getting data from status: Lines contains Your shares but no value found: {}", line); + } } - if line.contains("Uptime") { + if contains_end_status(&line) { // end of status status_output = false; } @@ -667,7 +676,7 @@ impl Helper { } } if lock!(gui_api).tick_status >= 10 && lock!(process).state == ProcessState::Alive { - debug!("reading status output of p2pool node"); + debug!("P2Pool Watchdog | Reading status output of p2pool node"); #[cfg(target_os = "windows")] if let Err(e) = write!(stdin, "statusfromgupaxx\r\n") { error!("P2Pool Watchdog | STDIN error: {}", e); diff --git a/src/utils/constants.rs b/src/utils/constants.rs index 3ab64ce..c122d1d 100644 --- a/src/utils/constants.rs +++ b/src/utils/constants.rs @@ -464,12 +464,8 @@ pub const UNKNOWN_DATA: &str = "???"; pub const BLOCK_PPLNS_WINDOW_MINI: u64 = 2160; pub const BLOCK_PPLNS_WINDOW_MAIN: u64 = 363; pub const SECOND_PER_BLOCK_P2POOL: u64 = 10; -pub const TIME_PPLNS_WINDOW_MINI: Duration = - Duration::from_secs(BLOCK_PPLNS_WINDOW_MINI * SECOND_PER_BLOCK_P2POOL); -pub const TIME_PPLNS_WINDOW_MAIN: Duration = - Duration::from_secs(BLOCK_PPLNS_WINDOW_MAIN * SECOND_PER_BLOCK_P2POOL); - -use std::time::Duration; +// pub const TIME_PPLNS_WINDOW_MINI: Duration = Duration::from_secs(BLOCK_PPLNS_WINDOW_MINI * SECOND_PER_BLOCK_P2POOL); +// pub const TIME_PPLNS_WINDOW_MAIN: Duration = Duration::from_secs(BLOCK_PPLNS_WINDOW_MAIN * SECOND_PER_BLOCK_P2POOL); //---------------------------------------------------------------------------------------------------- Visuals use egui::epaint::{Rounding, Shadow, Stroke}; diff --git a/src/utils/regex.rs b/src/utils/regex.rs index 4b26dc0..0cf13ef 100644 --- a/src/utils/regex.rs +++ b/src/utils/regex.rs @@ -129,6 +129,38 @@ pub fn num_lines(s: &str) -> usize { static LINE_BREAKS: Lazy = Lazy::new(|| Regex::new(r"\r?\n").unwrap()); LINE_BREAKS.captures_iter(s).count() + 1 } +// get the number of current shares +pub fn nb_current_shares(s: &str) -> Option { + static CURRENT_SHARE: Lazy = + Lazy::new(|| Regex::new(r"Your shares = (?P\d+) blocks").unwrap()); + if let Some(c) = CURRENT_SHARE.captures(s) { + if let Some(m) = c.name("nb") { + return Some( + m.as_str().parse::().expect( + &[ + "the number of shares should have been a unit number but is :\n", + m.as_str(), + ] + .concat(), + ), + ); + } + } + None +} +pub fn contains_statuscommand(l: &str) -> bool { + static LINE_SHARE: Lazy = Lazy::new(|| Regex::new(r"^statusfromgupaxx").unwrap()); + LINE_SHARE.is_match(l) +} +pub fn contains_yourshare(l: &str) -> bool { + static LINE_SHARE: Lazy = + Lazy::new(|| Regex::new(r"^Your shares = ").unwrap()); + LINE_SHARE.is_match(l) +} +pub fn contains_end_status(l: &str) -> bool { + static LINE_SHARE: Lazy = Lazy::new(|| Regex::new(r"^Uptime ").unwrap()); + LINE_SHARE.is_match(l) +} //---------------------------------------------------------------------------------------------------- TEST #[cfg(test)] mod test {