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).
This commit is contained in:
Revertron
2021-03-21 14:34:32 +01:00
parent 0b5f5d0793
commit 0d719324a9
4 changed files with 27 additions and 52 deletions
+3 -7
View File
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.3.3"
version = "0.3.4"
authors = ["Revertron <alfis@revertron.com>"]
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
-16
View File
@@ -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<Vec<u8>, 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;
+2 -3
View File
@@ -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<Mutex<Context>>) {
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 => {
+22 -26
View File
@@ -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<Mutex<Context>>, digest: &mut dyn Digest, mut block: Block, running: Arc<AtomicBool>, top_block: Arc<AtomicU64>) -> Option<Block> {
fn find_hash(context: Arc<Mutex<Context>>, digest: &mut dyn Digest, mut block: Block, running: Arc<AtomicBool>) -> Option<Block> {
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<Mutex<Context>>, 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;
}
}
}
}
}