Compare commits
26 commits
rename-cra
...
master
Author | SHA1 | Date | |
---|---|---|---|
e761db1532 | |||
024abe6204 | |||
|
99dc1d3b0e | ||
|
b4be9ef6fa | ||
43f05abdbf | |||
|
aff58f02de | ||
7c986a00cb | |||
|
0ad175708b | ||
7398ee4b73 | |||
|
7559663bee | ||
112d7e8f93 | |||
e9037ff79e | |||
|
856500bd0b | ||
f3ff4d4467 | |||
7fd887221f | |||
|
c363d1e517 | ||
|
e2f3f789a6 | ||
|
cc954a674c | ||
7df7b619a0 | |||
|
af1138b457 | ||
bde6da08e9 | |||
927b3e449b | |||
12d7533339 | |||
0e96f03166 | |||
debd7805d0 | |||
05048711a3 |
8 changed files with 213 additions and 71 deletions
4
.github/workflows/linux.yml
vendored
4
.github/workflows/linux.yml
vendored
|
@ -42,7 +42,7 @@ jobs:
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
command: check
|
command: check
|
||||||
args: --all --bins --examples --tests
|
args: --all --bins --examples --tests --all-features
|
||||||
|
|
||||||
- name: tests
|
- name: tests
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
|
@ -56,7 +56,7 @@ jobs:
|
||||||
uses: actions-rs/tarpaulin@v0.1
|
uses: actions-rs/tarpaulin@v0.1
|
||||||
with:
|
with:
|
||||||
version: '0.15.0'
|
version: '0.15.0'
|
||||||
args: '-t 1200'
|
args: '-t 1200 --all-features'
|
||||||
|
|
||||||
- name: Upload to Codecov
|
- name: Upload to Codecov
|
||||||
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
||||||
|
|
17
CHANGELOG.md
17
CHANGELOG.md
|
@ -1,22 +1,39 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.5.0
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `stepped_prove_work_serialized` sends notifications after every few hash
|
||||||
|
computations.
|
||||||
|
|
||||||
|
## 0.4.0
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
Rename crate from `pow_sha256` to `mcaptcha_pow_sha256`
|
||||||
|
|
||||||
## 0.2.1
|
## 0.2.1
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- performance improvement: internally `get_difficulty` was being called
|
- performance improvement: internally `get_difficulty` was being called
|
||||||
multiple times while generating `PoW`, now it calls only once.
|
multiple times while generating `PoW`, now it calls only once.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- encoding and decoding configuration used internally was causing an
|
- encoding and decoding configuration used internally was causing an
|
||||||
error in `PoW` computation
|
error in `PoW` computation
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Difficulty factor is now an unsigned 32 bit number
|
- Difficulty factor is now an unsigned 32 bit number
|
||||||
|
|
||||||
## 0.1
|
## 0.1
|
||||||
|
|
||||||
### Added:
|
### Added:
|
||||||
|
|
||||||
- PoW constructor
|
- PoW constructor
|
||||||
- unique salt
|
- unique salt
|
||||||
|
|
102
Cargo.lock
generated
102
Cargo.lock
generated
|
@ -53,9 +53,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "darling"
|
name = "darling"
|
||||||
version = "0.14.4"
|
version = "0.20.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
|
checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"darling_core",
|
"darling_core",
|
||||||
"darling_macro",
|
"darling_macro",
|
||||||
|
@ -63,58 +63,58 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "darling_core"
|
name = "darling_core"
|
||||||
version = "0.14.4"
|
version = "0.20.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
|
checksum = "622687fe0bac72a04e5599029151f5796111b90f1baaa9b544d807a5e31cd120"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fnv",
|
"fnv",
|
||||||
"ident_case",
|
"ident_case",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"strsim",
|
"strsim",
|
||||||
"syn 1.0.109",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "darling_macro"
|
name = "darling_macro"
|
||||||
version = "0.14.4"
|
version = "0.20.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
|
checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"darling_core",
|
"darling_core",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 1.0.109",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "derive_builder"
|
name = "derive_builder"
|
||||||
version = "0.12.0"
|
version = "0.20.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8"
|
checksum = "cd33f37ee6a119146a1781d3356a7c26028f83d779b2e04ecd45fdc75c76877b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"derive_builder_macro",
|
"derive_builder_macro",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "derive_builder_core"
|
name = "derive_builder_core"
|
||||||
version = "0.12.0"
|
version = "0.20.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f"
|
checksum = "7431fa049613920234f22c47fdc33e6cf3ee83067091ea4277a3f8c4587aae38"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"darling",
|
"darling",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 1.0.109",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "derive_builder_macro"
|
name = "derive_builder_macro"
|
||||||
version = "0.12.0"
|
version = "0.20.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
|
checksum = "4abae7035bf79b9877b779505d8cf3749285b80c43941eda66604841889451dc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"derive_builder_core",
|
"derive_builder_core",
|
||||||
"syn 1.0.109",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -157,7 +157,7 @@ checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mcaptcha_pow_sha256"
|
name = "mcaptcha_pow_sha256"
|
||||||
version = "0.3.1"
|
version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bincode",
|
"bincode",
|
||||||
"derive_builder",
|
"derive_builder",
|
||||||
|
@ -168,9 +168,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num"
|
name = "num"
|
||||||
version = "0.4.1"
|
version = "0.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
|
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-complex",
|
"num-complex",
|
||||||
|
@ -182,11 +182,10 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-bigint"
|
name = "num-bigint"
|
||||||
version = "0.4.4"
|
version = "0.4.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
|
checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -194,9 +193,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-complex"
|
name = "num-complex"
|
||||||
version = "0.4.4"
|
version = "0.4.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
|
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -204,19 +203,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.45"
|
version = "0.1.46"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
|
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
|
||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-iter"
|
name = "num-iter"
|
||||||
version = "0.1.43"
|
version = "0.1.45"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
|
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
|
@ -225,11 +223,10 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-rational"
|
name = "num-rational"
|
||||||
version = "0.4.1"
|
version = "0.4.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
|
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -237,49 +234,49 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.17"
|
version = "0.2.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.69"
|
version = "1.0.85"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.33"
|
version = "1.0.36"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.189"
|
version = "1.0.210"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537"
|
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.189"
|
version = "1.0.210"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5"
|
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.38",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -295,26 +292,15 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.10.0"
|
version = "0.11.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.109"
|
version = "2.0.66"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.38"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mcaptcha_pow_sha256"
|
name = "mcaptcha_pow_sha256"
|
||||||
version = "0.3.1"
|
version = "0.5.0"
|
||||||
authors = [ "Aravinth Manivannan <realaravinth@bastsense.net>", "Robert Kornacki <robk@syre.io>"]
|
authors = [ "Aravinth Manivannan <realaravinth@bastsense.net>", "Robert Kornacki <robk@syre.io>"]
|
||||||
description = """
|
description = """
|
||||||
SHA256 PoW on any serializable datatype used in mCaptcha
|
SHA256 PoW on any serializable datatype used in mCaptcha
|
||||||
|
@ -17,5 +17,9 @@ categories = ["captcha", "algorithms", "cryptography::cryptocurrencies"]
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
bincode = "1.3"
|
bincode = "1.3"
|
||||||
derive_builder = "0.12"
|
derive_builder = "0.20"
|
||||||
num = { version = "0.4.0", default-features = false, features = ["serde", "num-bigint"]}
|
num = { version = "0.4.0", default-features = false, features = ["serde", "num-bigint"]}
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
incremental = []
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
define test_core
|
define test_core
|
||||||
cargo test --no-fail-fast
|
cargo test --no-fail-fast --all-features
|
||||||
endef
|
endef
|
||||||
|
|
||||||
default: ## Build library in debug mode
|
default: ## Build library in debug mode
|
||||||
|
|
|
@ -11,7 +11,7 @@ cargo-fuzz = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libfuzzer-sys = "0.4"
|
libfuzzer-sys = "0.4"
|
||||||
sha2 = "0.9"
|
sha2 = "0.10"
|
||||||
arbitrary = { version = "1", features = ["derive"] }
|
arbitrary = { version = "1", features = ["derive"] }
|
||||||
|
|
||||||
[dependencies.pow_sha256]
|
[dependencies.pow_sha256]
|
||||||
|
|
21
renovate.json
Normal file
21
renovate.json
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"config:recommended",
|
||||||
|
":dependencyDashboard"
|
||||||
|
],
|
||||||
|
"labels": [
|
||||||
|
"renovate-bot"
|
||||||
|
],
|
||||||
|
"prHourlyLimit": 0,
|
||||||
|
"timezone": "Asia/kolkata",
|
||||||
|
"prCreation": "immediate",
|
||||||
|
"vulnerabilityAlerts": {
|
||||||
|
"enabled": true,
|
||||||
|
"labels": [
|
||||||
|
"renovate-bot",
|
||||||
|
"renovate-security",
|
||||||
|
"security"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
114
src/lib.rs
114
src/lib.rs
|
@ -50,7 +50,94 @@ pub struct Config {
|
||||||
pub salt: String,
|
pub salt: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return value of incremental prooving. When proof is ready, IncrementalSolve::Work is returned
|
||||||
|
/// and when proof is not ready, IncrementalSolve::Intermediate is returned
|
||||||
|
#[cfg(feature = "incremental")]
|
||||||
|
pub enum IncrementalSolve<T> {
|
||||||
|
/// Intermediate result
|
||||||
|
Intermediate(u128, u64, Sha256, u128),
|
||||||
|
/// Final result
|
||||||
|
Work(PoW<T>),
|
||||||
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
///
|
||||||
|
/// step is used to control the number of cycles after which the function should exit, even
|
||||||
|
/// when the proof isn't ready
|
||||||
|
/// inter is used to keep track of state and complete proof generation. Set inter to None
|
||||||
|
/// during first cyle and pass the returned value of the previous cycle to continue proof
|
||||||
|
/// generation
|
||||||
|
|
||||||
|
#[cfg(feature = "incremental")]
|
||||||
|
pub fn stepped_prove_work<T>(
|
||||||
|
&self,
|
||||||
|
t: &T,
|
||||||
|
difficulty: u32,
|
||||||
|
step: usize,
|
||||||
|
inter: Option<IncrementalSolve<T>>,
|
||||||
|
) -> bincode::Result<IncrementalSolve<T>>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
bincode::serialize(t)
|
||||||
|
.map(|v| self.stepped_prove_work_serialized(&v, difficulty, step, inter))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "incremental")]
|
||||||
|
/// Create Proof of Work over item of type T.
|
||||||
|
///
|
||||||
|
/// Make sure difficulty is not too high. A 64 bit difficulty,
|
||||||
|
/// for example, takes a long time on a general purpose processor.
|
||||||
|
/// The input is assumed to be serialized using network byte order.
|
||||||
|
///
|
||||||
|
/// Make sure difficulty is not too high. A 64 bit difficulty,
|
||||||
|
/// for example, takes a long time on a general purpose processor.
|
||||||
|
/// step is used to control the number of cycles after which the function should exit, even
|
||||||
|
/// when the proof isn't ready
|
||||||
|
/// inter is used to keep track of state and complete proof generation. Set inter to None
|
||||||
|
/// during first cyle and pass the returned value of the previous cycle to continue proof
|
||||||
|
/// generation
|
||||||
|
/// Returns bincode::Error if serialization fails.
|
||||||
|
/// Create Proof of Work on an already serialized item of type T.
|
||||||
|
fn stepped_prove_work_serialized<T>(
|
||||||
|
&self,
|
||||||
|
prefix: &[u8],
|
||||||
|
difficulty: u32,
|
||||||
|
step: usize,
|
||||||
|
inter: Option<IncrementalSolve<T>>,
|
||||||
|
) -> IncrementalSolve<T>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
let (mut result, mut n, prefix_sha, difficulty) = match inter {
|
||||||
|
Some(IncrementalSolve::Intermediate(result, nonce, prefix, difficulty)) => {
|
||||||
|
(result, nonce, prefix, difficulty)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let prefix_sha = Sha256::new().chain(&self.salt).chain(prefix);
|
||||||
|
let n = 0;
|
||||||
|
let result = 0;
|
||||||
|
let difficulty = get_difficulty(difficulty);
|
||||||
|
(result, n, prefix_sha, difficulty)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut count = 0;
|
||||||
|
while result < difficulty {
|
||||||
|
if count > step {
|
||||||
|
return IncrementalSolve::Intermediate(result, n, prefix_sha, difficulty);
|
||||||
|
} else {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
n += 1;
|
||||||
|
result = dev::score(prefix_sha.clone(), n);
|
||||||
|
}
|
||||||
|
IncrementalSolve::Work(PoW {
|
||||||
|
nonce: n,
|
||||||
|
result: result.to_string(),
|
||||||
|
_spook: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Create Proof of Work over item of type T.
|
/// Create Proof of Work over item of type T.
|
||||||
///
|
///
|
||||||
/// Make sure difficulty is not too high. A 64 bit difficulty,
|
/// Make sure difficulty is not too high. A 64 bit difficulty,
|
||||||
|
@ -262,4 +349,31 @@ mod test {
|
||||||
assert!(config.is_sufficient_difficulty(&message.1, DIFFICULTY));
|
assert!(config.is_sufficient_difficulty(&message.1, DIFFICULTY));
|
||||||
assert!(config.is_valid_proof(&message.1, &target));
|
assert!(config.is_valid_proof(&message.1, &target));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "incremental")]
|
||||||
|
fn stepped_solve() {
|
||||||
|
let phrase = "Ex nihilo nihil fit.".to_owned();
|
||||||
|
let config = get_config();
|
||||||
|
|
||||||
|
let mut inter = None;
|
||||||
|
loop {
|
||||||
|
match config.stepped_prove_work(&phrase, 50000, 1000, inter) {
|
||||||
|
Ok(IncrementalSolve::Intermediate(result, nonce, prefix, difficulty)) => {
|
||||||
|
println!("Current nonce {nonce}");
|
||||||
|
inter = Some(IncrementalSolve::Intermediate(
|
||||||
|
result, nonce, prefix, difficulty,
|
||||||
|
));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(IncrementalSolve::Work(w)) => {
|
||||||
|
assert!(config.is_valid_proof(&w, &phrase));
|
||||||
|
assert!(config.is_sufficient_difficulty(&w, DIFFICULTY));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Err(e) => panic!("{}", e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue