1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

duplicate filter_block_tx for inputs and outputs

This commit is contained in:
Riccardo Casatta 2023-07-28 13:35:45 +02:00
parent cf74efd9d1
commit 5c80c06d2b
No known key found for this signature in database
GPG Key ID: FD986A969E450397

View File

@ -332,7 +332,7 @@ impl ScriptHashStatus {
let funding_blockhashes = index.limit_result(index.filter_by_funding(scripthash))?; let funding_blockhashes = index.limit_result(index.filter_by_funding(scripthash))?;
self.for_new_blocks(funding_blockhashes, daemon, |blockhash, block| { self.for_new_blocks(funding_blockhashes, daemon, |blockhash, block| {
let block_entries = result.entry(blockhash).or_default(); 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 { |FilteredTx {
pos, pos,
tx, tx,
@ -354,7 +354,7 @@ impl ScriptHashStatus {
.collect(); .collect();
self.for_new_blocks(spending_blockhashes, daemon, |blockhash, block| { self.for_new_blocks(spending_blockhashes, daemon, |blockhash, block| {
let block_entries = result.entry(blockhash).or_default(); 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 { |FilteredTx {
pos, pos,
tx, tx,
@ -514,10 +514,10 @@ struct FilteredTx<T> {
result: Vec<T>, result: Vec<T>,
} }
fn filter_block_txs<T: Send>( fn filter_block_txs_outputs(
block: SerBlock, block: SerBlock,
map_fn: impl Fn(&Transaction) -> Vec<T> + Sync, scripthash: ScriptHash,
) -> impl Iterator<Item = FilteredTx<T>> { ) -> impl Iterator<Item = FilteredTx<TxOutput>> {
// TODO convert into visitor // TODO convert into visitor
let block = Block::consensus_decode(&mut &block[..]).expect("core returned invalid block"); let block = Block::consensus_decode(&mut &block[..]).expect("core returned invalid block");
@ -526,7 +526,35 @@ fn filter_block_txs<T: Send>(
.into_par_iter() .into_par_iter()
.enumerate() .enumerate()
.filter_map(|(pos, tx)| { .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::<Vec<_>>()
.into_iter()
}
fn filter_block_txs_inputs(
block: SerBlock,
outpoints: &HashSet<OutPoint>,
) -> impl Iterator<Item = FilteredTx<OutPoint>> {
// 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() { if result.is_empty() {
return None; // skip irrelevant transaction return None; // skip irrelevant transaction
} }