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 {
@@ -441,19 +441,44 @@ pub struct TxsHistory {
pub pending: Vec<(TransactionDocumentV10, i64)>,
pub pending: Vec<(TransactionDocumentV10, i64)>,
}
}
// Needed for BMA only
/// Get TX full history (written and pending) of a pubkey. Needed for BMA only
pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
pub fn get_txs_history_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2DbReadable>(
gva_db_ro: &GvaDb,
gva_db_ro: &GvaDb,
txs_mp_db_ro: &TxsMpDb,
txs_mp_db_ro: &TxsMpDb,
pubkey: PublicKey,
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> {
) -> KvResult<TxsHistory> {
let script_hash = Hash::compute(WalletScriptV10::single_sig(pubkey).to_string().as_bytes());
let script_hash = Hash::compute(WalletScriptV10::single_sig(pubkey).to_string().as_bytes());
let start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(0));
let from = from.unwrap_or(0);
let end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(u32::MAX));
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
let sent = gva_db_ro
.txs_by_issuer()
.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();
let mut sent = SmallVec::<[GvaTxDbV1; 2]>::new();
for hash in hashs {
for hash in hashs {
if let Some(tx_db) = gva_db_ro.txs().get(HashKeyV2::from_ref(hash))? {
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
@@ -467,7 +492,7 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
let received = gva_db_ro
let received = gva_db_ro
.txs_by_recipient()
.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();
let mut sent = SmallVec::<[GvaTxDbV1; 2]>::new();
for hash in hashs {
for hash in hashs {
if let Some(tx_db) = gva_db_ro.txs().get(HashKeyV2::from_ref(hash))? {
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
@@ -478,6 +503,50 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
})
})
.flatten_ok()
.flatten_ok()
.collect::<KvResult<Vec<_>>>()?;
.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
let sending = txs_mp_db_ro
.txs_by_issuer()
.txs_by_issuer()
.get_ref_slice(&PubKeyKeyV2(pubkey), |hashs| {
.get_ref_slice(&PubKeyKeyV2(pubkey), |hashs| {
@@ -503,8 +572,8 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
@@ -503,8 +572,8 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
})?
})?
.unwrap_or_default();
.unwrap_or_default();
Ok(TxsHistory {
Ok(TxsHistory {
sent,
sent: Vec::new(),
received,
received: Vec::new(),
sending,
sending,
pending,
pending,
})
})
@@ -512,17 +581,19 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
@@ -512,17 +581,19 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
#[cfg(test)]
#[cfg(test)]
mod tests {
mod tests {
use super::*;
use duniter_core::{
use duniter_core::{
common::prelude::{BlockHash, Blockstamp},
common::prelude::{BlockHash, Blockstamp},
crypto::keys::ed25519::PublicKey,
crypto::keys::ed25519::PublicKey,
documents::transaction::{TransactionDocumentV10, TransactionDocumentV10Stringified},
documents::transaction::{TransactionDocumentV10, TransactionDocumentV10Stringified},
documents_parser::prelude::FromStringObject,
documents_parser::prelude::FromStringObject,
};
};
use duniter_gva_db::GvaV1DbWritable;
use maplit::btreeset;
use maplit::btreeset;
use unwrap::unwrap;
use unwrap::unwrap;
 
use duniter_gva_db::GvaV1DbWritable;
 
 
use super::*;
 
fn gen_tx(hash: Hash, written_block_number: BlockNumber) -> GvaTxDbV1 {
fn gen_tx(hash: Hash, written_block_number: BlockNumber) -> GvaTxDbV1 {
GvaTxDbV1 {
GvaTxDbV1 {
tx: unwrap!(TransactionDocumentV10::from_string_object(
tx: unwrap!(TransactionDocumentV10::from_string_object(
Loading