use std::time::Instant; use pow_sha256::ConfigBuilder; use rayon::prelude::*; use serde::{Deserialize, Serialize}; use crate::utils::get_random; #[derive(clap::Args, Debug, Clone)] #[command(author, version, about, long_about = None)] pub struct GenerateDeriveArgs { #[arg(long)] pub start: u32, #[arg(long)] pub max: u32, #[arg(long)] pub db: std::path::PathBuf, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Log { pub string: String, pub salt: String, pub time: u128, } impl GenerateDeriveArgs { pub fn run(&self) { let db = sled::open(&self.db).unwrap(); let salt = get_random(32); let string = get_random(32); let pow_config = ConfigBuilder::default().salt(salt.clone()).build().unwrap(); (self.start..self.max) .into_par_iter() .for_each(|difficulty| { let start = Instant::now(); pow_config.prove_work(&string, difficulty).unwrap(); let finish = Instant::now(); let time_elapsed = finish.duration_since(start); let time = time_elapsed.as_micros(); if difficulty % 10000 == 0 { log::info!("Difficulty factor {difficulty} generated in {time}"); } let log = Log { salt: salt.clone(), time, string: string.clone(), }; db.insert( bincode::serialize(&difficulty).unwrap(), bincode::serialize(&log).unwrap(), ) .unwrap(); }); } }