@@ -167,7 +167,7 @@ impl CoinSelectionResult {
167167/// that output in the future.
168168/// > change_cost = current_fee_rate * change_output_size + long_term_feerate * change_spend_size
169169///
170- /// Excess happens when there is not change, and the surplus of coins is spend as part of the fees
170+ /// Excess happens when there is no change, and the surplus of coins is spend as part of the fees
171171/// to the miner:
172172/// > excess = tx_total_value - tx_fees - target
173173///
@@ -176,12 +176,13 @@ impl CoinSelectionResult {
176176/// Creation cost can be zero if there is a perfect match as result of the coin selection
177177/// algorithm.
178178///
179- /// So, waste can be zero or negative if the creation cost is zero and the timing
180- /// cost is less than or equal to zero.
179+ /// So, waste can be zero if creation and timing cost are zero. Or can be negative, if timing cost
180+ /// is negative and the creation cost is lower enough (less than the absolute value of timing
181+ /// cost).
181182#[ derive( Debug , PartialEq , Eq , PartialOrd , Ord ) ]
182183pub struct Waste ( pub i64 ) ;
183184
184- const LONG_TERM_FEE_RATE : FeeRate = FeeRate :: from_sat_per_vb ( 10 .0) ;
185+ const LONG_TERM_FEE_RATE : FeeRate = FeeRate :: from_sat_per_vb ( 5 .0) ;
185186
186187impl Waste {
187188 /// Calculate the amount of waste for the given coin selection
@@ -1507,16 +1508,19 @@ mod test {
15071508 . map ( |u| OutputGroup :: new ( u, fee_rate) )
15081509 . collect ( ) ;
15091510
1510- let size_of_change = 31 ;
1511- let cost_of_change: u64 = ( size_of_change as f32 * fee_rate. as_sat_vb ( ) ) as u64 ;
1511+ let change_out_size = 31_f32 ;
1512+ let change_in_size = 68_f32 ;
1513+ let cost_of_change: u64 = ( change_out_size * fee_rate. as_sat_vb ( )
1514+ - change_in_size * LONG_TERM_FEE_RATE . as_sat_vb ( ) )
1515+ as u64 ;
15121516
15131517 let utxo_fee_diff: i64 = utxos. iter ( ) . fold ( 0 , |acc, utxo| {
1514- let fee_rate : i64 = utxo. fee as i64 ;
1518+ let fee : i64 = utxo. fee as i64 ;
15151519 let long_term_fee: i64 = LONG_TERM_FEE_RATE
15161520 . fee_wu ( TXIN_BASE_WEIGHT + utxo. weighted_utxo . satisfaction_weight )
15171521 as i64 ;
15181522
1519- acc + fee_rate - long_term_fee
1523+ acc + fee - long_term_fee
15201524 } ) ;
15211525
15221526 // Waste with change is the change cost and difference between fee and long term fee
@@ -1541,12 +1545,12 @@ mod test {
15411545 let utxos_fee: u64 = utxos. clone ( ) . iter ( ) . fold ( 0 , |acc, utxo| acc + utxo. fee ) ;
15421546
15431547 let utxo_fee_diff: i64 = utxos. iter ( ) . fold ( 0 , |acc, utxo| {
1544- let fee_rate : i64 = utxo. fee as i64 ;
1548+ let fee : i64 = utxo. fee as i64 ;
15451549 let long_term_fee: i64 = LONG_TERM_FEE_RATE
15461550 . fee_wu ( TXIN_BASE_WEIGHT + utxo. weighted_utxo . satisfaction_weight )
15471551 as i64 ;
15481552
1549- acc + fee_rate - long_term_fee
1553+ acc + fee - long_term_fee
15501554 } ) ;
15511555
15521556 let excess = available_value - utxos_fee - amount_needed;
@@ -1585,12 +1589,12 @@ mod test {
15851589 let utxos_fee: u64 = utxos. clone ( ) . iter ( ) . fold ( 0 , |acc, utxo| acc + utxo. fee ) ;
15861590
15871591 let utxo_fee_diff: i64 = utxos. iter ( ) . fold ( 0 , |acc, utxo| {
1588- let fee_rate : i64 = utxo. fee as i64 ;
1592+ let fee : i64 = utxo. fee as i64 ;
15891593 let long_term_fee: i64 = LONG_TERM_FEE_RATE
15901594 . fee_wu ( TXIN_BASE_WEIGHT + utxo. weighted_utxo . satisfaction_weight )
15911595 as i64 ;
15921596
1593- acc + fee_rate - long_term_fee
1597+ acc + fee - long_term_fee
15941598 } ) ;
15951599
15961600 let excess = available_value - utxos_fee - amount_needed;
@@ -1603,7 +1607,7 @@ mod test {
16031607
16041608 #[ test]
16051609 fn test_calculate_waste_with_no_timing_cost_and_no_creation_cost ( ) {
1606- let fee_rate = FeeRate :: from_sat_per_vb ( 10.0 ) ;
1610+ let fee_rate = LONG_TERM_FEE_RATE ;
16071611 let utxo_values = vec ! [ 200_000_000 ] ;
16081612 let selected = generate_utxos_of_values ( utxo_values. clone ( ) ) ;
16091613
0 commit comments