diff --git a/lib/core/core/src/lib.rs b/lib/core/core/src/lib.rs index 583a10f88b3b609f4d8be694db1a8b32fb36a843..2708250ccbde1c94760c8abcbabc87ed118c934c 100644 --- a/lib/core/core/src/lib.rs +++ b/lib/core/core/src/lib.rs @@ -319,24 +319,34 @@ impl DursCore<DuRsConf> { // Get profile path let profile_path = self.soft_meta_datas.profile_path; + // Define sync_opts + let sync_opts_opt = if let Some(ServerMode::Sync(sync_opts)) = self.server_command { + Some(sync_opts) + } else { + None + }; + + // Define cautious mode + let cautious_mode = if let Some(ref sync_opts) = sync_opts_opt { + sync_opts.cautious_mode + } else { + true + }; + // Instantiate blockchain module and load is conf let mut blockchain_module = BlockchainModule::load_blockchain_conf( bc_db, router_sender.clone(), profile_path, RequiredKeysContent::MemberKeyPair(None), + cautious_mode, ); info!("Success to load Blockchain module."); // Start blockchain module in thread let thread_builder = thread::Builder::new().name(BlockchainModule::name().0.into()); - let sync_opts = if let Some(ServerMode::Sync(opts)) = self.server_command { - Some(opts) - } else { - None - }; let blockchain_thread_handler = thread_builder - .spawn(move || blockchain_module.start_blockchain(&blockchain_receiver, sync_opts)) + .spawn(move || blockchain_module.start_blockchain(&blockchain_receiver, sync_opts_opt)) .expect("Fatal error: fail to spawn module main thread !"); // Wait until all modules threads are finished diff --git a/lib/modules/blockchain/blockchain/src/dubp.rs b/lib/modules/blockchain/blockchain/src/dubp.rs index 4150cc1cc20ce097c5c567051670796f324f509e..4b07984f1134c66546c94d46e3f904806bf4cb61 100644 --- a/lib/modules/blockchain/blockchain/src/dubp.rs +++ b/lib/modules/blockchain/blockchain/src/dubp.rs @@ -18,15 +18,13 @@ pub mod apply; pub mod check; -use crate::dubp::apply::{apply_valid_block, ApplyValidBlockError, ValidBlockApplyReqs}; -use crate::dubp::check::global::verify_global_validity_block; -use crate::dubp::check::local::verify_local_validity_block; -use crate::dubp::check::InvalidBlockError; +use crate::dubp::apply::{ApplyValidBlockError, WriteBlockQueries}; +use crate::dubp::check::CheckBlockError; use crate::BlockchainModule; use dubp_block_doc::block::BlockDocumentTrait; use dubp_block_doc::BlockDocument; use dubp_common_doc::traits::Document; -use dubp_common_doc::{BlockNumber, Blockstamp}; +use dubp_common_doc::BlockNumber; use durs_bc_db_reader::blocks::DbBlock; use durs_bc_db_reader::DbError; use durs_bc_db_writer::{BcDbRwWithWriter, Db, DbWriter}; @@ -34,7 +32,7 @@ use unwrap::unwrap; #[derive(Debug, Clone)] pub enum CheckAndApplyBlockReturn { - ValidMainBlock(ValidBlockApplyReqs), + ValidMainBlock(WriteBlockQueries), ForkBlock, OrphanBlock, } @@ -45,7 +43,7 @@ pub enum BlockError { ApplyValidBlockError(ApplyValidBlockError), BlockOrOutForkWindow, DbError(DbError), - InvalidBlock(InvalidBlockError), + InvalidBlock(CheckBlockError), } impl From<ApplyValidBlockError> for BlockError { @@ -60,8 +58,8 @@ impl From<DbError> for BlockError { } } -impl From<InvalidBlockError> for BlockError { - fn from(e: InvalidBlockError) -> Self { +impl From<CheckBlockError> for BlockError { + fn from(e: CheckBlockError) -> Self { Self::InvalidBlock(e) } } @@ -72,78 +70,63 @@ pub fn check_and_apply_block( w: &mut DbWriter, block_doc: BlockDocument, ) -> Result<CheckAndApplyBlockReturn, BlockError> { - // Get BlockDocument && check if already have block - let already_have_block = durs_bc_db_reader::blocks::already_have_block( + match check::check_block(bc, &BcDbRwWithWriter { db, w }, &block_doc)? { + check::BlockChainability::FullyValidAndChainableBLock => { + treat_chainable_block(bc, db, w, block_doc) + } + check::BlockChainability::LocalValidAndUnchainableBlock => { + treat_unchainable_block(bc, db, w, block_doc) + } + } +} + +fn treat_chainable_block( + bc: &mut BlockchainModule, + db: &Db, + w: &mut DbWriter, + block_doc: BlockDocument, +) -> Result<CheckAndApplyBlockReturn, BlockError> { + // Detect expire_certs + let blocks_expiring = Vec::with_capacity(0); // TODO + let expire_certs = durs_bc_db_reader::indexes::certs::find_expire_certs( &BcDbRwWithWriter { db, w }, - block_doc.blockstamp(), - block_doc.previous_hash(), + blocks_expiring, )?; - // Verify proof of work - // The case where the block has none hash is captured by verify_block_hashs below - if let Some(hash) = block_doc.hash() { - self::check::pow::verify_hash_pattern(hash.0, block_doc.pow_min()) - .map_err(InvalidBlockError::Pow)?; + // If we're in block genesis, get the currency parameters + if block_doc.number() == BlockNumber(0) { + // Open currency_params_db + let datas_path = durs_conf::get_datas_path(bc.profile_path.clone()); + // Get and write currency params + bc.currency_params = Some( + durs_bc_db_reader::currency_params::get_and_write_currency_params( + &datas_path, + &block_doc, + ), + ); } - // Verify block hashs - crate::dubp::check::hashs::verify_block_hashs(&block_doc).map_err(InvalidBlockError::Hashs)?; - - // Check block chainability - if (block_doc.number().0 == 0 && bc.current_blockstamp == Blockstamp::default()) - || (block_doc.number().0 == bc.current_blockstamp.id.0 + 1 - && unwrap!(block_doc.previous_hash()).to_string() - == bc.current_blockstamp.hash.0.to_string()) - { - debug!( - "check_and_apply_block: block {} chainable!", - block_doc.blockstamp() - ); + let write_block_queries: WriteBlockQueries = crate::dubp::apply::apply_valid_block( + db, + w, + block_doc, + &mut bc.wot_index, + &bc.wot_databases.wot_db, + &expire_certs, + )?; - // Local verification - verify_local_validity_block(&block_doc, bc.currency_params) - .map_err(InvalidBlockError::Local)?; - - // Detect expire_certs - let blocks_expiring = Vec::with_capacity(0); - let expire_certs = durs_bc_db_reader::indexes::certs::find_expire_certs( - &BcDbRwWithWriter { db, w }, - blocks_expiring, - )?; - - // Verify block validity (check all protocol rule, very long !) - verify_global_validity_block( - &block_doc, - &BcDbRwWithWriter { db, w }, - &bc.wot_index, - &bc.wot_databases.wot_db, - ) - .map_err(InvalidBlockError::Global)?; - - // If we're in block genesis, get the currency parameters - if block_doc.number() == BlockNumber(0) { - // Open currency_params_db - let datas_path = durs_conf::get_datas_path(bc.profile_path.clone()); - // Get and write currency params - bc.currency_params = Some( - durs_bc_db_reader::currency_params::get_and_write_currency_params( - &datas_path, - &block_doc, - ), - ); - } + Ok(CheckAndApplyBlockReturn::ValidMainBlock( + write_block_queries, + )) +} - Ok(CheckAndApplyBlockReturn::ValidMainBlock(apply_valid_block( - db, - w, - block_doc, - &mut bc.wot_index, - &bc.wot_databases.wot_db, - &expire_certs, - )?)) - } else if already_have_block { - Err(BlockError::AlreadyHaveBlock) - } else if block_doc.number().0 >= bc.current_blockstamp.id.0 +fn treat_unchainable_block( + bc: &mut BlockchainModule, + db: &Db, + w: &mut DbWriter, + block_doc: BlockDocument, +) -> Result<CheckAndApplyBlockReturn, BlockError> { + if block_doc.number().0 >= bc.current_blockstamp.id.0 || (bc.current_blockstamp.id.0 - block_doc.number().0) < unwrap!(bc.currency_params).fork_window_size as u32 { diff --git a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs index e30da6c8821f4cfaaa83fb3c8366199198c1e02a..517be653c0b993bb0422c3f7e770d4eac1f1e35e 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs @@ -32,7 +32,7 @@ use std::collections::{HashMap, HashSet}; #[derive(Debug, Clone)] /// Stores all queries to apply in database to "apply" the block -pub struct ValidBlockApplyReqs( +pub struct WriteBlockQueries( pub BlocksDBsWriteQuery, pub Vec<WotsDBsWriteQuery>, pub Vec<CurrencyDBsWriteQuery>, @@ -54,7 +54,7 @@ pub fn apply_valid_block<W: WebOfTrust>( wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinFreeStructDb<W>, expire_certs: &HashMap<(WotId, WotId), BlockNumber>, -) -> Result<ValidBlockApplyReqs, ApplyValidBlockError> { +) -> Result<WriteBlockQueries, ApplyValidBlockError> { match block { BlockDocument::V10(block_v10) => { apply_valid_block_v10(db, w, block_v10, wot_index, wot_db, expire_certs) @@ -69,7 +69,7 @@ pub fn apply_valid_block_v10<W: WebOfTrust>( wot_index: &mut HashMap<PubKey, WotId>, wot_db: &BinFreeStructDb<W>, expire_certs: &HashMap<(WotId, WotId), BlockNumber>, -) -> Result<ValidBlockApplyReqs, ApplyValidBlockError> { +) -> Result<WriteBlockQueries, ApplyValidBlockError> { trace!("apply_valid_block({})", block.blockstamp(),); let mut wot_dbs_requests = Vec::new(); let mut currency_dbs_requests = Vec::new(); @@ -287,7 +287,7 @@ pub fn apply_valid_block_v10<W: WebOfTrust>( expire_certs: Some(expire_certs.clone()), }; // Return DBs requests - Ok(ValidBlockApplyReqs( + Ok(WriteBlockQueries( BlocksDBsWriteQuery::WriteBlock(block_db), wot_dbs_requests, currency_dbs_requests, diff --git a/lib/modules/blockchain/blockchain/src/dubp/check.rs b/lib/modules/blockchain/blockchain/src/dubp/check.rs index 10b0ff01bc3da03e29a4963157840abb43005980..f9a0c14ecf8137dd699a4196364ef6c6cf859ed7 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check.rs @@ -20,10 +20,98 @@ pub mod hashs; pub mod local; pub mod pow; +use crate::dubp::BlockError; +use crate::BlockchainModule; +use dubp_block_doc::block::BlockDocumentTrait; +use dubp_block_doc::BlockDocument; +use dubp_common_doc::traits::Document; +use dubp_common_doc::Blockstamp; +use durs_bc_db_reader::BcDbInReadTx; +use durs_common_tools::traits::bool_ext::BoolExt; +use unwrap::unwrap; + #[derive(Debug)] -pub enum InvalidBlockError { +pub enum CheckBlockError { Global(global::GlobalVerifyBlockError), Hashs(dubp_block_doc::block::VerifyBlockHashError), Local(local::LocalVerifyBlockError), - Pow(pow::BlockPoWError), + Pow(pow::InvalidHashPattern), +} + +#[derive(Debug)] +pub enum BlockChainability { + FullyValidAndChainableBLock, + LocalValidAndUnchainableBlock, +} + +/// Check block validity +pub fn check_block<DB: BcDbInReadTx>( + bc: &mut BlockchainModule, + db: &DB, + block_doc: &BlockDocument, +) -> Result<BlockChainability, BlockError> { + let already_have_block; + if bc.cautious_mode { + // Check if we already have the block + // VERY IMPORTANT: there are cases where it's legitimate to check a block that we already have. + // For example, in case of rollback or application of orphan blocks that have become stackable. + // The fact of having already received the block is a cause of rejection only if the block is not chainable. + already_have_block = durs_bc_db_reader::blocks::already_have_block( + db, + block_doc.blockstamp(), + block_doc.previous_hash(), + ) + .map_err(BlockError::DbError)?; + + if already_have_block.not() { + // Verify proof of work + // The case where the block has none hash is captured by check_block_hashes below + if let Some(hash) = block_doc.hash() { + pow::verify_hash_pattern(hash.0, block_doc.pow_min()) + .map_err(CheckBlockError::Pow)?; + } + // Check block hashes. + crate::dubp::check::hashs::check_block_hashes(block_doc) + .map_err(CheckBlockError::Hashs)?; + } + } else { + // If we're not in cautious mode, we still need to check the block hashes. + crate::dubp::check::hashs::check_block_hashes(block_doc).map_err(CheckBlockError::Hashs)?; + already_have_block = false; + }; + + // Check block chainability + if (block_doc.number().0 == 0 && bc.current_blockstamp == Blockstamp::default()) + || (block_doc.number().0 == bc.current_blockstamp.id.0 + 1 + && unwrap!(block_doc.previous_hash()).to_string() + == bc.current_blockstamp.hash.0.to_string()) + { + debug!("check_block: block {} chainable!", block_doc.blockstamp()); + + if bc.cautious_mode { + // Local verification + local::verify_local_validity_block(block_doc, bc.currency_params) + .map_err(CheckBlockError::Local)?; + + // Verify block validity (check all protocol rule, very long !) + global::verify_global_validity_block( + block_doc, + db, + &bc.wot_index, + &bc.wot_databases.wot_db, + ) + .map_err(CheckBlockError::Global)?; + } + + Ok(BlockChainability::FullyValidAndChainableBLock) + } else { + // Check that we don't already have the block + already_have_block + .not() + .or_err(BlockError::AlreadyHaveBlock)?; + + // TODO check estimate pow_min + + Ok(BlockChainability::LocalValidAndUnchainableBlock) + } } diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/global.rs b/lib/modules/blockchain/blockchain/src/dubp/check/global.rs index 7e54357b0a0041e799e766cbc9fe501da375f80b..a3f1d357023a801a03c2d685eefbb460821c8576 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/global.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/global.rs @@ -67,5 +67,6 @@ where .or_err(GlobalVerifyBlockError::VersionDecrease)?; } + // TODO BR_G100 - issuerIsMember Ok(()) } diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/hashs.rs b/lib/modules/blockchain/blockchain/src/dubp/check/hashs.rs index f6b4673031e8249ac3cb0ee1b77d6fe727c5a761..55ee2a373c84ec5d498bff8487f32eb5181b2b28 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/hashs.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/hashs.rs @@ -18,7 +18,7 @@ use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait, VerifyBlockHashError}; /// Verify block hashs -pub fn verify_block_hashs(block_doc: &BlockDocument) -> Result<(), VerifyBlockHashError> { +pub fn check_block_hashes(block_doc: &BlockDocument) -> Result<(), VerifyBlockHashError> { trace!("complete_block #{}...", block_doc.number()); match block_doc.verify_inner_hash() { diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/local.rs b/lib/modules/blockchain/blockchain/src/dubp/check/local.rs index 3178bb2641f6d8608f60bf9168a15518f67a78c5..f79b90bc872769ea99d7511ea25c5a8e0fd8a5fe 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/local.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/local.rs @@ -29,7 +29,6 @@ use dubp_common_doc::errors::DocumentSigsErr; use dubp_common_doc::traits::Document; use dubp_common_doc::BlockNumber; use dubp_currency_params::CurrencyParameters; -use durs_common_tools::fatal_error; #[derive(Debug, PartialEq)] /// Local verification of a block error @@ -59,6 +58,8 @@ pub enum LocalVerifyBlockError { TooManyIssuers, /// Transaction Document Error TransactionDocumentError(TransactionDocumentError), + /// Receive not genesis block wityhout blockchain + RecvNotGenesisWithoutBlockchain, } impl From<LocalVerifyGenesisBlockError> for LocalVerifyBlockError { @@ -91,7 +92,7 @@ pub fn verify_local_validity_block( // Check the local rules specific to non-genesis blocks self::not_genesis::local_validation_not_genesis_block(block, currency_parameters)?; } else { - fatal_error!("We must have currency parameters when we check a non-genesis block."); + return Err(LocalVerifyBlockError::RecvNotGenesisWithoutBlockchain); } match block { diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/pow.rs b/lib/modules/blockchain/blockchain/src/dubp/check/pow.rs index 706498f1194b619753e9f2b1bd6f8ee0a69c865d..5b5850f0a8e1638f07aee42a9510edf4127d4ef1 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/pow.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/pow.rs @@ -20,19 +20,14 @@ use durs_common_tools::traits::bool_ext::BoolExt; static ZERO_STRING: &str = "0"; -/// Proof of Work error +/// Invalid PoW pattern #[derive(Debug, PartialEq)] -pub enum BlockPoWError { - /// Invalid pow_min - _InvalidPoWMin, - /// Invalid PoW pattern - InvalidHashPattern { - expected_pattern: String, - actual_hash: String, - }, +pub struct InvalidHashPattern { + expected_pattern: String, + actual_hash: String, } -pub fn verify_hash_pattern(hash: Hash, diffi: usize) -> Result<(), BlockPoWError> { +pub fn verify_hash_pattern(hash: Hash, diffi: usize) -> Result<(), InvalidHashPattern> { let hash_string = hash.to_hex(); let nb_zeros = diffi / 16; let expected_pattern_last_hex_digit = 16 - (diffi % 16); @@ -54,7 +49,7 @@ pub fn verify_hash_pattern(hash: Hash, diffi: usize) -> Result<(), BlockPoWError .expect("Hash type guarantees a valid hexadecimal string."); // remainder must be less than or equal to expected_end_pattern (actual_pattern_last_hex_digit <= expected_pattern_last_hex_digit).or_err( - BlockPoWError::InvalidHashPattern { + InvalidHashPattern { expected_pattern: expected_pattern.clone(), actual_hash: hash_string.clone(), }, @@ -65,7 +60,7 @@ pub fn verify_hash_pattern(hash: Hash, diffi: usize) -> Result<(), BlockPoWError }; hash_string .starts_with(&repeated_zero_string) - .or_err(BlockPoWError::InvalidHashPattern { + .or_err(InvalidHashPattern { expected_pattern, actual_hash: hash_string, })?; @@ -97,7 +92,7 @@ mod tests { ) ); assert_eq!( - Err(BlockPoWError::InvalidHashPattern { + Err(InvalidHashPattern { expected_pattern: "0000[0-a]*".to_owned(), actual_hash: "0000B3619ACBF80298F074D8339175901425BC97EF528ED02EBD73CD4CA5C559" .to_owned(), @@ -109,7 +104,7 @@ mod tests { ) ); assert_eq!( - Err(BlockPoWError::InvalidHashPattern { + Err(InvalidHashPattern { expected_pattern: "0000[0-a]*".to_owned(), actual_hash: "000313619ACBF80298F074D8339175901425BC97EF528ED02EBD73CD4CA5C559" .to_owned(), @@ -121,7 +116,7 @@ mod tests { ) ); assert_eq!( - Err(BlockPoWError::InvalidHashPattern { + Err(InvalidHashPattern { expected_pattern: "00000".to_owned(), actual_hash: "000313619ACBF80298F074D8339175901425BC97EF528ED02EBD73CD4CA5C559" .to_owned(), @@ -133,7 +128,7 @@ mod tests { ) ); assert_eq!( - Err(BlockPoWError::InvalidHashPattern { + Err(InvalidHashPattern { expected_pattern: "0000".to_owned(), actual_hash: "000313619ACBF80298F074D8339175901425BC97EF528ED02EBD73CD4CA5C559" .to_owned(), diff --git a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs index 57b8b0c8a04b4dee8981bab42e6f0cdb6e1ea8dd..dc969299cba74e0fd3a596648c5f22f04a4b0dd2 100644 --- a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs +++ b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs @@ -50,7 +50,7 @@ pub fn receive_blocks(bc: &mut BlockchainModule, blocks: Vec<BlockDocument>) { db.write(|mut w| { match check_and_apply_block(bc, &db, &mut w, block) { Ok(check_block_return) => match check_block_return { - CheckAndApplyBlockReturn::ValidMainBlock(ValidBlockApplyReqs( + CheckAndApplyBlockReturn::ValidMainBlock(WriteBlockQueries( bc_db_query, wot_dbs_queries, tx_dbs_queries, diff --git a/lib/modules/blockchain/blockchain/src/fork/rollback.rs b/lib/modules/blockchain/blockchain/src/fork/rollback.rs index ec11ff9f99a51e28a714d5f35c4af95a9d3b4a7a..8b4cd210f39b670a5391babc9ce1e466a7bbef23 100644 --- a/lib/modules/blockchain/blockchain/src/fork/rollback.rs +++ b/lib/modules/blockchain/blockchain/src/fork/rollback.rs @@ -92,7 +92,7 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>) new_branch_blocks.push(dal_block.clone()); match check_and_apply_block(bc, &db, &mut w, dal_block.block) { Ok(check_and_apply_block_return) => match check_and_apply_block_return { - CheckAndApplyBlockReturn::ValidMainBlock(ValidBlockApplyReqs( + CheckAndApplyBlockReturn::ValidMainBlock(WriteBlockQueries( bc_db_query, wot_dbs_queries, tx_dbs_queries, diff --git a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs index 846873440a48a5e707604cf2ea1f188394eee953..582dbcf166c6e0b88aff84f27f5e72a2f89f8a45 100644 --- a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs +++ b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs @@ -45,7 +45,7 @@ pub fn apply_stackable_blocks(bc: &mut BlockchainModule) { let db = bc.take_db(); let db_write_result = db.write(|mut w| { match check_and_apply_block(bc, &db, &mut w, stackable_block.block) { - Ok(CheckAndApplyBlockReturn::ValidMainBlock(ValidBlockApplyReqs( + Ok(CheckAndApplyBlockReturn::ValidMainBlock(WriteBlockQueries( bc_db_query, wot_dbs_queries, tx_dbs_queries, diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index 5ee4c5ef8abe44e08f051c9d2135b3de420c84d6..ea1b9fa363508ff4c64dfcb1301694c40b842ecb 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -50,7 +50,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; use crate::constants::*; use crate::dbex::DbExQuery; -use crate::dubp::apply::ValidBlockApplyReqs; +use crate::dubp::apply::WriteBlockQueries; use crate::dubp::*; use crate::fork::*; use dubp_block_doc::BlockDocument; @@ -88,6 +88,8 @@ pub static DISTANCE_CALCULATOR: &RustyDistanceCalculator = &RustyDistanceCalcula /// Blockchain Module pub struct BlockchainModule { + /// Cautious mode + pub cautious_mode: bool, /// Router sender pub router_sender: Sender<RouterThreadMessage<DursMsg>>, ///Path to the user datas profile @@ -173,6 +175,7 @@ pub enum SyncVerificationLevel { impl BlockchainModule { /// Instantiate blockchain module pub fn new( + cautious_mode: bool, router_sender: Sender<RouterThreadMessage<DursMsg>>, profile_path: PathBuf, currency_name: Option<CurrencyName>, @@ -193,6 +196,7 @@ impl BlockchainModule { db.r(|db_r| durs_bc_db_reader::indexes::identities::get_wot_index(db_r))?; Ok(BlockchainModule { + cautious_mode, router_sender, profile_path, currency: currency_name, @@ -220,6 +224,7 @@ impl BlockchainModule { router_sender: Sender<RouterThreadMessage<DursMsg>>, profile_path: PathBuf, _keys: RequiredKeysContent, + cautious_mode: bool, ) -> BlockchainModule { // Get db path let dbs_path = durs_conf::get_blockchain_db_path(profile_path.clone()); @@ -240,8 +245,8 @@ impl BlockchainModule { }; // Instanciate BlockchainModule - // TODO ESZ BlockchainModule::new( + cautious_mode, router_sender, profile_path, currency_name, diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs index f607b5ce513645e3a5bb832ed62aa352625de24d..9006ec6a5a322322b620c52abc864b720e7980e9 100644 --- a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs @@ -19,7 +19,7 @@ pub mod wot_worker; use crate::dubp; use crate::dubp::apply::apply_valid_block; -use crate::dubp::apply::{ApplyValidBlockError, ValidBlockApplyReqs}; +use crate::dubp::apply::{ApplyValidBlockError, WriteBlockQueries}; use crate::sync::SyncJobsMess; use crate::Db; use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait}; @@ -76,7 +76,7 @@ impl BlockApplicator { // Verify block hashs let verif_block_hashs_begin = SystemTime::now(); if self.verif_inner_hash { - dubp::check::hashs::verify_block_hashs(&block_doc) + dubp::check::hashs::check_block_hashes(&block_doc) .expect("Receive wrong block, please reset data and resync !"); } self.all_verif_block_hashs_duration += SystemTime::now() @@ -131,7 +131,7 @@ impl BlockApplicator { } else { fatal_error!("Dev error: BlockApplicator must have DB.") } - if let Ok(ValidBlockApplyReqs(block_req, wot_db_reqs, currency_db_reqs)) = + if let Ok(WriteBlockQueries(block_req, wot_db_reqs, currency_db_reqs)) = apply_valid_block_result { self.all_apply_valid_block_duration += SystemTime::now() diff --git a/lib/modules/blockchain/blockchain/tests/common.rs b/lib/modules/blockchain/blockchain/tests/common.rs index c02ac26b2345727e20e2cc0f70e1cdf8c123087a..21ed4455372f50f47e1a7bffc3d953b9b914dd0b 100644 --- a/lib/modules/blockchain/blockchain/tests/common.rs +++ b/lib/modules/blockchain/blockchain/tests/common.rs @@ -57,12 +57,14 @@ pub fn init_bc_module( tmp_path: &Path, ) -> BlockchainModule { let currency_name = CurrencyName(TEST_CURRENCY.to_owned()); + let cautious_mode = false; //let profile_path = tmp_profile_path.to_owned(); //let dbs_path = durs_conf::get_blockchain_db_path(profile_path.clone()); let db = durs_bc_db_writer::open_db(tmp_path).expect("Fail to open blockchain DB."); BlockchainModule::new( + cautious_mode, fake_router_sender, tmp_path.to_owned(), Some(currency_name.clone()), diff --git a/lib/tools/common-tools/src/traits/bool_ext.rs b/lib/tools/common-tools/src/traits/bool_ext.rs index 908b57c0b125b80b85d1aad11424e3361e43e18d..b834cce5724f4e38cb483493fa4bb1c9b4ff7c18 100644 --- a/lib/tools/common-tools/src/traits/bool_ext.rs +++ b/lib/tools/common-tools/src/traits/bool_ext.rs @@ -22,7 +22,7 @@ pub trait BoolExt { /// Transform any type to result<T, E> fn as_result<T, E>(self, ok: T, err: E) -> Result<T, E>; /// Reverse bool - fn not(self) -> bool; + fn not(&self) -> bool; } impl BoolExt for bool { @@ -43,7 +43,7 @@ impl BoolExt for bool { } } #[inline] - fn not(self) -> bool { + fn not(&self) -> bool { !self } }