From 101f4f68869520aae8e8084da041b86c6fc98d78 Mon Sep 17 00:00:00 2001 From: mostafaei2002 Date: Tue, 4 Jun 2024 12:21:49 +0330 Subject: [PATCH] feat: add xvb modes --- Dockerfile | 7 +++++++ src/app/mod.rs | 9 ++++++++- src/app/panels/middle/xvb.rs | 30 +++++++++++++++++++++++++----- src/disk/state.rs | 17 ++++++++++++++++- src/helper/xvb/algorithm.rs | 24 +++++++++++++++--------- src/helper/xvb/mod.rs | 8 ++++---- src/helper/xvb/priv_stats.rs | 30 +++++++++++++++++++++++++++++- 7 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9ff0c59 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM rust:latest + +WORKDIR /app + +COPY . . + +RUN cargo build --release diff --git a/src/app/mod.rs b/src/app/mod.rs index 68d1d13..4ab3a11 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -525,7 +525,14 @@ impl App { app.tab = app.state.gupax.tab; // Set saved Hero mode to runtime. - app.xvb_api.lock().unwrap().stats_priv.runtime_hero_mode = app.state.xvb.hero; + app.xvb_api.lock().unwrap().stats_priv.runtime_mode = app.state.xvb.mode.clone().into(); + app.xvb_api.lock().unwrap().stats_priv.runtime_manual_amount = match app.state.xvb.amount.parse() { + Ok(n) => n, + Err(_) => { + warn!("Cannot parse [amount] to u64, defaulting to 0"); + 0 + } + }; // Check if [P2pool.node] exists info!("App Init | Checking if saved remote node still exists..."); diff --git a/src/app/panels/middle/xvb.rs b/src/app/panels/middle/xvb.rs index c1b62a7..0890a23 100644 --- a/src/app/panels/middle/xvb.rs +++ b/src/app/panels/middle/xvb.rs @@ -6,11 +6,12 @@ use log::debug; use readable::num::Float; use readable::up::Uptime; +use crate::disk::state::XvbMode; use crate::helper::xvb::PubXvbApi; use crate::regex::num_lines; use crate::utils::constants::{ GREEN, LIGHT_GRAY, ORANGE, RED, XVB_DONATED_1H_FIELD, XVB_DONATED_24H_FIELD, XVB_FAILURE_FIELD, - XVB_HELP, XVB_HERO_SELECT, XVB_ROUND_TYPE_FIELD, XVB_TOKEN_FIELD, XVB_TOKEN_LEN, XVB_URL_RULES, + XVB_HELP, XVB_ROUND_TYPE_FIELD, XVB_TOKEN_FIELD, XVB_TOKEN_LEN, XVB_URL_RULES, XVB_WINNER_FIELD, }; use crate::utils::macros::lock; @@ -114,10 +115,29 @@ impl crate::disk::state::Xvb { ui.style_mut().spacing.icon_width_inner = width / 45.0; ui.style_mut().spacing.icon_width = width / 35.0; ui.style_mut().spacing.icon_spacing = space_h; - if ui.checkbox(&mut self.hero, "Hero Mode").on_hover_text(XVB_HERO_SELECT).clicked() { - // also change hero mode of runtime. - lock!(api).stats_priv.runtime_hero_mode = self.hero; - } + + egui::ComboBox::from_label("") + .selected_text(format!("{:?}", self.mode)) + .show_ui(ui, |ui| { + ui.horizontal(|ui| { + ui.selectable_value(&mut self.mode, XvbMode::Auto, "Automatic"); + ui.selectable_value(&mut self.mode, XvbMode::Hero, "Hero Mode"); + ui.selectable_value(&mut self.mode, XvbMode::ManuallyDonante, "Manually Donate"); + ui.selectable_value(&mut self.mode, XvbMode::ManuallyKeep, "Manually Keep"); + }) + }); + if self.mode == XvbMode::ManuallyDonante || self.mode == XvbMode::ManuallyKeep { + ui.horizontal(|ui| { + ui.add( + TextEdit::singleline(&mut self.amount) + ) + }); +} + + + + + // need to warn the user if no address is set in p2pool tab if !Regexes::addr_ok(address) { diff --git a/src/disk/state.rs b/src/disk/state.rs index a1aeffe..1708bba 100644 --- a/src/disk/state.rs +++ b/src/disk/state.rs @@ -243,10 +243,19 @@ pub struct Xmrig { pub token: String, } +#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)] +pub enum XvbMode { + Auto, + ManuallyDonante, + ManuallyKeep, + Hero, +} + #[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize, Default)] pub struct Xvb { pub token: String, - pub hero: bool, + pub mode: XvbMode, + pub amount: String } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -375,3 +384,9 @@ impl Default for Version { } } } + +impl Default for XvbMode { + fn default() -> Self { + Self::Auto + } +} \ No newline at end of file diff --git a/src/helper/xvb/algorithm.rs b/src/helper/xvb/algorithm.rs index ab2b086..f67addf 100644 --- a/src/helper/xvb/algorithm.rs +++ b/src/helper/xvb/algorithm.rs @@ -1,6 +1,5 @@ use std::{ - sync::{Arc, Mutex}, - time::Duration, + mem::ManuallyDrop, sync::{Arc, Mutex}, time::Duration }; use log::{debug, info, warn}; @@ -13,12 +12,9 @@ use crate::{ p2pool::PubP2poolApi, xmrig::{PrivXmrigApi, PubXmrigApi}, xvb::{nodes::XvbNode, output_console, output_console_without_time}, - }, - macros::lock, - BLOCK_PPLNS_WINDOW_MAIN, BLOCK_PPLNS_WINDOW_MINI, SECOND_PER_BLOCK_P2POOL, XMRIG_CONFIG_URI, - XVB_BUFFER, XVB_ROUND_DONOR_MEGA_MIN_HR, XVB_ROUND_DONOR_MIN_HR, XVB_ROUND_DONOR_VIP_MIN_HR, - XVB_ROUND_DONOR_WHALE_MIN_HR, XVB_TIME_ALGO, + }, macros::lock, BLOCK_PPLNS_WINDOW_MAIN, BLOCK_PPLNS_WINDOW_MINI, SECOND_PER_BLOCK_P2POOL, XMRIG_CONFIG_URI, XVB_BUFFER, XVB_MINING_ON_FIELD, XVB_ROUND_DONOR_MEGA_MIN_HR, XVB_ROUND_DONOR_MIN_HR, XVB_ROUND_DONOR_VIP_MIN_HR, XVB_ROUND_DONOR_WHALE_MIN_HR, XVB_TIME_ALGO }; +use crate::helper::xvb::priv_stats::RuntimeMode; use super::{PubXvbApi, SamplesAverageHour}; @@ -68,15 +64,25 @@ pub(crate) fn calcul_donated_time( if spared_time > 0 { // if not hero option - if !lock!(gui_api_xvb).stats_priv.runtime_hero_mode { + if lock!(gui_api_xvb).stats_priv.runtime_mode == RuntimeMode::Auto { let xvb_chr = lock!(gui_api_xvb).stats_priv.donor_1hr_avg * 1000.0; info!("current HR on XvB (last hour): {xvb_chr}"); let shr = calc_last_hour_avg_hash_rate(&lock!(gui_api_xvb).xvb_sent_last_hour_samples); // calculate how much time needed to be spared to be in most round type minimum HR + buffer spared_time = minimum_time_for_highest_accessible_round(spared_time, lhr, xvb_chr, shr); } + + let manual_amount = lock!(gui_api_xvb).stats_priv.runtime_manual_amount as u32; + if lock!(gui_api_xvb).stats_priv.runtime_mode == RuntimeMode::ManuallyDonante { + spared_time = XVB_TIME_ALGO * manual_amount / (avg_hr as u32); + } + + if lock!(gui_api_xvb).stats_priv.runtime_mode == RuntimeMode::ManuallyKeep { + spared_time = XVB_TIME_ALGO - (XVB_TIME_ALGO * manual_amount / (avg_hr as u32)); + } + } - if lock!(gui_api_xvb).stats_priv.runtime_hero_mode { + if lock!(gui_api_xvb).stats_priv.runtime_mode == RuntimeMode::Hero { output_console(gui_api_xvb, "Hero mode is enabled for this decision"); } spared_time diff --git a/src/helper/xvb/mod.rs b/src/helper/xvb/mod.rs index 39e92a6..81efeb2 100644 --- a/src/helper/xvb/mod.rs +++ b/src/helper/xvb/mod.rs @@ -400,11 +400,11 @@ impl PubXvbApi { if !buf.is_empty() { output.push_str(&buf); } - let runtime_hero_mode = std::mem::take(&mut gui_api.stats_priv.runtime_hero_mode); + let runtime_mode = std::mem::take(&mut gui_api.stats_priv.runtime_mode); *gui_api = Self { output, stats_priv: XvbPrivStats { - runtime_hero_mode, + runtime_mode, ..pub_api.stats_priv.clone() }, p2pool_sent_last_hour_samples: std::mem::take( @@ -694,14 +694,14 @@ fn signal_interrupt( } fn reset_data_xvb(pub_api: &Arc>, gui_api: &Arc>) { let current_node = mem::take(&mut lock!(pub_api).current_node.clone()); - let runtime_hero_mode = mem::take(&mut lock!(gui_api).stats_priv.runtime_hero_mode); + let runtime_mode = mem::take(&mut lock!(gui_api).stats_priv.runtime_mode); // let output = mem::take(&mut lock!(gui_api).output); *lock!(pub_api) = PubXvbApi::new(); *lock!(gui_api) = PubXvbApi::new(); // to keep the value modified by xmrig even if xvb is dead. lock!(pub_api).current_node = current_node; // to not loose the information of runtime hero mode between restart - lock!(gui_api).stats_priv.runtime_hero_mode = runtime_hero_mode; + lock!(gui_api).stats_priv.runtime_mode = runtime_mode; // message while starting must be preserved. // lock!(pub_api).output = output; } diff --git a/src/helper/xvb/priv_stats.rs b/src/helper/xvb/priv_stats.rs index 82ac577..59441db 100644 --- a/src/helper/xvb/priv_stats.rs +++ b/src/helper/xvb/priv_stats.rs @@ -14,9 +14,19 @@ use crate::{ macros::lock, XVB_URL, }; +use crate::disk::state::XvbMode; use super::{nodes::XvbNode, rounds::XvbRound, PubXvbApi}; + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +pub enum RuntimeMode { + Auto, + ManuallyDonante, + ManuallyKeep, + Hero, +} + #[derive(Debug, Clone, Default, Deserialize)] pub struct XvbPrivStats { pub fails: u8, @@ -36,7 +46,8 @@ pub struct XvbPrivStats { pub msg_indicator: String, #[serde(skip)] // so the hero mode can change between two decision of algorithm without restarting XvB. - pub runtime_hero_mode: bool, + pub runtime_mode: RuntimeMode, + pub runtime_manual_amount: u64 } impl XvbPrivStats { @@ -106,3 +117,20 @@ impl XvbPrivStats { } } } + +impl From for RuntimeMode { + fn from(mode: XvbMode) -> Self { + match mode { + XvbMode::Auto => Self::Auto, + XvbMode::ManuallyDonante => Self::ManuallyDonante, + XvbMode::ManuallyKeep => Self::ManuallyKeep, + XvbMode::Hero => Self::Hero, + } + } +} + +impl Default for RuntimeMode { + fn default() -> Self { + Self::Auto + } +} \ No newline at end of file