-
Notifications
You must be signed in to change notification settings - Fork 188
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
Changes from 5 commits
226e1bf
afd72e7
5454863
6741474
adb8cf4
d30ff01
f92b37c
cd70034
aaf238f
00e12e8
ac651f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,11 +5,14 @@ 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::{H256, Encode}; | ||||||||||||||||||||||||||||||||||||||
use sha3::{Digest, Sha3_256}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Our native executor instance. | ||||||||||||||||||||||||||||||||||||||
native_executor_instance!( | ||||||||||||||||||||||||||||||||||||||
|
@@ -193,6 +196,33 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> { | |||||||||||||||||||||||||||||||||||||
task_manager | ||||||||||||||||||||||||||||||||||||||
.spawn_essential_handle() | ||||||||||||||||||||||||||||||||||||||
.spawn_blocking("pow", worker_task); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Start Mining | ||||||||||||||||||||||||||||||||||||||
let mut numb: u8 = 0; | ||||||||||||||||||||||||||||||||||||||
thread::spawn(move || { | ||||||||||||||||||||||||||||||||||||||
loop { | ||||||||||||||||||||||||||||||||||||||
let worker = _worker.clone(); | ||||||||||||||||||||||||||||||||||||||
let metadata = worker.lock().metadata(); | ||||||||||||||||||||||||||||||||||||||
if let Some(metadata) = metadata { | ||||||||||||||||||||||||||||||||||||||
let nonce = H256::from_slice(Sha3_256::digest(&[numb]).as_slice()); | ||||||||||||||||||||||||||||||||||||||
let compute = Compute { | ||||||||||||||||||||||||||||||||||||||
difficulty: metadata.difficulty, | ||||||||||||||||||||||||||||||||||||||
pre_hash: metadata.pre_hash, | ||||||||||||||||||||||||||||||||||||||
nonce, | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
let seal = compute.compute(); | ||||||||||||||||||||||||||||||||||||||
if hash_meets_difficulty(&seal.work, seal.difficulty) { | ||||||||||||||||||||||||||||||||||||||
let mut worker = worker.lock(); | ||||||||||||||||||||||||||||||||||||||
worker.submit(seal.encode()); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
numb = numb.saturating_add(1u8); | ||||||||||||||||||||||||||||||||||||||
if numb == 255u8 { | ||||||||||||||||||||||||||||||||||||||
numb = 0; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
thread::sleep(Duration::new(0, 500_000_000)); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the logic here should be something like this?
Suggested change
The last line of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. putting the |
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
network_starter.start_network(); | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I am not familiar with writing consensus, so I am also learning here. A few questions here:
Why is
nonce
a hash, instead of simply an incrementing number? I see that numb pretty much act as an incrementing nonce. But then this number should probably be of typeU256
instead ofu8
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jimmychu0807 for the review!
The
nonce
as a Hash is mimicking the Kulupu implementation that uses the RandomX algorithm, as one of it's security goals that the Miners should not be able to identify "weaker" authorised parameters (such as specific nonce values) that make the PoW computation significantly more efficient, thus giving an advantage to a miner who would know such parameters, and in Kulupu implementation thenonce
is actully the Hash of a random valuelet nonce = H256::random_using(&mut rng);
In this implementation it doesn't really need to be a Hash at all, I think an
U256
would be a better fit, as we really do increment thenonce
by1
every unsuccessful attempt.the
u8
was used cuz i didn't change anything in the rest of the code, where thenonce
is a Hash, and theSha3_256::digest()
takes a&[u8]
as input, so i found it a faster way.Bottom line using the
U256
for the nonce would eliminate the need for thenumb
variable. It was just quick and dirty implementation to make it work as discussed in #432I updated the code to make it uses the type
U256
for the nonce.