diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs
index 7ab0ffa8..f69b227a 100644
--- a/crates/chain/src/indexed_tx_graph.rs
+++ b/crates/chain/src/indexed_tx_graph.rs
@@ -1,12 +1,9 @@
-use core::convert::Infallible;
-
use alloc::vec::Vec;
-use bitcoin::{OutPoint, Script, Transaction, TxOut};
+use bitcoin::{OutPoint, Transaction, TxOut};
use crate::{
- keychain::Balance,
tx_graph::{Additions, TxGraph},
- Anchor, Append, BlockId, ChainOracle, FullTxOut, ObservedAs,
+ Anchor, Append,
};
/// A struct that combines [`TxGraph`] and an [`Indexer`] implementation.
@@ -29,6 +26,14 @@ impl Default for IndexedTxGraph {
}
impl IndexedTxGraph {
+ /// Construct a new [`IndexedTxGraph`] with a given `index`.
+ pub fn new(index: I) -> Self {
+ Self {
+ index,
+ graph: TxGraph::default(),
+ }
+ }
+
/// Get a reference of the internal transaction graph.
pub fn graph(&self) -> &TxGraph {
&self.graph
@@ -157,115 +162,6 @@ where
}
}
-impl IndexedTxGraph {
- pub fn try_list_owned_txouts<'a, C: ChainOracle + 'a>(
- &'a self,
- chain: &'a C,
- chain_tip: BlockId,
- ) -> impl Iterator- >, C::Error>> + 'a {
- self.graph()
- .try_list_chain_txouts(chain, chain_tip)
- .filter(|r| {
- if let Ok(full_txout) = r {
- if !self.index.is_spk_owned(&full_txout.txout.script_pubkey) {
- return false;
- }
- }
- true
- })
- }
-
- pub fn list_owned_txouts<'a, C: ChainOracle + 'a>(
- &'a self,
- chain: &'a C,
- chain_tip: BlockId,
- ) -> impl Iterator
- >> + 'a {
- self.try_list_owned_txouts(chain, chain_tip)
- .map(|r| r.expect("oracle is infallible"))
- }
-
- pub fn try_list_owned_unspents<'a, C: ChainOracle + 'a>(
- &'a self,
- chain: &'a C,
- chain_tip: BlockId,
- ) -> impl Iterator
- >, C::Error>> + 'a {
- self.graph()
- .try_list_chain_unspents(chain, chain_tip)
- .filter(|r| {
- if let Ok(full_txout) = r {
- if !self.index.is_spk_owned(&full_txout.txout.script_pubkey) {
- return false;
- }
- }
- true
- })
- }
-
- pub fn list_owned_unspents<'a, C: ChainOracle + 'a>(
- &'a self,
- chain: &'a C,
- chain_tip: BlockId,
- ) -> impl Iterator
- >> + 'a {
- self.try_list_owned_unspents(chain, chain_tip)
- .map(|r| r.expect("oracle is infallible"))
- }
-
- pub fn try_balance(
- &self,
- chain: &C,
- chain_tip: BlockId,
- mut should_trust: F,
- ) -> Result
- where
- C: ChainOracle,
- F: FnMut(&Script) -> bool,
- {
- let tip_height = chain_tip.height;
-
- let mut immature = 0;
- let mut trusted_pending = 0;
- let mut untrusted_pending = 0;
- let mut confirmed = 0;
-
- for res in self.try_list_owned_unspents(chain, chain_tip) {
- let txout = res?;
-
- match &txout.chain_position {
- ObservedAs::Confirmed(_) => {
- if txout.is_confirmed_and_spendable(tip_height) {
- confirmed += txout.txout.value;
- } else if !txout.is_mature(tip_height) {
- immature += txout.txout.value;
- }
- }
- ObservedAs::Unconfirmed(_) => {
- if should_trust(&txout.txout.script_pubkey) {
- trusted_pending += txout.txout.value;
- } else {
- untrusted_pending += txout.txout.value;
- }
- }
- }
- }
-
- Ok(Balance {
- immature,
- trusted_pending,
- untrusted_pending,
- confirmed,
- })
- }
-
- pub fn balance(&self, chain: &C, chain_tip: BlockId, should_trust: F) -> Balance
- where
- C: ChainOracle,
- F: FnMut(&Script) -> bool,
- {
- self.try_balance(chain, chain_tip, should_trust)
- .expect("error is infallible")
- }
-}
-
/// A structure that represents changes to an [`IndexedTxGraph`].
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
@@ -324,9 +220,3 @@ pub trait Indexer {
/// Determines whether the transaction should be included in the index.
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
}
-
-/// A trait that extends [`Indexer`] to also index "owned" script pubkeys.
-pub trait OwnedIndexer: Indexer {
- /// Determines whether a given script pubkey (`spk`) is owned.
- fn is_spk_owned(&self, spk: &Script) -> bool;
-}
diff --git a/crates/chain/src/keychain/txout_index.rs b/crates/chain/src/keychain/txout_index.rs
index c7a8dd54..397c4338 100644
--- a/crates/chain/src/keychain/txout_index.rs
+++ b/crates/chain/src/keychain/txout_index.rs
@@ -1,6 +1,6 @@
use crate::{
collections::*,
- indexed_tx_graph::{Indexer, OwnedIndexer},
+ indexed_tx_graph::Indexer,
miniscript::{Descriptor, DescriptorPublicKey},
spk_iter::BIP32_MAX_INDEX,
ForEachTxOut, SpkIterator, SpkTxOutIndex,
@@ -109,12 +109,6 @@ impl Indexer for KeychainTxOutIndex {
}
}
-impl OwnedIndexer for KeychainTxOutIndex {
- fn is_spk_owned(&self, spk: &Script) -> bool {
- self.index_of_spk(spk).is_some()
- }
-}
-
impl KeychainTxOutIndex {
/// Scans an object for relevant outpoints, which are stored and indexed internally.
///
@@ -153,6 +147,11 @@ impl KeychainTxOutIndex {
&self.inner
}
+ /// Get a reference to the set of indexed outpoints.
+ pub fn outpoints(&self) -> &BTreeSet<((K, u32), OutPoint)> {
+ self.inner.outpoints()
+ }
+
/// Return a reference to the internal map of the keychain to descriptors.
pub fn keychains(&self) -> &BTreeMap> {
&self.keychains
diff --git a/crates/chain/src/spk_txout_index.rs b/crates/chain/src/spk_txout_index.rs
index ae944149..0eaec4bb 100644
--- a/crates/chain/src/spk_txout_index.rs
+++ b/crates/chain/src/spk_txout_index.rs
@@ -2,7 +2,7 @@ use core::ops::RangeBounds;
use crate::{
collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
- indexed_tx_graph::{Indexer, OwnedIndexer},
+ indexed_tx_graph::Indexer,
ForEachTxOut,
};
use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid};
@@ -75,12 +75,6 @@ impl Indexer for SpkTxOutIndex {
}
}
-impl OwnedIndexer for SpkTxOutIndex {
- fn is_spk_owned(&self, spk: &Script) -> bool {
- self.spk_indices.get(spk).is_some()
- }
-}
-
/// This macro is used instead of a member function of `SpkTxOutIndex`, which would result in a
/// compiler error[E0521]: "borrowed data escapes out of closure" when we attempt to take a
/// reference out of the `ForEachTxOut` closure during scanning.
@@ -126,6 +120,11 @@ impl SpkTxOutIndex {
scan_txout!(self, op, txout)
}
+ /// Get a reference to the set of indexed outpoints.
+ pub fn outpoints(&self) -> &BTreeSet<(I, OutPoint)> {
+ &self.spk_txouts
+ }
+
/// Iterate over all known txouts that spend to tracked script pubkeys.
pub fn txouts(
&self,
diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs
index ef3f3847..f0a095ac 100644
--- a/crates/chain/src/tx_graph.rs
+++ b/crates/chain/src/tx_graph.rs
@@ -56,10 +56,11 @@
//! ```
use crate::{
- collections::*, Anchor, Append, BlockId, ChainOracle, ForEachTxOut, FullTxOut, ObservedAs,
+ collections::*, keychain::Balance, Anchor, Append, BlockId, ChainOracle, ForEachTxOut,
+ FullTxOut, ObservedAs,
};
use alloc::vec::Vec;
-use bitcoin::{OutPoint, Transaction, TxOut, Txid};
+use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid};
use core::{
convert::Infallible,
ops::{Deref, RangeInclusive},
@@ -762,107 +763,190 @@ impl TxGraph {
.map(|r| r.expect("oracle is infallible"))
}
- /// List outputs that are in `chain` with `chain_tip`.
+ /// Get a filtered list of outputs from the given `outpoints` that are in `chain` with
+ /// `chain_tip`.
///
- /// Floating ouputs are not iterated over.
+ /// `outpoints` is a list of outpoints we are interested in, coupled with the associated txout's
+ /// script pubkey index (`S`).
///
- /// The `filter_predicate` should return true for outputs that we wish to iterate over.
+ /// Floating outputs are ignored.
///
/// # Error
///
- /// A returned item can error if the [`ChainOracle`] implementation (`chain`) fails.
+ /// An [`Iterator::Item`] can be an [`Err`] if the [`ChainOracle`] implementation (`chain`)
+ /// fails.
///
- /// If the [`ChainOracle`] is infallible, [`list_chain_txouts`] can be used instead.
+ /// If the [`ChainOracle`] implementation is infallible, [`filter_chain_txouts`] can be used
+ /// instead.
///
- /// [`list_chain_txouts`]: Self::list_chain_txouts
- pub fn try_list_chain_txouts<'a, C: ChainOracle + 'a>(
+ /// [`filter_chain_txouts`]: Self::filter_chain_txouts
+ pub fn try_filter_chain_txouts<'a, C: ChainOracle + 'a, S: Clone + 'a>(
&'a self,
chain: &'a C,
chain_tip: BlockId,
- ) -> impl Iterator
- >, C::Error>> + 'a {
- self.try_list_chain_txs(chain, chain_tip)
- .flat_map(move |tx_res| match tx_res {
- Ok(canonical_tx) => canonical_tx
- .node
- .output
- .iter()
- .enumerate()
- .map(|(vout, txout)| {
- let outpoint = OutPoint::new(canonical_tx.node.txid, vout as _);
- Ok((outpoint, txout.clone(), canonical_tx.clone()))
- })
- .collect::>(),
- Err(err) => vec![Err(err)],
- })
- .map(move |res| -> Result<_, C::Error> {
- let (
- outpoint,
- txout,
- CanonicalTx {
- observed_as,
- node: tx_node,
- },
- ) = res?;
- let chain_position = observed_as.cloned();
- let spent_by = self
- .try_get_chain_spend(chain, chain_tip, outpoint)?
- .map(|(obs_as, txid)| (obs_as.cloned(), txid));
- let is_on_coinbase = tx_node.tx.is_coin_base();
- Ok(FullTxOut {
- outpoint,
- txout,
- chain_position,
- spent_by,
- is_on_coinbase,
- })
- })
+ outpoints: impl IntoIterator
- + 'a,
+ ) -> impl Iterator
- >), C::Error>> + 'a {
+ outpoints
+ .into_iter()
+ .map(
+ move |(spk_i, op)| -> Result