Skip to content

Commit

Permalink
Mining threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mishk committed Jul 30, 2022
1 parent e9ffa22 commit 4825add
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
1 change: 1 addition & 0 deletions nodes/poscan-consensus/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
116 changes: 62 additions & 54 deletions nodes/poscan-consensus/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ pub fn new_partial(
pub fn new_full(
mut config: Configuration,
author: Option<&str>,
threads: usize,
) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client,
Expand Down Expand Up @@ -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,
Expand All @@ -730,10 +731,6 @@ pub fn new_full(
.spawn_essential_handle()
.spawn_blocking("poscan", None, worker_task);

// Start Mining
let mut poscan_data: Option<PoscanData> = 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(
Expand All @@ -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<PoscanData> = 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 {
Expand Down

0 comments on commit 4825add

Please sign in to comment.