@@ -9,8 +9,9 @@ use bitcoin::{self, OutPoint, Script, ScriptBuf, Transaction, TxOut, Txid};
99/// An index storing [`TxOut`]s that have a script pubkey that matches those in a list.
1010///
1111/// The basic idea is that you insert script pubkeys you care about into the index with
12- /// [`insert_spk`] and then when you call [`scan`], the index will look at any txouts you pass in and
13- /// store and index any txouts matching one of its script pubkeys.
12+ /// [`insert_spk`] and then when you call [`Indexer::index_tx`] or [`Indexer::index_txout`], the
13+ /// index will look at any txouts you pass in and store and index any txouts matching one of its
14+ /// script pubkeys.
1415///
1516/// Each script pubkey is associated with an application-defined index script index `I`, which must be
1617/// [`Ord`]. Usually, this is used to associate the derivation index of the script pubkey or even a
@@ -24,7 +25,6 @@ use bitcoin::{self, OutPoint, Script, ScriptBuf, Transaction, TxOut, Txid};
2425/// [`TxOut`]: bitcoin::TxOut
2526/// [`insert_spk`]: Self::insert_spk
2627/// [`Ord`]: core::cmp::Ord
27- /// [`scan`]: Self::scan
2828/// [`TxGraph`]: crate::tx_graph::TxGraph
2929#[ derive( Clone , Debug ) ]
3030pub struct SpkTxOutIndex < I > {
@@ -53,19 +53,35 @@ impl<I> Default for SpkTxOutIndex<I> {
5353}
5454
5555impl < I : Clone + Ord > Indexer for SpkTxOutIndex < I > {
56- type ChangeSet = ( ) ;
56+ type ChangeSet = BTreeSet < I > ;
5757
5858 fn index_txout ( & mut self , outpoint : OutPoint , txout : & TxOut ) -> Self :: ChangeSet {
59- self . scan_txout ( outpoint, txout) ;
60- Default :: default ( )
59+ let spk_i = self . spk_indices . get ( & txout. script_pubkey ) ;
60+ let mut scanned_indices = BTreeSet :: new ( ) ;
61+ if let Some ( spk_i) = spk_i {
62+ self . txouts . insert ( outpoint, ( spk_i. clone ( ) , txout. clone ( ) ) ) ;
63+ self . spk_txouts . insert ( ( spk_i. clone ( ) , outpoint) ) ;
64+ self . unused . remove ( spk_i) ;
65+ scanned_indices. insert ( spk_i. clone ( ) ) ;
66+ }
67+ scanned_indices
6168 }
6269
6370 fn index_tx ( & mut self , tx : & Transaction ) -> Self :: ChangeSet {
64- self . scan ( tx) ;
65- Default :: default ( )
71+ let mut scanned_indices = BTreeSet :: new ( ) ;
72+
73+ for ( i, txout) in tx. output . iter ( ) . enumerate ( ) {
74+ let op = OutPoint :: new ( tx. txid ( ) , i as u32 ) ;
75+ let mut txout_indices = self . index_txout ( op, txout) ;
76+ scanned_indices. append ( & mut txout_indices) ;
77+ }
78+
79+ scanned_indices
6680 }
6781
68- fn initial_changeset ( & self ) -> Self :: ChangeSet { }
82+ fn initial_changeset ( & self ) -> Self :: ChangeSet {
83+ self . spks . keys ( ) . cloned ( ) . collect ( )
84+ }
6985
7086 fn apply_changeset ( & mut self , _changeset : Self :: ChangeSet ) {
7187 // This applies nothing.
@@ -77,38 +93,6 @@ impl<I: Clone + Ord> Indexer for SpkTxOutIndex<I> {
7793}
7894
7995impl < I : Clone + Ord > SpkTxOutIndex < I > {
80- /// Scans a transaction containing many txouts.
81- ///
82- /// Typically, this is used in two situations:
83- ///
84- /// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
85- /// your txouts.
86- /// 2. When getting new data from the chain, you usually scan it before incorporating it into your chain state.
87- pub fn scan ( & mut self , tx : & bitcoin:: Transaction ) -> BTreeSet < I > {
88- let mut scanned_indices = BTreeSet :: new ( ) ;
89-
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) {
93- scanned_indices. insert ( spk_i. clone ( ) ) ;
94- }
95- }
96-
97- scanned_indices
98- }
99-
100- /// Scan a single `TxOut` for a matching script pubkey and returns the index that matches the
101- /// script pubkey (if any).
102- pub fn scan_txout ( & mut self , op : OutPoint , txout : & TxOut ) -> Option < & I > {
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
110- }
111-
11296 /// Get a reference to the set of indexed outpoints.
11397 pub fn outpoints ( & self ) -> & BTreeSet < ( I , OutPoint ) > {
11498 & self . spk_txouts
0 commit comments