Skip to content

Commit

Permalink
Merge pull request #2 from mCaptcha/feat-incremental
Browse files Browse the repository at this point in the history
feat: compute pow incrementally w progress notifications via callback fn
  • Loading branch information
realaravinth authored Oct 28, 2023
2 parents 5137b0f + 6c6e087 commit bbdf8e0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 56 deletions.
102 changes: 51 additions & 51 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
name: Coverage

on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- master

jobs:
build_and_test:
strategy:
fail-fast: false
matrix:
version:
# - stable
- 1.51.0

name: ${{ matrix.version }} - x86_64-unknown-linux-gnu
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: ⚡ Cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
node_modules
./docs/openapi/node_modules
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install ${{ matrix.version }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.version }}-x86_64-unknown-linux-gnu
profile: minimal
override: true

- name: Generate coverage file
if: matrix.version == '1.51.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
uses: actions-rs/tarpaulin@v0.1
with:
version: '0.15.0'
args: '-t 1200'

- name: Upload to Codecov
if: matrix.version == '1.51.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
uses: codecov/codecov-action@v2
#name: Coverage
#
#on:
# pull_request:
# types: [opened, synchronize, reopened]
# push:
# branches:
# - master
#
#jobs:
# build_and_test:
# strategy:
# fail-fast: false
# matrix:
# version:
# # - stable
# - 1.51.0
#
# name: ${{ matrix.version }} - x86_64-unknown-linux-gnu
# runs-on: ubuntu-latest
#
# steps:
# - uses: actions/checkout@v2
# - name: ⚡ Cache
# uses: actions/cache@v2
# with:
# path: |
# ~/.cargo/registry
# ~/.cargo/git
# node_modules
# ./docs/openapi/node_modules
# target
# key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
#
# - name: Install ${{ matrix.version }}
# uses: actions-rs/toolchain@v1
# with:
# toolchain: ${{ matrix.version }}-x86_64-unknown-linux-gnu
# profile: minimal
# override: true
#
# - name: Generate coverage file
# if: matrix.version == '1.51.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
# uses: actions-rs/tarpaulin@v0.1
# with:
# version: '0.15.0'
# args: '-t 1200'
#
# - name: Upload to Codecov
# if: matrix.version == '1.51.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
# uses: codecov/codecov-action@v2
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ wee_alloc = { version = "0.4.5", optional = true }
serde = "1.0.130"
serde_derive = "1.0.130"
serde_json = "1"
js-sys = "0.3.64"

mcaptcha_pow_sha256 = "0.4.0"
mcaptcha_pow_sha256 = { version = "0.5.0", features = ["incremental"]}

[dev-dependencies]
wasm-bindgen-test = "0.3.28"
Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ pub fn gen_pow(salt: String, phrase: String, difficulty_factor: u32) -> String {
serde_json::to_string(&work).unwrap()
}

#[wasm_bindgen]
pub fn stepped_gen_pow(
salt: String,
phrase: String,
difficulty_factor: u32,
step: usize,
f: &js_sys::Function,
) -> String {
use mcaptcha_pow_sha256::IncrementalSolve;

let config = ConfigBuilder::default().salt(salt).build().unwrap();
let this = JsValue::null();

let mut inter = None;
loop {
match config.stepped_prove_work(&phrase, difficulty_factor, step, inter) {
Ok(IncrementalSolve::Intermediate(result, nonce, prefix, difficulty)) => {
let data = JsValue::from(difficulty);
let _ = f.call1(&this, &data);
inter = Some(IncrementalSolve::Intermediate(
result, nonce, prefix, difficulty,
));
continue;
}

Ok(IncrementalSolve::Work(w)) => {
let work: Work = w.into();
return serde_json::to_string(&work).unwrap();
}
Err(e) => panic!("{}", e),
};
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit bbdf8e0

Please sign in to comment.