diff --git a/Cargo.lock b/Cargo.lock index 3ea57d5a4a3e2d4681b24ba4172b73e85d63f0ee..aac36f52123c3ca263cc09d9d59286d8c83ea43b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,22 @@ dependencies = [ "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bc-db-tests-tools" +version = "0.1.0" +dependencies = [ + "dubp-block-doc 0.1.0", + "dubp-blocks-tests-tools 0.1.0", + "dubp-common-doc 0.1.0", + "dubp-currency-params 0.2.0", + "dubp-user-docs 0.14.0", + "dup-crypto 0.7.0", + "dup-crypto-tests-tools 0.1.0", + "durs-bc-db-reader 0.3.0-dev", + "durs-bc-db-writer 0.3.0-dev", + "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bincode" version = "1.0.1" @@ -371,6 +387,19 @@ dependencies = [ "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dubp-blocks-tests-tools" +version = "0.1.0" +dependencies = [ + "dubp-block-doc 0.1.0", + "dubp-common-doc 0.1.0", + "dubp-currency-params 0.2.0", + "dubp-user-docs 0.14.0", + "dubp-user-docs-tests-tools 0.1.0", + "dup-crypto 0.7.0", + "dup-crypto-tests-tools 0.1.0", +] + [[package]] name = "dubp-common-doc" version = "0.1.0" @@ -440,7 +469,6 @@ dependencies = [ name = "dubp-user-docs-tests-tools" version = "0.1.0" dependencies = [ - "dubp-block-doc 0.1.0", "dubp-common-doc 0.1.0", "dubp-currency-params 0.2.0", "dubp-user-docs 0.14.0", @@ -494,6 +522,7 @@ name = "durs-bc-db-reader" version = "0.3.0-dev" dependencies = [ "dubp-block-doc 0.1.0", + "dubp-blocks-tests-tools 0.1.0", "dubp-common-doc 0.1.0", "dubp-currency-params 0.2.0", "dubp-indexes 0.1.0", @@ -520,6 +549,7 @@ name = "durs-bc-db-writer" version = "0.3.0-dev" dependencies = [ "dubp-block-doc 0.1.0", + "dubp-blocks-tests-tools 0.1.0", "dubp-common-doc 0.1.0", "dubp-currency-params 0.2.0", "dubp-indexes 0.1.0", @@ -548,6 +578,7 @@ version = "0.3.0-dev" dependencies = [ "dirs 1.0.5 (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", "dubp-currency-params 0.2.0", "dubp-user-docs 0.14.0", diff --git a/Cargo.toml b/Cargo.toml index 6298c974568d0ccc30b4821176069c26e34a5444..078c9139021e40696bfac6f6d08688d6cce9bf72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ members = [ "lib/modules/ws2p-v1-legacy", "lib/modules/ws2p/ws2p", "lib/modules/ws2p/ws2p-protocol", + "lib/tests-tools/bc-db-tests-tools", + "lib/tests-tools/blocks-tests-tools", "lib/tests-tools/crypto-tests-tools", "lib/tests-tools/user-docs-tests-tools", "lib/tests-tools/common-tests-tools", diff --git a/lib/modules-lib/bc-db-reader/Cargo.toml b/lib/modules-lib/bc-db-reader/Cargo.toml index 20bbab46c05a8d09158222f415d5e32caee36e27..e23d3c150523ed07643a7c6e95209790aedb2f7d 100644 --- a/lib/modules-lib/bc-db-reader/Cargo.toml +++ b/lib/modules-lib/bc-db-reader/Cargo.toml @@ -30,6 +30,7 @@ unwrap = "1.2.1" [dev-dependencies] dup-crypto-tests-tools = { path = "../../tests-tools/crypto-tests-tools" } dubp-user-docs-tests-tools = { path = "../../tests-tools/user-docs-tests-tools" } +dubp-blocks-tests-tools = { path = "../../tests-tools/blocks-tests-tools" } durs-common-tests-tools = { path = "../../tests-tools/common-tests-tools" } tempfile = "3.1.0" diff --git a/lib/modules-lib/bc-db-reader/src/blocks/fork_tree.rs b/lib/modules-lib/bc-db-reader/src/blocks/fork_tree.rs index 391f228c948e2beda0f777252e7722a8d510355a..87b1cb148cfb6ae8657a88585387041653bb0ee1 100644 --- a/lib/modules-lib/bc-db-reader/src/blocks/fork_tree.rs +++ b/lib/modules-lib/bc-db-reader/src/blocks/fork_tree.rs @@ -584,8 +584,7 @@ mod tests { fn insert_fork_blocks() { // Fill tree with 10 nodes let mut tree = ForkTree::default(); - let blockstamps: Vec<Blockstamp> = - dubp_user_docs_tests_tools::mocks::generate_blockstamps(10); + let blockstamps: Vec<Blockstamp> = dubp_blocks_tests_tools::mocks::generate_blockstamps(10); tree.insert_new_node(blockstamps[0], None, true); for i in 1..10 { tree.insert_new_node(blockstamps[i], Some(TreeNodeId(i - 1)), true); @@ -672,7 +671,7 @@ mod tests { fn insert_more_fork_window_size_nodes() { let mut tree = ForkTree::default(); let blockstamps: Vec<Blockstamp> = - dubp_user_docs_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); + dubp_blocks_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); // Fill tree with MAX_DEPTH nodes tree.insert_new_node(blockstamps[0], None, true); @@ -709,7 +708,7 @@ mod tests { fn test_change_main_branch() { let mut tree = ForkTree::default(); let blockstamps: Vec<Blockstamp> = - dubp_user_docs_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); + dubp_blocks_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); // Fill tree with MAX_DEPTH nodes tree.insert_new_node(blockstamps[0], None, true); diff --git a/lib/modules/blockchain/bc-db-writer/Cargo.toml b/lib/modules/blockchain/bc-db-writer/Cargo.toml index 4de7c2e9656d4ee7be66f1a79955fde8009e27a4..7b7ded536a453decea18df0673c328ddb5688d5d 100644 --- a/lib/modules/blockchain/bc-db-writer/Cargo.toml +++ b/lib/modules/blockchain/bc-db-writer/Cargo.toml @@ -30,6 +30,7 @@ unwrap = "1.2.1" [dev-dependencies] 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" diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks.rs b/lib/modules/blockchain/bc-db-writer/src/blocks.rs index 38246e68920bc205310afa870ce213bfd7ffb261..0cb83ff060fcf40300b0ef0ace5a58b817733e94 100644 --- a/lib/modules/blockchain/bc-db-writer/src/blocks.rs +++ b/lib/modules/blockchain/bc-db-writer/src/blocks.rs @@ -29,6 +29,7 @@ use durs_bc_db_reader::DbValue; use unwrap::unwrap; /// Insert new head Block in databases +/// Update MAIN_BLOCK and CURRENT_META_DATAS pub fn insert_new_head_block( db: &Db, w: &mut DbWriter, diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs b/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs index bb058054dba5ee5085fba0e4e7c93d7c164fd7f7..019eefa3642dac2c5f65d106de0c4528d10176e9 100644 --- a/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs +++ b/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs @@ -103,7 +103,7 @@ mod test { fn test_insert_new_head_block() -> Result<(), DbError> { // Create mock datas let blockstamps = - dubp_user_docs_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); + dubp_blocks_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); let mut fork_tree = ForkTree::default(); // Insert genesis block @@ -154,7 +154,7 @@ mod test { fn test_insert_new_fork_block() -> Result<(), DbError> { // Create mock datas let blockstamps = - dubp_user_docs_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 3); + dubp_blocks_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 3); let mut fork_tree = ForkTree::default(); // Insert 4 main blocks diff --git a/lib/modules/blockchain/blockchain/Cargo.toml b/lib/modules/blockchain/blockchain/Cargo.toml index 8d494dbb4129f0cb04268fa43955b44aa8faea8c..0e4667d0cf5f864c3ab5cc6f50a33965db01493b 100644 --- a/lib/modules/blockchain/blockchain/Cargo.toml +++ b/lib/modules/blockchain/blockchain/Cargo.toml @@ -41,4 +41,5 @@ unwrap = "1.2.1" [dev-dependencies] dup-crypto-tests-tools = { path = "../../../tests-tools/crypto-tests-tools" } dubp-user-docs-tests-tools = { path = "../../../tests-tools/user-docs-tests-tools" } +dubp-blocks-tests-tools = { path = "../../../tests-tools/blocks-tests-tools" } tempfile = "3.1.0" diff --git a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs index 645fef8c71c72be37795b81d3bc2df972783895b..d01bc5cf501fa4ab641352bc8fbbe1bb4bda0f81 100644 --- a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs +++ b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs @@ -118,10 +118,7 @@ mod tests { // Generate `FORK_WINDOW_SIZE + 2` mock blocks let main_branch: Vec<BlockDocument> = - dubp_user_docs_tests_tools::mocks::gen_empty_timed_blocks_v10( - fork_window_size + 2, - 0u64, - ); + dubp_blocks_tests_tools::mocks::gen_empty_timed_blocks_v10(fork_window_size + 2, 0u64); // Insert mock blocks in forks_dbs db.write(|mut w| { @@ -160,20 +157,18 @@ mod tests { let fork_point = &main_branch[main_branch.len() - 2]; let fork_blocks: Vec<BlockDocument> = (0..3) .map(|i| { - BlockDocument::V10( - dubp_user_docs_tests_tools::mocks::gen_empty_timed_block_v10( - Blockstamp { - id: BlockNumber(fork_point.number().0 + i + 1), - hash: BlockHash(dup_crypto_tests_tools::mocks::hash('A')), - }, - ADVANCE_TIME - 1, - if i == 0 { - fork_point.hash().expect("safe unwrap").0 - } else { - dup_crypto_tests_tools::mocks::hash('A') - }, - ), - ) + BlockDocument::V10(dubp_blocks_tests_tools::mocks::gen_empty_timed_block_v10( + Blockstamp { + id: BlockNumber(fork_point.number().0 + i + 1), + hash: BlockHash(dup_crypto_tests_tools::mocks::hash('A')), + }, + ADVANCE_TIME - 1, + if i == 0 { + fork_point.hash().expect("safe unwrap").0 + } else { + dup_crypto_tests_tools::mocks::hash('A') + }, + )) }) .collect(); @@ -208,7 +203,7 @@ mod tests { &mut fork_tree, DbBlock { block: BlockDocument::V10( - dubp_user_docs_tests_tools::mocks::gen_empty_timed_block_v10( + dubp_blocks_tests_tools::mocks::gen_empty_timed_block_v10( determining_blockstamp, *ADVANCE_TIME, dup_crypto_tests_tools::mocks::hash('A'), @@ -243,20 +238,18 @@ mod tests { // The old main branch catches up and overlaps with the fork let new_main_blocks: Vec<BlockDocument> = (0..7) .map(|i| { - BlockDocument::V10( - dubp_user_docs_tests_tools::mocks::gen_empty_timed_block_v10( - Blockstamp { - id: BlockNumber(fork_point.number().0 + i + 1), - hash: BlockHash(dup_crypto_tests_tools::mocks::hash('B')), - }, - ADVANCE_TIME * 2, - if i == 0 { - fork_point.hash().expect("safe unwrap").0 - } else { - dup_crypto_tests_tools::mocks::hash('B') - }, - ), - ) + BlockDocument::V10(dubp_blocks_tests_tools::mocks::gen_empty_timed_block_v10( + Blockstamp { + id: BlockNumber(fork_point.number().0 + i + 1), + hash: BlockHash(dup_crypto_tests_tools::mocks::hash('B')), + }, + ADVANCE_TIME * 2, + if i == 0 { + fork_point.hash().expect("safe unwrap").0 + } else { + dup_crypto_tests_tools::mocks::hash('B') + }, + )) }) .collect(); insert_fork_blocks(&db, &mut fork_tree, &new_main_blocks)?; diff --git a/lib/tests-tools/bc-db-tests-tools/Cargo.toml b/lib/tests-tools/bc-db-tests-tools/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..97ad6bd5b93bf3ce38d71e6dd7dd915fd8112a61 --- /dev/null +++ b/lib/tests-tools/bc-db-tests-tools/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "bc-db-tests-tools" +version = "0.1.0" +authors = ["elois <elois@duniter.org>"] +description = "Blockchain DB tests tools for Dunitrust." +repository = "https://git.duniter.org/nodes/rust/duniter-rs" +readme = "README.md" +keywords = ["dunitrust", "test", "tools"] +license = "AGPL-3.0" +edition = "2018" + +[lib] +path = "src/lib.rs" + +[dependencies] +dubp-block-doc = { path = "../../dubp/block-doc"} #, version = "0.1.0" } +dubp-common-doc = { path = "../../dubp/common-doc"} #, version = "0.1.0" } +dubp-currency-params = { path = "../../dubp/currency-params" } +dubp-user-docs = { path = "../../dubp/user-docs" } +dup-crypto = { path = "../../crypto" } +durs-bc-db-reader = { path = "../../modules-lib/bc-db-reader" } +durs-bc-db-writer = { path = "../../modules/blockchain/bc-db-writer" } +dup-crypto-tests-tools = { path = "../crypto-tests-tools" } +dubp-blocks-tests-tools = { path = "../blocks-tests-tools" } +tempfile = "3.1.0" + +[dev-dependencies] \ No newline at end of file diff --git a/lib/tests-tools/bc-db-tests-tools/src/lib.rs b/lib/tests-tools/bc-db-tests-tools/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..d48144441ec975a11b78a365b982398be433d7c4 --- /dev/null +++ b/lib/tests-tools/bc-db-tests-tools/src/lib.rs @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// 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/>. + +//! Crypto tests tools for projects use dup-crypto. + +#![deny( + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces +)] + +pub mod mocks; + +use durs_bc_db_writer::{Db, DbError}; +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> { + Db::open_db( + tempdir().map_err(DbError::FileSystemError)?.path(), + &durs_bc_db_reader::bc_db_schema(), + ) +} diff --git a/lib/tests-tools/bc-db-tests-tools/src/mocks.rs b/lib/tests-tools/bc-db-tests-tools/src/mocks.rs new file mode 100644 index 0000000000000000000000000000000000000000..1f28ca62ce642fd2cb79b3c89db60fee00191f7f --- /dev/null +++ b/lib/tests-tools/bc-db-tests-tools/src/mocks.rs @@ -0,0 +1,67 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// 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/>. + +//! Mocks for Dunitrust Blockchain DB + +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::{Db, DbError}; + +/// Warning : This function does not update the indexes and considers +/// that your block is valid (so chainable on the main chain). +/// To be used only for tests that do not use indexes. +/// To insert a fork block, use `insert_fork_block` instead. +pub fn insert_main_block( + db_tmp: &Db, + block: BlockDocument, + fork_tree: Option<&mut ForkTree>, +) -> Result<(), DbError> { + db_tmp.write(|mut w| { + insert_new_head_block( + &db_tmp, + &mut w, + fork_tree, + DbBlock { + block, + expire_certs: None, + }, + )?; + Ok(w) + }) +} + +/// Insert fork block +pub fn insert_fork_block( + db_tmp: &Db, + fork_tree: &mut ForkTree, + block: BlockDocument, +) -> Result<bool, DbError> { + let mut orphan = false; + db_tmp.write(|mut w| { + orphan = !insert_new_fork_block( + db_tmp, + &mut w, + fork_tree, + DbBlock { + block, + expire_certs: None, + }, + )?; + Ok(w) + })?; + Ok(orphan) +} diff --git a/lib/tests-tools/blocks-tests-tools/Cargo.toml b/lib/tests-tools/blocks-tests-tools/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..67547dd71bc82a41f88f01e238e3570b66c85810 --- /dev/null +++ b/lib/tests-tools/blocks-tests-tools/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "dubp-blocks-tests-tools" +version = "0.1.0" +authors = ["elois <elois@duniter.org>"] +description = "Tests tools for projects use dubp-block-doc." +repository = "https://git.duniter.org/nodes/rust/duniter-rs" +readme = "README.md" +keywords = ["rust", "test", "tools"] +license = "AGPL-3.0" +edition = "2018" + +[lib] +path = "src/lib.rs" + +[dependencies] +dubp-block-doc = { path = "../../dubp/block-doc"} #, version = "0.1.0" } +dubp-common-doc = { path = "../../dubp/common-doc"} #, version = "0.1.0" } +dubp-currency-params = { path = "../../dubp/currency-params" } +dubp-user-docs = { path = "../../dubp/user-docs" } +dup-crypto = { path = "../../crypto" } +dup-crypto-tests-tools = { path = "../crypto-tests-tools" } +dubp-user-docs-tests-tools = { path = "../user-docs-tests-tools" } + +[dev-dependencies] diff --git a/lib/tests-tools/blocks-tests-tools/src/lib.rs b/lib/tests-tools/blocks-tests-tools/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..d2daaa74f8e2a9d5a2793da65e8119da1c636536 --- /dev/null +++ b/lib/tests-tools/blocks-tests-tools/src/lib.rs @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// 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/>. + +//! Tests tools for projects use dubp-block-doc. + +#![deny( + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces +)] + +pub mod mocks; diff --git a/lib/tests-tools/blocks-tests-tools/src/mocks.rs b/lib/tests-tools/blocks-tests-tools/src/mocks.rs new file mode 100644 index 0000000000000000000000000000000000000000..12a82525ae08f220e7536cd5da7b2e99c160ed73 --- /dev/null +++ b/lib/tests-tools/blocks-tests-tools/src/mocks.rs @@ -0,0 +1,96 @@ +// Copyright (C) 2019 Éloïs SANCHEZ +// +// 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/>. + +//! Mocks for projects use dubp-block-doc + +use dubp_block_doc::{BlockDocument, BlockDocumentV10}; +use dubp_common_doc::blockstamp::Blockstamp; +use dubp_common_doc::{BlockHash, BlockNumber}; +use dubp_currency_params::CurrencyName; +use dup_crypto::hashs::Hash; + +/// Generate n mock blockstamps +pub fn generate_blockstamps(n: usize) -> Vec<Blockstamp> { + (0..n) + .map(|i| Blockstamp { + id: BlockNumber(i as u32), + hash: BlockHash(dup_crypto_tests_tools::mocks::hash_from_byte( + (i % 255) as u8, + )), + }) + .collect() +} + +/// Generate n empty timed block document +pub fn gen_empty_timed_blocks_v10(n: usize, time_step: u64) -> Vec<BlockDocument> { + (0..n) + .map(|i| { + BlockDocument::V10(gen_empty_timed_block_v10( + Blockstamp { + id: BlockNumber(i as u32), + hash: BlockHash(dup_crypto_tests_tools::mocks::hash_from_byte( + (i % 255) as u8, + )), + }, + time_step * n as u64, + if i == 0 { + Hash::default() + } else { + dup_crypto_tests_tools::mocks::hash_from_byte(((i - 1) % 255) as u8) + }, + )) + }) + .collect() +} + +/// Generate empty timed block document +/// (usefull for tests that only need blockstamp and median_time fields) +pub fn gen_empty_timed_block_v10( + blockstamp: Blockstamp, + time: u64, + previous_hash: Hash, +) -> BlockDocumentV10 { + BlockDocumentV10 { + version: 10, + nonce: 0, + number: blockstamp.id, + pow_min: 0, + time: 0, + median_time: time, + members_count: 0, + monetary_mass: 0, + unit_base: 0, + issuers_count: 0, + issuers_frame: 0, + issuers_frame_var: 0, + currency: CurrencyName("test_currency".to_owned()), + issuers: vec![], + signatures: vec![], + hash: Some(blockstamp.hash), + parameters: None, + previous_hash: Some(previous_hash), + previous_issuer: None, + dividend: None, + identities: vec![], + joiners: vec![], + actives: vec![], + leavers: vec![], + revoked: vec![], + excluded: vec![], + certifications: vec![], + transactions: vec![], + inner_hash: None, + } +} diff --git a/lib/tests-tools/user-docs-tests-tools/Cargo.toml b/lib/tests-tools/user-docs-tests-tools/Cargo.toml index 5906780fd37c1a1663874a7abbcb98ce41a64f90..a558a3edde3599ef080ed3fd991c76ae3c1175ca 100644 --- a/lib/tests-tools/user-docs-tests-tools/Cargo.toml +++ b/lib/tests-tools/user-docs-tests-tools/Cargo.toml @@ -13,7 +13,6 @@ edition = "2018" path = "src/lib.rs" [dependencies] -dubp-block-doc = { path = "../../dubp/block-doc"} #, version = "0.1.0" } dubp-common-doc = { path = "../../dubp/common-doc"} #, version = "0.1.0" } dubp-currency-params = { path = "../../dubp/currency-params" } dubp-user-docs = { path = "../../dubp/user-docs" } diff --git a/lib/tests-tools/user-docs-tests-tools/src/mocks/mod.rs b/lib/tests-tools/user-docs-tests-tools/src/mocks/mod.rs index 160cb73886dc8987439ce7b4ac9f75118ea12b82..0a0822e966a7a9883960a6f66efb25bf332d37a1 100644 --- a/lib/tests-tools/user-docs-tests-tools/src/mocks/mod.rs +++ b/lib/tests-tools/user-docs-tests-tools/src/mocks/mod.rs @@ -17,83 +17,3 @@ pub mod identity; pub mod tx; - -use dubp_block_doc::{BlockDocument, BlockDocumentV10}; -use dubp_common_doc::blockstamp::Blockstamp; -use dubp_common_doc::{BlockHash, BlockNumber}; -use dubp_currency_params::CurrencyName; -use dup_crypto::hashs::Hash; - -/// Generate n mock blockstamps -pub fn generate_blockstamps(n: usize) -> Vec<Blockstamp> { - (0..n) - .map(|i| Blockstamp { - id: BlockNumber(i as u32), - hash: BlockHash(dup_crypto_tests_tools::mocks::hash_from_byte( - (i % 255) as u8, - )), - }) - .collect() -} - -/// Generate n empty timed block document -pub fn gen_empty_timed_blocks_v10(n: usize, time_step: u64) -> Vec<BlockDocument> { - (0..n) - .map(|i| { - BlockDocument::V10(gen_empty_timed_block_v10( - Blockstamp { - id: BlockNumber(i as u32), - hash: BlockHash(dup_crypto_tests_tools::mocks::hash_from_byte( - (i % 255) as u8, - )), - }, - time_step * n as u64, - if i == 0 { - Hash::default() - } else { - dup_crypto_tests_tools::mocks::hash_from_byte(((i - 1) % 255) as u8) - }, - )) - }) - .collect() -} - -/// Generate empty timed block document -/// (usefull for tests that only need blockstamp and median_time fields) -pub fn gen_empty_timed_block_v10( - blockstamp: Blockstamp, - time: u64, - previous_hash: Hash, -) -> BlockDocumentV10 { - BlockDocumentV10 { - version: 10, - nonce: 0, - number: blockstamp.id, - pow_min: 0, - time: 0, - median_time: time, - members_count: 0, - monetary_mass: 0, - unit_base: 0, - issuers_count: 0, - issuers_frame: 0, - issuers_frame_var: 0, - currency: CurrencyName("test_currency".to_owned()), - issuers: vec![], - signatures: vec![], - hash: Some(blockstamp.hash), - parameters: None, - previous_hash: Some(previous_hash), - previous_issuer: None, - dividend: None, - identities: vec![], - joiners: vec![], - actives: vec![], - leavers: vec![], - revoked: vec![], - excluded: vec![], - certifications: vec![], - transactions: vec![], - inner_hash: None, - } -}