From 43c25ea4d69c59d5a7cdd9fbf24bfb7a834ed455 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Fri, 19 May 2023 10:59:24 +0200
Subject: [PATCH] [enh] rename BMA tx history functions

---
 dbs-reader/src/txs_history.rs | 63 ++++++++++++++++++++++++-----------
 src/lib.rs                    | 62 +++++++++++++++++++++++++---------
 2 files changed, 90 insertions(+), 35 deletions(-)

diff --git a/dbs-reader/src/txs_history.rs b/dbs-reader/src/txs_history.rs
index d6c7bac..d23b967 100644
--- a/dbs-reader/src/txs_history.rs
+++ b/dbs-reader/src/txs_history.rs
@@ -441,18 +441,19 @@ pub struct TxsHistory {
     pub pending: Vec<(TransactionDocumentV10, i64)>,
 }
 
-// Needed for BMA only
-pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
+/// Get TX full history (written and pending) of a pubkey. Needed for BMA only
+pub fn get_txs_history_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
     gva_db_ro: &GvaDb,
     txs_mp_db_ro: &TxsMpDb,
     pubkey: PublicKey,
 ) -> KvResult<TxsHistory> {
     // Get written TX
-    let TxsHistory { sent, received, .. } = get_written_transactions_for_bma(gva_db_ro, pubkey)?;
+    let TxsHistory { sent, received, .. } =
+        get_tx_history_bma_by_blocks(gva_db_ro, pubkey, None, None)?;
     // Get pending TX
     let TxsHistory {
         sending, pending, ..
-    } = get_pending_transactions_for_bma(txs_mp_db_ro, pubkey)?;
+    } = get_txs_history_bma_mempool(txs_mp_db_ro, pubkey)?;
     // Return all (written + pending)
     Ok(TxsHistory {
         sent,
@@ -462,24 +463,18 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
     })
 }
 
-// Needed for BMA only
-pub fn get_written_transactions_for_bma<GvaDb: GvaV1DbReadable>(
-    gva_db_ro: &GvaDb,
-    pubkey: PublicKey,
-) -> KvResult<TxsHistory> {
-    get_written_transactions_for_bma_with_range(gva_db_ro, pubkey, 0, u32::MAX)
-}
-
-// Needed for BMA only
-pub fn get_written_transactions_for_bma_with_range<GvaDb: GvaV1DbReadable>(
+/// Get TX history of a pubkey from a block range. Needed for BMA only
+pub fn get_tx_history_bma_by_blocks<GvaDb: GvaV1DbReadable>(
     gva_db_ro: &GvaDb,
     pubkey: PublicKey,
-    start_block: u32,
-    end_block: u32,
+    from: Option<u32>,
+    to: Option<u32>,
 ) -> KvResult<TxsHistory> {
     let script_hash = Hash::compute(WalletScriptV10::single_sig(pubkey).to_string().as_bytes());
-    let start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(start_block));
-    let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(end_block));
+    let from = from.unwrap_or(0);
+    let to = to.unwrap_or(u32::MAX);
+    let start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(from));
+    let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(to.saturating_add(1)));
 
     let sent = gva_db_ro
         .txs_by_issuer()
@@ -516,8 +511,36 @@ pub fn get_written_transactions_for_bma_with_range<GvaDb: GvaV1DbReadable>(
     })
 }
 
-// Needed for BMA only
-pub fn get_pending_transactions_for_bma<TxsMpDb: TxsMpV2DbReadable>(
+/// 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>(
+    gva_db_ro: &GvaDb,
+    pubkey: PublicKey,
+    from: Option<u64>,
+    to: Option<u64>,
+) -> KvResult<TxsHistory> {
+    let from_block = match from {
+        Some(from_time) => gva_db_ro
+            .blocks_by_common_time()
+            .iter(U64BE(from_time)..)
+            .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),
+        None => u32::MAX,
+    };
+    get_tx_history_bma_by_blocks(gva_db_ro, pubkey, Some(from_block), Some(to_block))
+}
+
+/// Get mempool TX of a pubkey. Needed for BMA only
+pub fn get_mempool_tx_bma<TxsMpDb: TxsMpV2DbReadable>(
     txs_mp_db_ro: &TxsMpDb,
     pubkey: PublicKey,
 ) -> KvResult<TxsHistory> {
diff --git a/src/lib.rs b/src/lib.rs
index 5632ab1..07967e3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -171,7 +171,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
         Ok(())
     }
     // Needed for BMA only
-    fn get_transactions_history_for_bma(
+    fn get_txs_history_bma(
         dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>,
         profile_path_opt: Option<&Path>,
         pubkey: PublicKey,
@@ -184,7 +184,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
             pending,
         } = dbs_pool
             .execute(move |dbs| {
-                duniter_gva_dbs_reader::txs_history::get_transactions_history_for_bma(
+                duniter_gva_dbs_reader::txs_history::get_txs_history_bma(
                     gva_db,
                     &dbs.txs_mp_db,
                     pubkey,
@@ -217,19 +217,16 @@ impl duniter_core::module::DuniterModule for GvaModule {
         }))
     }
     // Needed for BMA only
-    fn get_written_transactions_for_bma(
+    fn get_tx_history_bma_by_blocks(
         profile_path_opt: Option<&Path>,
         pubkey: PublicKey,
-        start_block: u32,
-        end_block: u32,
+        from: Option<u32>,
+        to: Option<u32>,
     ) -> 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_written_transactions_for_bma_with_range(
-                gva_db,
-                pubkey,
-                start_block,
-                end_block,
+            duniter_gva_dbs_reader::txs_history::get_tx_history_bma_by_blocks(
+                gva_db, pubkey, from, to,
             )?;
         Ok(Some(duniter_core::module::TxsHistoryForBma {
             sent: sent
@@ -258,7 +255,45 @@ impl duniter_core::module::DuniterModule for GvaModule {
         }))
     }
     // Needed for BMA only
-    fn get_pending_transactions_for_bma(
+    fn get_tx_history_bma_by_times(
+        profile_path_opt: Option<&Path>,
+        pubkey: PublicKey,
+        from: Option<u64>,
+        to: Option<u64>,
+    ) -> 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(
+                gva_db, pubkey, from, to,
+            )?;
+        Ok(Some(duniter_core::module::TxsHistoryForBma {
+            sent: sent
+                .into_iter()
+                .map(
+                    |GvaTxDbV1 {
+                         tx,
+                         written_block,
+                         written_time,
+                     }| (tx, written_block, written_time),
+                )
+                .collect(),
+            received: received
+                .into_iter()
+                .map(
+                    |GvaTxDbV1 {
+                         tx,
+                         written_block,
+                         written_time,
+                     }| (tx, written_block, written_time),
+                )
+                .collect(),
+
+            sending: Vec::new(),
+            pending: Vec::new(),
+        }))
+    }
+    // Needed for BMA only
+    fn get_mempool_tx_bma(
         dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>,
         pubkey: PublicKey,
     ) -> KvResult<Option<duniter_core::module::TxsHistoryForBma>> {
@@ -266,10 +301,7 @@ impl duniter_core::module::DuniterModule for GvaModule {
             sending, pending, ..
         } = dbs_pool
             .execute(move |dbs| {
-                duniter_gva_dbs_reader::txs_history::get_pending_transactions_for_bma(
-                    &dbs.txs_mp_db,
-                    pubkey,
-                )
+                duniter_gva_dbs_reader::txs_history::get_mempool_tx_bma(&dbs.txs_mp_db, pubkey)
             })
             .expect("dbs pool disconnected")?;
         Ok(Some(duniter_core::module::TxsHistoryForBma {
-- 
GitLab