Skip to content

Commit c56728f

Browse files
vladimirfomeneevanlinjin
authored andcommitted
refactor: Remove scan and scan_txout from SpkTxoutIndex and KeychainTxoutIndex
1 parent 32c40ac commit c56728f

File tree

5 files changed

+55
-84
lines changed

5 files changed

+55
-84
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,18 @@ pub trait Indexer {
233233
/// Scan and index the given `outpoint` and `txout`.
234234
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet;
235235

236-
/// Scan and index the given transaction.
236+
/// Scans a transaction for relevant outpoints, which are stored and indexed internally.
237+
///
238+
/// If the matched script pubkey is part of the lookahead, the last stored index is updated for
239+
/// the script pubkey's keychain and the [`ChangeSet`] returned will reflect the
240+
/// change.
241+
///
242+
/// Typically, this method is used in two situations:
243+
///
244+
/// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
245+
/// your txouts.
246+
/// 2. When getting new data from the chain, you usually scan it before incorporating it into
247+
/// your chain state.
237248
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet;
238249

239250
/// Apply changeset to itself.

crates/chain/src/keychain/txout_index.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
9191
type ChangeSet = super::ChangeSet<K>;
9292

9393
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
94-
self.scan_txout(outpoint, txout)
94+
let mut changeset = super::ChangeSet::<K>::default();
95+
for (keychain, index) in self.inner.index_txout(outpoint, txout) {
96+
changeset.append(self.reveal_to_target(&keychain, index).1);
97+
}
98+
changeset
9599
}
96100

97101
fn index_tx(&mut self, tx: &bitcoin::Transaction) -> Self::ChangeSet {
98-
self.scan(tx)
102+
let mut changeset = super::ChangeSet::<K>::default();
103+
for (op, txout) in tx.output.iter().enumerate() {
104+
changeset.append(self.index_txout(OutPoint::new(tx.txid(), op as u32), txout));
105+
}
106+
changeset
99107
}
100108

101109
fn initial_changeset(&self) -> Self::ChangeSet {
@@ -112,36 +120,6 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
112120
}
113121

114122
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
115-
/// Scans a transaction for relevant outpoints, which are stored and indexed internally.
116-
///
117-
/// If the matched script pubkey is part of the lookahead, the last stored index is updated for
118-
/// the script pubkey's keychain and the [`super::ChangeSet`] returned will reflect the
119-
/// change.
120-
///
121-
/// Typically, this method is used in two situations:
122-
///
123-
/// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
124-
/// your txouts.
125-
/// 2. When getting new data from the chain, you usually scan it before incorporating it into
126-
/// your chain state (i.e., `SparseChain`, `ChainGraph`).
127-
pub fn scan(&mut self, tx: &bitcoin::Transaction) -> super::ChangeSet<K> {
128-
let mut changeset = super::ChangeSet::<K>::default();
129-
for (op, txout) in tx.output.iter().enumerate() {
130-
changeset.append(self.scan_txout(OutPoint::new(tx.txid(), op as u32), txout));
131-
}
132-
changeset
133-
}
134-
135-
/// Scan a single outpoint for a matching script pubkey.
136-
///
137-
/// If it matches, this will store and index it.
138-
pub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> super::ChangeSet<K> {
139-
match self.inner.scan_txout(op, txout).cloned() {
140-
Some((keychain, index)) => self.reveal_to_target(&keychain, index).1,
141-
None => super::ChangeSet::default(),
142-
}
143-
}
144-
145123
/// Return a reference to the internal [`SpkTxOutIndex`].
146124
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
147125
&self.inner
@@ -198,14 +176,11 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
198176
/// Set the lookahead count for `keychain`.
199177
///
200178
/// The lookahead is the number of scripts to cache ahead of the last stored script index. This
201-
/// is useful during a scan via [`scan`] or [`scan_txout`].
179+
/// is useful during a scan via [`Indexer::index_tx`] or [`Indexer::index_txout`].
202180
///
203181
/// # Panics
204182
///
205183
/// This will panic if the `keychain` does not exist.
206-
///
207-
/// [`scan`]: Self::scan
208-
/// [`scan_txout`]: Self::scan_txout
209184
pub fn set_lookahead(&mut self, keychain: &K, lookahead: u32) {
210185
self.lookahead.insert(keychain.clone(), lookahead);
211186
self.replenish_lookahead(keychain);

crates/chain/src/spk_txout_index.rs

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
3030
pub struct SpkTxOutIndex<I> {
@@ -53,19 +53,35 @@ impl<I> Default for SpkTxOutIndex<I> {
5353
}
5454

5555
impl<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

7995
impl<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

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod common;
55
use bdk_chain::{
66
collections::BTreeMap,
7+
indexed_tx_graph::Indexer,
78
keychain::{self, KeychainTxOutIndex},
89
Append,
910
};
@@ -194,7 +195,7 @@ fn test_lookahead() {
194195
],
195196
..common::new_tx(external_index)
196197
};
197-
assert_eq!(txout_index.scan(&tx), keychain::ChangeSet::default());
198+
assert_eq!(txout_index.index_tx(&tx), keychain::ChangeSet::default());
198199
assert_eq!(
199200
txout_index.last_revealed_index(&TestKeychain::External),
200201
Some(last_external_index)
@@ -248,7 +249,7 @@ fn test_scan_with_lookahead() {
248249
value: 0,
249250
};
250251

251-
let changeset = txout_index.scan_txout(op, &txout);
252+
let changeset = txout_index.index_txout(op, &txout);
252253
assert_eq!(
253254
changeset.as_inner(),
254255
&[(TestKeychain::External, spk_i)].into()
@@ -273,7 +274,7 @@ fn test_scan_with_lookahead() {
273274
script_pubkey: spk_41,
274275
value: 0,
275276
};
276-
let changeset = txout_index.scan_txout(op, &txout);
277+
let changeset = txout_index.index_txout(op, &txout);
277278
assert!(changeset.is_empty());
278279
}
279280

crates/chain/tests/test_spk_txout_index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bdk_chain::SpkTxOutIndex;
1+
use bdk_chain::{indexed_tx_graph::Indexer, SpkTxOutIndex};
22
use bitcoin::{absolute, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
33

44
#[test]
@@ -22,7 +22,7 @@ fn spk_txout_sent_and_received() {
2222

2323
assert_eq!(index.sent_and_received(&tx1), (0, 42_000));
2424
assert_eq!(index.net_value(&tx1), 42_000);
25-
index.scan(&tx1);
25+
index.index_tx(&tx1);
2626
assert_eq!(
2727
index.sent_and_received(&tx1),
2828
(0, 42_000),
@@ -82,7 +82,7 @@ fn mark_used() {
8282
}],
8383
};
8484

85-
spk_index.scan(&tx1);
85+
spk_index.index_tx(&tx1);
8686
spk_index.unmark_used(&1);
8787
assert!(
8888
spk_index.is_used(&1),

0 commit comments

Comments
 (0)