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

+ 72
6
@@ -441,15 +441,40 @@ pub struct TxsHistory {
@@ -441,15 +441,40 @@ 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_tx_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_tx_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.saturating_add(1)));
let sent = gva_db_ro
let sent = gva_db_ro
.txs_by_issuer()
.txs_by_issuer()
@@ -478,6 +503,47 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
@@ -478,6 +503,47 @@ 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_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> {
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 +569,8 @@ pub fn get_transactions_history_for_bma<GvaDb: GvaV1DbReadable, TxsMpDb: TxsMpV2
@@ -503,8 +569,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,
})
})
Loading