From dbb46bc18aab08ab03ec060928e85d2dd3e9c06d Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 20 Apr 2020 00:19:05 +0800 Subject: [PATCH] use Arc --- btc-address/src/main.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/btc-address/src/main.rs b/btc-address/src/main.rs index 0dc6c24..3a6f357 100644 --- a/btc-address/src/main.rs +++ b/btc-address/src/main.rs @@ -1,15 +1,24 @@ -use std::thread; +use std::{ + thread, + sync::{ Arc, Mutex, }, +}; use rand::rngs::OsRng; -use secp256k1::{Secp256k1, key::PublicKey, }; +use secp256k1::{ Secp256k1, key::PublicKey, }; use sha2::Sha256; use ripemd160::Ripemd160; use digest::{ Input, FixedOutput, }; +const PREFIX: &[&str] = &["1Hatter"]; + fn main() { + println!("Prefix: {:?}", PREFIX); + + let stop_flag = Arc::new(Mutex::new(false)); let mut handles = vec![]; - for ind in 0..8 { + for ind in 0..4 { + let the_stop_flag = Arc::clone(&stop_flag); let child = thread::spawn(move || { - run_one_task(ind); + run_one_task(ind, the_stop_flag); }); handles.push(child); } @@ -19,21 +28,29 @@ fn main() { println!("Finished!"); } -fn run_one_task(ind: usize) { +fn run_one_task(ind: usize, the_stop_flag: Arc>) { let secp = Secp256k1::new(); let mut rng = OsRng::new().expect("OsRng"); - for i in 0..1_000_000 { + for i in 0..10_000_000 { let (secret_key, public_key) = secp.generate_keypair(&mut rng); let s = make_btc_address(&public_key); - if i > 0 && i % 10000 == 0 { - println!("> {} - {}", ind, i); + if i % 10_000 == 0 { + if *the_stop_flag.lock().unwrap() { + return; + } + if i > 0 && i % 100_000 == 0 { + println!("> {} - {}", ind, i); + } } - if s.starts_with("1Hatt") { + if PREFIX.iter().any(|p| s.starts_with(p)) { println!("{}", s); println!("{}", secret_key); + + *the_stop_flag.lock().unwrap() = true; + break; } } }