From 5c80c06d2baabfe74b16f6648b98574c5c9c5a89 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 28 Jul 2023 13:35:45 +0200 Subject: [PATCH] duplicate filter_block_tx for inputs and outputs --- src/status.rs | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/status.rs b/src/status.rs index 9f3efeb..8d4a8fe 100644 --- a/src/status.rs +++ b/src/status.rs @@ -332,7 +332,7 @@ impl ScriptHashStatus { let funding_blockhashes = index.limit_result(index.filter_by_funding(scripthash))?; self.for_new_blocks(funding_blockhashes, daemon, |blockhash, block| { let block_entries = result.entry(blockhash).or_default(); - filter_block_txs(block, |tx| filter_outputs(tx, scripthash)).for_each( + filter_block_txs_outputs(block, scripthash).for_each( |FilteredTx { pos, tx, @@ -354,7 +354,7 @@ impl ScriptHashStatus { .collect(); self.for_new_blocks(spending_blockhashes, daemon, |blockhash, block| { let block_entries = result.entry(blockhash).or_default(); - filter_block_txs(block, |tx| filter_inputs(tx, outpoints)).for_each( + filter_block_txs_inputs(block, &outpoints).for_each( |FilteredTx { pos, tx, @@ -514,10 +514,10 @@ struct FilteredTx { result: Vec, } -fn filter_block_txs( +fn filter_block_txs_outputs( block: SerBlock, - map_fn: impl Fn(&Transaction) -> Vec + Sync, -) -> impl Iterator> { + scripthash: ScriptHash, +) -> impl Iterator> { // TODO convert into visitor let block = Block::consensus_decode(&mut &block[..]).expect("core returned invalid block"); @@ -526,7 +526,35 @@ fn filter_block_txs( .into_par_iter() .enumerate() .filter_map(|(pos, tx)| { - let result = map_fn(&tx); + let result = filter_outputs(&tx, scripthash); + if result.is_empty() { + return None; // skip irrelevant transaction + } + let txid = tx.txid(); + Some(FilteredTx { + tx, + txid, + pos, + result, + }) + }) + .collect::>() + .into_iter() +} + +fn filter_block_txs_inputs( + block: SerBlock, + outpoints: &HashSet, +) -> impl Iterator> { + // TODO convert into visitor + let block = Block::consensus_decode(&mut &block[..]).expect("core returned invalid block"); + + block + .txdata + .into_par_iter() + .enumerate() + .filter_map(|(pos, tx)| { + let result = filter_inputs(&tx, outpoints); if result.is_empty() { return None; // skip irrelevant transaction }