pow_256 veresion bump
This commit is contained in:
parent
4fe205c73a
commit
d53d35468e
4 changed files with 14 additions and 20 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -102,9 +102,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.46"
|
version = "0.1.47"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4968268c4b1be4c2ee4a619dba411ec2db09acde6c26da368cb01a330b60f06c"
|
checksum = "7e098e9c493fdf92832223594d9a164f96bdf17ba81a42aff86f85c76768726a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -729,7 +729,7 @@ dependencies = [
|
||||||
"actix-rt",
|
"actix-rt",
|
||||||
"derive_builder",
|
"derive_builder",
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"pow_sha256 0.1.0",
|
"pow_sha256 0.2.0",
|
||||||
"rand 0.8.3",
|
"rand 0.8.3",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -983,8 +983,8 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pow_sha256"
|
name = "pow_sha256"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
source = "git+https://github.com/mcaptcha/pow_sha256#0bfbce0185f111b2eb8c14ff74d95dde5af22388"
|
source = "git+https://github.com/mcaptcha/pow_sha256#d0889fc9008e63795b024b57432ed0034a703d0b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bincode",
|
"bincode",
|
||||||
"derive_builder",
|
"derive_builder",
|
||||||
|
|
|
@ -24,7 +24,7 @@ derive_builder = "0.9"
|
||||||
derive_more = "0.99"
|
derive_more = "0.99"
|
||||||
|
|
||||||
rand = "0.8"
|
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]
|
[dev-dependencies]
|
||||||
actix-rt = "1"
|
actix-rt = "1"
|
||||||
|
|
16
src/pow.rs
16
src/pow.rs
|
@ -75,7 +75,6 @@ where
|
||||||
|
|
||||||
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<bool> {
|
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<bool> {
|
||||||
use crate::cache::messages::Retrive;
|
use crate::cache::messages::Retrive;
|
||||||
use crate::utils::get_difficulty;
|
|
||||||
|
|
||||||
let string = work.string.clone();
|
let string = work.string.clone();
|
||||||
let msg = Retrive(string.clone());
|
let msg = Retrive(string.clone());
|
||||||
|
@ -83,10 +82,7 @@ where
|
||||||
let pow: PoW<String> = work.into();
|
let pow: PoW<String> = work.into();
|
||||||
match difficulty {
|
match difficulty {
|
||||||
Ok(Some(difficulty)) => {
|
Ok(Some(difficulty)) => {
|
||||||
if self
|
if self.pow.is_sufficient_difficulty(&pow, difficulty) {
|
||||||
.pow
|
|
||||||
.is_sufficient_difficulty(&pow, get_difficulty(difficulty))
|
|
||||||
{
|
|
||||||
Ok(self.pow.is_valid_proof(&pow, &string))
|
Ok(self.pow.is_valid_proof(&pow, &string))
|
||||||
} else {
|
} else {
|
||||||
Err(CaptchaError::InsuffiencientDifficulty)
|
Err(CaptchaError::InsuffiencientDifficulty)
|
||||||
|
@ -122,9 +118,9 @@ mod tests {
|
||||||
use pow_sha256::ConfigBuilder;
|
use pow_sha256::ConfigBuilder;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::cache::HashCache;
|
||||||
use crate::master::*;
|
use crate::master::*;
|
||||||
use crate::mcaptcha::tests::*;
|
use crate::mcaptcha::tests::*;
|
||||||
use crate::{cache::HashCache, utils::get_difficulty};
|
|
||||||
|
|
||||||
const MCAPTCHA_NAME: &str = "batsense.net";
|
const MCAPTCHA_NAME: &str = "batsense.net";
|
||||||
|
|
||||||
|
@ -170,11 +166,11 @@ mod tests {
|
||||||
let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
|
|
||||||
let difficulty = get_difficulty(work_req.difficulty_factor);
|
let work = config
|
||||||
let work = config.prove_work(&work_req.string, difficulty).unwrap();
|
.prove_work(&work_req.string, work_req.difficulty_factor)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let difficulty = 1;
|
let insufficient_work = config.prove_work(&work_req.string, 1).unwrap();
|
||||||
let insufficient_work = config.prove_work(&work_req.string, difficulty).unwrap();
|
|
||||||
let insufficient_work_payload = Work {
|
let insufficient_work_payload = Work {
|
||||||
string: work_req.string.clone(),
|
string: work_req.string.clone(),
|
||||||
result: insufficient_work.result,
|
result: insufficient_work.result,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
// utility function to get a randomly generated string
|
||||||
|
// of size len
|
||||||
pub fn get_random(len: usize) -> String {
|
pub fn get_random(len: usize) -> String {
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
|
@ -11,7 +13,3 @@ pub fn get_random(len: usize) -> String {
|
||||||
.take(len)
|
.take(len)
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_difficulty(difficulty_factor: u32) -> u128 {
|
|
||||||
u128::max_value() - u128::max_value() / difficulty_factor as u128
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue