diff --git a/lib/modules-lib/bc-db-reader/src/blocks.rs b/lib/modules-lib/bc-db-reader/src/blocks.rs index 3281d0f18822c2d8991f1ad0bb00eeb19e62dc06..46b4ff738b3338d2734de9067195f455fc86358a 100644 --- a/lib/modules-lib/bc-db-reader/src/blocks.rs +++ b/lib/modules-lib/bc-db-reader/src/blocks.rs @@ -52,84 +52,89 @@ impl DbBlock { } /// Return true if the node already knows this block -pub fn already_have_block<DB: DbReadable>( +pub fn already_have_block<DB: BcDbInReadTx>( db: &DB, blockstamp: Blockstamp, previous_hash: Option<Hash>, ) -> Result<bool, DbError> { - db.read(|r| { - let blockstamp_bytes: Vec<u8> = blockstamp.into(); - if db - .get_store(FORK_BLOCKS) - .get(r, &blockstamp_bytes)? - .is_some() + let blockstamp_bytes: Vec<u8> = blockstamp.into(); + if db + .db() + .get_store(FORK_BLOCKS) + .get(db.r(), &blockstamp_bytes)? + .is_some() + { + return Ok(true); + } else if blockstamp.id > BlockNumber(0) { + let previous_blockstamp_bytes: Vec<u8> = PreviousBlockstamp { + id: BlockNumber(blockstamp.id.0 - 1), + hash: BlockHash(previous_hash.expect("no genesis block must have previous hash")), + } + .into(); + if let Some(v) = db + .db() + .get_store(ORPHAN_BLOCKSTAMP) + .get(db.r(), &previous_blockstamp_bytes)? { - return Ok(true); - } else if blockstamp.id > BlockNumber(0) { - let previous_blockstamp_bytes: Vec<u8> = PreviousBlockstamp { - id: BlockNumber(blockstamp.id.0 - 1), - hash: BlockHash(previous_hash.expect("no genesis block must have previous hash")), - } - .into(); - if let Some(v) = db - .get_store(ORPHAN_BLOCKSTAMP) - .get(r, &previous_blockstamp_bytes)? - { - for orphan_blockstamp in DB::from_db_value::<Vec<Blockstamp>>(v)? { - if orphan_blockstamp == blockstamp { - return Ok(true); - } + for orphan_blockstamp in from_db_value::<Vec<Blockstamp>>(v)? { + if orphan_blockstamp == blockstamp { + return Ok(true); } } } - if let Some(v) = db.get_int_store(MAIN_BLOCKS).get(r, blockstamp.id.0)? { - if DB::from_db_value::<DbBlock>(v)?.block.blockstamp() == blockstamp { - Ok(true) - } else { - Ok(false) - } + } + if let Some(v) = db + .db() + .get_int_store(MAIN_BLOCKS) + .get(db.r(), blockstamp.id.0)? + { + if from_db_value::<DbBlock>(v)?.block.blockstamp() == blockstamp { + Ok(true) } else { Ok(false) } - }) + } else { + Ok(false) + } } /// Get block -pub fn get_block<DB: DbReadable, R: DbReader>( +pub fn get_block<DB: BcDbInReadTx>( db: &DB, - r: &R, blockstamp: Blockstamp, ) -> Result<Option<DbBlock>, DbError> { - let opt_dal_block = get_db_block_in_local_blockchain(db, r, blockstamp.id)?; + let opt_dal_block = get_db_block_in_local_blockchain(db, blockstamp.id)?; if opt_dal_block.is_none() { - get_fork_block(db, r, blockstamp) + get_fork_block(db, blockstamp) } else { Ok(opt_dal_block) } } /// Get fork block -pub fn get_fork_block<DB: DbReadable, R: DbReader>( +pub fn get_fork_block<DB: BcDbInReadTx>( db: &DB, - r: &R, blockstamp: Blockstamp, ) -> Result<Option<DbBlock>, DbError> { let blockstamp_bytes: Vec<u8> = blockstamp.into(); - if let Some(v) = db.get_store(FORK_BLOCKS).get(r, &blockstamp_bytes)? { - Ok(Some(DB::from_db_value(v)?)) + if let Some(v) = db + .db() + .get_store(FORK_BLOCKS) + .get(db.r(), &blockstamp_bytes)? + { + Ok(Some(from_db_value(v)?)) } else { Ok(None) } } /// Get block hash -pub fn get_block_hash<DB: DbReadable, R: DbReader>( +pub fn get_block_hash<DB: BcDbInReadTx>( db: &DB, - r: &R, block_number: BlockNumber, ) -> Result<Option<BlockHash>, DbError> { Ok( - if let Some(block) = get_block_in_local_blockchain(db, r, block_number)? { + if let Some(block) = get_block_in_local_blockchain(db, block_number)? { block.hash() } else { None @@ -139,40 +144,41 @@ pub fn get_block_hash<DB: DbReadable, R: DbReader>( /// Get block in local blockchain #[inline] -pub fn get_block_in_local_blockchain<DB: DbReadable, R: DbReader>( +pub fn get_block_in_local_blockchain<DB: BcDbInReadTx>( db: &DB, - r: &R, block_number: BlockNumber, ) -> Result<Option<BlockDocument>, DbError> { - Ok(get_db_block_in_local_blockchain(db, r, block_number)?.map(|dal_block| dal_block.block)) + Ok(get_db_block_in_local_blockchain(db, block_number)?.map(|dal_block| dal_block.block)) } /// Get block in local blockchain -pub fn get_db_block_in_local_blockchain<DB: DbReadable, R: DbReader>( +pub fn get_db_block_in_local_blockchain<DB: BcDbInReadTx>( db: &DB, - r: &R, block_number: BlockNumber, ) -> Result<Option<DbBlock>, DbError> { - if let Some(v) = db.get_int_store(MAIN_BLOCKS).get(r, block_number.0)? { - Ok(Some(DB::from_db_value(v)?)) + if let Some(v) = db + .db() + .get_int_store(MAIN_BLOCKS) + .get(db.r(), block_number.0)? + { + Ok(Some(from_db_value(v)?)) } else { Ok(None) } } /// Get several blocks in local blockchain -pub fn get_blocks_in_local_blockchain<DB: DbReadable, R: DbReader>( +pub fn get_blocks_in_local_blockchain<DB: BcDbInReadTx>( db: &DB, - r: &R, first_block_number: BlockNumber, mut count: u32, ) -> Result<Vec<BlockDocument>, DbError> { - let bc_store = db.get_int_store(MAIN_BLOCKS); + let bc_store = db.db().get_int_store(MAIN_BLOCKS); let mut blocks = Vec::with_capacity(count as usize); let mut current_block_number = first_block_number; - while let Some(v) = bc_store.get(r, current_block_number.0)? { - blocks.push(DB::from_db_value::<DbBlock>(v)?.block); + while let Some(v) = bc_store.get(db.r(), current_block_number.0)? { + blocks.push(from_db_value::<DbBlock>(v)?.block); count -= 1; if count > 0 { current_block_number = BlockNumber(current_block_number.0 + 1); @@ -185,14 +191,13 @@ pub fn get_blocks_in_local_blockchain<DB: DbReadable, R: DbReader>( /// Get several blocks in local blockchain by their number #[cfg(feature = "client-indexer")] -pub fn get_blocks_in_local_blockchain_by_numbers<DB: DbReadable, R: DbReader>( +pub fn get_blocks_in_local_blockchain_by_numbers<DB: BcDbInReadTx>( db: &DB, - r: &R, numbers: Vec<BlockNumber>, ) -> Result<Vec<DbBlock>, DbError> { numbers .into_iter() - .filter_map(|n| match get_db_block_in_local_blockchain(db, r, n) { + .filter_map(|n| match get_db_block_in_local_blockchain(db, n) { Ok(Some(db_block)) => Some(Ok(db_block)), Ok(None) => None, Err(e) => Some(Err(e)), @@ -201,20 +206,17 @@ pub fn get_blocks_in_local_blockchain_by_numbers<DB: DbReadable, R: DbReader>( } /// Get current frame of calculating members -pub fn get_current_frame<DB: DbReadable>( +pub fn get_current_frame<DB: BcDbInReadTx>( current_block: &BlockDocument, db: &DB, ) -> Result<HashMap<PubKey, usize>, DbError> { let frame_begin = current_block.number().0 - current_block.current_frame_size() as u32; - let blocks = db.read(|r| { - get_blocks_in_local_blockchain( - db, - r, - BlockNumber(frame_begin), - current_block.current_frame_size() as u32, - ) - })?; + let blocks = get_blocks_in_local_blockchain( + db, + BlockNumber(frame_begin), + current_block.current_frame_size() as u32, + )?; let mut current_frame: HashMap<PubKey, usize> = HashMap::new(); for block in blocks { @@ -232,7 +234,7 @@ pub fn get_current_frame<DB: DbReadable>( /// Get stackables blocks #[inline] -pub fn get_stackables_blocks<DB: DbReadable>( +pub fn get_stackables_blocks<DB: BcDbInReadTx>( db: &DB, current_blockstamp: Blockstamp, ) -> Result<Vec<DbBlock>, DbError> { @@ -240,26 +242,32 @@ pub fn get_stackables_blocks<DB: DbReadable>( } /// Get orphan blocks -pub fn get_orphan_blocks<DB: DbReadable>( +pub fn get_orphan_blocks<DB: BcDbInReadTx>( db: &DB, blockstamp: PreviousBlockstamp, ) -> Result<Vec<DbBlock>, DbError> { let blockstamp_bytes: Vec<u8> = blockstamp.into(); - db.read(|r| { - if let Some(v) = db.get_store(ORPHAN_BLOCKSTAMP).get(r, &blockstamp_bytes)? { - let orphan_blockstamps = DB::from_db_value::<Vec<Blockstamp>>(v)?; - let mut orphan_blocks = Vec::with_capacity(orphan_blockstamps.len()); - for orphan_blockstamp in orphan_blockstamps { - let orphan_blockstamp_bytes: Vec<u8> = orphan_blockstamp.into(); - if let Some(v) = db.get_store(FORK_BLOCKS).get(r, &orphan_blockstamp_bytes)? { - orphan_blocks.push(DB::from_db_value::<DbBlock>(v)?); - } else { - return Err(DbError::DBCorrupted); - } + if let Some(v) = db + .db() + .get_store(ORPHAN_BLOCKSTAMP) + .get(db.r(), &blockstamp_bytes)? + { + let orphan_blockstamps = from_db_value::<Vec<Blockstamp>>(v)?; + let mut orphan_blocks = Vec::with_capacity(orphan_blockstamps.len()); + for orphan_blockstamp in orphan_blockstamps { + let orphan_blockstamp_bytes: Vec<u8> = orphan_blockstamp.into(); + if let Some(v) = db + .db() + .get_store(FORK_BLOCKS) + .get(db.r(), &orphan_blockstamp_bytes)? + { + orphan_blocks.push(from_db_value::<DbBlock>(v)?); + } else { + return Err(DbError::DBCorrupted); } - Ok(orphan_blocks) - } else { - Ok(vec![]) } - }) + Ok(orphan_blocks) + } else { + Ok(vec![]) + } } 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..1463a2d3addf0eeeeb51cf9541d9ec743e49f9b0 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 @@ -59,7 +59,7 @@ pub fn get_db_version<DB: DbReadable>(db: &DB) -> Result<usize, DbError> { db.read(|r| { if let Some(v) = db .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::DbVersion.to_u32())? + .get(&r, CurrentMetaDataKey::DbVersion.to_u32())? { if let DbValue::U64(db_version) = v { Ok(db_version as usize) @@ -73,37 +73,28 @@ pub fn get_db_version<DB: DbReadable>(db: &DB) -> Result<usize, DbError> { } /// Get currency name -pub fn get_currency_name<DB: DbReadable>(db: &DB) -> Result<Option<CurrencyName>, DbError> { - db.read(|r| { - if let Some(v) = db - .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::CurrencyName.to_u32())? - { - if let DbValue::Str(curency_name) = v { - Ok(Some(CurrencyName(curency_name.to_owned()))) - } else { - Err(DbError::DBCorrupted) - } +pub fn get_currency_name<DB: BcDbInReadTx>(db: &DB) -> Result<Option<CurrencyName>, DbError> { + if let Some(v) = db + .db() + .get_int_store(CURRENT_METAS_DATAS) + .get(db.r(), CurrentMetaDataKey::CurrencyName.to_u32())? + { + if let DbValue::Str(curency_name) = v { + Ok(Some(CurrencyName(curency_name.to_owned()))) } else { - Ok(None) + Err(DbError::DBCorrupted) } - }) -} - -/// Get current blockstamp -#[inline] -pub fn get_current_blockstamp<DB: DbReadable>(db: &DB) -> Result<Option<Blockstamp>, DbError> { - db.read(|r| get_current_blockstamp_(db, r)) + } else { + Ok(None) + } } /// Get current blockstamp -pub fn get_current_blockstamp_<DB: DbReadable, R: DbReader>( - db: &DB, - r: &R, -) -> Result<Option<Blockstamp>, DbError> { +pub fn get_current_blockstamp<DB: BcDbInReadTx>(db: &DB) -> Result<Option<Blockstamp>, DbError> { if let Some(v) = db + .db() .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::CurrentBlockstamp.to_u32())? + .get(db.r(), CurrentMetaDataKey::CurrentBlockstamp.to_u32())? { if let DbValue::Blob(current_blockstamp_bytes) = v { Ok(Some( @@ -117,21 +108,12 @@ pub fn get_current_blockstamp_<DB: DbReadable, R: DbReader>( Ok(None) } } - /// Get current common time (also named "blockchain time") -#[inline] -pub fn get_current_common_time<DB: DbReadable>(db: &DB) -> Result<u64, DbError> { - db.read(|r| get_current_common_time_(db, r)) -} - -/// Get current common time (also named "blockchain time") -pub fn get_current_common_time_<DB: DbReadable, R: DbReader>( - db: &DB, - r: &R, -) -> Result<u64, DbError> { +pub fn get_current_common_time_<DB: BcDbInReadTx>(db: &DB) -> Result<u64, DbError> { if let Some(v) = db + .db() .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::CurrentBlockchainTime.to_u32())? + .get(db.r(), CurrentMetaDataKey::CurrentBlockchainTime.to_u32())? { if let DbValue::U64(current_common_time) = v { Ok(current_common_time) @@ -144,25 +126,25 @@ pub fn get_current_common_time_<DB: DbReadable, R: DbReader>( } /// Get fork tree root -pub fn get_fork_tree<DB: DbReadable>(db: &DB) -> Result<ForkTree, DbError> { - db.read(|r| { - if let Some(v) = db - .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::ForkTree.to_u32())? - { - Ok(DB::from_db_value::<ForkTree>(v)?) - } else { - Ok(ForkTree::default()) - } - }) +pub fn get_fork_tree<DB: BcDbInReadTx>(db: &DB) -> Result<ForkTree, DbError> { + if let Some(v) = db + .db() + .get_int_store(CURRENT_METAS_DATAS) + .get(db.r(), CurrentMetaDataKey::ForkTree.to_u32())? + { + Ok(from_db_value::<ForkTree>(v)?) + } else { + Ok(ForkTree::default()) + } } /// Get greatest wot id #[inline] -pub fn get_greatest_wot_id_<DB: DbReadable, R: DbReader>(db: &DB, r: &R) -> Result<WotId, DbError> { +pub fn get_greatest_wot_id_<DB: BcDbInReadTx>(db: &DB) -> Result<WotId, DbError> { if let Some(v) = db + .db() .get_int_store(CURRENT_METAS_DATAS) - .get(r, CurrentMetaDataKey::NextWotId.to_u32())? + .get(db.r(), CurrentMetaDataKey::NextWotId.to_u32())? { if let DbValue::U64(greatest_wot_id) = v { Ok(WotId(greatest_wot_id as usize)) diff --git a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs index aa94ed03abf283a36edd3a4ce6dc8ea2f4aace5f..69ebc41dcfb872aaf8b0f1f4c692fd27f2c8660f 100644 --- a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs +++ b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs @@ -22,16 +22,16 @@ use durs_wot::WotId; use std::collections::HashMap; /// Find certifications that emitted in indicated blocks expiring -pub fn find_expire_certs<DB: DbReadable, R: DbReader>( +pub fn find_expire_certs<DB: BcDbInReadTx>( db: &DB, - r: &R, blocks_expiring: Vec<BlockNumber>, ) -> Result<HashMap<(WotId, WotId), BlockNumber>, DbError> { let mut all_expire_certs = HashMap::new(); for expire_block_id in blocks_expiring { for entry_result in db + .db() .get_multi_int_store(CERTS_BY_CREATED_BLOCK) - .get(r, expire_block_id.0)? + .get(db.r(), expire_block_id.0)? { if let Some(value) = entry_result?.1 { if let DbValue::U64(cert) = value { diff --git a/lib/modules-lib/bc-db-reader/src/indexes/identities.rs b/lib/modules-lib/bc-db-reader/src/indexes/identities.rs index c3219da3c033f547fde346b5475bca8deef45bde..8a23e570e0f198075e2ab2bd12128b6c95a9a285 100644 --- a/lib/modules-lib/bc-db-reader/src/indexes/identities.rs +++ b/lib/modules-lib/bc-db-reader/src/indexes/identities.rs @@ -96,7 +96,7 @@ pub struct DbIdentity { } /// Get identities in databases -pub fn get_identities<DB: DbReadable>( +pub fn get_identities<DB: BcDbInReadTx>( db: &DB, filters: IdentitiesFilter, current_block_id: BlockNumber, @@ -109,20 +109,18 @@ pub fn get_identities<DB: DbReadable>( } } else { let mut identities: Vec<DbIdentity> = Vec::new(); - db.read(|r| { - let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db, r)?; - for wot_id in 0..=greatest_wot_id.0 { - if let Some(db_idty) = get_identity_by_wot_id(db, r, WotId(wot_id))? { - if filters - .paging - .check_created_on(db_idty.idty_doc.blockstamp().id, current_block_id) - { - identities.push(db_idty); - } + let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db)?; + for wot_id in 0..=greatest_wot_id.0 { + if let Some(db_idty) = get_identity_by_wot_id(db, WotId(wot_id))? { + if filters + .paging + .check_created_on(db_idty.idty_doc.blockstamp().id, current_block_id) + { + identities.push(db_idty); } } - Ok(()) - })?; + } + identities.sort_by(|i1, i2| { i1.idty_doc .blockstamp() @@ -138,21 +136,20 @@ pub fn get_identities<DB: DbReadable>( } /// Get identity by pubkey in databases -pub fn get_identity_by_pubkey<DB: DbReadable>( +pub fn get_identity_by_pubkey<DB: BcDbInReadTx>( db: &DB, pubkey: &PubKey, ) -> Result<Option<DbIdentity>, DbError> { - db.read(|r| get_identity_by_pubkey_(db, r, pubkey)) + get_identity_by_pubkey_(db, pubkey) } /// Get identity by pubkey -pub fn get_identity_by_pubkey_<DB: DbReadable, R: DbReader>( +pub fn get_identity_by_pubkey_<DB: BcDbInReadTx>( db: &DB, - r: &R, pubkey: &PubKey, ) -> Result<Option<DbIdentity>, DbError> { - if let Some(wot_id) = get_wot_id_(db, r, pubkey)? { - get_identity_by_wot_id(db, r, wot_id) + if let Some(wot_id) = get_wot_id(db, pubkey)? { + get_identity_by_wot_id(db, wot_id) } else { Ok(None) } @@ -160,13 +157,16 @@ pub fn get_identity_by_pubkey_<DB: DbReadable, R: DbReader>( /// Get identity by pubkey #[inline] -pub fn get_identity_by_wot_id<DB: DbReadable, R: DbReader>( +pub fn get_identity_by_wot_id<DB: BcDbInReadTx>( db: &DB, - r: &R, wot_id: WotId, ) -> Result<Option<DbIdentity>, DbError> { - if let Some(v) = db.get_int_store(IDENTITIES).get(r, wot_id.0 as u32)? { - Ok(Some(DB::from_db_value(v)?)) + if let Some(v) = db + .db() + .get_int_store(IDENTITIES) + .get(db.r(), wot_id.0 as u32)? + { + Ok(Some(from_db_value(v)?)) } else { Ok(None) } @@ -174,51 +174,35 @@ pub fn get_identity_by_wot_id<DB: DbReadable, R: DbReader>( /// Get uid from pubkey #[inline] -pub fn get_uid<DB: DbReadable>(db: &DB, pubkey: &PubKey) -> Result<Option<String>, DbError> { +pub fn get_uid<DB: BcDbInReadTx>(db: &DB, pubkey: &PubKey) -> Result<Option<String>, DbError> { Ok(get_identity_by_pubkey(db, pubkey)?.map(|db_idty| db_idty.idty_doc.username().to_owned())) } /// Get uid from pubkey #[inline] -pub fn get_uid_<DB: DbReadable, R: DbReader>( - db: &DB, - r: &R, - pubkey: &PubKey, -) -> Result<Option<String>, DbError> { - Ok(get_identity_by_pubkey_(db, r, pubkey)? - .map(|db_idty| db_idty.idty_doc.username().to_owned())) +pub fn get_uid_<DB: BcDbInReadTx>(db: &DB, pubkey: &PubKey) -> Result<Option<String>, DbError> { + Ok(get_identity_by_pubkey_(db, pubkey)?.map(|db_idty| db_idty.idty_doc.username().to_owned())) } /// Get wot id from uid -pub fn get_wot_id_from_uid<DB: DbReadable>(db: &DB, uid: &str) -> Result<Option<WotId>, DbError> { - db.read(|r| { - let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db, r)?; - for wot_id in 0..=greatest_wot_id.0 { - if let Some(db_idty) = get_identity_by_wot_id(db, r, WotId(wot_id))? { - if db_idty.idty_doc.username() == uid { - return Ok(Some(WotId(wot_id))); - } +pub fn get_wot_id_from_uid<DB: BcDbInReadTx>(db: &DB, uid: &str) -> Result<Option<WotId>, DbError> { + let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db)?; + for wot_id in 0..=greatest_wot_id.0 { + if let Some(db_idty) = get_identity_by_wot_id(db, WotId(wot_id))? { + if db_idty.idty_doc.username() == uid { + return Ok(Some(WotId(wot_id))); } } - Ok(None) - }) -} - -/// Get identity wot_id -pub fn get_wot_id<DB: DbReadable>(db: &DB, pubkey: &PubKey) -> Result<Option<WotId>, DbError> { - db.read(|r| get_wot_id_(db, r, pubkey)) + } + Ok(None) } - /// Get identity wot_id #[inline] -pub fn get_wot_id_<DB: DbReadable, R: DbReader>( - db: &DB, - r: &R, - pubkey: &PubKey, -) -> Result<Option<WotId>, DbError> { +pub fn get_wot_id<DB: BcDbInReadTx>(db: &DB, pubkey: &PubKey) -> Result<Option<WotId>, DbError> { if let Some(v) = db + .db() .get_store(WOT_ID_INDEX) - .get(r, &pubkey.to_bytes_vector())? + .get(db.r(), &pubkey.to_bytes_vector())? { if let DbValue::U64(wot_id) = v { Ok(Some(WotId(wot_id as usize))) @@ -230,34 +214,30 @@ pub fn get_wot_id_<DB: DbReadable, R: DbReader>( } } /// Get wot_id index -pub fn get_wot_index<DB: DbReadable>(db: &DB) -> Result<HashMap<PubKey, WotId>, DbError> { - db.read(|r| { - let mut wot_index = HashMap::new(); - for entry in db.get_store(WOT_ID_INDEX).iter_start(r)? { - let (k, v_opt) = entry?; - if let Some(DbValue::U64(wot_id)) = v_opt { - wot_index.insert( - PubKey::from_bytes(k).map_err(|_| DbError::DBCorrupted)?, - WotId(wot_id as usize), - ); - } +pub fn get_wot_index<DB: BcDbInReadTx>(db: &DB) -> Result<HashMap<PubKey, WotId>, DbError> { + let mut wot_index = HashMap::new(); + for entry in db.db().get_store(WOT_ID_INDEX).iter_start(db.r())? { + let (k, v_opt) = entry?; + if let Some(DbValue::U64(wot_id)) = v_opt { + wot_index.insert( + PubKey::from_bytes(k).map_err(|_| DbError::DBCorrupted)?, + WotId(wot_id as usize), + ); } - Ok(wot_index) - }) + } + Ok(wot_index) } /// Get wot_uid index -pub fn get_wot_uid_index<DB: DbReadable>(db: &DB) -> Result<HashMap<WotId, String>, DbError> { - db.read(|r| { - let mut wot_uid_index = HashMap::new(); - let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db, r)?; - for wot_id in 0..=greatest_wot_id.0 { - if let Some(db_idty) = get_identity_by_wot_id(db, r, WotId(wot_id))? { - wot_uid_index.insert(WotId(wot_id), db_idty.idty_doc.username().to_owned()); - } +pub fn get_wot_uid_index<DB: BcDbInReadTx>(db: &DB) -> Result<HashMap<WotId, String>, DbError> { + let mut wot_uid_index = HashMap::new(); + let greatest_wot_id = crate::current_meta_datas::get_greatest_wot_id_(db)?; + for wot_id in 0..=greatest_wot_id.0 { + if let Some(db_idty) = get_identity_by_wot_id(db, WotId(wot_id))? { + wot_uid_index.insert(WotId(wot_id), db_idty.idty_doc.username().to_owned()); } - Ok(wot_uid_index) - }) + } + Ok(wot_uid_index) } #[cfg(test)] @@ -307,12 +287,12 @@ mod test { let idty_bin = durs_dbs_tools::to_bytes(idty)?; db.write(|mut w| { db.get_store(WOT_ID_INDEX).put( - w.as_mut(), + db.w.as_mut(), &idty.idty_doc.issuers()[0].to_bytes_vector(), &DbValue::U64(wot_id), )?; db.get_int_store(IDENTITIES).put( - w.as_mut(), + db.w.as_mut(), wot_id as u32, &KvFileDbHandler::db_value(&idty_bin)?, )?; @@ -324,7 +304,7 @@ mod test { // Write greatest wot id db.write(|mut w| { db.get_int_store(CURRENT_METAS_DATAS).put( - w.as_mut(), + db.w.as_mut(), CurrentMetaDataKey::NextWotId.to_u32(), &DbValue::U64(wot_id), )?; diff --git a/lib/modules-lib/bc-db-reader/src/indexes/sources.rs b/lib/modules-lib/bc-db-reader/src/indexes/sources.rs index 77978c8d6b022fd0ff050d44c21c58e47f8f73b1..e3dcc2b40380a39774f1d56438cc9723cd82d410 100644 --- a/lib/modules-lib/bc-db-reader/src/indexes/sources.rs +++ b/lib/modules-lib/bc-db-reader/src/indexes/sources.rs @@ -126,42 +126,29 @@ impl UTXO { } /// Get utxo v10 -pub fn get_utxo_v10<DB: DbReadable>( +pub fn get_utxo_v10<DB: BcDbInReadTx>( db: &DB, utxo_id: UniqueIdUTXOv10, ) -> Result<Option<TransactionOutput>, DbError> { let utxo_id_bytes: Vec<u8> = utxo_id.into(); - db.read(|r| { - if let Some(v) = db.get_store(UTXOS).get(r, &utxo_id_bytes)? { - Ok(Some(DB::from_db_value(v)?)) - } else { - Ok(None) - } - }) -} - -/// Get utxo v10 -pub fn get_utxo_v10_<DB: DbReadable, R: DbReader>( - db: &DB, - r: &R, - utxo_id: UniqueIdUTXOv10, -) -> Result<Option<TransactionOutput>, DbError> { - let utxo_id_bytes: Vec<u8> = utxo_id.into(); - if let Some(v) = db.get_store(UTXOS).get(r, &utxo_id_bytes)? { - Ok(Some(DB::from_db_value(v)?)) + if let Some(v) = db.db().get_store(UTXOS).get(db.r(), &utxo_id_bytes)? { + Ok(Some(from_db_value(v)?)) } else { Ok(None) } } /// Get block consumed sources -pub fn get_block_consumed_sources_<DB: DbReadable, R: DbReader>( +pub fn get_block_consumed_sources_<DB: BcDbInReadTx>( db: &DB, - r: &R, block_number: BlockNumber, ) -> Result<Option<HashMap<UniqueIdUTXOv10, TransactionOutput>>, DbError> { - if let Some(v) = db.get_int_store(CONSUMED_UTXOS).get(r, block_number.0)? { - Ok(Some(DB::from_db_value(v)?)) + if let Some(v) = db + .db() + .get_int_store(CONSUMED_UTXOS) + .get(db.r(), block_number.0)? + { + Ok(Some(from_db_value(v)?)) } else { Ok(None) } diff --git a/lib/modules-lib/bc-db-reader/src/lib.rs b/lib/modules-lib/bc-db-reader/src/lib.rs index 8dc889bd315ee734fc1cab3eaf70cdcb717684d3..2c148ed0e5082e7a3df5623e74fad312a70dca80 100644 --- a/lib/modules-lib/bc-db-reader/src/lib.rs +++ b/lib/modules-lib/bc-db-reader/src/lib.rs @@ -33,16 +33,17 @@ pub mod current_meta_datas; pub mod indexes; pub mod paging; pub mod tools; -pub mod r#trait; +pub mod traits; pub use durs_dbs_tools::kv_db::{ - KvFileDbRead as DbReadable, KvFileDbReader as Reader, KvFileDbRoHandler as BcDbRo, - KvFileDbSchema, KvFileDbStoreType, KvFileDbValue as DbValue, Readable as DbReader, + from_db_value, KvFileDbRead as DbReadable, KvFileDbReader as Reader, + KvFileDbRoHandler as BcDbRo, KvFileDbSchema, KvFileDbStoreType, KvFileDbValue as DbValue, + Readable as DbReader, }; pub use durs_dbs_tools::DbError; #[cfg(feature = "mock")] -pub use r#trait::MockBcDbRoTrait; -pub use r#trait::{BcDbRoTrait, BcDbRoWithReader}; +pub use traits::MockBcDbInReadTx_ as MockBcDbInReadTx; +pub use traits::{BcDbInReadTx, BcDbInReadTx_, BcDbRead, BcDbWithReader}; use constants::*; use maplit::hashmap; @@ -74,6 +75,31 @@ pub fn open_db_ro(path: &Path) -> Result<BcDbRo, DbError> { BcDbRo::open_db_ro(path, &bc_db_schema()) } +pub struct BcDbWithReaderStruct<'r, 'db: 'r, DB> +where + DB: DbReadable, +{ + pub db: &'db DB, + pub r: Reader<'r>, +} + +pub type BcDbRoWithReader<'r, 'db> = BcDbWithReaderStruct<'r, 'db, BcDbRo>; + +impl<'r, 'db: 'r, DB> BcDbWithReader for BcDbWithReaderStruct<'r, 'db, DB> +where + DB: DbReadable, +{ + type DB = DB; + type R = Reader<'r>; + + fn db(&self) -> &Self::DB { + self.db + } + fn r(&self) -> &Self::R { + &self.r + } +} + #[cfg(test)] pub mod tests { diff --git a/lib/modules-lib/bc-db-reader/src/trait.rs b/lib/modules-lib/bc-db-reader/src/traits.rs similarity index 64% rename from lib/modules-lib/bc-db-reader/src/trait.rs rename to lib/modules-lib/bc-db-reader/src/traits.rs index eba22403aa956c3c2b5632c4f541fa6b0c24552c..9e8f8e33e379143afda0079f950213311f5ff006 100644 --- a/lib/modules-lib/bc-db-reader/src/trait.rs +++ b/lib/modules-lib/bc-db-reader/src/traits.rs @@ -17,16 +17,51 @@ // ! Define read only trait use crate::blocks::DbBlock; -use crate::{BcDbRo, Reader}; +use crate::{BcDbWithReaderStruct, DbReadable, DbReader}; use dubp_common_doc::{BlockNumber, Blockstamp}; use dup_crypto::keys::PubKey; use durs_dbs_tools::DbError; - #[cfg(feature = "mock")] use mockall::*; +pub trait BcDbRead<DB> +where + DB: DbReadable, +{ + /// Read datas in Db + fn r<D, F>(&self, f: F) -> Result<D, DbError> + where + DB: DbReadable, + F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>; +} + +impl<DB> BcDbRead<DB> for DB +where + DB: DbReadable, +{ + fn r<D, F>(&self, f: F) -> Result<D, DbError> + where + DB: DbReadable, + F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>, + { + self.read(|r| f(&BcDbWithReaderStruct { db: self, r })) + } +} + +pub trait BcDbInReadTx: BcDbWithReader + BcDbInReadTx_ {} + +impl<T> BcDbInReadTx for T where T: BcDbWithReader + BcDbInReadTx_ {} + +pub trait BcDbWithReader { + type DB: DbReadable; + type R: DbReader; + + fn db(&self) -> &Self::DB; + fn r(&self) -> &Self::R; +} + #[cfg_attr(feature = "mock", automock)] -pub trait BcDbRoTrait { +pub trait BcDbInReadTx_ { fn get_current_blockstamp(&self) -> Result<Option<Blockstamp>, DbError>; fn get_current_block(&self) -> Result<Option<DbBlock>, DbError>; fn get_db_block_in_local_blockchain( @@ -41,20 +76,16 @@ pub trait BcDbRoTrait { fn get_uid_from_pubkey(&self, pubkey: &PubKey) -> Result<Option<String>, DbError>; } -pub struct BcDbRoWithReader<'r, 'db: 'r> { - pub db: &'db BcDbRo, - pub r: Reader<'r>, -} - -impl<'r, 'db: 'r> BcDbRoTrait for BcDbRoWithReader<'r, 'db> { +impl<T> BcDbInReadTx_ for T +where + T: BcDbWithReader, +{ fn get_current_blockstamp(&self) -> Result<Option<Blockstamp>, DbError> { - crate::current_meta_datas::get_current_blockstamp_(self.db, self.r) + crate::current_meta_datas::get_current_blockstamp(self) } fn get_current_block(&self) -> Result<Option<DbBlock>, DbError> { - if let Some(current_blockstamp) = - crate::current_meta_datas::get_current_blockstamp_(self.db, self.r)? - { - crate::blocks::get_db_block_in_local_blockchain(self.db, self.r, current_blockstamp.id) + if let Some(current_blockstamp) = crate::current_meta_datas::get_current_blockstamp(self)? { + crate::blocks::get_db_block_in_local_blockchain(self, current_blockstamp.id) } else { Ok(None) } @@ -63,16 +94,16 @@ impl<'r, 'db: 'r> BcDbRoTrait for BcDbRoWithReader<'r, 'db> { &self, block_number: BlockNumber, ) -> Result<Option<DbBlock>, DbError> { - crate::blocks::get_db_block_in_local_blockchain(self.db, self.r, block_number) + crate::blocks::get_db_block_in_local_blockchain(self, block_number) } #[cfg(feature = "client-indexer")] fn get_db_blocks_in_local_blockchain( &self, numbers: Vec<BlockNumber>, ) -> Result<Vec<DbBlock>, DbError> { - crate::blocks::get_blocks_in_local_blockchain_by_numbers(self.db, self.r, numbers) + crate::blocks::get_blocks_in_local_blockchain_by_numbers(self, numbers) } fn get_uid_from_pubkey(&self, pubkey: &PubKey) -> Result<Option<String>, DbError> { - crate::indexes::identities::get_uid_(self.db, self.r, pubkey) + crate::indexes::identities::get_uid_(self, pubkey) } } diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks.rs b/lib/modules/blockchain/bc-db-writer/src/blocks.rs index 6b6f8d3710e71eed6046de91c27b730f5371be18..4c4ab106be29e487f6c0124616f436c860709018 100644 --- a/lib/modules/blockchain/bc-db-writer/src/blocks.rs +++ b/lib/modules/blockchain/bc-db-writer/src/blocks.rs @@ -23,7 +23,7 @@ use dubp_common_doc::traits::Document; use durs_bc_db_reader::blocks::fork_tree::ForkTree; use durs_bc_db_reader::blocks::DbBlock; use durs_bc_db_reader::constants::*; -use durs_bc_db_reader::DbValue; +use durs_bc_db_reader::{from_db_value, DbValue}; use unwrap::unwrap; /// Insert new head Block in databases @@ -101,9 +101,10 @@ pub fn insert_new_fork_block( )?; // As long as orphan blocks can succeed the last inserted block, they are inserted - for stackable_block in - durs_bc_db_reader::blocks::get_stackables_blocks(db, dal_block.blockstamp())? - { + for stackable_block in durs_bc_db_reader::blocks::get_stackables_blocks( + &BcDbRwWithWriter { db, w }, + dal_block.blockstamp(), + )? { let _ = insert_new_fork_block(db, w, fork_tree, stackable_block); } @@ -115,7 +116,7 @@ pub fn insert_new_fork_block( let mut orphan_blockstamps = if let Some(v) = orphan_blockstamps_store.get(w.as_ref(), &previous_blockstamp_bytes)? { - Db::from_db_value::<Vec<Blockstamp>>(v)? + from_db_value::<Vec<Blockstamp>>(v)? } else { vec![] }; diff --git a/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs b/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs index 23c2ae754361703154a912c7e5f4779d209aa1de..92c8e4d7d20ad021f12056b106da05399ca0e0eb 100644 --- a/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs +++ b/lib/modules/blockchain/bc-db-writer/src/indexes/certs.rs @@ -15,13 +15,13 @@ //! Certifications stored indexes: write requests. -use crate::{Db, DbError, DbWriter}; +use crate::{BcDbRwWithWriter, Db, DbError, DbWriter}; use dubp_common_doc::BlockNumber; use dubp_currency_params::CurrencyParameters; use dubp_user_docs::documents::certification::CompactCertificationDocumentV10; use durs_bc_db_reader::constants::*; use durs_bc_db_reader::indexes::identities::DbIdentity; -use durs_bc_db_reader::{DbReadable, DbValue}; +use durs_bc_db_reader::{from_db_value, DbReadable, DbValue}; use durs_wot::WotId; /// Apply "certification" event in databases @@ -35,9 +35,11 @@ pub fn write_certification( written_timestamp: u64, ) -> Result<(), DbError> { // Get cert_chainable_on - let mut member_datas = - durs_bc_db_reader::indexes::identities::get_identity_by_wot_id(db, w.as_ref(), source)? - .expect("Try to write certification with unexist certifier."); + let mut member_datas = durs_bc_db_reader::indexes::identities::get_identity_by_wot_id( + &BcDbRwWithWriter { db, w }, + source, + )? + .expect("Try to write certification with unexist certifier."); // Push new cert_chainable_on member_datas .cert_chainable_on @@ -76,7 +78,7 @@ pub fn revert_write_cert( // Pop last cert_chainable_on let identities_store = db.get_int_store(IDENTITIES); if let Some(v) = identities_store.get(w.as_ref(), source.0 as u32)? { - let mut member_datas = Db::from_db_value::<DbIdentity>(v)?; + let mut member_datas = from_db_value::<DbIdentity>(v)?; member_datas.cert_chainable_on.pop(); let bin_member_datas = durs_dbs_tools::to_bytes(&member_datas)?; identities_store.put( diff --git a/lib/modules/blockchain/bc-db-writer/src/indexes/identities.rs b/lib/modules/blockchain/bc-db-writer/src/indexes/identities.rs index e8a1500f9af83ea2be22ecab6ef86b4b95a0eb2b..e524c028afc6cd52c919feae5cc4d0e6d3e33b5e 100644 --- a/lib/modules/blockchain/bc-db-writer/src/indexes/identities.rs +++ b/lib/modules/blockchain/bc-db-writer/src/indexes/identities.rs @@ -15,7 +15,7 @@ //! Identities stored indexes: write requests. -use crate::{Db, DbError, DbWriter}; +use crate::{BcDbRwWithWriter, Db, DbError, DbWriter}; use dubp_common_doc::traits::Document; use dubp_common_doc::{BlockNumber, Blockstamp}; use dubp_currency_params::CurrencyParameters; @@ -24,7 +24,7 @@ use dup_crypto::keys::PubKey; use dup_crypto::keys::PublicKey; use durs_bc_db_reader::constants::*; use durs_bc_db_reader::current_meta_datas::CurrentMetaDataKey; -use durs_bc_db_reader::indexes::identities::get_wot_id_; +use durs_bc_db_reader::indexes::identities::get_wot_id; use durs_bc_db_reader::indexes::identities::{DbIdentity, DbIdentityState}; use durs_bc_db_reader::{DbReadable, DbValue}; use durs_common_tools::fatal_error; @@ -32,8 +32,11 @@ use durs_wot::WotId; /// Remove identity from databases pub fn revert_create_identity(db: &Db, w: &mut DbWriter, pubkey: &PubKey) -> Result<(), DbError> { - let dal_idty = durs_bc_db_reader::indexes::identities::get_identity_by_pubkey(db, pubkey)? - .expect("Try to revert unexist idty."); + let dal_idty = durs_bc_db_reader::indexes::identities::get_identity_by_pubkey( + &BcDbRwWithWriter { db, w }, + pubkey, + )? + .expect("Try to revert unexist idty."); // Remove membership db.get_multi_int_store(MBS_BY_CREATED_BLOCK).delete( w.as_mut(), @@ -121,9 +124,11 @@ pub fn exclude_identity( exclusion_blockstamp: &Blockstamp, revert: bool, ) -> Result<(), DbError> { - let mut idty_datas = - durs_bc_db_reader::indexes::identities::get_identity_by_pubkey(db, pubkey)? - .expect("Try to exclude unexist idty."); + let mut idty_datas = durs_bc_db_reader::indexes::identities::get_identity_by_pubkey( + &BcDbRwWithWriter { db, w }, + pubkey, + )? + .expect("Try to exclude unexist idty."); idty_datas.state = if revert { match idty_datas.state { DbIdentityState::ExpireMember(renewed_counts) => { @@ -146,7 +151,7 @@ pub fn exclude_identity( }; // Write new identity datas let bin_idty = durs_dbs_tools::to_bytes(&idty_datas)?; - if let Some(wot_id) = get_wot_id_(db, w.as_ref(), &pubkey)? { + if let Some(wot_id) = get_wot_id(&BcDbRwWithWriter { db, w }, &pubkey)? { db.get_int_store(IDENTITIES) .put(w.as_mut(), wot_id.0 as u32, &DbValue::Blob(&bin_idty))?; Ok(()) @@ -164,9 +169,11 @@ pub fn revoke_identity( explicit: bool, revert: bool, ) -> Result<(), DbError> { - let mut member_datas = - durs_bc_db_reader::indexes::identities::get_identity_by_pubkey(db, pubkey)? - .expect("Try to revoke unexist idty."); + let mut member_datas = durs_bc_db_reader::indexes::identities::get_identity_by_pubkey( + &BcDbRwWithWriter { db, w }, + pubkey, + )? + .expect("Try to revoke unexist idty."); member_datas.state = if revert { match member_datas.state { @@ -202,7 +209,7 @@ pub fn revoke_identity( // Update idty let bin_idty = durs_dbs_tools::to_bytes(&member_datas)?; - if let Some(wot_id) = get_wot_id_(db, w.as_ref(), &pubkey)? { + if let Some(wot_id) = get_wot_id(&BcDbRwWithWriter { db, w }, &pubkey)? { db.get_int_store(IDENTITIES) .put(w.as_mut(), wot_id.0 as u32, &DbValue::Blob(&bin_idty))?; Ok(()) @@ -223,8 +230,7 @@ pub fn renewal_identity( ) -> Result<(), DbError> { // Get idty_datas let mut idty_datas = durs_bc_db_reader::indexes::identities::get_identity_by_wot_id( - db, - w.as_ref(), + &BcDbRwWithWriter { db, w }, idty_wot_id, )? .expect("Fatal error : try to renewal unknow identity !"); @@ -283,7 +289,7 @@ pub fn renewal_identity( /// Remove identity from databases pub fn remove_identity(db: &Db, w: &mut DbWriter, pubkey: PubKey) -> Result<(), DbError> { - if let Some(wot_id) = get_wot_id_(db, w.as_ref(), &pubkey)? { + if let Some(wot_id) = get_wot_id(&BcDbRwWithWriter { db, w }, &pubkey)? { db.get_int_store(IDENTITIES) .delete(w.as_mut(), wot_id.0 as u32)?; Ok(()) diff --git a/lib/modules/blockchain/bc-db-writer/src/indexes/transactions.rs b/lib/modules/blockchain/bc-db-writer/src/indexes/transactions.rs index 7df63fa75e3f7e2912bffadd9954b487c99960e4..9dcf7c0293cbf46957e6af7d85628aa55cbc55ea 100644 --- a/lib/modules/blockchain/bc-db-writer/src/indexes/transactions.rs +++ b/lib/modules/blockchain/bc-db-writer/src/indexes/transactions.rs @@ -17,7 +17,7 @@ use dubp_user_docs::documents::transaction::*; use durs_bc_db_reader::constants::*; -use durs_bc_db_reader::DbValue; +use durs_bc_db_reader::{from_db_value, DbValue}; use durs_common_tools::fatal_error; use crate::*; @@ -145,7 +145,7 @@ pub fn apply_and_write_tx( .map(|utxo_id| { let utxo_id_bytes: Vec<u8> = (*utxo_id).into(); if let Some(value) = db.get_store(UTXOS).get(w.as_ref(), &utxo_id_bytes)? { - let utxo_content: TransactionOutput = Db::from_db_value(value)?; + let utxo_content: TransactionOutput = from_db_value(value)?; Ok((*utxo_id, utxo_content)) } else { fatal_error!("Try to persist unexist consumed source."); @@ -154,9 +154,12 @@ pub fn apply_and_write_tx( .collect::<Result<HashMap<UniqueIdUTXOv10, TransactionOutput>, DbError>>()?; let consumed_sources_bytes = durs_dbs_tools::to_bytes(&consumed_sources)?; let block_number = - durs_bc_db_reader::current_meta_datas::get_current_blockstamp_(db, w.as_ref())? - .unwrap_or_default() - .id; + durs_bc_db_reader::current_meta_datas::get_current_blockstamp(&BcDbRwWithWriter { + db, + w, + })? + .unwrap_or_default() + .id; db.get_int_store(CONSUMED_UTXOS).put( w.as_mut(), block_number.0, diff --git a/lib/modules/blockchain/bc-db-writer/src/lib.rs b/lib/modules/blockchain/bc-db-writer/src/lib.rs index 6e84fd0e815be172d508e8b18d3cc972cf4893c2..6bcfceac1f50f672dd21aaccb2bddce972573bb3 100644 --- a/lib/modules/blockchain/bc-db-writer/src/lib.rs +++ b/lib/modules/blockchain/bc-db-writer/src/lib.rs @@ -68,6 +68,29 @@ pub fn open_db(path: &Path) -> Result<Db, DbError> { Db::open_db(path, &durs_bc_db_reader::bc_db_schema()) } +/// R/W Database with reader +pub type BcDbRwWithReader<'r, 'db> = durs_bc_db_reader::BcDbWithReaderStruct<'r, 'db, Db>; + +/// R/W Database with writer +pub struct BcDbRwWithWriter<'w, 'db: 'w> { + /// R/W database handler + pub db: &'db Db, + /// Reader + pub w: &'w DbWriter<'w>, +} + +impl<'w, 'db: 'w> durs_bc_db_reader::BcDbWithReader for BcDbRwWithWriter<'w, 'db> { + type DB = Db; + type R = DbWriter<'w>; + + fn db(&self) -> &Self::DB { + self.db + } + fn r(&self) -> &Self::R { + self.w + } +} + #[derive(Debug)] /// Set of databases storing web of trust information pub struct WotsV10DBs { diff --git a/lib/modules/blockchain/blockchain/src/dbex.rs b/lib/modules/blockchain/blockchain/src/dbex.rs index 73415b3185c2a3eeb488afa37e99ab8a435b606b..d08b5943273303135c5309fa178a0cafa61cca58 100644 --- a/lib/modules/blockchain/blockchain/src/dbex.rs +++ b/lib/modules/blockchain/blockchain/src/dbex.rs @@ -20,7 +20,7 @@ use dubp_block_doc::block::BlockDocumentTrait; use dubp_common_doc::BlockNumber; use dup_crypto::keys::*; use durs_bc_db_reader::constants::*; -use durs_bc_db_reader::{BcDbRo, DbValue}; +use durs_bc_db_reader::{BcDbRead, BcDbRo, DbValue}; use durs_wot::data::rusty::RustyWebOfTrust; use durs_wot::data::WebOfTrust; use durs_wot::operations::distance::{DistanceCalculator, WotDistance, WotDistanceParameters}; @@ -142,13 +142,14 @@ pub fn dbex_bc(profile_path: PathBuf, _csv: bool, _query: DbExBcQuery) -> Result ); if let Some(current_blockstamp) = - durs_bc_db_reader::current_meta_datas::get_current_blockstamp(&db)? + db.r(|db_r| durs_bc_db_reader::current_meta_datas::get_current_blockstamp(db_r))? { println!("Current block: #{}.", current_blockstamp); - if let Some(current_block) = db.read(|r| { - durs_bc_db_reader::blocks::get_block_in_local_blockchain(&db, r, current_blockstamp.id) + if let Some(current_block) = db.r(|db_r| { + durs_bc_db_reader::blocks::get_block_in_local_blockchain(db_r, current_blockstamp.id) })? { - let map_pubkey = durs_bc_db_reader::blocks::get_current_frame(¤t_block, &db)?; + let map_pubkey = + db.r(|db_r| durs_bc_db_reader::blocks::get_current_frame(¤t_block, db_r))?; let mut vec = map_pubkey.iter().collect::<Vec<(&PubKey, &usize)>>(); vec.sort_by(|a, b| b.1.cmp(&a.1)); @@ -156,11 +157,11 @@ pub fn dbex_bc(profile_path: PathBuf, _csv: bool, _query: DbExBcQuery) -> Result if _csv { println!("{},{},{}", &BLOCK, &USERNAME, &PUB_KEY); for (pub_key, v) in &vec { - if let Ok(Some(identity)) = + if let Ok(Some(identity)) = db.r(|db_r| { durs_bc_db_reader::indexes::identities::get_identity_by_pubkey( - &db, &pub_key, + db_r, &pub_key, ) - { + }) { println!( "{},{},{}", v, @@ -173,11 +174,11 @@ pub fn dbex_bc(profile_path: PathBuf, _csv: bool, _query: DbExBcQuery) -> Result //let mut table = Table::new(); //table.add_row(row![&BLOCK, &USERNAME, &PUB_KEY]); for (pub_key, _v) in &vec { - if let Ok(Some(_identity)) = + if let Ok(Some(_identity)) = db.r(|db_r| { durs_bc_db_reader::indexes::identities::get_identity_by_pubkey( - &db, &pub_key, + db_r, &pub_key, ) - { + }) { //table.add_row(row![v, identity.idty_doc.username(), pub_key.to_string()]); } } @@ -208,8 +209,9 @@ pub fn dbex_fork_tree(profile_path: PathBuf, _csv: bool) { load_db_duration.as_secs(), load_db_duration.subsec_millis() ); - let fork_tree = - durs_bc_db_reader::current_meta_datas::get_fork_tree(&db).expect("fail to get fork tree"); + let fork_tree = db + .r(|db_r| durs_bc_db_reader::current_meta_datas::get_fork_tree(db_r)) + .expect("fail to get fork tree"); // Print all fork branches for (tree_node_id, blockstamp) in fork_tree.get_sheets() { debug!( @@ -317,15 +319,18 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { let currency_params = unwrap!(currency_params_db_datas).1; // get wot_index - let wot_index = durs_bc_db_reader::indexes::identities::get_wot_index(&db).expect("DbError"); + let wot_index = db + .r(|db_r| durs_bc_db_reader::indexes::identities::get_wot_index(db_r)) + .expect("DbError"); // get wot_reverse_index let wot_reverse_index: HashMap<WotId, &PubKey> = wot_index.iter().map(|(p, id)| (*id, p)).collect(); // get wot uid index - let wot_uid_index = - durs_bc_db_reader::indexes::identities::get_wot_uid_index(&db).expect("DbError"); + let wot_uid_index = db + .r(|db_r| durs_bc_db_reader::indexes::identities::get_wot_uid_index(db_r)) + .expect("DbError"); // Open wot db let wot_db = BinFreeStructDb::File( @@ -403,10 +408,9 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { let db = durs_bc_db_reader::open_db_ro(&db_path.as_path()).expect("Fail to open DB."); // Get blocks_times let all_blocks = db - .read(|r| { + .r(|db_r| { durs_bc_db_reader::blocks::get_blocks_in_local_blockchain( - &db, - r, + db_r, BlockNumber(0), 10_000_000, ) @@ -428,7 +432,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { if created_ms_time > min_created_ms_time { for entry_result in db .get_multi_int_store(MBS_BY_CREATED_BLOCK) - .get(r, block_id)? + .get(&r, block_id)? { if let Some(DbValue::U64(wot_id)) = entry_result?.1 { expire_dates.push(( @@ -454,18 +458,17 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { DbExWotQuery::MemberDatas(ref uid_or_pubkey) => { println!(" Members count = {}.", members_count); let wot_id_opt = match uid_or_pubkey { - UidOrPubkey::Uid(ref uid) => { - durs_bc_db_reader::indexes::identities::get_wot_id_from_uid(&db, uid) - .expect("get_wot_id_from_uid() : DbError !") - } + UidOrPubkey::Uid(ref uid) => db + .r(|db_r| { + durs_bc_db_reader::indexes::identities::get_wot_id_from_uid(db_r, uid) + }) + .expect("get_wot_id_from_uid() : DbError !"), UidOrPubkey::Pubkey(ref pubkey) => wot_index.get(pubkey).copied(), }; if let Some(wot_id) = wot_id_opt { let idty = db - .read(|r| { - durs_bc_db_reader::indexes::identities::get_identity_by_wot_id( - &db, r, wot_id, - ) + .r(|db_r| { + durs_bc_db_reader::indexes::identities::get_identity_by_wot_id(db_r, wot_id) }) .expect("DB error: ") .expect("DB corrupted: all WotId must be point to an identity."); @@ -502,12 +505,15 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { .expect("Fail to get links source !"); println!("Certifiers : {}", sources.len()); for (i, source) in sources.iter().enumerate() { - let source_uid = durs_bc_db_reader::indexes::identities::get_uid( - &db, - wot_reverse_index[&source], - ) - .expect("get_uid() : DbError") - .expect("Not found source_uid !"); + let source_uid = db + .r(|db_r| { + durs_bc_db_reader::indexes::identities::get_uid( + db_r, + wot_reverse_index[&source], + ) + }) + .expect("get_uid() : DbError") + .expect("Not found source_uid !"); println!("{}: {}", i + 1, source_uid); } } else { diff --git a/lib/modules/blockchain/blockchain/src/dubp.rs b/lib/modules/blockchain/blockchain/src/dubp.rs index cd964bd215f94ec6190f82c8860ac99931e492f8..4150cc1cc20ce097c5c567051670796f324f509e 100644 --- a/lib/modules/blockchain/blockchain/src/dubp.rs +++ b/lib/modules/blockchain/blockchain/src/dubp.rs @@ -29,7 +29,7 @@ use dubp_common_doc::traits::Document; use dubp_common_doc::{BlockNumber, Blockstamp}; use durs_bc_db_reader::blocks::DbBlock; use durs_bc_db_reader::DbError; -use durs_bc_db_writer::{Db, DbWriter}; +use durs_bc_db_writer::{BcDbRwWithWriter, Db, DbWriter}; use unwrap::unwrap; #[derive(Debug, Clone)] @@ -74,7 +74,7 @@ pub fn check_and_apply_block( ) -> Result<CheckAndApplyBlockReturn, BlockError> { // Get BlockDocument && check if already have block let already_have_block = durs_bc_db_reader::blocks::already_have_block( - db, + &BcDbRwWithWriter { db, w }, block_doc.blockstamp(), block_doc.previous_hash(), )?; @@ -106,14 +106,15 @@ pub fn check_and_apply_block( // Detect expire_certs let blocks_expiring = Vec::with_capacity(0); - let expire_certs = - durs_bc_db_reader::indexes::certs::find_expire_certs(db, w.as_ref(), blocks_expiring)?; + 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, - db, - w.as_ref(), + &BcDbRwWithWriter { db, w }, &bc.wot_index, &bc.wot_databases.wot_db, ) diff --git a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs index 2aee3b68bac247b77c1bd0a4114e7a22300fe1a7..e30da6c8821f4cfaaa83fb3c8366199198c1e02a 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/apply/mod.rs @@ -24,7 +24,7 @@ use durs_bc_db_reader::blocks::DbBlock; use durs_bc_db_reader::indexes::sources::get_block_consumed_sources_; use durs_bc_db_reader::indexes::sources::SourceAmount; use durs_bc_db_writer::writers::requests::*; -use durs_bc_db_writer::{BinFreeStructDb, Db, DbError, DbWriter}; +use durs_bc_db_writer::{BcDbRwWithWriter, BinFreeStructDb, Db, DbError, DbWriter}; use durs_common_tools::fatal_error; use durs_wot::data::NewLinkResult; use durs_wot::{WebOfTrust, WotId}; @@ -302,7 +302,8 @@ pub fn exec_currency_queries( block_number: BlockNumber, currency_queries: Vec<CurrencyDBsWriteQuery>, ) -> Result<(), DbError> { - let mut block_consumed_sources = get_block_consumed_sources_(db, w.as_ref(), block_number)?; + let mut block_consumed_sources = + get_block_consumed_sources_(&BcDbRwWithWriter { db, w }, block_number)?; for query in ¤cy_queries { query.apply(db, w, block_consumed_sources.as_mut(), true)?; } diff --git a/lib/modules/blockchain/blockchain/src/dubp/check/global.rs b/lib/modules/blockchain/blockchain/src/dubp/check/global.rs index 483c2ece4f78dc945fd678789b2c15db3b9b2bec..7e54357b0a0041e799e766cbc9fe501da375f80b 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/check/global.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/check/global.rs @@ -19,7 +19,7 @@ use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait}; use dubp_common_doc::traits::Document; use dubp_common_doc::BlockNumber; use dup_crypto::keys::PubKey; -use durs_bc_db_reader::{DbError, DbReadable, DbReader}; +use durs_bc_db_reader::{BcDbInReadTx, DbError}; use durs_bc_db_writer::BinFreeStructDb; use durs_common_tools::traits::bool_ext::BoolExt; use durs_wot::*; @@ -38,16 +38,14 @@ impl From<DbError> for GlobalVerifyBlockError { } } -pub fn verify_global_validity_block<DB, R, W>( +pub fn verify_global_validity_block<DB, W>( block: &BlockDocument, db: &DB, - r: &R, _wot_index: &HashMap<PubKey, WotId>, _wot_db: &BinFreeStructDb<W>, ) -> Result<(), GlobalVerifyBlockError> where - DB: DbReadable, - R: DbReader, + DB: BcDbInReadTx, W: WebOfTrust, { // Rules that do not concern genesis block @@ -55,7 +53,6 @@ where // Get previous block let previous_block_opt = durs_bc_db_reader::blocks::get_block_in_local_blockchain( db, - r, BlockNumber(block.number().0 - 1), )?; diff --git a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs index 6277d7a345095cc9ed5b970699558e353064f44f..57b8b0c8a04b4dee8981bab42e6f0cdb6e1ea8dd 100644 --- a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs +++ b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs @@ -89,8 +89,7 @@ pub fn receive_blocks(bc: &mut BlockchainModule, blocks: Vec<BlockDocument>) { CheckAndApplyBlockReturn::ForkBlock => { info!("blockchain: new fork block(#{})", blockstamp); if let Ok(Some(new_bc_branch)) = fork_algo::fork_resolution_algo( - &db, - w.as_ref(), + &BcDbRwWithWriter { db: &db, w: &w }, &bc.fork_tree, unwrap!(bc.currency_params).fork_window_size, bc.current_blockstamp, diff --git a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs index cf635c60ab72f37a80fabf91c65388a9ec149276..96e22cd135bda5ff3adae73dcd16d0f5abf9eaeb 100644 --- a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs +++ b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs @@ -16,7 +16,7 @@ use dubp_block_doc::block::BlockDocumentTrait; use dubp_common_doc::Blockstamp; use durs_bc_db_reader::blocks::fork_tree::ForkTree; -use durs_bc_db_reader::{DbReadable, DbReader}; +use durs_bc_db_reader::BcDbInReadTx; use durs_bc_db_writer::DbError; use std::collections::HashSet; @@ -25,15 +25,14 @@ pub static ADVANCE_BLOCKS: &u32 = &3; /// Advance blockchain time required (in seconds) pub static ADVANCE_TIME: &u64 = &900; -pub fn fork_resolution_algo<DB: DbReadable, R: DbReader>( +pub fn fork_resolution_algo<DB: BcDbInReadTx>( db: &DB, - r: &R, fork_tree: &ForkTree, fork_window_size: usize, current_blockstamp: Blockstamp, invalid_blocks: &HashSet<Blockstamp>, ) -> Result<Option<Vec<Blockstamp>>, DbError> { - let current_bc_time = durs_bc_db_reader::current_meta_datas::get_current_common_time_(db, r)?; + let current_bc_time = durs_bc_db_reader::current_meta_datas::get_current_common_time_(db)?; debug!( "fork_resolution_algo({}, {})", @@ -54,7 +53,7 @@ pub fn fork_resolution_algo<DB: DbReadable, R: DbReader>( let branch_head_blockstamp = branch.last().expect("safe unwrap"); let branch_head_median_time = - durs_bc_db_reader::blocks::get_fork_block(db, r, *branch_head_blockstamp)? + durs_bc_db_reader::blocks::get_fork_block(db, *branch_head_blockstamp)? .unwrap_or_else(|| { panic!( "Db corrupted: fork block {} referenced in fork tree but not exist in db.", @@ -183,8 +182,7 @@ mod tests { assert_eq!( None, db.read(|r| fork_resolution_algo( - &db, - r, + &BcDbRwWithReader { db, r }, &fork_tree, fork_window_size, current_blockstamp, @@ -228,8 +226,7 @@ mod tests { determining_blockstamp, ]), db.read(|r| fork_resolution_algo( - &db, - r, + &BcDbRwWithReader { db, r }, &mut fork_tree, fork_window_size, current_blockstamp, @@ -261,8 +258,7 @@ mod tests { assert_eq!( Some(new_main_blocks.iter().map(|b| b.blockstamp()).collect()), db.read(|r| fork_resolution_algo( - &db, - r, + &BcDbRwWithReader { db, r }, &mut fork_tree, fork_window_size, current_blockstamp, diff --git a/lib/modules/blockchain/blockchain/src/fork/rollback.rs b/lib/modules/blockchain/blockchain/src/fork/rollback.rs index 27b155896e474e6a516373e318a6eef0dd5b2a9e..e7a01da76edbf6ae0b38fdb4f7f049a7a63c52bd 100644 --- a/lib/modules/blockchain/blockchain/src/fork/rollback.rs +++ b/lib/modules/blockchain/blockchain/src/fork/rollback.rs @@ -18,6 +18,7 @@ use crate::fork::revert_block::ValidBlockRevertReqs; use crate::*; use dubp_common_doc::traits::Document; use dubp_common_doc::Blockstamp; +use durs_bc_db_reader::BcDbRead; use durs_common_tools::fatal_error; use unwrap::unwrap; @@ -35,11 +36,11 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>) let db_tx_result = db.write(|mut w| { // Rollback (revert old branch) while bc.current_blockstamp.id.0 > last_common_block_number { - if let Some(dal_block) = - durs_bc_db_reader::blocks::get_fork_block(&db, w.as_ref(), bc.current_blockstamp) - .unwrap_or_else(|_| { - fatal_error!("revert block {} fail !", bc.current_blockstamp); - }) + if let Some(dal_block) = db + .r(|db_r| durs_bc_db_reader::blocks::get_fork_block(db_r, bc.current_blockstamp)) + .unwrap_or_else(|_| { + fatal_error!("revert block {} fail !", bc.current_blockstamp); + }) { let blockstamp = dal_block.block.blockstamp(); debug!("try to revert block #{}", blockstamp); @@ -85,7 +86,7 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>) let mut new_branch_is_valid = true; for blockstamp in &new_bc_branch { if let Ok(Some(dal_block)) = - durs_bc_db_reader::blocks::get_fork_block(&db, w.as_ref(), *blockstamp) + db.r(|db_r| durs_bc_db_reader::blocks::get_fork_block(db_r, *blockstamp)) { new_branch_blocks.push(dal_block.clone()); match check_and_apply_block(bc, &db, &mut w, dal_block.block) { diff --git a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs index 447d0f82a18c5ee51d0b7abf76e2a671f3e4b5b6..846873440a48a5e707604cf2ea1f188394eee953 100644 --- a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs +++ b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs @@ -19,12 +19,16 @@ use crate::dubp::apply::exec_currency_queries; use crate::*; use dubp_block_doc::block::BlockDocumentTrait; use dubp_common_doc::traits::Document; +use durs_bc_db_reader::BcDbRead; use unwrap::unwrap; pub fn apply_stackable_blocks(bc: &mut BlockchainModule) { 'blocks: loop { let stackable_blocks = - durs_bc_db_reader::blocks::get_stackables_blocks(bc.db(), bc.current_blockstamp) + bc.db() + .r(|db_r| { + durs_bc_db_reader::blocks::get_stackables_blocks(db_r, bc.current_blockstamp) + }) .expect("Fatal error : Fail to read ForksDB !"); if stackable_blocks.is_empty() { diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index 3c6137f3ad2315ef11c8eed371fff8ab16c7ab79..5ee4c5ef8abe44e08f051c9d2135b3de420c84d6 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -59,6 +59,7 @@ use dubp_common_doc::Blockstamp; use dubp_currency_params::{CurrencyName, CurrencyParameters}; use dup_crypto::keys::*; use durs_bc_db_reader::blocks::fork_tree::ForkTree; +use durs_bc_db_reader::BcDbRead; use durs_bc_db_writer::*; use durs_common_tools::fatal_error; use durs_message::events::*; @@ -180,15 +181,16 @@ impl BlockchainModule { wot_databases: WotsV10DBs, ) -> Result<BlockchainModule, DbError> { // Get current blockstamp - let current_blockstamp = - durs_bc_db_reader::current_meta_datas::get_current_blockstamp(&db)?.unwrap_or_default(); + let current_blockstamp = db + .r(|db_r| durs_bc_db_reader::current_meta_datas::get_current_blockstamp(db_r))? + .unwrap_or_default(); // Get fork tree - let fork_tree = durs_bc_db_reader::current_meta_datas::get_fork_tree(&db)?; + let fork_tree = db.r(|db_r| durs_bc_db_reader::current_meta_datas::get_fork_tree(db_r))?; // Get wot index let wot_index: HashMap<PubKey, WotId> = - durs_bc_db_reader::indexes::identities::get_wot_index(&db)?; + db.r(|db_r| durs_bc_db_reader::indexes::identities::get_wot_index(db_r))?; Ok(BlockchainModule { router_sender, diff --git a/lib/modules/blockchain/blockchain/src/requests/received.rs b/lib/modules/blockchain/blockchain/src/requests/received.rs index 2734eeec05710b6f871910bdd3ed47b893b3c12d..be35e86fea8c9f63493b7af2d2846d449a0a5240 100644 --- a/lib/modules/blockchain/blockchain/src/requests/received.rs +++ b/lib/modules/blockchain/blockchain/src/requests/received.rs @@ -17,6 +17,7 @@ use crate::*; //use dubp_user_docs::documents::identity::IdentityDocument; +use durs_bc_db_reader::BcDbRead; use durs_message::requests::*; use durs_module::*; @@ -37,10 +38,9 @@ pub fn receive_req( BlockchainRequest::CurrentBlock => { debug!("BlockchainModule : receive BlockchainRequest::CurrentBlock()"); - if let Ok(block_opt) = bc.db().read(|r| { + if let Ok(block_opt) = bc.db().r(|db_r| { durs_bc_db_reader::blocks::get_block_in_local_blockchain( - bc.db(), - r, + db_r, bc.current_blockstamp.id, ) }) { @@ -73,12 +73,8 @@ pub fn receive_req( block_number ); - if let Ok(block_opt) = bc.db().read(|r| { - durs_bc_db_reader::blocks::get_block_in_local_blockchain( - bc.db(), - r, - block_number, - ) + if let Ok(block_opt) = bc.db().r(|db_r| { + durs_bc_db_reader::blocks::get_block_in_local_blockchain(db_r, block_number) }) { if let Some(block) = block_opt { debug!( @@ -112,10 +108,9 @@ pub fn receive_req( first_block_number, count ); - if let Ok(blocks) = bc.db().read(|r| { + if let Ok(blocks) = bc.db().r(|db_r| { durs_bc_db_reader::blocks::get_blocks_in_local_blockchain( - bc.db(), - r, + db_r, first_block_number, count, ) @@ -150,16 +145,22 @@ pub fn receive_req( req_from, req_id, &BlockchainResponse::UIDs( - pubkeys - .into_iter() - .map(|p| { - ( - p, - durs_bc_db_reader::indexes::identities::get_uid(bc.db(), &p) - .expect("Fatal error : get_uid : Fail to read WotV10DB !"), - ) + bc.db() + .r(|db_r| { + Ok(pubkeys + .into_iter() + .map(|p| { + ( + p, + durs_bc_db_reader::indexes::identities::get_uid( + db_r, &p, + ) + .unwrap_or(None), + ) + }) + .collect()) }) - .collect(), + .expect("Fatal error : get_uid : Fail to read DB !"), ), ); } /*BlockchainRequest::GetIdentities(filters) => { diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs b/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs index f88328b3929a8464afddb373323f3298e69206a9..1c3e993f3d6d92cc3fea4df2850bd9d9f62a2a16 100644 --- a/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs +++ b/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs @@ -14,6 +14,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::sync::*; +use durs_bc_db_reader::BcDbRead; use pbr::ProgressBar; pub fn execute( @@ -29,8 +30,9 @@ pub fn execute( let blocks_job_begin = SystemTime::now(); // Get fork tree - let mut fork_tree = - durs_bc_db_reader::current_meta_datas::get_fork_tree(&db).expect("Fail to read DB."); + let mut fork_tree = db + .r(|db_r| durs_bc_db_reader::current_meta_datas::get_fork_tree(db_r)) + .expect("Fail to read DB."); // Listen db requets let mut chunk_index = 0; diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs index 07aa12b02c59fa9bc7a61e46317bda379062b380..f607b5ce513645e3a5bb832ed62aa352625de24d 100644 --- a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs @@ -27,7 +27,7 @@ use dubp_common_doc::traits::Document; use dubp_common_doc::{BlockNumber, Blockstamp}; use dubp_currency_params::{CurrencyName, CurrencyParameters}; use dup_crypto::keys::PubKey; -use durs_bc_db_reader::DbReadable; +use durs_bc_db_reader::BcDbRead; use durs_bc_db_writer::writers::requests::WotsDBsWriteQuery; use durs_bc_db_writer::WotsV10DBs; use durs_common_tools::fatal_error; @@ -98,8 +98,8 @@ impl BlockApplicator { // Find expire_certs let expire_certs = if let Some(db) = self.db.take() { let expire_certs = db - .read(|r| { - durs_bc_db_reader::indexes::certs::find_expire_certs(&db, r, blocks_expiring) + .r(|db_r| { + durs_bc_db_reader::indexes::certs::find_expire_certs(db_r, blocks_expiring) }) .expect("find_expire_certs() : DbError"); self.db = Some(db); diff --git a/lib/modules/blockchain/blockchain/src/sync/download/json_reader_worker.rs b/lib/modules/blockchain/blockchain/src/sync/download/json_reader_worker.rs index 029afb83994dc50c8dd9abb6ee9059ede2cb9c2f..ba9faceb3839f7d729941a941216f34bb1a45414 100644 --- a/lib/modules/blockchain/blockchain/src/sync/download/json_reader_worker.rs +++ b/lib/modules/blockchain/blockchain/src/sync/download/json_reader_worker.rs @@ -17,6 +17,7 @@ use crate::sync::*; use dubp_block_doc::parser::parse_json_block; use dubp_common_doc::traits::Document; use dubp_common_doc::Blockstamp; +use durs_bc_db_reader::BcDbRead; use durs_common_tools::fatal_error; use failure::Error; use rayon::prelude::*; @@ -96,10 +97,10 @@ pub fn json_reader_worker( debug!("Get local current blockstamp..."); let db_path = durs_conf::get_blockchain_db_path(profile_path); let db = durs_bc_db_reader::open_db_ro(&db_path).expect("Fail to open DB."); - let current_blockstamp: Blockstamp = - durs_bc_db_reader::current_meta_datas::get_current_blockstamp(&db) - .expect("get_current_blockstamp: Fail to read DB !") - .unwrap_or_default(); + let current_blockstamp = db + .r(|db_r| durs_bc_db_reader::current_meta_datas::get_current_blockstamp(db_r)) + .expect("get_current_blockstamp: Fail to read DB !") + .unwrap_or_default(); info!("Local current blockstamp = {}", current_blockstamp); // Get first chunk number diff --git a/lib/modules/blockchain/blockchain/src/sync/mod.rs b/lib/modules/blockchain/blockchain/src/sync/mod.rs index ab580d34431bb32746d472cdc9e7e3d781c76a7e..8f3b279597872f10a10887f97c721a600a577043 100644 --- a/lib/modules/blockchain/blockchain/src/sync/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/mod.rs @@ -23,6 +23,7 @@ use dubp_common_doc::Blockstamp; use dubp_common_doc::{BlockHash, BlockNumber}; use dubp_currency_params::{CurrencyName, CurrencyParameters}; use dup_crypto::keys::*; +use durs_bc_db_reader::BcDbRead; use durs_bc_db_writer::writers::requests::*; use durs_common_tools::fatal_error; use durs_wot::WotId; @@ -189,10 +190,10 @@ pub fn local_sync<DC: DursConfTrait>( // Get local current blockstamp debug!("Get local current blockstamp..."); - let current_blockstamp: Blockstamp = - durs_bc_db_reader::current_meta_datas::get_current_blockstamp(&db) - .expect("DbError : fail to get current blockstamp !") - .unwrap_or_default(); + let current_blockstamp: Blockstamp = db + .r(|db_r| durs_bc_db_reader::current_meta_datas::get_current_blockstamp(db_r)) + .expect("DbError : fail to get current blockstamp !") + .unwrap_or_default(); debug!("Success to get local current blockstamp."); // Node is already synchronized ? @@ -202,9 +203,9 @@ pub fn local_sync<DC: DursConfTrait>( } // Get wot index - let wot_index: HashMap<PubKey, WotId> = - durs_bc_db_reader::indexes::identities::get_wot_index(&db) - .expect("Fatal eror : get_wot_index : Fail to read blockchain databases"); + let wot_index: HashMap<PubKey, WotId> = db + .r(|db_r| durs_bc_db_reader::indexes::identities::get_wot_index(db_r)) + .expect("Fatal eror : get_wot_index : Fail to read blockchain databases"); // Start sync let sync_start_time = SystemTime::now(); diff --git a/lib/modules/blockchain/blockchain/tests/common.rs b/lib/modules/blockchain/blockchain/tests/common.rs index d87405b5d93ea8eb736d234e3a323975d7d29654..fe02380fa83befab7442250df802792989bc51b2 100644 --- a/lib/modules/blockchain/blockchain/tests/common.rs +++ b/lib/modules/blockchain/blockchain/tests/common.rs @@ -39,12 +39,12 @@ pub fn stop_and_clean( tmp_profile_path: PathBuf, ) { // Send STOP signal to blockchain module - bc_sender + /*bc_sender .send(DursMsg::Stop) .expect("Fail to send stop signal to blockchain module."); handle .join() - .expect("Blockchain module fail to stop correctly."); + .expect("Blockchain module fail to stop correctly.");*/ // Clear user datas std::fs::remove_dir_all(tmp_profile_path).expect("Fail to remove tmp dir."); diff --git a/lib/modules/gva/src/db.rs b/lib/modules/gva/src/db.rs index 1b492c0d07c0f0b8561185f96c7938ad54a7409f..dd893c58b2a6fc1d93de89f4e68b59850f4a9c7e 100644 --- a/lib/modules/gva/src/db.rs +++ b/lib/modules/gva/src/db.rs @@ -15,7 +15,7 @@ //! Gva Module: database requests +#[cfg(test)] +pub(crate) use durs_bc_db_reader::BcDbInReadTx as BcDbRo; #[cfg(not(test))] pub(crate) use durs_bc_db_reader::BcDbRo; -#[cfg(test)] -pub(crate) use durs_bc_db_reader::MockBcDbRoTrait as BcDbRo; diff --git a/lib/modules/gva/src/schema/entities/block.rs b/lib/modules/gva/src/schema/entities/block.rs index 65e0e2b13e51d0b5a2c98e2832b38f8a8074f9a9..972a9bc669dd130346236e5ba97843e7fc9a56ce 100644 --- a/lib/modules/gva/src/schema/entities/block.rs +++ b/lib/modules/gva/src/schema/entities/block.rs @@ -21,7 +21,7 @@ use chrono::NaiveDateTime; use dubp_block_doc::block::BlockDocumentTrait; use dubp_common_doc::traits::Document; use durs_bc_db_reader::blocks::DbBlock; -use durs_bc_db_reader::{BcDbRoTrait, DbError}; +use durs_bc_db_reader::{BcDbInReadTx_, BcDbWithReader, DbError}; use durs_common_tools::fatal_error; use juniper::{Executor, FieldResult}; use juniper_from_schema::{QueryTrail, Walked}; @@ -43,7 +43,7 @@ impl Block { trail.issuer_name() } // Convert BlockDb (db entity) into Block (gva entity) - pub(crate) fn from_block_db<DB: BcDbRoTrait>( + pub(crate) fn from_block_db<DB: BcDbWithReader>( db: &DB, block_db: DbBlock, ask_issuer_name: bool, diff --git a/lib/modules/gva/src/schema/queries/block.rs b/lib/modules/gva/src/schema/queries/block.rs index 884d9b55098633856acfc2eac1b29e4339c823e2..09cde41e968c35bba1901d9591003abe0972850d 100644 --- a/lib/modules/gva/src/schema/queries/block.rs +++ b/lib/modules/gva/src/schema/queries/block.rs @@ -17,10 +17,10 @@ use crate::schema::entities::block::Block; use dubp_common_doc::BlockNumber; -use durs_bc_db_reader::{BcDbRoTrait, DbError}; +use durs_bc_db_reader::{BcDbInReadTx_, BcDbWithReader, DbError}; use juniper_from_schema::{QueryTrail, Walked}; -pub(crate) fn execute<DB: BcDbRoTrait>( +pub(crate) fn execute<DB: BcDbWithReader>( db: &DB, trail: &QueryTrail<'_, Block, Walked>, number: i32, diff --git a/lib/modules/gva/src/schema/queries/blocks.rs b/lib/modules/gva/src/schema/queries/blocks.rs index 5e539ad348043dda2cd8e31bb84b59f9a6492b3f..9c0fd89852b351a10ccc7c669edc5f11d5d9b307 100644 --- a/lib/modules/gva/src/schema/queries/blocks.rs +++ b/lib/modules/gva/src/schema/queries/blocks.rs @@ -22,10 +22,10 @@ use crate::schema::inputs::paging::{FilledPaging, Paging}; use crate::schema::inputs::sort_order::SortOrder; use dubp_common_doc::BlockNumber; use durs_bc_db_reader::blocks::DbBlock; -use durs_bc_db_reader::{BcDbRoTrait, DbError}; +use durs_bc_db_reader::{BcDbInReadTx_, BcDbWithReader, DbError}; use juniper_from_schema::{QueryTrail, Walked}; -pub(crate) fn execute<DB: BcDbRoTrait>( +pub(crate) fn execute<DB: BcDbWithReader>( db: &DB, trail: &QueryTrail<'_, BlocksPage, Walked>, paging_opt: Option<Paging>, diff --git a/lib/modules/gva/src/schema/queries/current.rs b/lib/modules/gva/src/schema/queries/current.rs index ab52fdeebaf1a065dae84da34ca50c83c5007836..2a8e3b20abef72b216c605f3b268d33761dc1373 100644 --- a/lib/modules/gva/src/schema/queries/current.rs +++ b/lib/modules/gva/src/schema/queries/current.rs @@ -16,10 +16,10 @@ // ! Module execute GraphQl schema current query use crate::schema::entities::block::Block; -use durs_bc_db_reader::{BcDbRoTrait, DbError}; +use durs_bc_db_reader::{BcDbInReadTx_, BcDbWithReader, DbError}; use juniper_from_schema::{QueryTrail, Walked}; -pub(crate) fn execute<DB: BcDbRoTrait>( +pub(crate) fn execute<DB: BcDbWithReader>( db: &DB, trail: &QueryTrail<'_, Block, Walked>, ) -> Result<Option<Block>, DbError> { diff --git a/lib/tools/dbs-tools/src/kv_db.rs b/lib/tools/dbs-tools/src/kv_db.rs index 4edbf67ebf74a059112da76c4fa9cbd74f068e60..e94247a9931ad4ffd13118f8b987f050da8acff6 100644 --- a/lib/tools/dbs-tools/src/kv_db.rs +++ b/lib/tools/dbs-tools/src/kv_db.rs @@ -18,8 +18,8 @@ mod file; pub use file::{ - KvFileDbHandler, KvFileDbRead, KvFileDbReader, KvFileDbRoHandler, KvFileDbSchema, - KvFileDbStoreType, KvFileDbWriter, + from_db_value, KvFileDbHandler, KvFileDbRead, KvFileDbReader, KvFileDbRoHandler, + KvFileDbSchema, KvFileDbStoreType, KvFileDbWriter, }; pub use rkv::{ store::multi::Iter, IntegerStore, MultiIntegerStore, MultiStore, diff --git a/lib/tools/dbs-tools/src/kv_db/file.rs b/lib/tools/dbs-tools/src/kv_db/file.rs index 6f15d753502c51a3495c35d4b442cb535a2e2d7d..7f0d52f6bd4c067752ad5e9366e45c1b247a38e7 100644 --- a/lib/tools/dbs-tools/src/kv_db/file.rs +++ b/lib/tools/dbs-tools/src/kv_db/file.rs @@ -27,34 +27,38 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; /// Key-value database reader -pub type KvFileDbReader<'a> = &'a rkv::Reader<'a>; +pub struct KvFileDbReader<'r>(&'r rkv::Reader<'r>); -/// Mock db reader -pub struct MockKvFileDbReader; - -impl MockKvFileDbReader { - pub fn new() -> Self { - Self - } -} - -impl rkv::Readable for MockKvFileDbReader { +impl<'r> rkv::Readable for KvFileDbReader<'r> { fn get<K: AsRef<[u8]>>( &self, - _db: rkv::Database, - _k: &K, + db: rkv::Database, + k: &K, ) -> Result<Option<Value>, rkv::StoreError> { - unimplemented!() + self.0.get(db, k) } - fn open_ro_cursor(&self, _db: rkv::Database) -> Result<rkv::RoCursor, rkv::StoreError> { - unimplemented!() + fn open_ro_cursor(&self, db: rkv::Database) -> Result<rkv::RoCursor, rkv::StoreError> { + self.0.open_ro_cursor(db) } } /// Key-value database writer -pub struct KvFileDbWriter<'a> { +pub struct KvFileDbWriter<'w> { buffer: Vec<u8>, - writer: rkv::Writer<'a>, + writer: rkv::Writer<'w>, +} + +impl<'w> rkv::Readable for KvFileDbWriter<'w> { + fn get<K: AsRef<[u8]>>( + &self, + db: rkv::Database, + k: &K, + ) -> Result<Option<Value>, rkv::StoreError> { + self.writer.get(db, k) + } + fn open_ro_cursor(&self, db: rkv::Database) -> Result<rkv::RoCursor, rkv::StoreError> { + self.writer.open_ro_cursor(db) + } } impl<'a> AsRef<rkv::Writer<'a>> for KvFileDbWriter<'a> { @@ -69,6 +73,16 @@ impl<'a> AsMut<rkv::Writer<'a>> for KvFileDbWriter<'a> { } } +#[inline] +/// Convert DB value to a rust type +pub fn from_db_value<T: DeserializeOwned>(v: Value) -> Result<T, DbError> { + if let Value::Blob(bytes) = v { + Ok(bincode::deserialize::<T>(bytes)?) + } else { + Err(DbError::DBCorrupted) + } +} + /// Key-value file Database handler #[derive(Debug)] pub struct KvFileDbHandler { @@ -150,9 +164,6 @@ impl KvFileDbRoHandler { /// Key-value file Database read operations pub trait KvFileDbRead: Sized { - /// Convert DB value to a rust type - fn from_db_value<T: DeserializeOwned>(v: Value) -> Result<T, DbError>; - /// get a single store fn get_store(&self, store_name: &str) -> &super::SingleStore; @@ -172,10 +183,6 @@ pub trait KvFileDbRead: Sized { } impl KvFileDbRead for KvFileDbRoHandler { - #[inline] - fn from_db_value<T: DeserializeOwned>(v: Value) -> Result<T, DbError> { - KvFileDbHandler::from_db_value(v) - } #[inline] fn get_store(&self, store_name: &str) -> &super::SingleStore { self.0.get_store(store_name) @@ -245,14 +252,6 @@ impl Debug for KvFileDbStore { } impl KvFileDbRead for KvFileDbHandler { - #[inline] - fn from_db_value<T: DeserializeOwned>(v: Value) -> Result<T, DbError> { - if let Value::Blob(bytes) = v { - Ok(bincode::deserialize::<T>(bytes)?) - } else { - Err(DbError::DBCorrupted) - } - } fn get_int_store(&self, store_name: &str) -> &super::IntegerStore<u32> { if let Some(store_enum) = self.stores.get(store_name) { if let KvFileDbStore::SingleIntKey(store) = store_enum { @@ -304,7 +303,7 @@ impl KvFileDbRead for KvFileDbHandler { where F: FnOnce(KvFileDbReader) -> Result<R, DbError>, { - Ok(f(&self.arc_clone().read()?.read()?)?) + Ok(f(KvFileDbReader(&self.arc_clone().read()?.read()?))?) } } @@ -437,7 +436,7 @@ mod tests { let store_test1 = db.get_int_store("test1"); db.write(|mut w| { - store_test1.put(w.as_mut(), 3, &Value::Str("toto"))?; + store_test1.put(db.w.as_mut(), 3, &Value::Str("toto"))?; Ok(w) })?; @@ -449,7 +448,7 @@ mod tests { ); db.write(|mut w| { - store_test1.put(w.as_mut(), 3, &Value::Str("titi"))?; + store_test1.put(db.w.as_mut(), 3, &Value::Str("titi"))?; Ok(w) })?; @@ -459,7 +458,7 @@ mod tests { ); db.write(|mut w| { - store_test1.put(w.as_mut(), 3, &Value::Str("tutu"))?; + store_test1.put(db.w.as_mut(), 3, &Value::Str("tutu"))?; assert_eq!( Some("titi".to_owned()), get_int_store_str_val(&ro_db, "test1", 3)?