feat: copy from sled to postgres and compute mean, variance, min and max

with random string and random salt for difficulty factors
This commit is contained in:
Aravinth Manivannan 2023-08-03 14:53:48 +05:30
parent d7fd79196a
commit 2f4c6b867f
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
5 changed files with 84 additions and 8 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
#export DATABASE_URL=sqlite:///src/atm/code/mcaptcha/powd/db.sqlite
export DATABASE_URL="postgres://postgres:password@localhost:5420/postgres"
export db=powd-postgres

1
Cargo.lock generated
View File

@ -1621,6 +1621,7 @@ dependencies = [
"sha2",
"sqlx-core",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
"syn 1.0.109",
"tempfile",

View File

@ -17,5 +17,5 @@ rayon = "1.7.0"
serde = { version = "1.0.169", features = ["derive"] }
serde_json = "1.0.100"
sled = "0.34.7"
sqlx = { version = "0.7", features = [ "runtime-tokio", "sqlite" ] }
sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres" ] }
url = { version = "2.4.0", features = ["serde"] }

View File

@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS logs (
string VARCHAR(100) NOT NULL,
salt VARCHAR(100) NOT NULL,
time VARCHAR(100) NOT NULL,
time INTEGER NOT NULL,
difficulty INTEGER NOT NULL UNIQUE,
ID INTEGER PRIMARY KEY NOT NULL
ID SERIAL PRIMARY KEY NOT NULL
);

View File

@ -6,7 +6,8 @@ use clap::Parser;
use pow_sha256::ConfigBuilder;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::postgres::PgPoolOptions;
use std::time::Duration;
use std::time::Instant;
#[derive(Parser)] // requires `derive` feature
@ -16,6 +17,58 @@ enum PoWdCli {
Generate(GenerateDeriveArgs),
PrintDB(PrintRes),
Sqlite(Sqlite),
ForDiff(ForDiff),
}
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
struct ForDiff {
#[arg(long)]
factors: String,
}
impl ForDiff {
fn run(&self) {
let factors: Vec<u32> = self
.factors
.split(',')
.map(|d| d.parse::<u32>().unwrap())
.collect();
let mut summary = String::new();
for difficulty in factors.iter() {
let trials = 100;
let mut times: Vec<u128> = Vec::with_capacity(trials);
for _ in 0..trials {
let salt = get_random(32);
let string = get_random(32);
let pow_config = ConfigBuilder::default().salt(salt.clone()).build().unwrap();
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();
log::info!("Difficulty factor {difficulty} generated in {time}");
times.push(time);
}
let mean = (times.iter().sum::<u128>()/ trials as u128) as u32 ;
let simple_variance: u32 = (times
.iter()
.map(|d| {
let x = (*d as u32 - mean ) as u32;
x * x
})
.sum::<u32>())
/ (trials as u32 - 1);
let simple_variance: usize = ((simple_variance as f32).sqrt() as usize);
let min = times.iter().min().unwrap();
let max = times.iter().max().unwrap();
summary = format!("{summary}\ndifficulty: {difficulty} min: {min} max: {max} mean: {mean} variance {simple_variance}");
}
println!("{summary}");
}
}
#[derive(clap::Args, Debug, Clone)]
@ -87,6 +140,15 @@ struct Sqlite {
sqlite: url::Url,
}
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
struct Plot {
#[arg(long)]
graph: std::path::PathBuf,
#[arg(long)]
sqlite: url::Url,
}
#[actix_rt::main]
async fn main() {
std::env::set_var("RUST_LOG", "INFO");
@ -108,7 +170,13 @@ async fn main() {
}
PoWdCli::Sqlite(args) => {
let sqlite_db = SqlitePoolOptions::new().connect(&args.sqlite.to_string()).await.unwrap();
let sqlite_db = PgPoolOptions::new()
.max_connections(1)
.acquire_timeout(Duration::new(1000, 0))
.idle_timeout(Duration::new(1000, 0))
.connect(&args.sqlite.to_string())
.await
.unwrap();
println!("{:?}", args);
let sled_db = sled::open(args.sled).unwrap();
@ -117,16 +185,20 @@ async fn main() {
let log: Log = bincode::deserialize::<Log>(&entry[..]).unwrap();
let difficulty: u32 = bincode::deserialize::<u32>(&difficulty).unwrap();
let time = log.time.to_string();
let time = log.time as i32;
sqlx::query!(
"INSERT OR IGNORE INTO logs (string, salt, time, difficulty) VALUES ($1, $2, $3, $4);",
"INSERT INTO logs (string, salt, time, difficulty) VALUES ($1, $2, $3, $4) ON CONFLICT(difficulty) DO NOTHING;",
log.string,
log.salt,
time,
difficulty
difficulty as i32,
).execute(&sqlite_db).await.unwrap();
}
}
PoWdCli::ForDiff(args) => {
println!("{:?}", args);
args.run();
}
}
}