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

feat: notify dummy miner for new work #4126

Merged
merged 1 commit into from
Aug 31, 2023
Merged
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
29 changes: 24 additions & 5 deletions miner/src/worker/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indicatif::ProgressBar;
use rand::thread_rng;
use rand_distr::{self as dist, Distribution as _};
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};

pub struct Dummy {
delay: Delay,
Expand Down Expand Up @@ -94,10 +94,29 @@ impl Dummy {
}
}

fn solve(&self, pow_hash: Byte32, work: Work, nonce: u128) {
thread::sleep(self.delay.duration());
if let Err(err) = self.nonce_tx.send((pow_hash, work, nonce)) {
error!("nonce_tx send error {:?}", err);
fn solve(&mut self, mut pow_hash: Byte32, mut work: Work, nonce: u128) {
let instant = Instant::now();
let delay = self.delay.duration();
loop {
thread::sleep(Duration::from_millis(10));
if instant.elapsed() > delay {
if let Err(err) = self.nonce_tx.send((pow_hash, work, nonce)) {
error!("nonce_tx send error {:?}", err);
}
return;
}
// if there is new work and pow_hash changed, start working on the new one
if let Ok(WorkerMessage::NewWork {
pow_hash: new_pow_hash,
work: new_work,
..
}) = self.worker_rx.try_recv()
{
if new_pow_hash != pow_hash {
pow_hash = new_pow_hash;
work = new_work;
}
}
Comment on lines +97 to +119
Copy link
Collaborator

@eval-exec eval-exec Aug 30, 2023

Choose a reason for hiding this comment

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

How about this:

use crossbeam_channel::select;

...


    fn solve(&self, pow_hash: Byte32, work: Work, nonce: u128) {
        select! {
            recv(self.worker_rx) -> msg => {
                if let Ok(WorkerMessage::NewWork {
                        pow_hash: new_pow_hash,
                        work: new_work,
                        ..
                }) = msg {
                    if new_pow_hash != pow_hash {
                        pow_hash = new_pow_hash;
                        work = new_work;
                    }
                }
            },
            default(self.delay.duration()) => {
                if let Err(err) = self.nonce_tx.send((pow_hash, work, nonce)) {
                    error!("nonce_tx send error {:?}", err);
                }
                return;

            }
        }
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

The current develop branch use a simple thread sleep, and I wanted to keep the code as unchanged as possible, rather than introduce a tokio runtime.

Copy link
Collaborator

@eval-exec eval-exec Aug 30, 2023

Choose a reason for hiding this comment

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

I wanted to keep the code as unchanged as possible

OK

rather than introduce a tokio runtime.

But self.nonce_tx's type is crossbeam_channel::Sender. We won't need tokio runtime here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, I thought it's tokio's select macro, let me change it and test later, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

after test, I found the code generated by select macro is different, the pow_hash and work vars are not read again, I will keep current code.

}
}
}
Expand Down