Skip to content

Commit

Permalink
Adding the actual mining process to the recipes (basic-pow, hybrid-co…
Browse files Browse the repository at this point in the history
…nsensus) (#433)

Co-authored-by: Jimmy Chu <jimmychu0807@gmail.com>
  • Loading branch information
Mhnd3 and jimmychu0807 committed May 11, 2021
1 parent c85a43a commit a2a346c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
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));
}
});
}

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::{U256, Encode};

// 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));
}
});
}

let grandpa_config = sc_finality_grandpa::Config {
Expand Down

0 comments on commit a2a346c

Please sign in to comment.