feat: compute pow for random strings and salts for a number of trials and avg them out

This commit is contained in:
Aravinth Manivannan 2023-08-04 12:54:44 +05:30
parent 262bb6bfbf
commit 752b2f6d9f
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
10 changed files with 217 additions and 104 deletions

View file

@ -0,0 +1,18 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO logs_avg\n (\n minimum_time,\n maximum_time,\n variation_time,\n difficulty,\n mean_time\n ) VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT(difficulty) DO NOTHING;",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int4",
"Int4",
"Int4",
"Int4",
"Int4"
]
},
"nullable": []
},
"hash": "5c9d24d6f9b03814be9b3f43e09acfdf42189ff91079502bbf4ab07965ce3d92"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO logs (string, salt, time, difficulty) VALUES ($1, $2, $3, $4) ON CONFLICT(difficulty) DO NOTHING;",
"query": "INSERT INTO logs_avg_individual\n (string, salt, time, difficulty) VALUES ($1, $2, $3, $4);",
"describe": {
"columns": [],
"parameters": {
@ -13,5 +13,5 @@
},
"nullable": []
},
"hash": "5d59a2fc2e04273e22b9949031084b6fdff395c09bd63011bc8d28b09cd55e32"
"hash": "996c3e1f6c9c6e742b2341668dd27790b3feb71e399372954c081ae44a38c4b7"
}

View file

@ -0,0 +1,17 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO logs\n (string, salt, time, difficulty)\n VALUES\n ($1, $2, $3, $4)\n ON CONFLICT(difficulty) DO NOTHING;",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Varchar",
"Varchar",
"Int4",
"Int4"
]
},
"nullable": []
},
"hash": "ecde2936f82a710898039c8d5ce3d1d43fef53ef1ca55302dbc68f0247d28b47"
}

148
Cargo.lock generated
View file

@ -2,27 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "actix-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [
"quote",
"syn 2.0.24",
]
[[package]]
name = "actix-rt"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15265b6b8e2347670eb363c47fc8c75208b4a4994b27192f345fcbe707804f3e"
dependencies = [
"actix-macros",
"futures-core",
"tokio",
]
[[package]]
name = "addr2line"
version = "0.20.0"
@ -203,9 +182,12 @@ checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
[[package]]
name = "cc"
version = "1.0.79"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
@ -215,9 +197,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.3.11"
version = "4.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1640e5cc7fb47dbb8338fd471b105e7ed6c3cb2aeb00c2e067127ffd3764a05d"
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
dependencies = [
"clap_builder",
"clap_derive",
@ -226,9 +208,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.3.11"
version = "4.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98c59138d527eeaf9b53f35a77fcc1fad9d883116070c63d5de1c7dc7b00c72b"
checksum = "01c6a3f08f1fe5662a35cfe393aec09c4df95f60ee93b7556505260f75eee9e1"
dependencies = [
"anstream",
"anstyle",
@ -238,14 +220,14 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "4.3.2"
version = "4.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f"
checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.24",
"syn 2.0.28",
]
[[package]]
@ -459,9 +441,9 @@ checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "either"
version = "1.8.1"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
dependencies = [
"serde",
]
@ -487,9 +469,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
dependencies = [
"errno-dragonfly",
"libc",
@ -804,9 +786,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "1.0.8"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "lazy_static"
@ -842,9 +824,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.4.3"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0"
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
[[package]]
name = "lock_api"
@ -924,9 +906,9 @@ dependencies = [
[[package]]
name = "num"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
@ -1010,9 +992,9 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.15"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
dependencies = [
"autocfg",
"libm",
@ -1129,7 +1111,7 @@ checksum = "ec2e072ecce94ec471b13398d5402c188e76ac03cf74dd1a975161b23a3f6d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.24",
"syn 2.0.28",
]
[[package]]
@ -1187,7 +1169,6 @@ dependencies = [
name = "powd"
version = "0.1.0"
dependencies = [
"actix-rt",
"bincode",
"clap",
"log",
@ -1199,6 +1180,7 @@ dependencies = [
"serde_json",
"sled",
"sqlx",
"tokio",
"url",
]
@ -1220,18 +1202,18 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.64"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.29"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
dependencies = [
"proc-macro2",
]
@ -1320,9 +1302,9 @@ dependencies = [
[[package]]
name = "regex-automata"
version = "0.3.2"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83d3daa6976cffb758ec878f108ba0e062a45b2d6ca3a2cca965338855476caf"
checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294"
dependencies = [
"aho-corasick",
"memchr",
@ -1331,9 +1313,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab07dc67230e4a4718e70fd5c20055a4334b121f1f9db8fe63ef39ce9b8c846"
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
[[package]]
name = "rsa"
@ -1365,9 +1347,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.3"
version = "0.38.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac5ffa1efe7548069688cd7028f32591853cd7b5b756d41bcffd2353e4fc75b4"
checksum = "1ee020b1716f0a80e2ace9b03441a749e402e86712f15f16fe8a8f75afac732f"
dependencies = [
"bitflags 2.3.3",
"errno",
@ -1378,41 +1360,41 @@ dependencies = [
[[package]]
name = "ryu"
version = "1.0.14"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "scopeguard"
version = "1.1.0"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.169"
version = "1.0.181"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd51c3db8f9500d531e6c12dd0fd4ad13d133e9117f5aebac3cdbb8b6d9824b0"
checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.169"
version = "1.0.181"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27738cfea0d944ab72c3ed01f3d5f23ec4322af8a1431e40ce630e4c01ea74fd"
checksum = "be02f6cb0cd3a5ec20bbcfbcbd749f57daddb1a0882dc2e46a6c236c90b977ed"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.24",
"syn 2.0.28",
]
[[package]]
name = "serde_json"
version = "1.0.100"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f1e14e89be7aa4c4b78bdbdc9eb5bf8517829a600ae8eaa39a6e1d960b5185c"
checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c"
dependencies = [
"itoa",
"ryu",
@ -1441,15 +1423,6 @@ dependencies = [
"digest",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "signature"
version = "2.1.0"
@ -1767,9 +1740,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.24"
version = "2.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36ccaf716a23c35ff908f91c971a86a9a71af5998c1d8f10e828d9f55f68ac00"
checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567"
dependencies = [
"proc-macro2",
"quote",
@ -1815,7 +1788,7 @@ checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.24",
"syn 2.0.28",
]
[[package]]
@ -1844,13 +1817,24 @@ dependencies = [
"bytes",
"libc",
"mio",
"parking_lot 0.12.1",
"num_cpus",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
]
[[package]]
name = "tokio-macros"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.28",
]
[[package]]
name = "tokio-stream"
version = "0.1.14"
@ -1883,7 +1867,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.24",
"syn 2.0.28",
]
[[package]]
@ -1909,9 +1893,9 @@ checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
[[package]]
name = "unicode-ident"
version = "1.0.10"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "unicode-normalization"

View file

@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-rt = "2.8.0"
bincode = "1.3.3"
clap = { version = "4.3.11", features = ["derive"] }
log = "0.4.19"
@ -18,4 +17,5 @@ serde = { version = "1.0.169", features = ["derive"] }
serde_json = "1.0.100"
sled = "0.34.7"
sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres" ] }
tokio = { version = "1.29.1", features = ["rt-multi-thread", "macros", "rt"] }
url = { version = "2.4.0", features = ["serde"] }

View file

@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS logs_avg_individual (
string VARCHAR(100) NOT NULL,
salt VARCHAR(100) NOT NULL,
time INTEGER NOT NULL,
difficulty INTEGER NOT NULL,
ID SERIAL PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS logs_avg (
difficulty INTEGER NOT NULL UNIQUE,
mean_time INTEGER NOT NULL,
variation_time INTEGER NOT NULL,
minimum_time INTEGER NOT NULL,
maximum_time INTEGER NOT NULL,
ID SERIAL PRIMARY KEY NOT NULL
);

View file

@ -1,9 +1,9 @@
use std::time::Instant;
use rayon::prelude::*;
use pow_sha256::ConfigBuilder;
use rayon::prelude::*;
use crate::utils::get_random;
use crate::{gen::Log, utils::get_random};
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
@ -12,18 +12,24 @@ pub struct AvgGenerate {
pub start: u32,
#[arg(long)]
pub max: u32,
#[arg(long)]
pub step: usize,
#[arg(long)]
pub trials: u32,
#[arg(long)]
pub db: url::Url,
#[arg(long)]
/// database connections
pub conn: u32,
}
impl AvgGenerate {
pub fn run(&self) {
(self.start..self.max)
.into_par_iter()
.for_each(|difficulty| {
let mut summary = String::new();
pub async fn run(&self) {
let tokio_rt = tokio::runtime::Handle::current();
let db = crate::db::DB::new(&self.db, self.conn).await;
(self.start..self.max).into_par_iter().step_by(self.step).for_each(|difficulty| {
let mut summary = String::new();
let mut times: Vec<u128> = Vec::with_capacity(self.trials as usize);
for _ in 0..self.trials {
@ -31,14 +37,22 @@ impl AvgGenerate {
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_elapsed = finish.duration_since(start);
let time = time_elapsed.as_micros();
log::info!("Difficulty factor {difficulty} generated in {time}");
times.push(time);
let log = Log { string, salt, time };
let db2 = db.clone();
let fut = async move {
db2.write_avg_individual_log(&log, difficulty).await;
};
tokio_rt.spawn(fut);
}
let mean = (times.iter().sum::<u128>() / self.trials as u128) as u32;
let simple_variance: u32 = (times
@ -53,7 +67,29 @@ impl AvgGenerate {
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}");
println!("{summary}");
let avg = Avg {
difficulty,
minimum_time: *min as u32,
maximum_time: *max as u32,
mean_time: mean,
variation_time: simple_variance as u32,
};
let db2 = db.clone();
let fut = async move {
db2.write_avg(&avg).await;
};
tokio_rt.spawn(fut);
});
}
}
pub struct Avg {
pub difficulty: u32,
pub minimum_time: u32,
pub maximum_time: u32,
pub variation_time: u32,
pub mean_time: u32,
}

View file

@ -3,6 +3,7 @@ use std::time::Duration;
use sqlx::postgres::{PgPool, PgPoolOptions};
use url::Url;
#[derive(Clone)]
pub struct DB {
pool: PgPool,
}
@ -17,18 +18,62 @@ impl DB {
.await
.unwrap();
sqlx::migrate!("./migrations/").run(&pool).await.unwrap();
let _ = sqlx::migrate!("./migrations/").run(&pool).await;
Self { pool }
}
pub async fn write_log(&self, log: &crate::Log, difficulty: u32) {
let time = log.time as i32;
sqlx::query!(
"INSERT INTO logs (string, salt, time, difficulty) VALUES ($1, $2, $3, $4) ON CONFLICT(difficulty) DO NOTHING;",
log.string,
log.salt,
time,
difficulty as i32,
).execute(&self.pool).await.unwrap();
"INSERT INTO logs
(string, salt, time, difficulty)
VALUES
($1, $2, $3, $4)
ON CONFLICT(difficulty) DO NOTHING;",
log.string,
log.salt,
time,
difficulty as i32,
)
.execute(&self.pool)
.await
.unwrap();
}
pub async fn write_avg_individual_log(&self, log: &crate::Log, difficulty: u32) {
let time = log.time as i32;
sqlx::query!(
"INSERT INTO logs_avg_individual
(string, salt, time, difficulty) VALUES ($1, $2, $3, $4);",
log.string,
log.salt,
time,
difficulty as i32,
)
.execute(&self.pool)
.await
.unwrap();
}
pub async fn write_avg(&self, avg: &crate::avg::Avg) {
sqlx::query!(
"INSERT INTO logs_avg
(
minimum_time,
maximum_time,
variation_time,
difficulty,
mean_time
) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT(difficulty) DO NOTHING;",
avg.minimum_time as i32,
avg.maximum_time as i32,
avg.variation_time as i32,
avg.difficulty as i32,
avg.mean_time as i32,
)
.execute(&self.pool)
.await
.unwrap();
}
}

View file

@ -54,5 +54,3 @@ impl ForDiff {
println!("{summary}");
}
}

View file

@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
use crate::utils::get_random;
#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct GenerateDeriveArgs {