From 4825add8287e489fec995695dd7d3744f65ee3a0 Mon Sep 17 00:00:00 2001 From: MikhailK Date: Sat, 30 Jul 2022 13:29:51 +0300 Subject: [PATCH] Mining threads --- nodes/poscan-consensus/src/command.rs | 1 + nodes/poscan-consensus/src/service.rs | 116 ++++++++++++++------------ 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/nodes/poscan-consensus/src/command.rs b/nodes/poscan-consensus/src/command.rs index bed8329b..4317d596 100644 --- a/nodes/poscan-consensus/src/command.rs +++ b/nodes/poscan-consensus/src/command.rs @@ -235,6 +235,7 @@ pub fn run() -> sc_cli::Result<()> { _ => service::new_full( config, cli.author.as_ref().map(|s| s.as_str()), + cli.threads.unwrap_or(1), ), } .map_err(sc_cli::Error::Service) diff --git a/nodes/poscan-consensus/src/service.rs b/nodes/poscan-consensus/src/service.rs index 77f80561..e1c56d04 100644 --- a/nodes/poscan-consensus/src/service.rs +++ b/nodes/poscan-consensus/src/service.rs @@ -592,6 +592,7 @@ pub fn new_partial( pub fn new_full( mut config: Configuration, author: Option<&str>, + threads: usize, ) -> Result { let sc_service::PartialComponents { client, @@ -709,7 +710,7 @@ pub fn new_full( let can_author_with = sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()); - let (_worker, worker_task) = sc_consensus_poscan::start_mining_worker( + let (worker, worker_task) = sc_consensus_poscan::start_mining_worker( Box::new(pow_block_import), client.clone(), select_chain, @@ -730,10 +731,6 @@ pub fn new_full( .spawn_essential_handle() .spawn_blocking("poscan", None, worker_task); - // Start Mining - let mut poscan_data: Option = None; - let mut poscan_hash: H256 = H256::random(); - let pre_digest = author.encode(); let author = sc_consensus_poscan::app::Public::decode(&mut &pre_digest[..]).map_err(|_| { ServiceError::Other( @@ -757,59 +754,70 @@ pub fn new_full( "Unable to mine: key not found in keystore".to_string(), ))?; - debug!(target:"poscan", ">>> Spawn mining loop"); - - thread::spawn(move || loop { - let worker = _worker.clone(); - let metadata = worker.metadata(); - if let Some(metadata) = metadata { - - // info!(">>> poscan_hash: compute: {:x?}", &poscan_hash); - let compute = Compute { - difficulty: metadata.difficulty, - pre_hash: metadata.pre_hash, - poscan_hash, - }; - - let signature = compute.sign(&pair); - let seal = compute.seal(signature.clone()); - if hash_meets_difficulty(&seal.work, seal.difficulty) { - // let mut worker = worker.lock(); - - if let Some(ref psdata) = poscan_data { - // let _ = psdata.encode(); - info!(">>> hash_meets_difficulty: submit it: {}, {}, {}", &seal.work, &seal.poscan_hash, &seal.difficulty); - // TODO: pass signature to submit - info!(">>> signature: {:x?}", &signature.to_vec()); - info!(">>> pre_hsash: {:x?}", compute.pre_hash); - info!(">>> check verify: {}", compute.verify(&signature.clone(), &author)); - let _ = futures::executor::block_on(worker.submit(seal.encode(), &psdata)); - // worker.submit(seal.encode(), &psdata); - } - } else { - let mut lock = DEQUE.lock(); - let maybe_mining_prop = (*lock).pop_front(); - if let Some(mp) = maybe_mining_prop { - let hashes = get_obj_hashes(&mp.pre_obj); - if hashes.len() > 0 { - let obj_hash = hashes[0]; - let dh = DoubleHash { pre_hash: metadata.pre_hash, obj_hash }; - poscan_hash = dh.calc_hash(); - poscan_data = Some(PoscanData { hashes, obj: mp.pre_obj }); + info!(">>> Spawn mining loop"); + + // Start Mining + let poscan_data: Option = None; + let poscan_hash: H256 = H256::random(); + + info!(">>> Spawn mining loop(s)"); + + for _i in 0..threads { + let worker = worker.clone(); + let author = author.clone(); + let mut poscan_data = poscan_data.clone(); + let mut poscan_hash = poscan_hash.clone(); + let pair = pair.clone(); + + thread::spawn(move || loop { + let metadata = worker.metadata(); + if let Some(metadata) = metadata { + + // info!(">>> poscan_hash: compute: {:x?}", &poscan_hash); + let compute = Compute { + difficulty: metadata.difficulty, + pre_hash: metadata.pre_hash, + poscan_hash, + }; + + let signature = compute.sign(&pair); + let seal = compute.seal(signature.clone()); + if hash_meets_difficulty(&seal.work, seal.difficulty) { + // let mut worker = worker.lock(); + + if let Some(ref psdata) = poscan_data { + // let _ = psdata.encode(); + info!(">>> hash_meets_difficulty: submit it: {}, {}, {}", &seal.work, &seal.poscan_hash, &seal.difficulty); + // TODO: pass signature to submit + info!(">>> signature: {:x?}", &signature.to_vec()); + info!(">>> pre_hsash: {:x?}", compute.pre_hash); + info!(">>> check verify: {}", compute.verify(&signature.clone(), &author)); + let _ = futures::executor::block_on(worker.submit(seal.encode(), &psdata)); + // worker.submit(seal.encode(), &psdata); } - else { - warn!(">>> Empty hash set for obj {}", mp.id); + } else { + let mut lock = DEQUE.lock(); + let maybe_mining_prop = (*lock).pop_front(); + if let Some(mp) = maybe_mining_prop { + let hashes = get_obj_hashes(&mp.pre_obj); + if hashes.len() > 0 { + let obj_hash = hashes[0]; + let dh = DoubleHash { pre_hash: metadata.pre_hash, obj_hash }; + poscan_hash = dh.calc_hash(); + poscan_data = Some(PoscanData { hashes, obj: mp.pre_obj }); + } else { + warn!(">>> Empty hash set for obj {}", mp.id); + } + // thread::sleep(Duration::new(1, 0)); + } else { + thread::sleep(Duration::new(1, 0)); } - // thread::sleep(Duration::new(1, 0)); - } - else { - thread::sleep(Duration::new(1, 0)); } + } else { + thread::sleep(Duration::new(1, 0)); } - } else { - thread::sleep(Duration::new(1, 0)); - } - }); + }); + } } let grandpa_config = sc_finality_grandpa::Config {