@@ -1665,14 +1665,19 @@ impl InteractiveTxConstructor {
1665
1665
/// Determine whether a change output should be added or not, and if so, of what size,
1666
1666
/// considering our given inputs, outputs, and intended contribution.
1667
1667
/// Computes and takes into account fees.
1668
- /// Return value is the value computed for the change output (in satoshis),
1669
- /// or None if a change is not needed/possible.
1668
+ /// Three outcomes are possible:
1669
+ /// - Inputs are sufficient for intended contribution, fees, and a larger-than-dust change:
1670
+ /// Ok(Some(change_amount))
1671
+ /// - Inputs are sufficient for intended contribution and fees, but not for a change:
1672
+ /// Ok(None)
1673
+ /// - Insputs are not sufficent to cover contribution and fees:
1674
+ /// Err(AbortReason::InsufficientFees)
1670
1675
#[ allow( dead_code) ] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
1671
1676
pub ( super ) fn calculate_change_output_value (
1672
1677
is_initiator : bool , our_contribution : u64 , funding_inputs_prev_outputs : & Vec < & TxOut > ,
1673
1678
funding_outputs : & Vec < OutputOwned > , funding_feerate_sat_per_1000_weight : u32 ,
1674
1679
holder_dust_limit_satoshis : u64 ,
1675
- ) -> Option < u64 > {
1680
+ ) -> Result < Option < u64 > , AbortReason > {
1676
1681
let our_funding_inputs_weight =
1677
1682
funding_inputs_prev_outputs. iter ( ) . fold ( 0u64 , |weight, prev_output| {
1678
1683
weight. saturating_add ( estimate_input_weight ( prev_output) . to_wu ( ) )
@@ -1695,13 +1700,19 @@ pub(super) fn calculate_change_output_value(
1695
1700
funding_inputs_prev_outputs. iter ( ) . map ( |out| out. value . to_sat ( ) ) . sum ( ) ;
1696
1701
1697
1702
// Note: in case of additional outputs, they will have to be subtracted here
1698
- let remaining_value =
1699
- total_input_satoshis. saturating_sub ( our_contribution) . saturating_sub ( fees_sats) ;
1700
1703
1701
- if remaining_value <= holder_dust_limit_satoshis {
1702
- None
1704
+ let min_contribution_and_fees = our_contribution. saturating_add ( fees_sats) ;
1705
+ let min_contribution_and_fees_and_dust = min_contribution_and_fees. saturating_add ( holder_dust_limit_satoshis) ;
1706
+ if total_input_satoshis < min_contribution_and_fees {
1707
+ // Not enough to cover contribution plus fees
1708
+ Err ( AbortReason :: InsufficientFees )
1709
+ } else if total_input_satoshis < min_contribution_and_fees_and_dust {
1710
+ // Enough to cover contribution plus fees, but leftover is below dust limit
1711
+ Ok ( None )
1703
1712
} else {
1704
- Some ( remaining_value)
1713
+ // Enough to have over-dust change
1714
+ let remaining_value = total_input_satoshis. saturating_sub ( min_contribution_and_fees) ;
1715
+ Ok ( Some ( remaining_value) )
1705
1716
}
1706
1717
}
1707
1718
@@ -2665,7 +2676,7 @@ mod tests {
2665
2676
funding_feerate_sat_per_1000_weight,
2666
2677
300 ,
2667
2678
) ;
2668
- assert_eq ! ( res. unwrap( ) , gross_change - fees - common_fees) ;
2679
+ assert_eq ! ( res. unwrap( ) . unwrap ( ) , gross_change - fees - common_fees) ;
2669
2680
}
2670
2681
{
2671
2682
// There is leftover for change, without common fees
@@ -2677,7 +2688,7 @@ mod tests {
2677
2688
funding_feerate_sat_per_1000_weight,
2678
2689
300 ,
2679
2690
) ;
2680
- assert_eq ! ( res. unwrap( ) , gross_change - fees) ;
2691
+ assert_eq ! ( res. unwrap( ) . unwrap ( ) , gross_change - fees) ;
2681
2692
}
2682
2693
{
2683
2694
// Larger fee, smaller change
@@ -2689,7 +2700,7 @@ mod tests {
2689
2700
9000 ,
2690
2701
300 ,
2691
2702
) ;
2692
- assert_eq ! ( res. unwrap( ) , 14384 ) ;
2703
+ assert_eq ! ( res. unwrap( ) . unwrap ( ) , 14384 ) ;
2693
2704
}
2694
2705
{
2695
2706
// Insufficient inputs, no leftover
@@ -2701,7 +2712,7 @@ mod tests {
2701
2712
funding_feerate_sat_per_1000_weight,
2702
2713
300 ,
2703
2714
) ;
2704
- assert ! ( res. is_none ( ) ) ;
2715
+ assert_eq ! ( res. err ( ) . unwrap ( ) , AbortReason :: InsufficientFees ) ;
2705
2716
}
2706
2717
{
2707
2718
// Very small leftover
@@ -2713,7 +2724,7 @@ mod tests {
2713
2724
funding_feerate_sat_per_1000_weight,
2714
2725
300 ,
2715
2726
) ;
2716
- assert ! ( res. is_none( ) ) ;
2727
+ assert ! ( res. unwrap ( ) . is_none( ) ) ;
2717
2728
}
2718
2729
{
2719
2730
// Small leftover, but not dust
@@ -2725,7 +2736,7 @@ mod tests {
2725
2736
funding_feerate_sat_per_1000_weight,
2726
2737
100 ,
2727
2738
) ;
2728
- assert_eq ! ( res. unwrap( ) , 154 ) ;
2739
+ assert_eq ! ( res. unwrap( ) . unwrap ( ) , 154 ) ;
2729
2740
}
2730
2741
}
2731
2742
}
0 commit comments