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