diff --git a/consensus/sha3pow/src/lib.rs b/consensus/sha3pow/src/lib.rs index e67cf20e8..f66ab74e2 100644 --- a/consensus/sha3pow/src/lib.rs +++ b/consensus/sha3pow/src/lib.rs @@ -12,7 +12,7 @@ use std::sync::Arc; /// The test is done by multiplying the two together. If the product /// overflows the bounds of U256, then the product (and thus the hash) /// was too high. -fn hash_meets_difficulty(hash: &H256, difficulty: U256) -> bool { +pub fn hash_meets_difficulty(hash: &H256, difficulty: U256) -> bool { let num_hash = U256::from(&hash[..]); let (_, overflowed) = num_hash.overflowing_mul(difficulty); @@ -25,7 +25,7 @@ fn hash_meets_difficulty(hash: &H256, difficulty: U256) -> bool { pub struct Seal { pub difficulty: U256, pub work: H256, - pub nonce: H256, + pub nonce: U256, } /// A not-yet-computed attempt to solve the proof of work. Calling the @@ -34,7 +34,7 @@ pub struct Seal { pub struct Compute { pub difficulty: U256, pub pre_hash: H256, - pub nonce: H256, + pub nonce: U256, } impl Compute { diff --git a/nodes/basic-pow/src/service.rs b/nodes/basic-pow/src/service.rs index 08b6ae334..ff5b8a8a1 100644 --- a/nodes/basic-pow/src/service.rs +++ b/nodes/basic-pow/src/service.rs @@ -5,11 +5,13 @@ use sc_client_api::{ExecutorProvider, RemoteBackend}; use sc_executor::native_executor_instance; pub use sc_executor::NativeExecutor; use sc_service::{error::Error as ServiceError, Configuration, PartialComponents, TaskManager}; -use sha3pow::MinimalSha3Algorithm; +use sha3pow::*; use sp_api::TransactionFor; use sp_consensus::import_queue::BasicQueue; use sp_inherents::InherentDataProviders; use std::{sync::Arc, time::Duration}; +use std::thread; +use sp_core::{U256, Encode}; // Our native executor instance. native_executor_instance!( @@ -193,6 +195,33 @@ pub fn new_full(config: Configuration) -> Result { task_manager .spawn_essential_handle() .spawn_blocking("pow", worker_task); + + // Start Mining + let mut nonce: U256 = U256::from(0); + thread::spawn(move || loop { + let worker = _worker.clone(); + let metadata = worker.lock().metadata(); + if let Some(metadata) = metadata { + let compute = Compute { + difficulty: metadata.difficulty, + pre_hash: metadata.pre_hash, + nonce, + }; + let seal = compute.compute(); + if hash_meets_difficulty(&seal.work, seal.difficulty) { + nonce = U256::from(0); + let mut worker = worker.lock(); + worker.submit(seal.encode()); + } else { + nonce = nonce.saturating_add(U256::from(1)); + if nonce == U256::MAX { + nonce = U256::from(0); + } + } + } else { + thread::sleep(Duration::new(1, 0)); + } + }); } network_starter.start_network(); diff --git a/nodes/hybrid-consensus/src/service.rs b/nodes/hybrid-consensus/src/service.rs index abbb5d814..9e0f1199e 100644 --- a/nodes/hybrid-consensus/src/service.rs +++ b/nodes/hybrid-consensus/src/service.rs @@ -6,12 +6,14 @@ use sc_executor::native_executor_instance; pub use sc_executor::NativeExecutor; use sc_finality_grandpa::GrandpaBlockImport; use sc_service::{error::Error as ServiceError, Configuration, PartialComponents, TaskManager}; -use sha3pow::MinimalSha3Algorithm; +use sha3pow::*; use sp_api::TransactionFor; use sp_consensus::import_queue::BasicQueue; use sp_inherents::InherentDataProviders; use std::sync::Arc; use std::time::Duration; +use std::thread; +use sp_core::{U256, Encode}; // Our native executor instance. native_executor_instance!( @@ -206,6 +208,33 @@ pub fn new_full(config: Configuration) -> Result { task_manager .spawn_essential_handle() .spawn_blocking("pow", worker_task); + + // Start Mining + let mut nonce: U256 = U256::from(0); + thread::spawn(move || loop { + let worker = _worker.clone(); + let metadata = worker.lock().metadata(); + if let Some(metadata) = metadata { + let compute = Compute { + difficulty: metadata.difficulty, + pre_hash: metadata.pre_hash, + nonce, + }; + let seal = compute.compute(); + if hash_meets_difficulty(&seal.work, seal.difficulty) { + nonce = U256::from(0); + let mut worker = worker.lock(); + worker.submit(seal.encode()); + } else { + nonce = nonce.saturating_add(U256::from(1)); + if nonce == U256::MAX { + nonce = U256::from(0); + } + } + } else { + thread::sleep(Duration::new(1, 0)); + } + }); } let grandpa_config = sc_finality_grandpa::Config {