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

Conversation

Mhnd3
Copy link
Contributor

@Mhnd3 Mhnd3 commented May 4, 2021

No description provided.

Mhnd3 added 4 commits May 4, 2021 02:54
Add the actual mining process.
Add the actual mining process.
- public "hash_meets_difficulty" function.
- reduce difficulty due to the use of u8 type as nonce for pow mining.
jimmychu0807
jimmychu0807 previously approved these changes May 6, 2021
Copy link
Collaborator

@jimmychu0807 jimmychu0807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mhnd3 Thanks for the fix on making basic-pow and hybrid-consensus node start producing blocks again.

@JoshOrndorff @Mhnd3 there are some questions I am not sure about in the PoW code, raised in the code inline. Could you shed me some light here? Thanks.

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());
Copy link
Collaborator

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 type U256 instead of u8.

Copy link
Contributor Author

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 the nonce is actully the Hash of a random value
let 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 the nonce by 1 every unsuccessful attempt.
the u8 was used cuz i didn't change anything in the rest of the code, where the nonce is a Hash, and the Sha3_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 the numb variable. It was just quick and dirty implementation to make it work as discussed in #432
I updated the code to make it uses the type U256 for the nonce.

Comment on lines 214 to 222
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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the logic here should be something like this?

Suggested change
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));
if hash_meets_difficulty(&seal.work, seal.difficulty) {
let mut worker = worker.lock();
worker.submit(seal.encode());
} else {
numb = numb.saturating_add(1u8);
if numb == 255u8 {
numb = 0;
}
}

The last line of thread::sleep is in the else branch of if let Some(metadata) = metadata {..} else { thread::sleep }, based on code structure in https://github.com/kulupu/kulupu/blob/master/src/service.rs#L330-L377

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putting the thread::sleep in the else branch is for producing a delay in case no Block is yet ready to start mining, but i added it to create a delay in the mining process itself, since the difficulty was reduced due to the usage of the u8 numb variable to generate a nonce.
i updated this, the nonce uses the type U256 now.

@jimmychu0807 jimmychu0807 dismissed their stale review May 6, 2021 04:38

Some questions arise

Mhnd3 added 3 commits May 6, 2021 18:48
- since there is no need for a random nonce, the nonce type `H256` was replaced with `U256`.
- minor changes.
change the nonce type from `H256` to `U256`, as a Hash is not needed as nonce for this implementation.
@jimmychu0807
Copy link
Collaborator

@Mhnd3 thanks for addressing my concern and having the fixes. I just update the indenting spaces to tabs (we prefer tabs in rust code), and all look good to me.

@jimmychu0807 jimmychu0807 merged commit a2a346c into JoshOrndorff:master May 11, 2021
@JoshOrndorff
Copy link
Owner

Thank you for contributing to the recipes @Mhnd3 ! We really appreciate it! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants