From 87ecae8b1945ae37f48ce216f35199d3fadac0fe Mon Sep 17 00:00:00 2001 From: Ash Manning Date: Tue, 27 Feb 2024 16:51:29 +0800 Subject: [PATCH] Fix gui miner, bump version for release --- Cargo.lock | 4 ++-- app/Cargo.toml | 2 +- app/app.rs | 8 ++++---- app/gui/miner.rs | 37 +++++++++++++++++++++++++++++++------ app/gui/mod.rs | 4 ++-- lib/Cargo.toml | 2 +- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b765de..9121a9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3278,7 +3278,7 @@ checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" [[package]] name = "plain_bitnames" -version = "0.5.1" +version = "0.5.2" dependencies = [ "addr", "async_zmq", @@ -3315,7 +3315,7 @@ dependencies = [ [[package]] name = "plain_bitnames_app" -version = "0.5.1" +version = "0.5.2" dependencies = [ "anyhow", "async_zmq", diff --git a/app/Cargo.toml b/app/Cargo.toml index f70df2d..0775881 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plain_bitnames_app" -version = "0.5.1" +version = "0.5.2" edition = "2021" authors = [ "Ash Manning " ] diff --git a/app/app.rs b/app/app.rs index 1a79adf..52d3b87 100644 --- a/app/app.rs +++ b/app/app.rs @@ -23,8 +23,8 @@ use crate::cli::Config; #[derive(Clone)] pub struct App { - pub node: Node, - pub wallet: Wallet, + pub node: Arc, + pub wallet: Arc, pub miner: Arc>, pub utxos: Arc>>, pub runtime: Arc, @@ -88,8 +88,8 @@ impl App { Arc::new(RwLock::new(utxos)) }; Ok(Self { - node, - wallet, + node: Arc::new(node), + wallet: Arc::new(wallet), miner: Arc::new(TokioRwLock::new(miner)), utxos, runtime: Arc::new(runtime), diff --git a/app/gui/miner.rs b/app/gui/miner.rs index f9117be..9633bf8 100644 --- a/app/gui/miner.rs +++ b/app/gui/miner.rs @@ -1,16 +1,27 @@ +use std::sync::{ + atomic::{self, AtomicBool}, + Arc, +}; + +use eframe::egui::{self, Button}; + use crate::app::App; -use eframe::egui; -pub struct Miner; +#[derive(Debug)] +pub struct Miner { + running: Arc, +} impl Default for Miner { fn default() -> Self { - Self + Self { + running: Arc::new(AtomicBool::new(false)), + } } } impl Miner { - pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) { + pub fn show(&mut self, app: &App, ui: &mut egui::Ui) { let block_height = app.node.get_height().unwrap_or(0); let best_hash = app.node.get_best_hash().unwrap_or([0; 32].into()); ui.label("Block height: "); @@ -18,8 +29,22 @@ impl Miner { ui.label("Best hash: "); let best_hash = &format!("{best_hash}")[0..8]; ui.monospace(format!("{best_hash}...")); - if ui.button("mine").clicked() { - let _result = app.mine(None); + + let running = self.running.load(atomic::Ordering::SeqCst); + if ui.add_enabled(!running, Button::new("Mine")).clicked() { + self.running.store(true, atomic::Ordering::SeqCst); + app.runtime.spawn({ + let app = app.clone(); + let running = self.running.clone(); + async move { + tracing::debug!("Mining..."); + let mining_result = app.mine(None).await; + running.store(false, atomic::Ordering::SeqCst); + if let Err(err) = mining_result { + tracing::error!("{:#}", anyhow::Error::new(err)) + } + } + }); } } } diff --git a/app/gui/mod.rs b/app/gui/mod.rs index c43a00c..07bc02c 100644 --- a/app/gui/mod.rs +++ b/app/gui/mod.rs @@ -69,7 +69,7 @@ impl EguiApp { Self { app, set_seed: SetSeed::default(), - miner: Miner, + miner: Miner::default(), deposit: Deposit::default(), bitname_explorer: BitnameExplorer::default(), tab: Tab::Coins, @@ -111,7 +111,7 @@ impl eframe::App for EguiApp { }); egui::TopBottomPanel::bottom("util").show(ctx, |ui| { ui.horizontal(|ui| { - self.miner.show(&mut self.app, ui); + self.miner.show(&self.app, ui); ui.separator(); self.deposit.show(&mut self.app, ui); }); diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 797521a..9c5058f 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plain_bitnames" -version = "0.5.1" +version = "0.5.2" edition = "2021" authors = [ "Ash Manning " ]