@@ -183,7 +183,7 @@ impl<D: Database> CoinSelectionAlgorithm<D> for LargestFirstCoinSelection {
183183 mut optional_utxos : Vec < WeightedUtxo > ,
184184 fee_rate : FeeRate ,
185185 amount_needed : u64 ,
186- mut fee_amount : u64 ,
186+ fee_amount : u64 ,
187187 ) -> Result < CoinSelectionResult , Error > {
188188 log:: debug!(
189189 "amount_needed = `{}`, fee_amount = `{}`, fee_rate = `{:?}`" ,
@@ -202,44 +202,7 @@ impl<D: Database> CoinSelectionAlgorithm<D> for LargestFirstCoinSelection {
202202 . chain ( optional_utxos. into_iter ( ) . rev ( ) . map ( |utxo| ( false , utxo) ) )
203203 } ;
204204
205- // Keep including inputs until we've got enough.
206- // Store the total input value in selected_amount and the total fee being paid in fee_amount
207- let mut selected_amount = 0 ;
208- let selected = utxos
209- . scan (
210- ( & mut selected_amount, & mut fee_amount) ,
211- |( selected_amount, fee_amount) , ( must_use, weighted_utxo) | {
212- if must_use || * * selected_amount < amount_needed + * * fee_amount {
213- * * fee_amount +=
214- fee_rate. fee_wu ( TXIN_BASE_WEIGHT + weighted_utxo. satisfaction_weight ) ;
215- * * selected_amount += weighted_utxo. utxo . txout ( ) . value ;
216-
217- log:: debug!(
218- "Selected {}, updated fee_amount = `{}`" ,
219- weighted_utxo. utxo. outpoint( ) ,
220- fee_amount
221- ) ;
222-
223- Some ( weighted_utxo. utxo )
224- } else {
225- None
226- }
227- } ,
228- )
229- . collect :: < Vec < _ > > ( ) ;
230-
231- let amount_needed_with_fees = amount_needed + fee_amount;
232- if selected_amount < amount_needed_with_fees {
233- return Err ( Error :: InsufficientFunds {
234- needed : amount_needed_with_fees,
235- available : selected_amount,
236- } ) ;
237- }
238-
239- Ok ( CoinSelectionResult {
240- selected,
241- fee_amount,
242- } )
205+ select_sorted_utxos ( utxos, fee_rate, amount_needed, fee_amount)
243206 }
244207}
245208
@@ -258,7 +221,7 @@ impl<D: Database> CoinSelectionAlgorithm<D> for OldestFirstCoinSelection {
258221 mut optional_utxos : Vec < WeightedUtxo > ,
259222 fee_rate : FeeRate ,
260223 amount_needed : u64 ,
261- mut fee_amount : u64 ,
224+ fee_amount : u64 ,
262225 ) -> Result < CoinSelectionResult , Error > {
263226 // query db and create a blockheight lookup table
264227 let blockheights = optional_utxos
@@ -298,45 +261,52 @@ impl<D: Database> CoinSelectionAlgorithm<D> for OldestFirstCoinSelection {
298261 . chain ( optional_utxos. into_iter ( ) . map ( |utxo| ( false , utxo) ) )
299262 } ;
300263
301- // Keep including inputs until we've got enough.
302- // Store the total input value in selected_amount and the total fee being paid in fee_amount
303- let mut selected_amount = 0 ;
304- let selected = utxos
305- . scan (
306- ( & mut selected_amount, & mut fee_amount) ,
307- |( selected_amount, fee_amount) , ( must_use, weighted_utxo) | {
308- if must_use || * * selected_amount < amount_needed + * * fee_amount {
309- * * fee_amount +=
310- fee_rate. fee_wu ( TXIN_BASE_WEIGHT + weighted_utxo. satisfaction_weight ) ;
311- * * selected_amount += weighted_utxo. utxo . txout ( ) . value ;
312-
313- log:: debug!(
314- "Selected {}, updated fee_amount = `{}`" ,
315- weighted_utxo. utxo. outpoint( ) ,
316- fee_amount
317- ) ;
318-
319- Some ( weighted_utxo. utxo )
320- } else {
321- None
322- }
323- } ,
324- )
325- . collect :: < Vec < _ > > ( ) ;
326-
327- let amount_needed_with_fees = amount_needed + fee_amount;
328- if selected_amount < amount_needed_with_fees {
329- return Err ( Error :: InsufficientFunds {
330- needed : amount_needed_with_fees,
331- available : selected_amount,
332- } ) ;
333- }
264+ select_sorted_utxos ( utxos, fee_rate, amount_needed, fee_amount)
265+ }
266+ }
334267
335- Ok ( CoinSelectionResult {
336- selected,
337- fee_amount,
338- } )
268+ fn select_sorted_utxos (
269+ utxos : impl Iterator < Item = ( bool , WeightedUtxo ) > ,
270+ fee_rate : FeeRate ,
271+ amount_needed : u64 ,
272+ mut fee_amount : u64 ,
273+ ) -> Result < CoinSelectionResult , Error > {
274+ let mut selected_amount = 0 ;
275+ let selected = utxos
276+ . scan (
277+ ( & mut selected_amount, & mut fee_amount) ,
278+ |( selected_amount, fee_amount) , ( must_use, weighted_utxo) | {
279+ if must_use || * * selected_amount < amount_needed + * * fee_amount {
280+ * * fee_amount +=
281+ fee_rate. fee_wu ( TXIN_BASE_WEIGHT + weighted_utxo. satisfaction_weight ) ;
282+ * * selected_amount += weighted_utxo. utxo . txout ( ) . value ;
283+
284+ log:: debug!(
285+ "Selected {}, updated fee_amount = `{}`" ,
286+ weighted_utxo. utxo. outpoint( ) ,
287+ fee_amount
288+ ) ;
289+
290+ Some ( weighted_utxo. utxo )
291+ } else {
292+ None
293+ }
294+ } ,
295+ )
296+ . collect :: < Vec < _ > > ( ) ;
297+
298+ let amount_needed_with_fees = amount_needed + fee_amount;
299+ if selected_amount < amount_needed_with_fees {
300+ return Err ( Error :: InsufficientFunds {
301+ needed : amount_needed_with_fees,
302+ available : selected_amount,
303+ } ) ;
339304 }
305+
306+ Ok ( CoinSelectionResult {
307+ selected,
308+ fee_amount,
309+ } )
340310}
341311
342312#[ derive( Debug , Clone ) ]
0 commit comments