From 8d8defa7cc51800eec74e53789e3d58460cf4b9f Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sun, 3 Nov 2019 22:57:48 +0100
Subject: [PATCH] [ref] bc-db-reader: blocks: use parent transaction

---
 Cargo.lock                                    |  2 +
 lib/modules-lib/bc-db-reader/src/blocks.rs    | 59 ++++++++++---------
 lib/modules/blockchain/blockchain/src/dbex.rs | 16 +++--
 .../blockchain/src/requests/received.rs       | 13 ++--
 4 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 687a3333..f40c81f1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -737,6 +737,8 @@ dependencies = [
  "dubp-user-docs-tests-tools 0.1.0",
  "dup-crypto 0.7.0",
  "dup-crypto-tests-tools 0.1.0",
+ "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "json-pest-parser 0.2.0",
 ]
 
 [[package]]
diff --git a/lib/modules-lib/bc-db-reader/src/blocks.rs b/lib/modules-lib/bc-db-reader/src/blocks.rs
index 2e276722..f5d62ad4 100644
--- a/lib/modules-lib/bc-db-reader/src/blocks.rs
+++ b/lib/modules-lib/bc-db-reader/src/blocks.rs
@@ -95,18 +95,17 @@ pub fn already_have_block<DB: DbReadable>(
 }
 
 /// Get block
-pub fn get_block<DB: DbReadable>(
+pub fn get_block<DB: DbReadable, R: DbReader>(
     db: &DB,
+    r: &R,
     blockstamp: Blockstamp,
 ) -> Result<Option<DbBlock>, DbError> {
-    db.read(|r| {
-        let opt_dal_block = get_db_block_in_local_blockchain(db, r, blockstamp.id)?;
-        if opt_dal_block.is_none() {
-            get_fork_block(db, r, blockstamp)
-        } else {
-            Ok(opt_dal_block)
-        }
-    })
+    let opt_dal_block = get_dal_block_in_local_blockchain(db, r, blockstamp.id)?;
+    if opt_dal_block.is_none() {
+        get_fork_block(db, r, blockstamp)
+    } else {
+        Ok(opt_dal_block)
+    }
 }
 
 /// Get fork block
@@ -162,27 +161,26 @@ pub fn get_db_block_in_local_blockchain<DB: DbReadable, R: DbReader>(
 }
 
 /// Get several blocks in local blockchain
-pub fn get_blocks_in_local_blockchain<DB: DbReadable>(
+pub fn get_blocks_in_local_blockchain<DB: DbReadable, R: DbReader>(
     db: &DB,
+    r: &R,
     first_block_number: BlockNumber,
     mut count: u32,
 ) -> Result<Vec<BlockDocument>, DbError> {
-    db.read(|r| {
-        let bc_store = db.get_int_store(MAIN_BLOCKS);
-        let mut blocks = Vec::with_capacity(count as usize);
-        let mut current_block_number = first_block_number;
+    let bc_store = 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);
-            count -= 1;
-            if count > 0 {
-                current_block_number = BlockNumber(current_block_number.0 + 1);
-            } else {
-                return Ok(blocks);
-            }
+    while let Some(v) = bc_store.get(r, current_block_number.0)? {
+        blocks.push(DB::from_db_value::<DbBlock>(v)?.block);
+        count -= 1;
+        if count > 0 {
+            current_block_number = BlockNumber(current_block_number.0 + 1);
+        } else {
+            return Ok(blocks);
         }
-        Ok(blocks)
-    })
+    }
+    Ok(blocks)
 }
 
 /// Get several blocks in local blockchain by their number
@@ -208,11 +206,14 @@ pub fn get_current_frame<DB: DbReadable>(
 ) -> Result<HashMap<PubKey, usize>, DbError> {
     let frame_begin = current_block.number().0 - 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 blocks = db.read(|r| {
+        get_blocks_in_local_blockchain(
+            db,
+            r,
+            BlockNumber(frame_begin),
+            current_block.current_frame_size() as u32,
+        )
+    })?;
 
     let mut current_frame: HashMap<PubKey, usize> = HashMap::new();
     for block in blocks {
diff --git a/lib/modules/blockchain/blockchain/src/dbex.rs b/lib/modules/blockchain/blockchain/src/dbex.rs
index 8efde336..73415b31 100644
--- a/lib/modules/blockchain/blockchain/src/dbex.rs
+++ b/lib/modules/blockchain/blockchain/src/dbex.rs
@@ -402,12 +402,16 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) {
             // Open blockchain database
             let db = durs_bc_db_reader::open_db_ro(&db_path.as_path()).expect("Fail to open DB.");
             // Get blocks_times
-            let all_blocks = durs_bc_db_reader::blocks::get_blocks_in_local_blockchain(
-                &db,
-                BlockNumber(0),
-                10_000_000,
-            )
-            .expect("Fail to get all blocks");
+            let all_blocks = db
+                .read(|r| {
+                    durs_bc_db_reader::blocks::get_blocks_in_local_blockchain(
+                        &db,
+                        r,
+                        BlockNumber(0),
+                        10_000_000,
+                    )
+                })
+                .expect("Fail to get all blocks");
             let current_bc_number = all_blocks.last().expect("empty blockchain").number();
             let current_bc_time = all_blocks.last().expect("empty blockchain").common_time();
             let blocks_times: HashMap<BlockNumber, u64> = all_blocks
diff --git a/lib/modules/blockchain/blockchain/src/requests/received.rs b/lib/modules/blockchain/blockchain/src/requests/received.rs
index 48c4c7ef..2734eeec 100644
--- a/lib/modules/blockchain/blockchain/src/requests/received.rs
+++ b/lib/modules/blockchain/blockchain/src/requests/received.rs
@@ -112,11 +112,14 @@ pub fn receive_req(
                     first_block_number, count
                 );
 
-                if let Ok(blocks) = durs_bc_db_reader::blocks::get_blocks_in_local_blockchain(
-                    bc.db(),
-                    first_block_number,
-                    count,
-                ) {
+                if let Ok(blocks) = bc.db().read(|r| {
+                    durs_bc_db_reader::blocks::get_blocks_in_local_blockchain(
+                        bc.db(),
+                        r,
+                        first_block_number,
+                        count,
+                    )
+                }) {
                     if blocks.is_empty() {
                         debug!(
                             "BlockchainModule : Req : not found chunk (#{}, {}) in bdd !",
-- 
GitLab