From fc9070621334c090e60403b7e7b860fc66e76171 Mon Sep 17 00:00:00 2001 From: Revertron Date: Thu, 25 Mar 2021 20:55:09 +0100 Subject: [PATCH] Added success notifications from miner. --- src/event.rs | 4 +- src/keys.rs | 4 +- src/miner.rs | 11 ++++-- src/web_ui.rs | 32 +++++++++++++++- src/webview/index.html | 7 +++- src/webview/scripts.js | 83 ++++++++++++++++++++++++------------------ src/webview/styles.css | 10 +++++ 7 files changed, 106 insertions(+), 45 deletions(-) diff --git a/src/event.rs b/src/event.rs index 658ba48..b30ba33 100644 --- a/src/event.rs +++ b/src/event.rs @@ -3,9 +3,9 @@ use crate::{Bytes, Keystore}; #[derive(Clone, PartialEq, Debug)] pub enum Event { MinerStarted, - MinerStopped, + MinerStopped { success: bool, full: bool }, KeyGeneratorStarted, - KeyGeneratorStopped, + KeyGeneratorStopped { success: bool }, KeyCreated { path: String, public: String, hash: String }, KeyLoaded { path: String, public: String, hash: String }, KeySaved { path: String, public: String, hash: String }, diff --git a/src/keys.rs b/src/keys.rs index be2210f..f43e7c5 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -163,6 +163,7 @@ pub fn create_key(context: Arc>) { let miners_count = miners_count.clone(); thread::spawn(move || { miners_count.fetch_add(1, atomic::Ordering::SeqCst); + let mut success = false; match generate_key(KEYSTORE_DIFFICULTY, mining.clone()) { None => { debug!("Keystore mining finished"); @@ -174,11 +175,12 @@ pub fn create_key(context: Arc>) { info!("Key mined successfully: {:?}, hash: {}", &keystore.get_public(), &hash); context.bus.post(Event::KeyCreated { path: keystore.get_path().to_owned(), public: keystore.get_public().to_string(), hash }); context.set_keystore(Some(keystore)); + success = true; } } let miners = miners_count.fetch_sub(1, atomic::Ordering::SeqCst) - 1; if miners == 0 { - context.lock().unwrap().bus.post(Event::KeyGeneratorStopped); + context.lock().unwrap().bus.post(Event::KeyGeneratorStopped { success }); } }); } diff --git a/src/miner.rs b/src/miner.rs index 22a36b5..d4bda19 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -113,7 +113,7 @@ impl Miner { job.block.pub_key = job.keystore.get_public(); if !check_public_key_strength(&job.block.pub_key, KEYSTORE_DIFFICULTY) { warn!("Can not mine block with weak public key!"); - context.lock().unwrap().bus.post(Event::MinerStopped); + context.lock().unwrap().bus.post(Event::MinerStopped { success: false, full: false }); mining.store(false, Ordering::SeqCst); return; } @@ -124,7 +124,7 @@ impl Miner { // If we were doing something else and got new block before we could mine this block if last_block.index > job.block.index || last_block.hash != job.block.prev_block_hash { warn!("We missed block to lock"); - context.lock().unwrap().bus.post(Event::MinerStopped); + context.lock().unwrap().bus.post(Event::MinerStopped { success: false, full: false }); mining.store(false, Ordering::SeqCst); return; } @@ -150,6 +150,7 @@ impl Miner { let live_threads = Arc::clone(&live_threads); thread::spawn(move || { live_threads.fetch_add(1, Ordering::SeqCst); + let full = job.block.transaction.is_some(); match find_hash(Arc::clone(&context), job.block, Arc::clone(&mining)) { None => { debug!("Mining was cancelled"); @@ -157,13 +158,14 @@ impl Miner { // If this is the last thread, but mining was not stopped by another thread if count == 1 { let mut context = context.lock().unwrap(); - context.bus.post(Event::MinerStopped); + context.bus.post(Event::MinerStopped { success: false, full }); } }, Some(mut block) => { let index = block.index; let mut context = context.lock().unwrap(); block.signature = Bytes::from_bytes(&job.keystore.sign(&block.as_bytes())); + let mut success = false; if context.chain.check_new_block(&block) != BlockQuality::Good { warn!("Error adding mined block!"); if index == 0 { @@ -174,8 +176,9 @@ impl Miner { context.settings.origin = block.hash.to_string(); } context.chain.add_block(block); + success = true; } - context.bus.post(Event::MinerStopped); + context.bus.post(Event::MinerStopped { success, full }); mining.store(false, Ordering::SeqCst); }, } diff --git a/src/web_ui.rs b/src/web_ui.rs index 2f78cd6..b3a3fcc 100644 --- a/src/web_ui.rs +++ b/src/web_ui.rs @@ -190,13 +190,33 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { status.mining = true; String::from("setLeftStatusBarText('Mining...'); showMiningIndicator(true, false);") } - Event::MinerStopped | Event::KeyGeneratorStopped => { + Event::MinerStopped {success, full} => { status.mining = false; - if status.syncing { + let mut s = if status.syncing { String::from("setLeftStatusBarText('Syncing...'); showMiningIndicator(true, true);") } else { String::from("setLeftStatusBarText('Idle'); showMiningIndicator(false, false);") + }; + if full { + match success { + true => { s.push_str(" showSuccess('Block successfully mined!')"); } + false => { s.push_str(" showSuccess('Mining unsuccessful, sorry.')"); } + } } + s + } + Event::KeyGeneratorStopped {success} => { + status.mining = false; + let mut s = if status.syncing { + String::from("setLeftStatusBarText('Syncing...'); showMiningIndicator(true, true);") + } else { + String::from("setLeftStatusBarText('Idle'); showMiningIndicator(false, false);") + }; + match success { + true => { s.push_str(" showSuccess('Key pair successfully mined!
Don`t forget to save!')"); } + false => { s.push_str(" showSuccess('Key mining got nothing, sorry.')"); } + } + s } Event::Syncing { have, height } => { status.syncing = true; @@ -321,6 +341,14 @@ fn show_warning(web_view: &mut WebView<()>, text: &str) { } } +fn show_success(web_view: &mut WebView<()>, text: &str) { + let str = text.replace('\'', "\\'"); + match web_view.eval(&format!("showSuccess('{}');", &str)) { + Ok(_) => {} + Err(_) => { warn!("Error showing success!"); } + } +} + fn create_domain(context: Arc>, miner: Arc>, name: &str, data: &str, difficulty: u32, keystore: &Keystore) { let name = name.to_owned(); info!("Generating domain or zone {}", &name); diff --git a/src/webview/index.html b/src/webview/index.html index 11ffa44..8247840 100644 --- a/src/webview/index.html +++ b/src/webview/index.html @@ -191,10 +191,15 @@ + +