From d53d35468e4db6321db639aa3c8ccafc119b223c Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 8 Mar 2021 18:33:35 +0530 Subject: [PATCH] pow_256 veresion bump --- Cargo.lock | 10 +++++----- Cargo.toml | 2 +- src/pow.rs | 16 ++++++---------- src/utils.rs | 6 ++---- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd10010..53f35fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,9 +102,9 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.46" +version = "0.1.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4968268c4b1be4c2ee4a619dba411ec2db09acde6c26da368cb01a330b60f06c" +checksum = "7e098e9c493fdf92832223594d9a164f96bdf17ba81a42aff86f85c76768726a" dependencies = [ "proc-macro2", "quote", @@ -729,7 +729,7 @@ dependencies = [ "actix-rt", "derive_builder", "derive_more", - "pow_sha256 0.1.0", + "pow_sha256 0.2.0", "rand 0.8.3", "serde", "serde_json", @@ -983,8 +983,8 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pow_sha256" -version = "0.1.0" -source = "git+https://github.com/mcaptcha/pow_sha256#0bfbce0185f111b2eb8c14ff74d95dde5af22388" +version = "0.2.0" +source = "git+https://github.com/mcaptcha/pow_sha256#d0889fc9008e63795b024b57432ed0034a703d0b" dependencies = [ "bincode", "derive_builder", diff --git a/Cargo.toml b/Cargo.toml index 6b281e3..bdf7245 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ derive_builder = "0.9" derive_more = "0.99" rand = "0.8" -pow_sha256 = { version = "0.1", git = "https://github.com/mcaptcha/pow_sha256" } +pow_sha256 = { version = "0.2", git = "https://github.com/mcaptcha/pow_sha256" } [dev-dependencies] actix-rt = "1" diff --git a/src/pow.rs b/src/pow.rs index 8bf594a..08cd74c 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -75,7 +75,6 @@ where pub async fn verify_pow(&self, work: Work) -> CaptchaResult { use crate::cache::messages::Retrive; - use crate::utils::get_difficulty; let string = work.string.clone(); let msg = Retrive(string.clone()); @@ -83,10 +82,7 @@ where let pow: PoW = work.into(); match difficulty { Ok(Some(difficulty)) => { - if self - .pow - .is_sufficient_difficulty(&pow, get_difficulty(difficulty)) - { + if self.pow.is_sufficient_difficulty(&pow, difficulty) { Ok(self.pow.is_valid_proof(&pow, &string)) } else { Err(CaptchaError::InsuffiencientDifficulty) @@ -122,9 +118,9 @@ mod tests { use pow_sha256::ConfigBuilder; use super::*; + use crate::cache::HashCache; use crate::master::*; use crate::mcaptcha::tests::*; - use crate::{cache::HashCache, utils::get_difficulty}; const MCAPTCHA_NAME: &str = "batsense.net"; @@ -170,11 +166,11 @@ mod tests { let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap(); let config = get_config(); - let difficulty = get_difficulty(work_req.difficulty_factor); - let work = config.prove_work(&work_req.string, difficulty).unwrap(); + let work = config + .prove_work(&work_req.string, work_req.difficulty_factor) + .unwrap(); - let difficulty = 1; - let insufficient_work = config.prove_work(&work_req.string, difficulty).unwrap(); + let insufficient_work = config.prove_work(&work_req.string, 1).unwrap(); let insufficient_work_payload = Work { string: work_req.string.clone(), result: insufficient_work.result, diff --git a/src/utils.rs b/src/utils.rs index 21fb2cd..69dc751 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,5 @@ +// utility function to get a randomly generated string +// of size len pub fn get_random(len: usize) -> String { use std::iter; @@ -11,7 +13,3 @@ pub fn get_random(len: usize) -> String { .take(len) .collect::() } - -pub fn get_difficulty(difficulty_factor: u32) -> u128 { - u128::max_value() - u128::max_value() / difficulty_factor as u128 -}