@@ -9,8 +9,7 @@ use bdk_chain::{
99use bdk_coin_select:: DrainWeights ;
1010use bdk_testenv:: { bitcoincore_rpc:: RpcApi , TestEnv } ;
1111use bdk_tx:: {
12- CPFPParams , CPFPSet , CanonicalUnspents , Input , InputCandidates , RbfParams , TxStatus ,
13- TxWithStatus ,
12+ CanonicalUnspents , Input , InputCandidates , RbfParams , Selection , TxStatus , TxWithStatus ,
1413} ;
1514use bitcoin:: { absolute, Address , BlockHash , FeeRate , OutPoint , Transaction , Txid } ;
1615use miniscript:: {
@@ -89,8 +88,6 @@ impl Wallet {
8988 Ok ( ( tip_height, tip_time) )
9089 }
9190
92- // TODO: Maybe create an `AssetsBuilder` or `AssetsExt` that makes it easier to add
93- // assets from descriptors, etc.
9491 pub fn assets ( & self ) -> Assets {
9592 let index = & self . graph . index ;
9693 let tip = self . chain . tip ( ) . block_id ( ) ;
@@ -207,50 +204,28 @@ impl Wallet {
207204 ) )
208205 }
209206
210- pub fn cpfp_candidates (
211- & self ,
207+ pub fn create_cpfp_transaction (
208+ & mut self ,
212209 parent_txids : impl IntoIterator < Item = Txid > ,
213- tip_height : absolute:: Height ,
214- target_feerate : FeeRate ,
215- ) -> anyhow:: Result < ( InputCandidates , CPFPParams ) > {
210+ target_package_feerate : FeeRate ,
211+ ) -> anyhow:: Result < Selection > {
216212 let assets = self . assets ( ) ;
217213 let canon_utxos = CanonicalUnspents :: new ( self . canonical_txs ( ) ) ;
218- let index = & self . graph . index ;
219- let parent_txids: Vec < Txid > = parent_txids. into_iter ( ) . collect ( ) ;
220214
221- let cpfpset = CPFPSet :: new ( parent_txids. clone ( ) , self . graph . graph ( ) , tip_height) ?;
222- let cpfp_params = cpfpset. selector_cpfp_params ( target_feerate) ;
223-
224- let must_select = cpfpset
225- . must_select_largest_input_of_each_parent ( & canon_utxos) ?
226- . into_iter ( )
227- . map ( |op| {
228- let plan = self
229- . plan_of_output ( op, & assets)
230- . ok_or_else ( || anyhow:: anyhow!( "failed to derive plan for outpoint {}" , op) ) ?;
231- canon_utxos. try_get_unspent ( op, plan) . ok_or_else ( || {
232- anyhow:: anyhow!( "failed to get unspent input for outpoint {}" , op)
233- } )
234- } )
235- . collect :: < Result < Vec < Input > , _ > > ( ) ?;
236-
237- // Select other spendable outputs as optional candidates
238- let can_select = index
239- . outpoints ( )
215+ let script_pubkey = self . next_address ( ) . unwrap ( ) . script_pubkey ( ) ;
216+ let cpfpset = canon_utxos. build_cpfp_set_from_txids ( parent_txids, self . graph . graph ( ) ) ?;
217+ let plans = cpfpset
218+ . selected_outpoints
240219 . iter ( )
241- . filter_map ( |( _, op) | {
242- if canon_utxos. is_unspent ( * op) {
243- self . plan_of_output ( * op, & assets)
244- . map ( |plan| canon_utxos. try_get_unspent ( * op, plan) )
245- } else {
246- None
247- }
248- } )
249- . flatten ( ) ;
220+ . filter_map ( |op| self . plan_of_output ( * op, & assets) ) ;
250221
251- let candidates = InputCandidates :: new ( must_select, can_select)
252- . filter ( cpfpset. candidate_filter ( & canon_utxos, tip_height) ) ;
222+ let selection = cpfpset. create_cpfp_transaction (
223+ & canon_utxos,
224+ target_package_feerate,
225+ & script_pubkey,
226+ plans,
227+ ) ?;
253228
254- Ok ( ( candidates , cpfp_params ) )
229+ Ok ( selection )
255230 }
256231}
0 commit comments