From 0d719324a97cb5c59b5265058feefbbea94ad288 Mon Sep 17 00:00:00 2001 From: Revertron Date: Sun, 21 Mar 2021 14:34:32 +0100 Subject: [PATCH] Removed setting mining threads priority, as it seems that it has no effect. Changed monitoring for chain changes in miner to support MIPS architecture (needs testing). --- Cargo.toml | 10 +++------- src/commons/mod.rs | 16 ---------------- src/keys.rs | 5 ++--- src/miner.rs | 48 +++++++++++++++++++++------------------------- 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c0ec437..ff779fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alfis" -version = "0.3.3" +version = "0.3.4" authors = ["Revertron "] edition = "2018" build = "build.rs" @@ -35,20 +35,16 @@ web-view = { version = "0.7.3", features = [], optional = true } tinyfiledialogs = { version = "3.3.10", optional = true } [target.'cfg(windows)'.dependencies] -thread-priority = "0.2.1" winapi = { version = "0.3.7", features = ["impl-default", "wincon", "shellscalingapi"]} -[target.'cfg(target_os = "linux")'.dependencies] -thread-priority = "0.2.1" - [build-dependencies] minreq = { version = "2.3.1", features = ["punycode", "https-rustls"] } rust-crypto = "^0.2" winres = "0.1" [dev-dependencies] -serde_bytes = "0.11.2" -serde_derive = "1.0.27" +serde_bytes = "0.11.5" +serde_derive = "1.0.124" [profile.dev] opt-level = 2 diff --git a/src/commons/mod.rs b/src/commons/mod.rs index a196533..ccc4f8b 100644 --- a/src/commons/mod.rs +++ b/src/commons/mod.rs @@ -1,7 +1,5 @@ use std::num; use rand::Rng; -#[cfg(not(target_os = "macos"))] -use thread_priority::*; pub mod constants; pub use constants::*; @@ -22,20 +20,6 @@ pub fn from_hex(string: &str) -> Result, num::ParseIntError> { .collect() } -#[cfg(not(target_os = "macos"))] -#[allow(unused_variables)] -pub fn setup_miner_thread(cpu: u32) { - let _ = set_current_thread_priority(ThreadPriority::Min); - #[cfg(target_os = "windows")] - let _ = set_current_thread_ideal_processor(IdealProcessor::from(cpu)); -} - -#[cfg(target_os = "macos")] -#[allow(unused_variables)] -pub fn setup_miner_thread(cpu: u32) { - // MacOS is not supported by thread_priority crate -} - pub fn check_domain(name: &str, allow_dots: bool) -> bool { if name.starts_with('.') || name.starts_with('-') || name.ends_with('.') || name.ends_with('-') { return false; diff --git a/src/keys.rs b/src/keys.rs index 35d9a44..027ae62 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -17,7 +17,7 @@ use rand::{Rng, RngCore, thread_rng}; use serde::{Deserialize, Serialize}; use crate::blockchain::hash_utils::*; -use crate::{Context, setup_miner_thread}; +use crate::Context; use crate::event::Event; use crate::commons::KEYSTORE_DIFFICULTY; use crate::bytes::Bytes; @@ -117,12 +117,11 @@ pub fn create_key(context: Arc>) { let mining = Arc::new(AtomicBool::new(true)); let miners_count = Arc::new(AtomicUsize::new(0)); { context.lock().unwrap().bus.post(Event::KeyGeneratorStarted); } - for cpu in 0..num_cpus::get() { + for _cpu in 0..num_cpus::get() { let context = context.clone(); let mining = mining.clone(); let miners_count = miners_count.clone(); thread::spawn(move || { - setup_miner_thread(cpu as u32); miners_count.fetch_add(1, atomic::Ordering::SeqCst); match generate_key(KEYSTORE_DIFFICULTY, mining.clone()) { None => { diff --git a/src/miner.rs b/src/miner.rs index e28f3f7..34e9695 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -1,5 +1,5 @@ use std::sync::{Arc, Condvar, Mutex}; -use std::sync::atomic::{AtomicBool, AtomicU32, Ordering, AtomicU64}; +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::thread; use std::time::Duration; @@ -9,7 +9,7 @@ use crypto::digest::Digest; use log::{debug, error, info, trace, warn}; use num_cpus; -use crate::{Block, Bytes, Context, setup_miner_thread}; +use crate::{Block, Bytes, Context}; use crate::commons::{CHAIN_VERSION, LOCKER_DIFFICULTY, KEYSTORE_DIFFICULTY}; use crate::blockchain::enums::BlockQuality; use crate::blockchain::hash_utils::*; @@ -140,33 +140,17 @@ impl Miner { context.lock().unwrap().bus.post(Event::MinerStarted); let thread_spawn_interval = Duration::from_millis(10); let live_threads = Arc::new(AtomicU32::new(0u32)); - let top_block = Arc::new(AtomicU64::new(block.index - 1)); let cpus = num_cpus::get(); debug!("Starting {} threads for mining", cpus); - for cpu in 0..cpus { + for _cpu in 0..cpus { let context = Arc::clone(&context); let block = block.clone(); let mining = Arc::clone(&mining); - let top_block = Arc::clone(&top_block); let live_threads = Arc::clone(&live_threads); thread::spawn(move || { - // Register this thread to receive events from bus - let top = Arc::clone(&top_block); - context.lock().unwrap().bus.register(move |_uuid, e| { - match e { - Event::NewBlockReceived => {} - Event::BlockchainChanged { index } => { - top.store(index, Ordering::SeqCst); - } - _ => {} - } - true - }); - - setup_miner_thread(cpu as u32); live_threads.fetch_add(1, Ordering::SeqCst); let mut hasher = get_hasher_for_version(block.version); - match find_hash(Arc::clone(&context), &mut *hasher, block, Arc::clone(&mining), top_block) { + match find_hash(Arc::clone(&context), &mut *hasher, block, Arc::clone(&mining)) { None => { debug!("Mining was cancelled"); let count = live_threads.fetch_sub(1, Ordering::SeqCst); @@ -201,14 +185,23 @@ impl Miner { } } -fn find_hash(context: Arc>, digest: &mut dyn Digest, mut block: Block, running: Arc, top_block: Arc) -> Option { +fn find_hash(context: Arc>, digest: &mut dyn Digest, mut block: Block, running: Arc) -> Option { let mut buf: [u8; 32] = [0; 32]; let difficulty = block.difficulty as usize; let full = block.transaction.is_some(); loop { block.random = rand::random(); - block.index = context.lock().unwrap().chain.height() + 1; - if full && context.lock().unwrap().chain.next_allowed_block() > block.index { + let next_allowed_block = { + let context = context.lock().unwrap(); + // We use this block to fill some fields of our block as well + block.index = context.chain.height() + 1; + if let Some(b) = context.chain.last_block() { + block.prev_block_hash = b.hash; + } + context.chain.next_allowed_block() + }; + + if full && next_allowed_block > block.index { // We can't mine now, as we need to wait for block to be signed thread::sleep(Duration::from_millis(1000)); continue; @@ -229,9 +222,12 @@ fn find_hash(context: Arc>, digest: &mut dyn Digest, mut block: B return Some(block); } - if top_block.load(Ordering::SeqCst) >= block.index { - // If there is a new block in chain we restart hashing with new data - break; + if nonce % 1000 == 0 { + if let Ok(context) = context.lock() { + if context.chain.height() >= block.index { + break; + } + } } } }