grunch / rana

Nostr public key mining tool
MIT License
269 stars 39 forks source link

Feature Request: Pausing #18

Closed kfrancis closed 1 year ago

kfrancis commented 1 year ago

It would be nice to be able to "pause" execution if that's possible, something like using ctrlc:

use std::sync::atomic::{AtomicBool, Ordering};
use ctrlc;

let running = AtomicBool::new(true);
ctrlc::set_handler(move || {
    running.store(!running.load(Ordering::SeqCst), Ordering::SeqCst);
})?;

while running.load(Ordering::SeqCst) {
    // Your program logic here
    if running.load(Ordering::SeqCst) {
        println!("Running...");
    } else {
        println!("Paused...");
    }
    std::thread::sleep(std::time::Duration::from_secs(1));
}

or perhaps implementing a background thread that listens for keyboard input and use channel to communicate with main thread?

use std::sync::mpsc::channel;
use std::thread;
use std::io::{stdin, Read};

fn main() {
    let (tx, rx) = channel();

    thread::spawn(move || {
        let stdin = stdin();
        let mut stdin = stdin.lock();

        loop {
            let mut input = [0u8; 1];
            if stdin.read(&mut input).unwrap() == 0 {
                break;
            }

            if input[0] == b'p' {
                tx.send("pause").unwrap();
            } else if input[0] == b'r' {
                tx.send("resume").unwrap();
            } else if input[0] == b'q' {
                tx.send("quit").unwrap();
                break;
            }
        }
    });

    let mut running = true;
    while running {
        match rx.recv().unwrap() {
            "pause" => {
                println!("Paused...");
                running = false;
            },
            "resume" => {
                println!("Resumed...");
                running = true;
            },
            "quit" => {
                println!("Quitting...");
                running = false;
            },
            _ => {}
        }

        if running {
            println!("Running...");
            // some work
            std::thread::sleep(std::time::Duration::from_secs(1));
        }
    }
}
kfrancis commented 1 year ago

Use case: The largest core machine I have is my dev machine and I'd like to generate a rather nice key but I need to use the machine. Pausing would allow me to be able to continue execution at the end of my day, then pause the next, etc - until the app completes normally.

pgerstbach commented 1 year ago

No need to pause, you can just stop and start over. The tool only generates random keys with Secp256k1 and checks them for validity. You do not miss anything when starting over.

kfrancis commented 1 year ago

Eh, after putting in 12 hours of work trying to generate a vanity pub key - it would be nice not to lose that time simply because I need to use the machine.

pgerstbach commented 1 year ago

Once more: You don't lose any time. Random numbers are generated, there is no "list" that is worked off. It is like a new block on Bitcoin blockchain: on average it takes 10mins for a new block, but sometimes it only takes 1 second.

So you can just stop and start the script and don't lose time. If you are lucky your preferred vanity key will be created in 1s if your are unlucky you might wait forever. How long you have to wait is coincidence and does not depend on how long you have crunched the numbers. This might not be intuitive but it is still true! ;-)