From 085e50200475af53e625c62b15d9e19010309f47 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Fri, 19 May 2023 15:46:29 +0200
Subject: [PATCH] [enh] rename BMA tx history functions

---
 dbs-reader/src/txs_history.rs | 33 +++++++++++++++++++--------------
 src/lib.rs                    | 15 +++++++++------
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/dbs-reader/src/txs_history.rs b/dbs-reader/src/txs_history.rs
index d23b967..3d3b443 100644
--- a/dbs-reader/src/txs_history.rs
+++ b/dbs-reader/src/txs_history.rs
@@ -449,7 +449,7 @@ pub fn get_txs_history_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
 ) -> KvResult<TxsHistory> {
     // Get written TX
     let TxsHistory { sent, received, .. } =
-        get_tx_history_bma_by_blocks(gva_db_ro, pubkey, None, None)?;
+        get_txs_history_bma_by_blocks(gva_db_ro, pubkey, None, None)?;
     // Get pending TX
     let TxsHistory {
         sending, pending, ..
@@ -464,7 +464,7 @@ pub fn get_txs_history_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
 }
 
 /// Get TX history of a pubkey from a block range. Needed for BMA only
-pub fn get_tx_history_bma_by_blocks<GvaDb: GvaV1DbReadable>(
+pub fn get_txs_history_bma_by_blocks<GvaDb: GvaV1DbReadable>(
     gva_db_ro: &GvaDb,
     pubkey: PublicKey,
     from: Option<u32>,
@@ -512,7 +512,7 @@ pub fn get_tx_history_bma_by_blocks<GvaDb: GvaV1DbReadable>(
 }
 
 /// Get TX history of a pubkey from a median_time range. Needed for BMA only
-pub fn get_tx_history_bma_by_times<GvaDb: GvaV1DbReadable>(
+pub fn get_txs_history_bma_by_times<GvaDb: GvaV1DbReadable>(
     gva_db_ro: &GvaDb,
     pubkey: PublicKey,
     from: Option<u64>,
@@ -521,26 +521,29 @@ pub fn get_tx_history_bma_by_times<GvaDb: GvaV1DbReadable>(
     let from_block = match from {
         Some(from_time) => gva_db_ro
             .blocks_by_common_time()
-            .iter(U64BE(from_time)..)
-            .values()
-            .next_res()?
+            .iter(U64BE(from_time).., |it| it.values().next_res())?
             .unwrap_or(u32::MAX),
         None => 0,
     };
     let to_block = match to {
         Some(to_time) => gva_db_ro
             .blocks_by_common_time()
-            .iter_rev(..U64BE(to_time))
-            .values()
-            .next_res()?
-            .unwrap_or(0),
+            .iter_rev(..U64BE(to_time), |it| it.values().next_res())?
+            .unwrap_or(u32::MAX),
         None => u32::MAX,
     };
-    get_tx_history_bma_by_blocks(gva_db_ro, pubkey, Some(from_block), Some(to_block))
+    let TxsHistory { sent, received, .. } =
+        get_txs_history_bma_by_blocks(gva_db_ro, pubkey, Some(from_block), Some(to_block))?;
+    Ok(TxsHistory {
+        sent,
+        received,
+        sending: Vec::new(),
+        pending: Vec::new(),
+    })
 }
 
 /// Get mempool TX of a pubkey. Needed for BMA only
-pub fn get_mempool_tx_bma<TxsMpDb: TxsMpV2DbReadable>(
+pub fn get_txs_history_bma_mempool<TxsMpDb: TxsMpV2DbReadable>(
     txs_mp_db_ro: &TxsMpDb,
     pubkey: PublicKey,
 ) -> KvResult<TxsHistory> {
@@ -578,17 +581,19 @@ pub fn get_mempool_tx_bma<TxsMpDb: TxsMpV2DbReadable>(
 
 #[cfg(test)]
 mod tests {
-    use super::*;
     use duniter_core::{
         common::prelude::{BlockHash, Blockstamp},
         crypto::keys::ed25519::PublicKey,
         documents::transaction::{TransactionDocumentV10, TransactionDocumentV10Stringified},
         documents_parser::prelude::FromStringObject,
     };
-    use duniter_gva_db::GvaV1DbWritable;
     use maplit::btreeset;
     use unwrap::unwrap;
 
+    use duniter_gva_db::GvaV1DbWritable;
+
+    use super::*;
+
     fn gen_tx(hash: Hash, written_block_number: BlockNumber) -> GvaTxDbV1 {
         GvaTxDbV1 {
             tx: unwrap!(TransactionDocumentV10::from_string_object(
diff --git a/src/lib.rs b/src/lib.rs
index 07967e3..1d73b2f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -217,7 +217,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
         }))
     }
     // Needed for BMA only
-    fn get_tx_history_bma_by_blocks(
+    fn get_txs_history_bma_by_blocks(
         profile_path_opt: Option<&Path>,
         pubkey: PublicKey,
         from: Option<u32>,
@@ -225,7 +225,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
     ) -> KvResult<Option<duniter_core::module::TxsHistoryForBma>> {
         let gva_db = get_gva_db_ro(profile_path_opt);
         let duniter_gva_dbs_reader::txs_history::TxsHistory { sent, received, .. } =
-            duniter_gva_dbs_reader::txs_history::get_tx_history_bma_by_blocks(
+            duniter_gva_dbs_reader::txs_history::get_txs_history_bma_by_blocks(
                 gva_db, pubkey, from, to,
             )?;
         Ok(Some(duniter_core::module::TxsHistoryForBma {
@@ -255,7 +255,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
         }))
     }
     // Needed for BMA only
-    fn get_tx_history_bma_by_times(
+    fn get_txs_history_bma_by_times(
         profile_path_opt: Option<&Path>,
         pubkey: PublicKey,
         from: Option<u64>,
@@ -263,7 +263,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
     ) -> KvResult<Option<duniter_core::module::TxsHistoryForBma>> {
         let gva_db = get_gva_db_ro(profile_path_opt);
         let duniter_gva_dbs_reader::txs_history::TxsHistory { sent, received, .. } =
-            duniter_gva_dbs_reader::txs_history::get_tx_history_bma_by_times(
+            duniter_gva_dbs_reader::txs_history::get_txs_history_bma_by_times(
                 gva_db, pubkey, from, to,
             )?;
         Ok(Some(duniter_core::module::TxsHistoryForBma {
@@ -293,7 +293,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
         }))
     }
     // Needed for BMA only
-    fn get_mempool_tx_bma(
+    fn get_txs_history_bma_mempool(
         dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>,
         pubkey: PublicKey,
     ) -> KvResult<Option<duniter_core::module::TxsHistoryForBma>> {
@@ -301,7 +301,10 @@ impl duniter_core::module::DuniterModule for GvaModule {
             sending, pending, ..
         } = dbs_pool
             .execute(move |dbs| {
-                duniter_gva_dbs_reader::txs_history::get_mempool_tx_bma(&dbs.txs_mp_db, pubkey)
+                duniter_gva_dbs_reader::txs_history::get_txs_history_bma_mempool(
+                    &dbs.txs_mp_db,
+                    pubkey,
+                )
             })
             .expect("dbs pool disconnected")?;
         Ok(Some(duniter_core::module::TxsHistoryForBma {
-- 
GitLab