Skip to content

Commit d308a61

Browse files
vladimirfomeneevanlinjin
authored andcommitted
refactor: Remove ForEachTxout trait
1 parent 9371625 commit d308a61

File tree

4 files changed

+20
-80
lines changed

4 files changed

+20
-80
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
indexed_tx_graph::Indexer,
44
miniscript::{Descriptor, DescriptorPublicKey},
55
spk_iter::BIP32_MAX_INDEX,
6-
ForEachTxOut, SpkIterator, SpkTxOutIndex,
6+
SpkIterator, SpkTxOutIndex,
77
};
88
use alloc::vec::Vec;
99
use bitcoin::{OutPoint, Script, TxOut};
@@ -112,7 +112,7 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
112112
}
113113

114114
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
115-
/// Scans an object for relevant outpoints, which are stored and indexed internally.
115+
/// Scans a transaction for relevant outpoints, which are stored and indexed internally.
116116
///
117117
/// If the matched script pubkey is part of the lookahead, the last stored index is updated for
118118
/// the script pubkey's keychain and the [`super::ChangeSet`] returned will reflect the
@@ -124,13 +124,11 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
124124
/// your txouts.
125125
/// 2. When getting new data from the chain, you usually scan it before incorporating it into
126126
/// your chain state (i.e., `SparseChain`, `ChainGraph`).
127-
///
128-
/// See [`ForEachTxout`] for the types that support this.
129-
///
130-
/// [`ForEachTxout`]: crate::ForEachTxOut
131-
pub fn scan(&mut self, txouts: &impl ForEachTxOut) -> super::ChangeSet<K> {
127+
pub fn scan(&mut self, tx: &bitcoin::Transaction) -> super::ChangeSet<K> {
132128
let mut changeset = super::ChangeSet::<K>::default();
133-
txouts.for_each_txout(|(op, txout)| changeset.append(self.scan_txout(op, txout)));
129+
for (op, txout) in tx.output.iter().enumerate() {
130+
changeset.append(self.scan_txout(OutPoint::new(tx.txid(), op as u32), txout));
131+
}
134132
changeset
135133
}
136134

crates/chain/src/spk_txout_index.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use core::ops::RangeBounds;
33
use crate::{
44
collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
55
indexed_tx_graph::Indexer,
6-
ForEachTxOut,
76
};
87
use bitcoin::{self, OutPoint, Script, ScriptBuf, Transaction, TxOut, Txid};
98

@@ -77,49 +76,37 @@ impl<I: Clone + Ord> Indexer for SpkTxOutIndex<I> {
7776
}
7877
}
7978

80-
/// This macro is used instead of a member function of `SpkTxOutIndex`, which would result in a
81-
/// compiler error[E0521]: "borrowed data escapes out of closure" when we attempt to take a
82-
/// reference out of the `ForEachTxOut` closure during scanning.
83-
macro_rules! scan_txout {
84-
($self:ident, $op:expr, $txout:expr) => {{
85-
let spk_i = $self.spk_indices.get(&$txout.script_pubkey);
86-
if let Some(spk_i) = spk_i {
87-
$self.txouts.insert($op, (spk_i.clone(), $txout.clone()));
88-
$self.spk_txouts.insert((spk_i.clone(), $op));
89-
$self.unused.remove(&spk_i);
90-
}
91-
spk_i
92-
}};
93-
}
94-
9579
impl<I: Clone + Ord> SpkTxOutIndex<I> {
96-
/// Scans an object containing many txouts.
80+
/// Scans a transaction containing many txouts.
9781
///
9882
/// Typically, this is used in two situations:
9983
///
10084
/// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
10185
/// your txouts.
10286
/// 2. When getting new data from the chain, you usually scan it before incorporating it into your chain state.
103-
///
104-
/// See [`ForEachTxout`] for the types that support this.
105-
///
106-
/// [`ForEachTxout`]: crate::ForEachTxOut
107-
pub fn scan(&mut self, txouts: &impl ForEachTxOut) -> BTreeSet<I> {
87+
pub fn scan(&mut self, tx: &bitcoin::Transaction) -> BTreeSet<I> {
10888
let mut scanned_indices = BTreeSet::new();
10989

110-
txouts.for_each_txout(|(op, txout)| {
111-
if let Some(spk_i) = scan_txout!(self, op, txout) {
90+
for (i, txout) in tx.output.iter().enumerate() {
91+
let op = OutPoint::new(tx.txid(), i as u32);
92+
if let Some(spk_i) = self.scan_txout(op, txout) {
11293
scanned_indices.insert(spk_i.clone());
11394
}
114-
});
95+
}
11596

11697
scanned_indices
11798
}
11899

119100
/// Scan a single `TxOut` for a matching script pubkey and returns the index that matches the
120101
/// script pubkey (if any).
121102
pub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> Option<&I> {
122-
scan_txout!(self, op, txout)
103+
let spk_i = self.spk_indices.get(&txout.script_pubkey);
104+
if let Some(spk_i) = spk_i {
105+
self.txouts.insert(op, (spk_i.clone(), txout.clone()));
106+
self.spk_txouts.insert((spk_i.clone(), op));
107+
self.unused.remove(spk_i);
108+
}
109+
spk_i
123110
}
124111

125112
/// Get a reference to the set of indexed outpoints.

crates/chain/src/tx_data_traits.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,6 @@ use crate::collections::BTreeMap;
22
use crate::collections::BTreeSet;
33
use crate::BlockId;
44
use alloc::vec::Vec;
5-
use bitcoin::{Block, OutPoint, Transaction, TxOut};
6-
7-
/// Trait to do something with every txout contained in a structure.
8-
///
9-
/// We would prefer to just work with things that can give us an `Iterator<Item=(OutPoint, &TxOut)>`
10-
/// here, but rust's type system makes it extremely hard to do this (without trait objects).
11-
pub trait ForEachTxOut {
12-
/// The provided closure `f` will be called with each `outpoint/txout` pair.
13-
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut)));
14-
}
15-
16-
impl ForEachTxOut for Block {
17-
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
18-
for tx in self.txdata.iter() {
19-
tx.for_each_txout(&mut f)
20-
}
21-
}
22-
}
23-
24-
impl ForEachTxOut for Transaction {
25-
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
26-
let txid = self.txid();
27-
for (i, txout) in self.output.iter().enumerate() {
28-
f((
29-
OutPoint {
30-
txid,
31-
vout: i as u32,
32-
},
33-
txout,
34-
))
35-
}
36-
}
37-
}
385

396
/// Trait that "anchors" blockchain data to a specific block of height and hash.
407
///

crates/chain/src/tx_graph.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
5353
use crate::{
5454
collections::*, keychain::Balance, local_chain::LocalChain, Anchor, Append, BlockId,
55-
ChainOracle, ChainPosition, ForEachTxOut, FullTxOut,
55+
ChainOracle, ChainPosition, FullTxOut,
5656
};
5757
use alloc::vec::Vec;
5858
use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid};
@@ -1072,18 +1072,6 @@ impl<A> AsRef<TxGraph<A>> for TxGraph<A> {
10721072
}
10731073
}
10741074

1075-
impl<A> ForEachTxOut for ChangeSet<A> {
1076-
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
1077-
self.txouts().for_each(f)
1078-
}
1079-
}
1080-
1081-
impl<A> ForEachTxOut for TxGraph<A> {
1082-
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
1083-
self.all_txouts().for_each(f)
1084-
}
1085-
}
1086-
10871075
/// An iterator that traverses transaction descendants.
10881076
///
10891077
/// This `struct` is created by the [`walk_descendants`] method of [`TxGraph`].

0 commit comments

Comments
 (0)