diff --git a/dbs-reader/src/txs_history.rs b/dbs-reader/src/txs_history.rs index 1f533e3e0168ffff53ae54b1259b6131f5eedaa9..d6c7bac4c34cae7ad39e86086be8891cce322945 100644 --- a/dbs-reader/src/txs_history.rs +++ b/dbs-reader/src/txs_history.rs @@ -446,10 +446,40 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2 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)?; + // Get pending TX + let TxsHistory { + sending, pending, .. + } = get_pending_transactions_for_bma(txs_mp_db_ro, pubkey)?; + // Return all (written + pending) + Ok(TxsHistory { + sent, + received, + sending, + pending, + }) +} + +// 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>( + gva_db_ro: &GvaDb, + pubkey: PublicKey, + start_block: u32, + end_block: u32, ) -> KvResult<TxsHistory> { let script_hash = Hash::compute(WalletScriptV10::single_sig(pubkey).to_string().as_bytes()); - let start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(0)); - let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(u32::MAX)); + let start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(start_block)); + let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(end_block)); let sent = gva_db_ro .txs_by_issuer() @@ -478,6 +508,19 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2 }) .flatten_ok() .collect::<KvResult<Vec<_>>>()?; + Ok(TxsHistory { + sent, + received, + sending: Vec::new(), + pending: Vec::new(), + }) +} + +// Needed for BMA only +pub fn get_pending_transactions_for_bma<TxsMpDb: TxsMpV2DbReadable>( + txs_mp_db_ro: &TxsMpDb, + pubkey: PublicKey, +) -> KvResult<TxsHistory> { let sending = txs_mp_db_ro .txs_by_issuer() .get_ref_slice(&PubKeyKeyV2(pubkey), |hashs| { @@ -503,8 +546,8 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2 })? .unwrap_or_default(); Ok(TxsHistory { - sent, - received, + sent: Vec::new(), + received: Vec::new(), sending, pending, }) diff --git a/src/lib.rs b/src/lib.rs index 8195a239553cb24a65d5ee02b6d07d368762b2b6..5632ab122a45d95d4739f377efdad29806b4ee83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,7 +170,7 @@ impl duniter_core::module::DuniterModule for GvaModule { } Ok(()) } - // Needed for BMA only, will be removed when the migration is complete. + // Needed for BMA only fn get_transactions_history_for_bma( dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, profile_path_opt: Option<&Path>, @@ -216,7 +216,70 @@ impl duniter_core::module::DuniterModule for GvaModule { pending, })) } - // Needed for BMA only, will be removed when the migration is complete. + // Needed for BMA only + fn get_written_transactions_for_bma( + profile_path_opt: Option<&Path>, + pubkey: PublicKey, + start_block: u32, + end_block: 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, + )?; + 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_pending_transactions_for_bma( + dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, + pubkey: PublicKey, + ) -> KvResult<Option<duniter_core::module::TxsHistoryForBma>> { + let duniter_gva_dbs_reader::txs_history::TxsHistory { + sending, pending, .. + } = dbs_pool + .execute(move |dbs| { + duniter_gva_dbs_reader::txs_history::get_pending_transactions_for_bma( + &dbs.txs_mp_db, + pubkey, + ) + }) + .expect("dbs pool disconnected")?; + Ok(Some(duniter_core::module::TxsHistoryForBma { + sent: Vec::new(), + received: Vec::new(), + sending, + pending, + })) + } + // Needed for BMA only fn get_tx_by_hash( dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, hash: Hash,