diff --git a/Cargo.lock b/Cargo.lock index aac36f52123c3ca263cc09d9d59286d8c83ea43b..a56b5b7923bd05f566a13dcfd200c065feef7522 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,11 @@ dependencies = [ "ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.41" @@ -265,6 +270,39 @@ dependencies = [ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "criterion" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.2.0 (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)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", + "tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "criterion-plot" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-deque" version = "0.7.1" @@ -548,6 +586,7 @@ dependencies = [ name = "durs-bc-db-writer" version = "0.3.0-dev" dependencies = [ + "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-block-doc 0.1.0", "dubp-blocks-tests-tools 0.1.0", "dubp-common-doc 0.1.0", @@ -1087,6 +1126,14 @@ dependencies = [ "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itertools" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.4.4" @@ -1665,6 +1712,15 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_os" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_pcg" version = "0.1.2" @@ -1682,6 +1738,14 @@ dependencies = [ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_xoshiro" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rayon" version = "1.2.0" @@ -1870,6 +1934,14 @@ name = "safemem" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "same-file" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "scopeguard" version = "1.0.0" @@ -2157,6 +2229,15 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tinytemplate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "toml" version = "0.4.10" @@ -2264,6 +2345,16 @@ name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "walkdir" +version = "2.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "wasi" version = "0.5.0" @@ -2377,6 +2468,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -2443,6 +2542,7 @@ dependencies = [ "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "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 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" @@ -2452,6 +2552,8 @@ dependencies = [ "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "995a44c877f9212528ccc74b21a232f66ad69001e40ede5bcee2ac9ef2657120" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +"checksum criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "938703e165481c8d612ea3479ac8342e5615185db37765162e762ec3523e2fc6" +"checksum criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eccdc6ce8bbe352ca89025bee672aa6d24f4eb8c53e3a8b5d1bc58011da072a2" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" "checksum crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fedcd6772e37f3da2a9af9bf12ebe046c0dfe657992377b4df982a2b54cd37a9" "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" @@ -2486,6 +2588,7 @@ dependencies = [ "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" +"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum js-sys 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)" = "1efc4f2a556c58e79c5500912e221dd826bec64ff4aabd8ce71ccef6da02d7d4" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" @@ -2549,8 +2652,10 @@ dependencies = [ "checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" "checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" "checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a788ae3edb696cfcba1c19bfd388cc4b8c21f8a408432b199c072825084da58a" "checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +"checksum rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e18c91676f670f6f0312764c759405f13afb98d5d73819840cf72a518487bff" "checksum rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "83a27732a533a1be0a0035a111fe76db89ad312f6f0347004c220c57f209a123" "checksum rayon-core 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "98dcf634205083b17d0861252431eb2acbfb698ab7478a2d20de07954f47ec7b" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" @@ -2571,6 +2676,7 @@ dependencies = [ "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" +"checksum same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421" "checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" "checksum scrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "656c79d0e90d0ab28ac86bf3c3d10bfbbac91450d3f190113b4e76d9fec3cfdd" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" @@ -2604,6 +2710,7 @@ dependencies = [ "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4574b75faccaacddb9b284faecdf0b544b80b6b294f3d062d325c5726a209c20" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum ucd-trie 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8f00ed7be0c1ff1e24f46c3d2af4859f7e863672ba3a6e92e7cff702bf9f06c2" @@ -2621,6 +2728,7 @@ dependencies = [ "checksum vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "33dd455d0f96e90a75803cfeb7f948768c08d70a6de9a8d2362461935698bf95" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e" "checksum wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fd5442abcac6525a045cc8c795aedb60da7a2e5e89c7bf18a0d5357849bb23c7" "checksum wasm-bindgen 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "dcddca308b16cd93c2b67b126c688e5467e4ef2e28200dc7dfe4ae284f2faefc" "checksum wasm-bindgen-backend 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "f805d9328b5fc7e5c6399960fd1889271b9b58ae17bdb2417472156cc9fafdd0" @@ -2634,6 +2742,7 @@ dependencies = [ "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767" "checksum ws 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a6f5bb86663ff4d1639408410f50bf6050367a8525d644d49a6894cd618a631" diff --git a/lib/modules-lib/bc-db-reader/src/current_frame.rs b/lib/modules-lib/bc-db-reader/src/current_frame.rs new file mode 100644 index 0000000000000000000000000000000000000000..a69091ea0d559dd161b65295bb2886273ffe62ad --- /dev/null +++ b/lib/modules-lib/bc-db-reader/src/current_frame.rs @@ -0,0 +1,56 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Current frame : Interval of blocks taken for the calculation of the personalized difficulty. + +//use crate::constants::*; +use crate::*; +use durs_dbs_tools::DbError; +use durs_wot::WotId; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +/// Describe a member in current frame +pub struct MemberInCurrentFrame { + /// Number of blocks forged by the member in the current frame. + pub forged_blocks: usize, + /// Personal difficulty of the member. + pub difficulty: PersonalDifficulty, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +/// Personal difficulty of a member. +pub struct PersonalDifficulty { + /// Exclusion factor + pub exclusion_factor: usize, + /// handicap + pub handicap: usize, +} + +/// Get current frame datas +pub fn get_current_frame<DB: DbReadable>( + _db: &DB, +) -> Result<Vec<(WotId, MemberInCurrentFrame)>, DbError> { + unimplemented!(); +} + +/// 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, +) -> Result<PersonalDifficulty, DbError> { + unimplemented!(); +} diff --git a/lib/modules-lib/bc-db-reader/src/lib.rs b/lib/modules-lib/bc-db-reader/src/lib.rs index 9364d67a32dcf7512dbe1301f0dc4bcfb90ab18f..2edf971581e8a90561a7752e2774387e5f2f8821 100644 --- a/lib/modules-lib/bc-db-reader/src/lib.rs +++ b/lib/modules-lib/bc-db-reader/src/lib.rs @@ -30,6 +30,7 @@ pub mod blocks; pub mod constants; pub mod currency_params; +pub mod current_frame; pub mod current_meta_datas; pub mod indexes; pub mod paging; diff --git a/lib/modules/blockchain/bc-db-writer/Cargo.toml b/lib/modules/blockchain/bc-db-writer/Cargo.toml index 7b7ded536a453decea18df0673c328ddb5688d5d..6c242bf183f8b84a0f65696b776353e328530e04 100644 --- a/lib/modules/blockchain/bc-db-writer/Cargo.toml +++ b/lib/modules/blockchain/bc-db-writer/Cargo.toml @@ -29,10 +29,15 @@ serde_derive = "1.0.*" unwrap = "1.2.1" [dev-dependencies] +criterion = "0.3.0" dup-crypto-tests-tools = { path = "../../../tests-tools/crypto-tests-tools" } dubp-blocks-tests-tools = { path = "../../../tests-tools/blocks-tests-tools" } dubp-user-docs-tests-tools = { path = "../../../tests-tools/user-docs-tests-tools" } durs-common-tests-tools = { path = "../../../tests-tools/common-tests-tools" } tempfile = "3.1.0" +[[bench]] +name = "current_frame" +harness = false + [features] diff --git a/lib/modules/blockchain/bc-db-writer/benches/common/mod.rs b/lib/modules/blockchain/bc-db-writer/benches/common/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..c83e5f6c20bb186550267cb6b1aadad688c4921f --- /dev/null +++ b/lib/modules/blockchain/bc-db-writer/benches/common/mod.rs @@ -0,0 +1,44 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use dubp_block_doc::BlockDocument; +use dup_crypto::keys::{PubKey, PublicKey}; +use durs_bc_db_reader::blocks::DbBlock; +use durs_bc_db_reader::constants::*; +use durs_bc_db_reader::{DbReadable, DbValue}; +use durs_bc_db_writer::DbWriter; +use durs_bc_db_writer::{Db, DbError}; +use durs_wot::WotId; + +pub fn to_db_block(block: BlockDocument) -> DbBlock { + DbBlock { + block, + expire_certs: None, + } +} + +pub fn insert_wot_index_entry( + db: &Db, + w: &mut DbWriter, + wot_id: WotId, + pubkey: PubKey, +) -> Result<(), DbError> { + db.get_store(WOT_ID_INDEX).put( + w.as_mut(), + &pubkey.to_bytes_vector(), + &DbValue::U64(wot_id.0 as u64), + )?; + Ok(()) +} diff --git a/lib/modules/blockchain/bc-db-writer/benches/current_frame.rs b/lib/modules/blockchain/bc-db-writer/benches/current_frame.rs new file mode 100644 index 0000000000000000000000000000000000000000..4b89be72b01f6516adc535855661264f164d7fe9 --- /dev/null +++ b/lib/modules/blockchain/bc-db-writer/benches/current_frame.rs @@ -0,0 +1,62 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +#[macro_use] +extern crate criterion; + +use criterion::black_box; +use criterion::Criterion; +use dubp_block_doc::BlockDocument; +use dubp_blocks_tests_tools::mocks::gen_empty_block_v10_with_issuer_and_pow_min as gen_empty_block; +use dubp_common_doc::BlockNumber; +use durs_bc_db_writer::blocks::insert_new_head_block; +use durs_bc_db_writer::{Db, DbError}; +use durs_wot::WotId; +use tempfile::tempdir; + +mod common; + +const INITIAL_POW_MIN: usize = 70; + +fn insert_250_blocks_with_distinct_issuers(db: &Db) -> Result<(), DbError> { + for i in 0..250 { + let byte = (i % 255) as u8; + let pubkey = dup_crypto_tests_tools::mocks::pubkey_from_byte(byte); + let block = BlockDocument::V10(gen_empty_block( + BlockNumber(i as u32), + pubkey, + INITIAL_POW_MIN + (i % 25), + )); + db.write(|mut w| { + common::insert_wot_index_entry(&db, &mut w, WotId(i), pubkey)?; + insert_new_head_block(&db, &mut w, None, common::to_db_block(block))?; + Ok(w) + })?; + } + Ok(()) +} + +fn criterion_benchmark(c: &mut Criterion) { + // Open temporary database + let db = durs_bc_db_writer::open_db(tempdir().expect("Fail open tmp db").path()) + .expect("Fail open tmp db"); + + c.bench_function("insert 250 blocks", |b| { + b.iter(|| insert_250_blocks_with_distinct_issuers(black_box(&db))) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks.rs b/lib/modules/blockchain/bc-db-writer/src/blocks.rs index 6b6f8d3710e71eed6046de91c27b730f5371be18..7757e50c91618393bb5e5edc04f90507894e2543 100644 --- a/lib/modules/blockchain/bc-db-writer/src/blocks.rs +++ b/lib/modules/blockchain/bc-db-writer/src/blocks.rs @@ -27,7 +27,6 @@ use durs_bc_db_reader::DbValue; use unwrap::unwrap; /// Insert new head Block in databases -/// Update MAIN_BLOCK only pub fn insert_new_head_block( db: &Db, w: &mut DbWriter, @@ -47,6 +46,12 @@ pub fn insert_new_head_block( &Db::db_value(&bin_dal_block)?, )?; + // Update current meta datas + crate::current_meta_datas::update_current_meta_datas(db, w, &dal_block.block)?; + + // Update stores linked to MAIN_BLOCKS + crate::store_name::update_store_name(db, w, &dal_block.block)?; + if let Some(fork_tree) = fork_tree { // Insert head block in fork tree let removed_blockstamps = diff --git a/lib/modules/blockchain/bc-db-writer/src/current_meta_datas.rs b/lib/modules/blockchain/bc-db-writer/src/current_meta_datas.rs index 0ec353e3041fd8ec760e931013ed828e35bd6732..75df9398c4ccff32d409c93071c43c2adf4a28bf 100644 --- a/lib/modules/blockchain/bc-db-writer/src/current_meta_datas.rs +++ b/lib/modules/blockchain/bc-db-writer/src/current_meta_datas.rs @@ -24,7 +24,7 @@ use durs_bc_db_reader::current_meta_datas::CurrentMetaDataKey; use durs_bc_db_reader::DbValue; /// Update CURRENT_META_DATAS -pub fn update_current_meta_datas( +pub(crate) fn update_current_meta_datas( db: &Db, w: &mut DbWriter, new_current_block: &BlockDocument, diff --git a/lib/modules/blockchain/bc-db-writer/src/lib.rs b/lib/modules/blockchain/bc-db-writer/src/lib.rs index 6e84fd0e815be172d508e8b18d3cc972cf4893c2..282e4e5f4542d17029ddf6a65671da9c5133e807 100644 --- a/lib/modules/blockchain/bc-db-writer/src/lib.rs +++ b/lib/modules/blockchain/bc-db-writer/src/lib.rs @@ -33,6 +33,7 @@ extern crate log; pub mod blocks; pub mod current_meta_datas; pub mod indexes; +pub mod store_name; pub mod writers; pub use durs_dbs_tools::kv_db::{ diff --git a/lib/modules/blockchain/bc-db-writer/src/store_name.rs b/lib/modules/blockchain/bc-db-writer/src/store_name.rs new file mode 100644 index 0000000000000000000000000000000000000000..dee6273e17f5dfe9e3b9bfe80d974fcb3138536b --- /dev/null +++ b/lib/modules/blockchain/bc-db-writer/src/store_name.rs @@ -0,0 +1,29 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! "store name" storage: define write requests. + +use crate::{Db, DbWriter}; +use dubp_block_doc::BlockDocument; +use durs_dbs_tools::DbError; + +pub(crate) fn update_store_name( + _db: &Db, + _w: &mut DbWriter, + _new_current_block: &BlockDocument, +) -> Result<(), DbError> { + //unimplemented!() + Ok(()) +} diff --git a/lib/modules/blockchain/bc-db-writer/src/writers/requests.rs b/lib/modules/blockchain/bc-db-writer/src/writers/requests.rs index 045dac7777a40e71a8a076e98c012437df0f4895..fcbd528da38d0f7c178674547fda3c1bae5b149d 100644 --- a/lib/modules/blockchain/bc-db-writer/src/writers/requests.rs +++ b/lib/modules/blockchain/bc-db-writer/src/writers/requests.rs @@ -67,7 +67,6 @@ impl BlocksDBsWriteQuery { BlocksDBsWriteQuery::WriteBlock(dal_block) => { let dal_block: DbBlock = dal_block; trace!("BlocksDBsWriteQuery::WriteBlock..."); - crate::current_meta_datas::update_current_meta_datas(db, w, &dal_block.block)?; if sync_target.is_none() || dal_block.blockstamp().id.0 + fork_window_size as u32 >= sync_target.expect("safe unwrap").id.0 diff --git a/lib/modules/blockchain/bc-db-writer/tests/common/mod.rs b/lib/modules/blockchain/bc-db-writer/tests/common/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..2aa956c519ebac47560d0308702f15ba3867ba9f --- /dev/null +++ b/lib/modules/blockchain/bc-db-writer/tests/common/mod.rs @@ -0,0 +1,52 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use dubp_block_doc::BlockDocument; +use dup_crypto::keys::{PubKey, PublicKey}; +use durs_bc_db_reader::blocks::DbBlock; +use durs_bc_db_reader::constants::*; +use durs_bc_db_reader::{DbReadable, DbValue}; +use durs_bc_db_writer::DbWriter; +use durs_bc_db_writer::{Db, DbError}; +use durs_wot::WotId; +use tempfile::tempdir; + +#[inline] +/// Open database in an arbitrary temporary directory given by OS +/// and automatically cleaned when `Db` is dropped +pub fn open_tmp_db() -> Result<Db, DbError> { + durs_bc_db_writer::open_db(tempdir().map_err(DbError::FileSystemError)?.path()) +} + +pub fn to_db_block(block: BlockDocument) -> DbBlock { + DbBlock { + block, + expire_certs: None, + } +} + +pub fn insert_wot_index_entry( + db: &Db, + w: &mut DbWriter, + wot_id: WotId, + pubkey: PubKey, +) -> Result<(), DbError> { + db.get_store(WOT_ID_INDEX).put( + w.as_mut(), + &pubkey.to_bytes_vector(), + &DbValue::U64(wot_id.0 as u64), + )?; + Ok(()) +} diff --git a/lib/modules/blockchain/bc-db-writer/tests/current_frame.rs b/lib/modules/blockchain/bc-db-writer/tests/current_frame.rs new file mode 100644 index 0000000000000000000000000000000000000000..a3daa223d5ec259bec769c5c9b560cf32585b3e9 --- /dev/null +++ b/lib/modules/blockchain/bc-db-writer/tests/current_frame.rs @@ -0,0 +1,185 @@ +// Copyright (C) 2017-2019 The AXIOM TEAM Association. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use dubp_block_doc::BlockDocument; +use dubp_blocks_tests_tools::mocks::gen_empty_block_v10_with_issuer_and_pow_min as gen_empty_block; +use dubp_common_doc::BlockNumber; +use dup_crypto_tests_tools::mocks::pubkey; +use durs_bc_db_reader::current_frame::*; +use durs_bc_db_reader::DbReadable; +use durs_bc_db_writer::blocks::insert_new_head_block; +use durs_bc_db_writer::DbError; +use durs_wot::WotId; + +mod common; + +const INITIAL_POW_MIN: usize = 70; + +//#[ignore] +#[test] +fn test_current_frame() -> Result<(), DbError> { + // Open temporary database + let db = common::open_tmp_db()?; + + // Create and insert fake wot index + let issuer_a = pubkey('A'); + db.write(|mut w| { + common::insert_wot_index_entry(&db, &mut w, WotId(0), issuer_a)?; + Ok(w) + })?; + + // Insert genesis block + let genesis_block = + BlockDocument::V10(gen_empty_block(BlockNumber(0), issuer_a, INITIAL_POW_MIN)); + db.write(|mut w| { + insert_new_head_block(&db, &mut w, None, common::to_db_block(genesis_block))?; + Ok(w) + })?; + + // Verify current frame #0 + let current_frame = get_current_frame(&db)?; + assert_eq!(1, current_frame.len()); + assert_eq!( + ( + WotId(0), + MemberInCurrentFrame { + forged_blocks: 1, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 0, + }, + } + ), + current_frame[0] + ); + + // Insert block #1 + let block_1 = BlockDocument::V10(gen_empty_block( + BlockNumber(1), + issuer_a, + INITIAL_POW_MIN + 1, + )); + db.write(|mut w| { + insert_new_head_block(&db, &mut w, None, common::to_db_block(block_1))?; + Ok(w) + })?; + + // Verify current frame #1 + let current_frame = get_current_frame(&db)?; + assert_eq!(2, current_frame.len()); + assert_eq!( + ( + WotId(0), + MemberInCurrentFrame { + forged_blocks: 2, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 0, + }, + } + ), + current_frame[0] + ); + assert_eq!( + PersonalDifficulty { + exclusion_factor: 1, + handicap: 2, + }, + db.read(|r| get_member_diffi(&db, r, WotId(0)))? + ); + + // Insert block #2 + let issuer_b = pubkey('B'); + let block_2 = BlockDocument::V10(gen_empty_block( + BlockNumber(2), + issuer_b, + INITIAL_POW_MIN + 2, + )); + db.write(|mut w| { + insert_new_head_block(&db, &mut w, None, common::to_db_block(block_2))?; + Ok(w) + })?; + // Verify current frame #2 + let current_frame = get_current_frame(&db)?; + assert_eq!(3, current_frame.len()); + assert_eq!( + ( + WotId(0), + MemberInCurrentFrame { + forged_blocks: 2, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 2, + }, + } + ), + current_frame[0] + ); + assert_eq!( + ( + WotId(1), + MemberInCurrentFrame { + forged_blocks: 1, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 2, + }, + } + ), + current_frame[1] + ); + + // Insert block #3 + let block_3 = BlockDocument::V10(gen_empty_block( + BlockNumber(3), + issuer_b, + INITIAL_POW_MIN + 3, + )); + db.write(|mut w| { + insert_new_head_block(&db, &mut w, None, common::to_db_block(block_3))?; + Ok(w) + })?; + // Verify current frame #3 + let current_frame = get_current_frame(&db)?; + assert_eq!(4, current_frame.len()); + assert_eq!( + ( + WotId(0), + MemberInCurrentFrame { + forged_blocks: 2, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 2, + }, + } + ), + current_frame[0] + ); + assert_eq!( + ( + WotId(1), + MemberInCurrentFrame { + forged_blocks: 2, + difficulty: PersonalDifficulty { + exclusion_factor: 1, + handicap: 2, + }, + } + ), + current_frame[1] + ); + + Ok(()) +} diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs index be5d5a91624de86d9de898795b4658492af70ca8..05f009ab6e9f1a28eaff396074d223c2000a856b 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/mod.rs @@ -21,14 +21,19 @@ use crate::dubp::BlockError; use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait}; use dubp_common_doc::traits::Document; use dubp_common_doc::BlockNumber; +use dup_crypto::hashs::Hash; use dup_crypto::keys::PubKey; use durs_bc_db_reader::DbReader; use durs_bc_db_writer::*; use durs_wot::*; use std::collections::HashMap; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub enum InvalidBlockError { + _InvalidPow { + expected_pattern: String, + block_hash: Hash, + }, NoPreviousBlock, VersionDecrease, } @@ -68,3 +73,13 @@ where Ok(()) } + +fn _verify_pow<DB: DbReadable, R: DbReader>( + _db: &DB, + _r: &R, + _wot_id: WotId, + _block_hash: Hash, + _block_pow_min: u32, +) -> Result<(), InvalidBlockError> { + unimplemented!(); +} diff --git a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs index cf635c60ab72f37a80fabf91c65388a9ec149276..d01bc5cf501fa4ab641352bc8fbbe1bb4bda0f81 100644 --- a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs +++ b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs @@ -123,9 +123,6 @@ mod tests { // Insert mock blocks in forks_dbs db.write(|mut w| { for block in &main_branch { - durs_bc_db_writer::current_meta_datas::update_current_meta_datas( - &db, &mut w, &block, - )?; durs_bc_db_writer::blocks::insert_new_head_block( &db, &mut w, diff --git a/lib/tests-tools/bc-db-tests-tools/src/mocks.rs b/lib/tests-tools/bc-db-tests-tools/src/mocks.rs index b541a2bb014b7948e69f72452d6a3366793eb202..1f28ca62ce642fd2cb79b3c89db60fee00191f7f 100644 --- a/lib/tests-tools/bc-db-tests-tools/src/mocks.rs +++ b/lib/tests-tools/bc-db-tests-tools/src/mocks.rs @@ -19,7 +19,6 @@ use dubp_block_doc::BlockDocument; use durs_bc_db_reader::blocks::fork_tree::ForkTree; use durs_bc_db_reader::blocks::DbBlock; use durs_bc_db_writer::blocks::{insert_new_fork_block, insert_new_head_block}; -use durs_bc_db_writer::current_meta_datas::update_current_meta_datas; use durs_bc_db_writer::{Db, DbError}; /// Warning : This function does not update the indexes and considers @@ -32,7 +31,6 @@ pub fn insert_main_block( fork_tree: Option<&mut ForkTree>, ) -> Result<(), DbError> { db_tmp.write(|mut w| { - update_current_meta_datas(db_tmp, &mut w, &block)?; insert_new_head_block( &db_tmp, &mut w, diff --git a/lib/tests-tools/blocks-tests-tools/src/mocks.rs b/lib/tests-tools/blocks-tests-tools/src/mocks.rs index 12a82525ae08f220e7536cd5da7b2e99c160ed73..63677f0bc7e439b9477d72a008d631af898c9de9 100644 --- a/lib/tests-tools/blocks-tests-tools/src/mocks.rs +++ b/lib/tests-tools/blocks-tests-tools/src/mocks.rs @@ -20,6 +20,7 @@ use dubp_common_doc::blockstamp::Blockstamp; use dubp_common_doc::{BlockHash, BlockNumber}; use dubp_currency_params::CurrencyName; use dup_crypto::hashs::Hash; +use dup_crypto::keys::PubKey; /// Generate n mock blockstamps pub fn generate_blockstamps(n: usize) -> Vec<Blockstamp> { @@ -55,6 +56,18 @@ pub fn gen_empty_timed_blocks_v10(n: usize, time_step: u64) -> Vec<BlockDocument .collect() } +/// Generate empty block v10 with issuer and pow_min +pub fn gen_empty_block_v10_with_issuer_and_pow_min( + block_number: BlockNumber, + issuer: PubKey, + pow_min: usize, +) -> BlockDocumentV10 { + let mut block = empty_block_v10(block_number); + block.issuers = vec![issuer]; + block.pow_min = pow_min; + block +} + /// Generate empty timed block document /// (usefull for tests that only need blockstamp and median_time fields) pub fn gen_empty_timed_block_v10( @@ -62,13 +75,21 @@ pub fn gen_empty_timed_block_v10( time: u64, previous_hash: Hash, ) -> BlockDocumentV10 { + let mut block = empty_block_v10(blockstamp.id); + block.median_time = time; + block.hash = Some(blockstamp.hash); + block.previous_hash = Some(previous_hash); + block +} + +fn empty_block_v10(block_number: BlockNumber) -> BlockDocumentV10 { BlockDocumentV10 { version: 10, nonce: 0, - number: blockstamp.id, + number: block_number, pow_min: 0, time: 0, - median_time: time, + median_time: 0, members_count: 0, monetary_mass: 0, unit_base: 0, @@ -78,9 +99,9 @@ pub fn gen_empty_timed_block_v10( currency: CurrencyName("test_currency".to_owned()), issuers: vec![], signatures: vec![], - hash: Some(blockstamp.hash), + hash: Some(BlockHash(Hash::default())), parameters: None, - previous_hash: Some(previous_hash), + previous_hash: None, previous_issuer: None, dividend: None, identities: vec![], diff --git a/lib/tests-tools/crypto-tests-tools/src/mocks/mod.rs b/lib/tests-tools/crypto-tests-tools/src/mocks/mod.rs index a517b49f510b004150b62a35a2c056c04b7b4b97..93b92d00963b44c5e5fdbd7756c469b740100517 100644 --- a/lib/tests-tools/crypto-tests-tools/src/mocks/mod.rs +++ b/lib/tests-tools/crypto-tests-tools/src/mocks/mod.rs @@ -43,3 +43,18 @@ pub fn pubkey(character: char) -> PubKey { ed25519::PublicKey::from_base58(&str_pub).expect("Fail to create mock pubkey !"), ) } + +/// Generate mock pubkey from one byte +pub fn pubkey_from_byte(byte: u8) -> PubKey { + let mut bytes = [0u8; 32]; + for b in &mut bytes { + *b = byte + } + + PubKey::Ed25519(ed25519::PublicKey(bytes)) +} + +/// Generate n mock pubkeys +pub fn generate_pubkeys(n: usize) -> Vec<PubKey> { + (0..n).map(|i| pubkey_from_byte((i % 255) as u8)).collect() +}