Skip to content
Snippets Groups Projects

feat(bma) add `get_written_transactions_for_bma()` and `get_pending_transactions_for_bma()` - close #1

Open Benoit Lavenier requested to merge fix/1/add_get_transactions_for_bma into master
1 unresolved thread

Files

+ 81
10
@@ -441,19 +441,44 @@ 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_txs_history_bma_by_blocks(gva_db_ro, pubkey, None, None)?;
// Get pending TX
let TxsHistory {
sending, pending, ..
} = get_txs_history_bma_mempool(txs_mp_db_ro, pubkey)?;
// Return all (written + pending)
Ok(TxsHistory {
sent,
received,
sending,
pending,
})
}
/// Get TX history of a pubkey from a block range. Needed for BMA only
pub fn get_txs_history_bma_by_blocks<GvaDb: GvaV1DbReadable>(
gva_db_ro: &GvaDb,
pubkey: PublicKey,
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(0));
let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(u32::MAX));
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));
let sent = gva_db_ro
.txs_by_issuer()
.iter_ref_slice(start_k..end_k, |_k, hashs| {
.iter_ref_slice(start_k..=end_k, |_k, hashs| {
let mut sent = SmallVec::<[GvaTxDbV1; 2]>::new();
for hash in hashs {
if let Some(tx_db) = gva_db_ro.txs().get(HashKeyV2::from_ref(hash))? {
@@ -467,7 +492,7 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
let received = gva_db_ro
.txs_by_recipient()
.iter_ref_slice(start_k..end_k, |_k, hashs| {
.iter_ref_slice(start_k..=end_k, |_k, hashs| {
let mut sent = SmallVec::<[GvaTxDbV1; 2]>::new();
for hash in hashs {
if let Some(tx_db) = gva_db_ro.txs().get(HashKeyV2::from_ref(hash))? {
@@ -478,6 +503,50 @@ 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(),
})
}
/// Get TX history of a pubkey from a median_time range. Needed for BMA only
pub fn get_txs_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).., |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), |it| it.values().next_res())?
.unwrap_or(u32::MAX),
None => u32::MAX,
};
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_txs_history_bma_mempool<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 +572,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,
})
@@ -512,17 +581,19 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
#[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(
Loading