diff --git a/Cargo.lock b/Cargo.lock index c22bb19bf1ea8829d83603e313f2a3b609c270a8..10bf4def0a4457e8bee8b3df823dde7536dc7b96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,24 @@ dependencies = [ "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bindgen" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "1.1.0" @@ -203,6 +221,14 @@ name = "cc" version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cexpr" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cfg-if" version = "0.1.9" @@ -227,6 +253,16 @@ dependencies = [ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "clang-sys" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "clap" version = "2.33.0" @@ -578,6 +614,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rkv 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1052,6 +1089,11 @@ dependencies = [ "wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "half" version = "1.3.0" @@ -1182,6 +1224,15 @@ name = "libc" version = "0.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libloading" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libz-sys" version = "1.0.25" @@ -1204,6 +1255,17 @@ dependencies = [ "lmdb-rkv-sys 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lmdb-rkv" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "lmdb-rkv-sys 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lmdb-rkv-sys" version = "0.8.6" @@ -1214,6 +1276,17 @@ dependencies = [ "pkg-config 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lmdb-rkv-sys" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bindgen 0.51.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "log" version = "0.4.8" @@ -1486,6 +1559,11 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -1919,6 +1997,25 @@ dependencies = [ "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rkv" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lmdb-rkv 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rpassword" version = "1.0.2" @@ -1970,6 +2067,14 @@ name = "rustc-demangle" version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc-hash" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc_version" version = "0.2.3" @@ -2086,6 +2191,11 @@ dependencies = [ "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "shlex" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "shrinkwraprs" version = "0.2.1" @@ -2586,6 +2696,7 @@ dependencies = [ "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" "checksum bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9f2fb9e29e72fd6bc12071533d5dc7664cb01480c59406f656d7ac25c7bd8ff7" +"checksum bindgen 0.51.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ebd71393f1ec0509b553aa012b9b58e81dadbdff7130bd3b8cba576e69b32f75" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum blake2b_simd 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bf775a81bb2d464e20ff170ac20316c7b08a43d11dbc72f0f82e8e8d3d6d0499" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" @@ -2598,9 +2709,11 @@ dependencies = [ "checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" "checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" "checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff" +"checksum cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7fa24eb00d5ffab90eaeaf1092ac85c04c64aaf358ea6f84505b8116d24c6af" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" "checksum chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77d2058ba29594f69c75e8a9018e0485e3914ca5084e3613cd64529042f5423b" "checksum chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "77d81f58b7301084de3b958691458a53c3f7e0b1d702f77e550b6a88e3a88abe" +"checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" @@ -2633,6 +2746,7 @@ dependencies = [ "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" "checksum getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "fc344b02d3868feb131e8b5fe2b9b0a1cc42942679af493061fc13b853243872" +"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9353c2a89d550b58fa0061d8ed8d002a7d8cdf2494eb0e432859bd3a9e543836" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" @@ -2649,9 +2763,12 @@ dependencies = [ "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" +"checksum libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" "checksum libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" "checksum lmdb-rkv 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e25b4069789bf7ac069d6fd58229f18aec20c6f7cc9173cb731d11c10dbb6b6e" +"checksum lmdb-rkv 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "605061e5465304475be2041f19967a900175ea1b6d8f47fbab84a84fb8c48452" "checksum lmdb-rkv-sys 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c60e2728ce41a4d4fa4ccf3d07c105bebf198721117e6328a3cf1cb7e4242c70" +"checksum lmdb-rkv-sys 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e4b19a1fdf5b74bc802cc9aa7c0c86a775e8b872ba9d5a4e606ffc5d076953" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" @@ -2682,6 +2799,7 @@ dependencies = [ "checksum output_vt100 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" "checksum pbkdf2 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "006c038a43a45995a9670da19e67600114740e8511d4333bf97a56e66a7542d9" "checksum pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "deb73390ab68d81992bd994d145f697451bb0b54fd39738e72eef32458ad6907" +"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" "checksum pest 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "933085deae3f32071f135d799d75667b63c8dc1f4537159756e3d4ceab41868c" "checksum pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0" @@ -2726,12 +2844,14 @@ dependencies = [ "checksum regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b143cceb2ca5e56d5671988ef8b15615733e7ee16cd348e064333b251b89343f" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum ring 0.16.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6747f8da1f2b1fabbee1aaa4eb8a11abf9adef0bf58a41cee45db5d59cecdfac" +"checksum rkv 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9aab7c645d32e977e186448b0a5c2c3139a91a7f630cfd8a8c314d1d145e78bf" "checksum rkv 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "4f9d6a4dd60be13a62ae1d19df68c0c85d77bbee3749b62bf35c49f207d3d750" "checksum rpassword 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b273c91bd242ca03ad6d71c143b6f17a48790e61f21a6c78568fa2b6774a24a4" "checksum rprompt 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1601f32bc5858aae3cbfa1c645c96c4d820cc5c16be0194f089560c00b6eb625" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" "checksum rustbreak 2.0.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)" = "b1c185a2ede13fcb28feb6864ee9412a20f57bd83b4be18dc81fde4d6e786982" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997" "checksum safemem 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d2b08423011dae9a5ca23f07cf57dac3857f5c885d352b76f6d95f4aea9434d0" @@ -2746,6 +2866,7 @@ dependencies = [ "checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704" "checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" +"checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum shrinkwraprs 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5f047b90b2ca2d1526ff73d67cba61f86f4cf9a8afddc99dd96702ded8e684" "checksum simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e95345f185d5adeb8ec93459d2dc99654e294cc6ccf5b75414d8ea262de9a13" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" diff --git a/lib/modules-lib/bc-db-reader/Cargo.toml b/lib/modules-lib/bc-db-reader/Cargo.toml index c5bff6033995025fc64f8bb5fbe58d667df271cb..ffef33272f32b19a145c99cd713df3b726db7839 100644 --- a/lib/modules-lib/bc-db-reader/Cargo.toml +++ b/lib/modules-lib/bc-db-reader/Cargo.toml @@ -27,6 +27,7 @@ fnv = "1.0.6" serde = { version = "1.0.*", features = ["derive"] } unwrap = "1.2.1" num = "0.2" +rkv = "0.10.2" [dev-dependencies] dup-crypto-tests-tools = { path = "../../tests-tools/crypto-tests-tools" } diff --git a/lib/modules-lib/bc-db-reader/src/current_frame.rs b/lib/modules-lib/bc-db-reader/src/current_frame.rs index 74482e605bd60fabe483bd827c8db096d374f1a2..b2e9577b3d084023fad8e78cfc6132e2c70554b5 100644 --- a/lib/modules-lib/bc-db-reader/src/current_frame.rs +++ b/lib/modules-lib/bc-db-reader/src/current_frame.rs @@ -20,10 +20,14 @@ use crate::*; extern crate num; +use crate::current_meta_datas::get_current_blockstamp_; +use current_meta_datas::CurrentMetaDataKey; use dubp_common_doc::BlockNumber; use durs_dbs_tools::DbError; use durs_wot::WotId; use num::Float; +use rkv::store::integer::IntegerStore; +use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -43,7 +47,7 @@ pub struct PersonalDifficulty { /// handicap pub handicap: usize, } -// FIXME: à tester + impl Default for PersonalDifficulty { fn default() -> Self { PersonalDifficulty { @@ -57,9 +61,20 @@ impl Default for PersonalDifficulty { /// in frame member info data store pub struct MemberFrameInfo { last_personal_block_number: BlockNumber, + last_personal_members_in_frame: usize, nb_personal_blocks_in_frame: usize, personal_difficulty: PersonalDifficulty, } +//impl Default for MemberFrameInfo { +// fn default() -> Self { +// MemberFrameInfo { +// last_personal_block_number: BlockNumber, +// last_personal_members_in_frame: usize, +// nb_personal_blocks_in_frame: usize, +// personal_difficulty: PersonalDifficulty, +// } +// } +//} /// Get current frame datas pub fn get_current_frame<DB: DbReadable>( @@ -68,17 +83,82 @@ pub fn get_current_frame<DB: DbReadable>( unimplemented!(); } +// TODO: tester ça +pub fn get_required_u64_in_int_store<DB: DbReadable, R: DbReader>( + db: &DB, + r: &R, + store_name: &str, + key: u32, +) -> Result<u64, DbError> { + if let Some(value) = db.get_int_store(store_name).get(r, key)? { + if let DbValue::U64(value_u64) = value { + Ok(value_u64) + } else { + Err(DbError::DBCorrupted) + } + } else { + Err(DbError::DBCorrupted) + } +} + +// TODO: tester ça +//pub fn get_required_bin_in_int_store<DB: DbReadable, R: DbReader, V: DeserializeOwned>( +// db: &DB, +// r: &R, +// store_name: &str, +// key: u32, +//) -> Result<V, DbError> { +// if let Some(value) = db.get_int_store(store_name).get(r, key)? { +// Ok(BcDbRo::from_db_value(value)?) +// } else { +// Err(DbError::DBCorrupted) +// } +//} +// TODO: tester ça +pub fn get_bin_in_int_store<DB: DbReadable, R: DbReader, V: DeserializeOwned>( + db: &DB, + r: &R, + store_name: &str, + key: u32, +) -> Result<Option<V>, DbError> { + if let Some(value) = db.get_int_store(store_name).get(r, key)? { + Ok(Some(BcDbRo::from_db_value(value)?)) + } else { + Ok(None) + } +} + /// Get the personal difficulty of a member. /// If the member is not in the current window, returns `pow_min`. pub fn get_member_diffi<DB: DbReadable, R: DbReader>( - _db: &DB, - _r: &R, - _wot_id: WotId, + db: &DB, + r: &R, + wot_id: WotId, ) -> Result<PersonalDifficulty, DbError> { - Ok(PersonalDifficulty { - exclusion_factor: 1, - handicap: 0, - }) + // si membre absent du store return personnal; + // difficulty par défaut + // sinon récupérer le résultat et continuer + let optional_member_info: Option<MemberFrameInfo> = + get_bin_in_int_store(db, r, CURRENT_FRAME_MEMBERS, wot_id.0 as u32)?; + if optional_member_info.is_none() { + Ok(PersonalDifficulty { + exclusion_factor: 1, + handicap: 0, + }) + } else { + let current_blockstamp = + get_current_blockstamp_(db, r)?.ok_or(DbError::DBCorrupted)?; + let median_frame_member = get_required_u64_in_int_store( + db, + r, + CURRENT_METAS_DATAS, + CurrentMetaDataKey::MedianFrameMember.to_u32(), + )?; + Ok(PersonalDifficulty { + exclusion_factor: 2, + handicap: 13, + }) + } } /// calcule la difficulté personnalisée à partir des données prêtes à l'emploi @@ -130,17 +210,16 @@ pub fn handicap(nb_personal_blocks_in_frame: usize, median_of_blocks_in_frame: u )) as f64) .ln() / 1.189.ln()) - .floor() as usize + .floor() as usize } #[cfg(test)] mod tests { use super::*; + use crate::tests::*; use durs_dbs_tools::kv_db::KvFileDbHandler; - //use durs_bc_db_reader::DbValue; - //use durs_bc_db_reader::Db; - //use Db::db_value; - //use log::kv::Value; + use dubp_common_doc::Blockstamp; + //TODO: déplacer la fonction métier dans blockchain ou bc-db-writer #[test] @@ -157,10 +236,13 @@ mod tests { #[test] fn test_default_personal_difficulty() { - assert_eq!(PersonalDifficulty { - exclusion_factor: 1, - handicap: 0, - },PersonalDifficulty::default()); + assert_eq!( + PersonalDifficulty { + exclusion_factor: 1, + handicap: 0, + }, + PersonalDifficulty::default() + ); } #[test] @@ -193,25 +275,75 @@ mod tests { } // test avec db mockée -// fn factory_member_frame_info(last: u32, nbr:usize) -> Result<DbValue, DbError> { -// Db::db_value(&durs_dbs_tools::to_bytes(&MemberFrameInfo { -// last_personal_block_number: BlockNumber(last), -// nb_personal_blocks_in_frame: nbr, -// ..Default::default() -// })?) -// } + fn factory_member_frame_info( + last_bn: u32, + last_mf: usize, + nbr_pbf: usize, + ) -> Result<Vec<u8>, DbError> { + durs_dbs_tools::to_bytes(&MemberFrameInfo { + last_personal_block_number: BlockNumber(last_bn), + last_personal_members_in_frame: last_mf, + nb_personal_blocks_in_frame: nbr_pbf, + personal_difficulty: PersonalDifficulty::default(), + }) + } + + fn put_member_frame_info_in_db( + db: &KvFileDbHandler, + w: &mut DbWriter, + wot_id: u32, + last_bn: u32, + last_mf: usize, + nbr_pbf: usize, + ) -> Result<(), DbError> { + db.get_int_store(CURRENT_FRAME_MEMBERS).put( + w.as_mut(), + wot_id, + &DbValue::Blob(&factory_member_frame_info(last_bn, last_mf, nbr_pbf)?), + )?; + Ok(()) + } + + /// cré un store CURRENT_FRAME_MEMBERS avec comme + /// |- clef les wot_id des membres de la fenêtre courante + /// |- valeurs : + /// |- le block_number du dernier block de ce membre + /// |- le nombre de membre dans la fenetre courante au dernier block de ce membre + /// |- le nombre de block de ce membre dans la fenêtre courante fn init_mocked_db() -> Result<KvFileDbHandler, DbError> { - let db = crate::tests::open_tmp_db()?; + let db = open_tmp_db()?; db.write(|mut w| { - //db.get_int_store(CURRENT_FRAME_MEMBERS).put(w.as_mut(), 1, &factory_member_frame_info(99,15)?); - - // créer une hashmap avec comme - // |- clef les wot_id des membres de la fenêtre courante - // |- valeurs : - // |- le block_number du dernier block de ce membre - // |- le nombre de block de ce membre dans la fenêtre courante - // {1:{99,15},3:{80,5},4:{97,3},5:{98,1},6:{90,1},8:{71,1}} - //common::insert_wot_index_entry(&db, &mut w, WotId(0), issuer_a)?; + put_member_frame_info_in_db(&db, &mut w, 1, 99, 6, 15)?; + put_member_frame_info_in_db(&db, &mut w, 3, 80, 6, 5)?; + put_member_frame_info_in_db(&db, &mut w, 4, 97, 6, 3)?; + put_member_frame_info_in_db(&db, &mut w, 5, 98, 6, 1)?; + put_member_frame_info_in_db(&db, &mut w, 6, 90, 6, 1)?; + put_member_frame_info_in_db(&db, &mut w, 8, 71, 6, 1)?; + + let blockstamp_bytes: Vec<u8> = Blockstamp{ + id:BlockNumber(100), + ..Default::default() + }.into(); + db.get_int_store(CURRENT_METAS_DATAS).put( + w.as_mut(), + CurrentMetaDataKey::CurrentBlockstamp.to_u32(), + &DbValue::Blob(&blockstamp_bytes), + )?; + // db.get_int_store(CURRENT_METAS_DATAS).put( + // w.as_mut(), + // CurrentMetaDataKey::CurrentFrameMembersSize.to_u32(), + // &DbValue::U64(6), + // )?; + db.get_int_store(CURRENT_METAS_DATAS).put( + w.as_mut(), + CurrentMetaDataKey::MedianFrameMember.to_u32(), + &DbValue::U64(2), + )?; + + //CurrentBlockNumber : BlockNumber + //CurrentFrameMembersSize : u32 + //MedianFrameMember : bloc_count + Ok(w) })?; Ok(db) @@ -230,16 +362,16 @@ mod tests { Ok(()) } -// #[test] -// fn test_personal_difficulty_member_supercalculator() -> Result<(), DbError> { -// let db = init_mocked_db()?; -// assert_eq!( -// PersonalDifficulty { -// exclusion_factor: 2, -// handicap: 13, -// }, -// db.read(|r| get_member_diffi(&db, r, WotId(1)))? -// ); -// Ok(()) -// } + #[test] + fn test_personal_difficulty_member_supercalculator() -> Result<(), DbError> { + let db = init_mocked_db()?; + assert_eq!( + PersonalDifficulty { + exclusion_factor: 2, + handicap: 13, + }, + db.read(|r| get_member_diffi(&db, r, WotId(1)))? + ); + Ok(()) + } } diff --git a/lib/modules-lib/bc-db-reader/src/current_meta_datas.rs b/lib/modules-lib/bc-db-reader/src/current_meta_datas.rs index ae41fd98db03099d643060309fca543d371bbabe..9b0ca83e408e4ca66aeeb16a40cb4c70c2b4edeb 100644 --- a/lib/modules-lib/bc-db-reader/src/current_meta_datas.rs +++ b/lib/modules-lib/bc-db-reader/src/current_meta_datas.rs @@ -38,6 +38,8 @@ pub enum CurrentMetaDataKey { ForkTree, /// Greatest wot id NextWotId, + /// Block by member median in current frame + MedianFrameMember, } impl CurrentMetaDataKey { @@ -50,6 +52,7 @@ impl CurrentMetaDataKey { Self::CurrentBlockchainTime => 3, Self::ForkTree => 4, Self::NextWotId => 5, + Self::MedianFrameMember => 6, } } } diff --git a/lib/modules-lib/bc-db-reader/src/lib.rs b/lib/modules-lib/bc-db-reader/src/lib.rs index 03585176b0aa775218a16abcb1ab87a3ef72af05..2128a0842d1c53834f3224592f3bb4fc8e317471 100644 --- a/lib/modules-lib/bc-db-reader/src/lib.rs +++ b/lib/modules-lib/bc-db-reader/src/lib.rs @@ -75,9 +75,9 @@ pub fn open_db_ro(path: &Path) -> Result<BcDbRo, DbError> { #[cfg(test)] pub mod tests { - use super::*; use durs_dbs_tools::kv_db::KvFileDbHandler; + pub use durs_dbs_tools::kv_db::KvFileDbWriter as DbWriter; use tempfile::tempdir; #[inline]