@@ -1759,23 +1759,32 @@ impl<Signer: Sign> Channel<Signer> {
1759
1759
( self . pending_inbound_htlcs . len ( ) as u32 , htlc_inbound_value_msat)
1760
1760
}
1761
1761
1762
- /// Returns (outbound_htlc_count, htlc_outbound_value_msat) *including* pending adds in our
1763
- /// holding cell.
1764
- fn get_outbound_pending_htlc_stats ( & self ) -> ( u32 , u64 ) {
1762
+ /// Returns (outbound_htlc_count, htlc_outbound_value_msat, outbound_dusted_htlc_value_msat ) *including*
1763
+ /// pending adds in our holding cell.
1764
+ fn get_outbound_pending_htlc_stats ( & self ) -> ( u32 , u64 , u64 ) {
1765
1765
let mut htlc_outbound_value_msat = 0 ;
1766
+ let mut outbound_dusted_htlc_msat = 0 ;
1767
+
1768
+ let real_dust_limit_success_sat = ( self . feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000 ) + self . counterparty_dust_limit_satoshis ;
1766
1769
for ref htlc in self . pending_outbound_htlcs . iter ( ) {
1767
1770
htlc_outbound_value_msat += htlc. amount_msat ;
1771
+ if htlc. amount_msat / 1000 < real_dust_limit_success_sat {
1772
+ outbound_dusted_htlc_msat += htlc. amount_msat + ( self . feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000 ) * 1000 ;
1773
+ }
1768
1774
}
1769
1775
1770
1776
let mut htlc_outbound_count = self . pending_outbound_htlcs . len ( ) ;
1771
1777
for update in self . holding_cell_htlc_updates . iter ( ) {
1772
1778
if let & HTLCUpdateAwaitingACK :: AddHTLC { ref amount_msat, .. } = update {
1773
1779
htlc_outbound_count += 1 ;
1774
1780
htlc_outbound_value_msat += amount_msat;
1781
+ if * amount_msat / 1000 < real_dust_limit_success_sat {
1782
+ outbound_dusted_htlc_msat += amount_msat + ( self . feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000 ) * 1000 ;
1783
+ }
1775
1784
}
1776
1785
}
1777
1786
1778
- ( htlc_outbound_count as u32 , htlc_outbound_value_msat)
1787
+ ( htlc_outbound_count as u32 , htlc_outbound_value_msat, outbound_dusted_htlc_msat )
1779
1788
}
1780
1789
1781
1790
/// Get the available (ie not including pending HTLCs) inbound and outbound balance in msat.
@@ -3432,6 +3441,10 @@ impl<Signer: Sign> Channel<Signer> {
3432
3441
cmp:: max ( self . config . cltv_expiry_delta , MIN_CLTV_EXPIRY_DELTA )
3433
3442
}
3434
3443
3444
+ pub fn get_max_outbound_dusted_htlc_msat ( & self ) -> u64 {
3445
+ self . config . max_outbound_dusted_htlc_msat
3446
+ }
3447
+
3435
3448
#[ cfg( test) ]
3436
3449
pub fn get_feerate ( & self ) -> u32 {
3437
3450
self . feerate_per_kw
@@ -4075,7 +4088,7 @@ impl<Signer: Sign> Channel<Signer> {
4075
4088
return Err ( ChannelError :: Ignore ( "Cannot send an HTLC while disconnected from channel counterparty" . to_owned ( ) ) ) ;
4076
4089
}
4077
4090
4078
- let ( outbound_htlc_count, htlc_outbound_value_msat) = self . get_outbound_pending_htlc_stats ( ) ;
4091
+ let ( outbound_htlc_count, htlc_outbound_value_msat, outbound_dusted_htlc_value_msat ) = self . get_outbound_pending_htlc_stats ( ) ;
4079
4092
if outbound_htlc_count + 1 > self . counterparty_max_accepted_htlcs as u32 {
4080
4093
return Err ( ChannelError :: Ignore ( format ! ( "Cannot push more than their max accepted HTLCs ({})" , self . counterparty_max_accepted_htlcs) ) ) ;
4081
4094
}
@@ -4095,6 +4108,10 @@ impl<Signer: Sign> Channel<Signer> {
4095
4108
}
4096
4109
}
4097
4110
4111
+ if outbound_dusted_htlc_value_msat + amount_msat > self . get_max_outbound_dusted_htlc_msat ( ) {
4112
+ return Err ( ChannelError :: Ignore ( format ! ( "Cannot send value that would put holder dusted balance on counterparty commitment over limit {}" , self . get_max_outbound_dusted_htlc_msat( ) ) ) ) ;
4113
+ }
4114
+
4098
4115
let pending_value_to_self_msat = self . value_to_self_msat - htlc_outbound_value_msat;
4099
4116
if pending_value_to_self_msat < amount_msat {
4100
4117
return Err ( ChannelError :: Ignore ( format ! ( "Cannot send value that would overdraw remaining funds. Amount: {}, pending value to self {}" , amount_msat, pending_value_to_self_msat) ) ) ;
0 commit comments