/// Scan and index the given `outpoint` and `txout`.
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet;
- /// Scan and index the given transaction.
+ /// Scans a transaction for relevant outpoints, which are stored and indexed internally.
+ ///
+ /// If the matched script pubkey is part of the lookahead, the last stored index is updated for
+ /// the script pubkey's keychain and the [`ChangeSet`] returned will reflect the
+ /// change.
+ ///
+ /// Typically, this method is used in two situations:
+ ///
+ /// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
+ /// your txouts.
+ /// 2. When getting new data from the chain, you usually scan it before incorporating it into
+ /// your chain state.
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet;
/// Apply changeset to itself.
type ChangeSet = super::ChangeSet<K>;
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
- self.scan_txout(outpoint, txout)
+ let mut changeset = super::ChangeSet::<K>::default();
+ for (keychain, index) in self.inner.index_txout(outpoint, txout) {
+ changeset.append(self.reveal_to_target(&keychain, index).1);
+ }
+ changeset
}
fn index_tx(&mut self, tx: &bitcoin::Transaction) -> Self::ChangeSet {
- self.scan(tx)
+ let mut changeset = super::ChangeSet::<K>::default();
+ for (op, txout) in tx.output.iter().enumerate() {
+ changeset.append(self.index_txout(OutPoint::new(tx.txid(), op as u32), txout));
+ }
+ changeset
}
fn initial_changeset(&self) -> Self::ChangeSet {
}
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
- /// Scans a transaction for relevant outpoints, which are stored and indexed internally.
- ///
- /// If the matched script pubkey is part of the lookahead, the last stored index is updated for
- /// the script pubkey's keychain and the [`super::ChangeSet`] returned will reflect the
- /// change.
- ///
- /// Typically, this method is used in two situations:
- ///
- /// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
- /// your txouts.
- /// 2. When getting new data from the chain, you usually scan it before incorporating it into
- /// your chain state (i.e., `SparseChain`, `ChainGraph`).
- pub fn scan(&mut self, tx: &bitcoin::Transaction) -> super::ChangeSet<K> {
- let mut changeset = super::ChangeSet::<K>::default();
- for (op, txout) in tx.output.iter().enumerate() {
- changeset.append(self.scan_txout(OutPoint::new(tx.txid(), op as u32), txout));
- }
- changeset
- }
-
- /// Scan a single outpoint for a matching script pubkey.
- ///
- /// If it matches, this will store and index it.
- pub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> super::ChangeSet<K> {
- match self.inner.scan_txout(op, txout).cloned() {
- Some((keychain, index)) => self.reveal_to_target(&keychain, index).1,
- None => super::ChangeSet::default(),
- }
- }
-
/// Return a reference to the internal [`SpkTxOutIndex`].
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
&self.inner
/// Set the lookahead count for `keychain`.
///
/// The lookahead is the number of scripts to cache ahead of the last stored script index. This
- /// is useful during a scan via [`scan`] or [`scan_txout`].
+ /// is useful during a scan via [`Indexer::index_tx`] or [`Indexer::index_txout`].
///
/// # Panics
///
/// This will panic if the `keychain` does not exist.
- ///
- /// [`scan`]: Self::scan
- /// [`scan_txout`]: Self::scan_txout
pub fn set_lookahead(&mut self, keychain: &K, lookahead: u32) {
self.lookahead.insert(keychain.clone(), lookahead);
self.replenish_lookahead(keychain);
/// An index storing [`TxOut`]s that have a script pubkey that matches those in a list.
///
/// The basic idea is that you insert script pubkeys you care about into the index with
-/// [`insert_spk`] and then when you call [`scan`], the index will look at any txouts you pass in and
-/// store and index any txouts matching one of its script pubkeys.
+/// [`insert_spk`] and then when you call [`Indexer::index_tx`] or [`Indexer::index_txout`], the
+/// index will look at any txouts you pass in and store and index any txouts matching one of its
+/// script pubkeys.
///
/// Each script pubkey is associated with an application-defined index script index `I`, which must be
/// [`Ord`]. Usually, this is used to associate the derivation index of the script pubkey or even a
/// [`TxOut`]: bitcoin::TxOut
/// [`insert_spk`]: Self::insert_spk
/// [`Ord`]: core::cmp::Ord
-/// [`scan`]: Self::scan
/// [`TxGraph`]: crate::tx_graph::TxGraph
#[derive(Clone, Debug)]
pub struct SpkTxOutIndex<I> {
}
impl<I: Clone + Ord> Indexer for SpkTxOutIndex<I> {
- type ChangeSet = ();
+ type ChangeSet = BTreeSet<I>;
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
- self.scan_txout(outpoint, txout);
- Default::default()
+ let spk_i = self.spk_indices.get(&txout.script_pubkey);
+ let mut scanned_indices = BTreeSet::new();
+ if let Some(spk_i) = spk_i {
+ self.txouts.insert(outpoint, (spk_i.clone(), txout.clone()));
+ self.spk_txouts.insert((spk_i.clone(), outpoint));
+ self.unused.remove(spk_i);
+ scanned_indices.insert(spk_i.clone());
+ }
+ scanned_indices
}
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet {
- self.scan(tx);
- Default::default()
+ let mut scanned_indices = BTreeSet::new();
+
+ for (i, txout) in tx.output.iter().enumerate() {
+ let op = OutPoint::new(tx.txid(), i as u32);
+ let mut txout_indices = self.index_txout(op, txout);
+ scanned_indices.append(&mut txout_indices);
+ }
+
+ scanned_indices
}
- fn initial_changeset(&self) -> Self::ChangeSet {}
+ fn initial_changeset(&self) -> Self::ChangeSet {
+ self.spks.keys().cloned().collect()
+ }
fn apply_changeset(&mut self, _changeset: Self::ChangeSet) {
// This applies nothing.
}
impl<I: Clone + Ord> SpkTxOutIndex<I> {
- /// Scans a transaction containing many txouts.
- ///
- /// Typically, this is used in two situations:
- ///
- /// 1. After loading transaction data from the disk, you may scan over all the txouts to restore all
- /// your txouts.
- /// 2. When getting new data from the chain, you usually scan it before incorporating it into your chain state.
- pub fn scan(&mut self, tx: &bitcoin::Transaction) -> BTreeSet<I> {
- let mut scanned_indices = BTreeSet::new();
-
- for (i, txout) in tx.output.iter().enumerate() {
- let op = OutPoint::new(tx.txid(), i as u32);
- if let Some(spk_i) = self.scan_txout(op, txout) {
- scanned_indices.insert(spk_i.clone());
- }
- }
-
- scanned_indices
- }
-
- /// Scan a single `TxOut` for a matching script pubkey and returns the index that matches the
- /// script pubkey (if any).
- pub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> Option<&I> {
- let spk_i = self.spk_indices.get(&txout.script_pubkey);
- if let Some(spk_i) = spk_i {
- self.txouts.insert(op, (spk_i.clone(), txout.clone()));
- self.spk_txouts.insert((spk_i.clone(), op));
- self.unused.remove(spk_i);
- }
- spk_i
- }
-
/// Get a reference to the set of indexed outpoints.
pub fn outpoints(&self) -> &BTreeSet<(I, OutPoint)> {
&self.spk_txouts
mod common;
use bdk_chain::{
collections::BTreeMap,
+ indexed_tx_graph::Indexer,
keychain::{self, KeychainTxOutIndex},
Append,
};
],
..common::new_tx(external_index)
};
- assert_eq!(txout_index.scan(&tx), keychain::ChangeSet::default());
+ assert_eq!(txout_index.index_tx(&tx), keychain::ChangeSet::default());
assert_eq!(
txout_index.last_revealed_index(&TestKeychain::External),
Some(last_external_index)
value: 0,
};
- let changeset = txout_index.scan_txout(op, &txout);
+ let changeset = txout_index.index_txout(op, &txout);
assert_eq!(
changeset.as_inner(),
&[(TestKeychain::External, spk_i)].into()
script_pubkey: spk_41,
value: 0,
};
- let changeset = txout_index.scan_txout(op, &txout);
+ let changeset = txout_index.index_txout(op, &txout);
assert!(changeset.is_empty());
}
-use bdk_chain::SpkTxOutIndex;
+use bdk_chain::{indexed_tx_graph::Indexer, SpkTxOutIndex};
use bitcoin::{absolute, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
#[test]
assert_eq!(index.sent_and_received(&tx1), (0, 42_000));
assert_eq!(index.net_value(&tx1), 42_000);
- index.scan(&tx1);
+ index.index_tx(&tx1);
assert_eq!(
index.sent_and_received(&tx1),
(0, 42_000),
}],
};
- spk_index.scan(&tx1);
+ spk_index.index_tx(&tx1);
spk_index.unmark_used(&1);
assert!(
spk_index.is_used(&1),