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

rayon parallel PoW grinding for CpuBackend #826

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions crates/prover/src/core/backend/cpu/grind.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
#[cfg(feature = "parallel")]
use rayon::prelude::*;

use super::CpuBackend;
use crate::core::channel::Channel;
use crate::core::proof_of_work::GrindOps;

impl<C: Channel> GrindOps<C> for CpuBackend {
impl<C> GrindOps<C> for CpuBackend
where
C: Channel + std::marker::Sync,
{
fn grind(channel: &C, pow_bits: u32) -> u64 {
// TODO(spapini): This is a naive implementation. Optimize it.
let mut nonce = 0;
loop {

let check_nonce = |nonce: &u64| {
let mut channel = channel.clone();
channel.mix_u64(nonce);
if channel.trailing_zeros() >= pow_bits {
return nonce;
channel.mix_u64(*nonce);
channel.trailing_zeros() >= pow_bits
};

{
let range = 0..u64::MAX;
#[cfg(not(feature = "parallel"))]
{
range.into_iter().find(check_nonce)
}

#[cfg(feature = "parallel")]
{
range.into_par_iter().find_any(check_nonce)
}
nonce += 1;
}
.expect("Grind failed to find a solution.")
}
}