Skip to content

Commit

Permalink
Simplify ScriptHashStatus::sync_confirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Aug 12, 2023
1 parent 2e6191e commit a716847
Showing 1 changed file with 24 additions and 42 deletions.
66 changes: 24 additions & 42 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,42 +336,31 @@ 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_outputs(block, scripthash).for_each(
|FilteredTx {
pos,
tx,
txid,
result: funding_outputs,
}| {
cache.add_tx(txid, move || tx);
outpoints.extend(make_outpoints(txid, &funding_outputs));
block_entries
.entry(pos)
.or_insert_with(|| TxEntry::new(txid))
.outputs = funding_outputs;
},
);
for filtered_outputs in filter_block_txs_outputs(block, scripthash) {
cache.add_tx(filtered_outputs.txid, move || filtered_outputs.tx);
outpoints.extend(make_outpoints(
filtered_outputs.txid,
&filtered_outputs.result,
));
block_entries
.entry(filtered_outputs.pos)
.or_insert_with(|| TxEntry::new(filtered_outputs.txid))
.outputs = filtered_outputs.result;
}
})?;
let spending_blockhashes: HashSet<BlockHash> = outpoints
.par_iter()
.flat_map_iter(|outpoint| index.filter_by_spending(*outpoint))
.collect();
self.for_new_blocks(spending_blockhashes, daemon, |blockhash, block| {
let block_entries = result.entry(blockhash).or_default();
filter_block_txs_inputs(&block, outpoints).for_each(
|FilteredTx {
pos,
tx,
txid,
result: spent_outpoints,
}| {
cache.add_tx(txid, move || tx);
block_entries
.entry(pos)
.or_insert_with(|| TxEntry::new(txid))
.spent = spent_outpoints;
},
);
for filtered_inputs in filter_block_txs_inputs(&block, outpoints) {
cache.add_tx(filtered_inputs.txid, move || filtered_inputs.tx);
block_entries
.entry(filtered_inputs.pos)
.or_insert_with(|| TxEntry::new(filtered_inputs.txid))
.spent = filtered_inputs.result;
}
})?;

Ok(result
Expand Down Expand Up @@ -518,10 +507,7 @@ struct FilteredTx<T> {
result: Vec<T>,
}

fn filter_block_txs_outputs(
block: SerBlock,
scripthash: ScriptHash,
) -> impl Iterator<Item = FilteredTx<TxOutput>> {
fn filter_block_txs_outputs(block: SerBlock, scripthash: ScriptHash) -> Vec<FilteredTx<TxOutput>> {
struct FindOutputs {
scripthash: ScriptHash,
result: Vec<FilteredTx<TxOutput>>,
Expand Down Expand Up @@ -565,13 +551,13 @@ fn filter_block_txs_outputs(

bsl::Block::visit(&block, &mut find_outputs).expect("core returned invalid block");

find_outputs.result.into_iter()
find_outputs.result
}

fn filter_block_txs_inputs(
block: &SerBlock,
outpoints: &HashSet<OutPoint>,
) -> impl Iterator<Item = FilteredTx<OutPoint>> {
) -> Vec<FilteredTx<OutPoint>> {
struct FindInputs<'a> {
outpoints: &'a HashSet<OutPoint>,
result: Vec<FilteredTx<OutPoint>>,
Expand Down Expand Up @@ -614,7 +600,7 @@ fn filter_block_txs_inputs(

bsl::Block::visit(block, &mut find_inputs).expect("core returned invalid block");

find_inputs.result.into_iter()
find_inputs.result
}

#[cfg(test)]
Expand Down Expand Up @@ -660,9 +646,7 @@ mod tests {
.assume_checked();
let scripthash = ScriptHash::new(&addr.script_pubkey());

let result = super::filter_block_txs_outputs(block, scripthash)
.next()
.unwrap();
let result = &super::filter_block_txs_outputs(block, scripthash)[0];
assert_eq!(
result.txid.to_string(),
"7bcdcb44422da5a99daad47d6ba1c3d6f2e48f961a75e42c4fa75029d4b0ef49"
Expand All @@ -682,9 +666,7 @@ mod tests {
let mut outpoints = HashSet::new();
outpoints.insert(outpoint);

let result = super::filter_block_txs_inputs(&block, &outpoints)
.next()
.unwrap();
let result = &super::filter_block_txs_inputs(&block, &outpoints)[0];
assert_eq!(
result.txid.to_string(),
"7bcdcb44422da5a99daad47d6ba1c3d6f2e48f961a75e42c4fa75029d4b0ef49"
Expand Down

0 comments on commit a716847

Please sign in to comment.