Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the actual mining process to the recipes (basic-pow, hybrid-consensus) #433

Merged
merged 11 commits into from
May 11, 2021
6 changes: 3 additions & 3 deletions consensus/sha3pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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 {
Expand Down
31 changes: 30 additions & 1 deletion nodes/basic-pow/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -193,6 +195,33 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
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));
}
});
jimmychu0807 marked this conversation as resolved.
Show resolved Hide resolved
}

network_starter.start_network();
Expand Down
31 changes: 30 additions & 1 deletion nodes/hybrid-consensus/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{H256, Encode};
jimmychu0807 marked this conversation as resolved.
Show resolved Hide resolved

// Our native executor instance.
native_executor_instance!(
Expand Down Expand Up @@ -206,6 +208,33 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
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));
}
});
jimmychu0807 marked this conversation as resolved.
Show resolved Hide resolved
}

let grandpa_config = sc_finality_grandpa::Config {
Expand Down