9191//! [`insert_txout`]: TxGraph::insert_txout
9292
9393use crate :: collections:: * ;
94+ use crate :: spk_txout:: SpkTxOutIndex ;
9495use crate :: BlockId ;
9596use crate :: CanonicalIter ;
9697use crate :: CanonicalReason ;
98+ use crate :: Indexer ;
9799use crate :: ObservedIn ;
98100use crate :: { Anchor , Balance , ChainOracle , ChainPosition , FullTxOut , Merge } ;
99101use alloc:: collections:: vec_deque:: VecDeque ;
@@ -105,7 +107,7 @@ use bitcoin::{Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txi
105107use core:: fmt:: { self , Formatter } ;
106108use core:: {
107109 convert:: Infallible ,
108- ops:: { Deref , RangeInclusive } ,
110+ ops:: { Deref , RangeBounds , RangeInclusive } ,
109111} ;
110112
111113impl < A : Ord > From < TxGraph < A > > for TxUpdate < A > {
@@ -1161,6 +1163,50 @@ impl<A: Anchor> TxGraph<A> {
11611163 self . try_balance ( chain, chain_tip, outpoints, trust_predicate)
11621164 . expect ( "oracle is infallible" )
11631165 }
1166+
1167+ /// Returns an iterator over unconfirmed transactions and their associated script pubkeys,
1168+ /// filtered within the specified `range`.
1169+ ///
1170+ /// This function scans the transaction graph for unconfirmed transactions relevant to the
1171+ /// provided [`SpkTxOutIndex`], determining which transactions should be considered based on
1172+ /// indexed outputs.
1173+ pub fn iter_spks_with_expected_txids < ' a , C , I > (
1174+ & ' a self ,
1175+ chain : & ' a C ,
1176+ indexer : & ' a SpkTxOutIndex < I > ,
1177+ range : impl RangeBounds < I > + ' a ,
1178+ ) -> impl Iterator < Item = ( Txid , ScriptBuf ) > + ' a
1179+ where
1180+ C : ChainOracle < Error = core:: convert:: Infallible > ,
1181+ I : fmt:: Debug + Clone + Ord ,
1182+ {
1183+ let chain_tip = chain. get_chain_tip ( ) . unwrap ( ) ;
1184+
1185+ self . list_canonical_txs ( chain, chain_tip)
1186+ . filter ( |c| !c. chain_position . is_confirmed ( ) && indexer. is_tx_relevant ( & c. tx_node ) )
1187+ . flat_map ( move |c| {
1188+ let txid = c. tx_node . txid ;
1189+ let spks = c
1190+ . tx_node
1191+ . input
1192+ . iter ( )
1193+ . map ( |txin| txin. previous_output )
1194+ . chain (
1195+ c. tx_node
1196+ . output
1197+ . iter ( )
1198+ . enumerate ( )
1199+ . map ( move |( vout, _) | OutPoint :: new ( txid, vout as u32 ) ) ,
1200+ )
1201+ . flat_map ( |op| match indexer. txout ( op) {
1202+ Some ( ( i, txo) ) if range. contains ( i) => Some ( txo. script_pubkey . clone ( ) ) ,
1203+ _ => None ,
1204+ } )
1205+ . collect :: < Vec < _ > > ( ) ;
1206+
1207+ core:: iter:: repeat ( txid) . zip ( spks)
1208+ } )
1209+ }
11641210}
11651211
11661212/// The [`ChangeSet`] represents changes to a [`TxGraph`].
0 commit comments