@@ -208,14 +208,14 @@ impl<'a> CoinSelector<'a> {
208
208
self . base_weight + self . input_weight ( ) + drain_weight
209
209
}
210
210
211
- /// How much the current selection overshoots the value needed to acheive `target`.
211
+ /// How much the current selection overshoots the value needed to achieve `target`.
212
212
///
213
213
/// In order for the resulting transaction to be valid this must be 0.
214
214
pub fn excess ( & self , target : Target , drain : Drain ) -> i64 {
215
215
self . selected_value ( ) as i64
216
216
- target. value as i64
217
217
- drain. value as i64
218
- - self . implied_fee ( target. feerate , target. min_fee , drain. weight ) as i64
218
+ - self . implied_fee ( target. feerate , target. min_fee , drain. weights . output_weight ) as i64
219
219
}
220
220
221
221
/// How much the current selection overshoots the value need to satisfy `target.feerate` and
@@ -224,7 +224,7 @@ impl<'a> CoinSelector<'a> {
224
224
self . selected_value ( ) as i64
225
225
- target. value as i64
226
226
- drain. value as i64
227
- - self . implied_fee_from_feerate ( target. feerate , drain. weight ) as i64
227
+ - self . implied_fee_from_feerate ( target. feerate , drain. weights . output_weight ) as i64
228
228
}
229
229
230
230
/// How much the current selection overshoots the value needed to satisfy `target.min_fee` and
@@ -236,11 +236,11 @@ impl<'a> CoinSelector<'a> {
236
236
- target. min_fee as i64
237
237
}
238
238
239
- /// The feerate the transaction would have if we were to use this selection of inputs to acheive
240
- /// the `target_value`
239
+ /// The feerate the transaction would have if we were to use this selection of inputs to achieve
240
+ /// the `target_value`.
241
241
pub fn implied_feerate ( & self , target_value : u64 , drain : Drain ) -> FeeRate {
242
242
let numerator = self . selected_value ( ) as i64 - target_value as i64 - drain. value as i64 ;
243
- let denom = self . weight ( drain. weight ) ;
243
+ let denom = self . weight ( drain. weights . output_weight ) ;
244
244
FeeRate :: from_sat_per_wu ( numerator as f32 / denom as f32 )
245
245
}
246
246
@@ -327,8 +327,8 @@ impl<'a> CoinSelector<'a> {
327
327
excess_waste *= excess_discount. max ( 0.0 ) . min ( 1.0 ) ;
328
328
waste += excess_waste;
329
329
} else {
330
- waste += drain. weight as f32 * target. feerate . spwu ( )
331
- + drain. spend_weight as f32 * long_term_feerate. spwu ( ) ;
330
+ waste += drain. weights . output_weight as f32 * target. feerate . spwu ( )
331
+ + drain. weights . spend_weight as f32 * long_term_feerate. spwu ( ) ;
332
332
}
333
333
334
334
waste
@@ -514,6 +514,34 @@ impl Candidate {
514
514
}
515
515
}
516
516
517
+ /// A structure that represents the weight costs of a drain (a.k.a. change) output.
518
+ ///
519
+ /// This structure can also represent multiple outputs.
520
+ #[ derive( Default , Debug , Clone , Copy , Hash , PartialEq , Eq ) ]
521
+ pub struct DrainWeights {
522
+ /// The weight of adding this drain output.
523
+ pub output_weight : u32 ,
524
+ /// The weight of spending this drain output (in the future).
525
+ pub spend_weight : u32 ,
526
+ }
527
+
528
+ impl DrainWeights {
529
+ /// The waste of adding this drain to a transaction according to the [waste metric].
530
+ ///
531
+ /// [waste metric]: https://bitcoin.stackexchange.com/questions/113622/what-does-waste-metric-mean-in-the-context-of-coin-selection
532
+ pub fn waste ( & self , feerate : FeeRate , long_term_feerate : FeeRate ) -> f32 {
533
+ self . output_weight as f32 * feerate. spwu ( )
534
+ + self . spend_weight as f32 * long_term_feerate. spwu ( )
535
+ }
536
+
537
+ pub fn new_tr_keyspend ( ) -> Self {
538
+ Self {
539
+ output_weight : TXOUT_BASE_WEIGHT + TR_SPK_WEIGHT ,
540
+ spend_weight : TXIN_BASE_WEIGHT + TR_KEYSPEND_SATISFACTION_WEIGHT ,
541
+ }
542
+ }
543
+ }
544
+
517
545
/// A drain (A.K.A. change) output.
518
546
/// Technically it could represent multiple outputs.
519
547
///
@@ -522,12 +550,10 @@ impl Candidate {
522
550
/// [`change_policy`]: crate::change_policy
523
551
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash , Default ) ]
524
552
pub struct Drain {
525
- /// The weight of adding this drain
526
- pub weight : u32 ,
527
- /// The value that should be assigned to the drain
553
+ /// Weight of adding drain output and spending the drain output.
554
+ pub weights : DrainWeights ,
555
+ /// The value that should be assigned to the drain.
528
556
pub value : u64 ,
529
- /// The weight of spending this drain
530
- pub spend_weight : u32 ,
531
557
}
532
558
533
559
impl Drain {
@@ -546,19 +572,11 @@ impl Drain {
546
572
!self . is_none ( )
547
573
}
548
574
549
- pub fn new_tr_keyspend ( ) -> Self {
550
- Self {
551
- weight : TXOUT_BASE_WEIGHT + TR_SPK_WEIGHT ,
552
- value : 0 ,
553
- spend_weight : TXIN_BASE_WEIGHT + TR_KEYSPEND_SATISFACTION_WEIGHT ,
554
- }
555
- }
556
-
557
575
/// The waste of adding this drain to a transaction according to the [waste metric].
558
576
///
559
577
/// [waste metric]: https://bitcoin.stackexchange.com/questions/113622/what-does-waste-metric-mean-in-the-context-of-coin-selection
560
578
pub fn waste ( & self , feerate : FeeRate , long_term_feerate : FeeRate ) -> f32 {
561
- self . weight as f32 * feerate . spwu ( ) + self . spend_weight as f32 * long_term_feerate. spwu ( )
579
+ self . weights . waste ( feerate , long_term_feerate)
562
580
}
563
581
}
564
582
0 commit comments