1- use core:: convert:: Infallible ;
2-
31use alloc:: vec:: Vec ;
4- use bitcoin:: { OutPoint , Script , Transaction , TxOut } ;
2+ use bitcoin:: { OutPoint , Transaction , TxOut } ;
53
64use crate :: {
7- keychain:: Balance ,
85 tx_graph:: { Additions , TxGraph } ,
9- Anchor , Append , BlockId , ChainOracle , FullTxOut , ObservedAs ,
6+ Anchor , Append ,
107} ;
118
129/// A struct that combines [`TxGraph`] and an [`Indexer`] implementation.
@@ -29,6 +26,14 @@ impl<A, I: Default> Default for IndexedTxGraph<A, I> {
2926}
3027
3128impl < A , I > IndexedTxGraph < A , I > {
29+ /// Construct a new [`IndexedTxGraph`] with a given `index`.
30+ pub fn new ( index : I ) -> Self {
31+ Self {
32+ index,
33+ graph : TxGraph :: default ( ) ,
34+ }
35+ }
36+
3237 /// Get a reference of the internal transaction graph.
3338 pub fn graph ( & self ) -> & TxGraph < A > {
3439 & self . graph
@@ -157,115 +162,6 @@ where
157162 }
158163}
159164
160- impl < A : Anchor , I : OwnedIndexer > IndexedTxGraph < A , I > {
161- pub fn try_list_owned_txouts < ' a , C : ChainOracle + ' a > (
162- & ' a self ,
163- chain : & ' a C ,
164- chain_tip : BlockId ,
165- ) -> impl Iterator < Item = Result < FullTxOut < ObservedAs < A > > , C :: Error > > + ' a {
166- self . graph ( )
167- . try_list_chain_txouts ( chain, chain_tip)
168- . filter ( |r| {
169- if let Ok ( full_txout) = r {
170- if !self . index . is_spk_owned ( & full_txout. txout . script_pubkey ) {
171- return false ;
172- }
173- }
174- true
175- } )
176- }
177-
178- pub fn list_owned_txouts < ' a , C : ChainOracle < Error = Infallible > + ' a > (
179- & ' a self ,
180- chain : & ' a C ,
181- chain_tip : BlockId ,
182- ) -> impl Iterator < Item = FullTxOut < ObservedAs < A > > > + ' a {
183- self . try_list_owned_txouts ( chain, chain_tip)
184- . map ( |r| r. expect ( "oracle is infallible" ) )
185- }
186-
187- pub fn try_list_owned_unspents < ' a , C : ChainOracle + ' a > (
188- & ' a self ,
189- chain : & ' a C ,
190- chain_tip : BlockId ,
191- ) -> impl Iterator < Item = Result < FullTxOut < ObservedAs < A > > , C :: Error > > + ' a {
192- self . graph ( )
193- . try_list_chain_unspents ( chain, chain_tip)
194- . filter ( |r| {
195- if let Ok ( full_txout) = r {
196- if !self . index . is_spk_owned ( & full_txout. txout . script_pubkey ) {
197- return false ;
198- }
199- }
200- true
201- } )
202- }
203-
204- pub fn list_owned_unspents < ' a , C : ChainOracle < Error = Infallible > + ' a > (
205- & ' a self ,
206- chain : & ' a C ,
207- chain_tip : BlockId ,
208- ) -> impl Iterator < Item = FullTxOut < ObservedAs < A > > > + ' a {
209- self . try_list_owned_unspents ( chain, chain_tip)
210- . map ( |r| r. expect ( "oracle is infallible" ) )
211- }
212-
213- pub fn try_balance < C , F > (
214- & self ,
215- chain : & C ,
216- chain_tip : BlockId ,
217- mut should_trust : F ,
218- ) -> Result < Balance , C :: Error >
219- where
220- C : ChainOracle ,
221- F : FnMut ( & Script ) -> bool ,
222- {
223- let tip_height = chain_tip. height ;
224-
225- let mut immature = 0 ;
226- let mut trusted_pending = 0 ;
227- let mut untrusted_pending = 0 ;
228- let mut confirmed = 0 ;
229-
230- for res in self . try_list_owned_unspents ( chain, chain_tip) {
231- let txout = res?;
232-
233- match & txout. chain_position {
234- ObservedAs :: Confirmed ( _) => {
235- if txout. is_confirmed_and_spendable ( tip_height) {
236- confirmed += txout. txout . value ;
237- } else if !txout. is_mature ( tip_height) {
238- immature += txout. txout . value ;
239- }
240- }
241- ObservedAs :: Unconfirmed ( _) => {
242- if should_trust ( & txout. txout . script_pubkey ) {
243- trusted_pending += txout. txout . value ;
244- } else {
245- untrusted_pending += txout. txout . value ;
246- }
247- }
248- }
249- }
250-
251- Ok ( Balance {
252- immature,
253- trusted_pending,
254- untrusted_pending,
255- confirmed,
256- } )
257- }
258-
259- pub fn balance < C , F > ( & self , chain : & C , chain_tip : BlockId , should_trust : F ) -> Balance
260- where
261- C : ChainOracle < Error = Infallible > ,
262- F : FnMut ( & Script ) -> bool ,
263- {
264- self . try_balance ( chain, chain_tip, should_trust)
265- . expect ( "error is infallible" )
266- }
267- }
268-
269165/// A structure that represents changes to an [`IndexedTxGraph`].
270166#[ derive( Clone , Debug , PartialEq ) ]
271167#[ cfg_attr(
@@ -324,9 +220,3 @@ pub trait Indexer {
324220 /// Determines whether the transaction should be included in the index.
325221 fn is_tx_relevant ( & self , tx : & Transaction ) -> bool ;
326222}
327-
328- /// A trait that extends [`Indexer`] to also index "owned" script pubkeys.
329- pub trait OwnedIndexer : Indexer {
330- /// Determines whether a given script pubkey (`spk`) is owned.
331- fn is_spk_owned ( & self , spk : & Script ) -> bool ;
332- }
0 commit comments