@@ -48,28 +48,65 @@ pub(crate) struct NextCommitmentStats {
4848 pub extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat : Option < u64 > ,
4949}
5050
51- #[ rustfmt:: skip]
5251fn excess_fees_on_counterparty_tx_dust_exposure_msat (
53- next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 ,
54- excess_feerate : u32 , counterparty_dust_limit_satoshis : u64 , dust_htlc_exposure_msat : u64 ,
52+ next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 , excess_feerate : u32 ,
53+ counterparty_dust_limit_satoshis : u64 , dust_htlc_exposure_msat : u64 ,
5554 channel_type : & ChannelTypeFeatures ,
5655) -> ( u64 , u64 ) {
57-
58- let on_counterparty_tx_accepted_nondust_htlcs = next_commitment_htlcs. iter ( ) . filter ( |htlc| htlc. outbound && !htlc. is_dust ( false , dust_buffer_feerate, counterparty_dust_limit_satoshis, channel_type) ) . count ( ) ;
59- let on_counterparty_tx_offered_nondust_htlcs = next_commitment_htlcs. iter ( ) . filter ( |htlc| !htlc. outbound && !htlc. is_dust ( false , dust_buffer_feerate, counterparty_dust_limit_satoshis, channel_type) ) . count ( ) ;
60-
61- let commitment_fee_sat = commit_tx_fee_sat ( excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs, channel_type) ;
62- let second_stage_fees_sat = htlc_tx_fees_sat ( excess_feerate, on_counterparty_tx_accepted_nondust_htlcs, on_counterparty_tx_offered_nondust_htlcs, channel_type) ;
63- let on_counterparty_tx_dust_exposure_msat = dust_htlc_exposure_msat + ( commitment_fee_sat + second_stage_fees_sat) * 1000 ;
64-
65- let extra_htlc_commitment_fee_sat = commit_tx_fee_sat ( excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + 1 + on_counterparty_tx_offered_nondust_htlcs, channel_type) ;
66- let extra_htlc_second_stage_fees_sat = htlc_tx_fees_sat ( excess_feerate, on_counterparty_tx_accepted_nondust_htlcs + 1 , on_counterparty_tx_offered_nondust_htlcs, channel_type) ;
67- let extra_htlc_dust_exposure_msat = dust_htlc_exposure_msat + ( extra_htlc_commitment_fee_sat + extra_htlc_second_stage_fees_sat) * 1000 ;
68-
69- (
70- on_counterparty_tx_dust_exposure_msat,
71- extra_htlc_dust_exposure_msat,
72- )
56+ let on_counterparty_tx_accepted_nondust_htlcs = next_commitment_htlcs
57+ . iter ( )
58+ . filter ( |htlc| {
59+ htlc. outbound
60+ && !htlc. is_dust (
61+ false ,
62+ dust_buffer_feerate,
63+ counterparty_dust_limit_satoshis,
64+ channel_type,
65+ )
66+ } )
67+ . count ( ) ;
68+ let on_counterparty_tx_offered_nondust_htlcs = next_commitment_htlcs
69+ . iter ( )
70+ . filter ( |htlc| {
71+ !htlc. outbound
72+ && !htlc. is_dust (
73+ false ,
74+ dust_buffer_feerate,
75+ counterparty_dust_limit_satoshis,
76+ channel_type,
77+ )
78+ } )
79+ . count ( ) ;
80+
81+ let commitment_fee_sat = commit_tx_fee_sat (
82+ excess_feerate,
83+ on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs,
84+ channel_type,
85+ ) ;
86+ let second_stage_fees_sat = htlc_tx_fees_sat (
87+ excess_feerate,
88+ on_counterparty_tx_accepted_nondust_htlcs,
89+ on_counterparty_tx_offered_nondust_htlcs,
90+ channel_type,
91+ ) ;
92+ let on_counterparty_tx_dust_exposure_msat =
93+ dust_htlc_exposure_msat + ( commitment_fee_sat + second_stage_fees_sat) * 1000 ;
94+
95+ let extra_htlc_commitment_fee_sat = commit_tx_fee_sat (
96+ excess_feerate,
97+ on_counterparty_tx_accepted_nondust_htlcs + 1 + on_counterparty_tx_offered_nondust_htlcs,
98+ channel_type,
99+ ) ;
100+ let extra_htlc_second_stage_fees_sat = htlc_tx_fees_sat (
101+ excess_feerate,
102+ on_counterparty_tx_accepted_nondust_htlcs + 1 ,
103+ on_counterparty_tx_offered_nondust_htlcs,
104+ channel_type,
105+ ) ;
106+ let extra_htlc_dust_exposure_msat = dust_htlc_exposure_msat
107+ + ( extra_htlc_commitment_fee_sat + extra_htlc_second_stage_fees_sat) * 1000 ;
108+
109+ ( on_counterparty_tx_dust_exposure_msat, extra_htlc_dust_exposure_msat)
73110}
74111
75112fn subtract_addl_outputs (
@@ -288,7 +325,6 @@ impl TxBuilder for SpecTxBuilder {
288325
289326 ( local_balance_before_fee_msat, remote_balance_before_fee_msat)
290327 }
291- #[ rustfmt:: skip]
292328 fn build_commitment_transaction < L : Deref > (
293329 & self , local : bool , commitment_number : u64 , per_commitment_point : & PublicKey ,
294330 channel_parameters : & ChannelTransactionParameters , secp_ctx : & Secp256k1 < secp256k1:: All > ,
@@ -326,7 +362,14 @@ impl TxBuilder for SpecTxBuilder {
326362 remote_htlc_total_msat += htlc. amount_msat ;
327363 }
328364 if is_dust ( htlc. offered , htlc. amount_msat ) {
329- log_trace ! ( logger, " ...trimming {} HTLC with value {}sat, hash {}, due to dust limit {}" , if htlc. offered == local { "outbound" } else { "inbound" } , htlc. amount_msat / 1000 , htlc. payment_hash, broadcaster_dust_limit_satoshis) ;
365+ log_trace ! (
366+ logger,
367+ " ...trimming {} HTLC with value {}sat, hash {}, due to dust limit {}" ,
368+ if htlc. offered == local { "outbound" } else { "inbound" } ,
369+ htlc. amount_msat / 1000 ,
370+ htlc. payment_hash,
371+ broadcaster_dust_limit_satoshis
372+ ) ;
330373 false
331374 } else {
332375 true
@@ -338,12 +381,25 @@ impl TxBuilder for SpecTxBuilder {
338381 // The value going to each party MUST be 0 or positive, even if all HTLCs pending in the
339382 // commitment clear by failure.
340383
341- let commit_tx_fee_sat = self . commit_tx_fee_sat ( feerate_per_kw, htlcs_in_tx. len ( ) , & channel_parameters. channel_type_features ) ;
342- let value_to_self_after_htlcs_msat = value_to_self_msat. checked_sub ( local_htlc_total_msat) . unwrap ( ) ;
343- let value_to_remote_after_htlcs_msat =
344- ( channel_parameters. channel_value_satoshis * 1000 ) . checked_sub ( value_to_self_msat) . unwrap ( ) . checked_sub ( remote_htlc_total_msat) . unwrap ( ) ;
345- let ( local_balance_before_fee_msat, remote_balance_before_fee_msat) =
346- self . subtract_non_htlc_outputs ( channel_parameters. is_outbound_from_holder , value_to_self_after_htlcs_msat, value_to_remote_after_htlcs_msat, & channel_parameters. channel_type_features ) ;
384+ let commit_tx_fee_sat = self . commit_tx_fee_sat (
385+ feerate_per_kw,
386+ htlcs_in_tx. len ( ) ,
387+ & channel_parameters. channel_type_features ,
388+ ) ;
389+ let value_to_self_after_htlcs_msat =
390+ value_to_self_msat. checked_sub ( local_htlc_total_msat) . unwrap ( ) ;
391+ let value_to_remote_after_htlcs_msat = ( channel_parameters. channel_value_satoshis * 1000 )
392+ . checked_sub ( value_to_self_msat)
393+ . unwrap ( )
394+ . checked_sub ( remote_htlc_total_msat)
395+ . unwrap ( ) ;
396+ let ( local_balance_before_fee_msat, remote_balance_before_fee_msat) = self
397+ . subtract_non_htlc_outputs (
398+ channel_parameters. is_outbound_from_holder ,
399+ value_to_self_after_htlcs_msat,
400+ value_to_remote_after_htlcs_msat,
401+ & channel_parameters. channel_type_features ,
402+ ) ;
347403
348404 // We MUST use saturating subs here, as the funder's balance is not guaranteed to be greater
349405 // than or equal to `commit_tx_fee_sat`.
@@ -353,29 +409,47 @@ impl TxBuilder for SpecTxBuilder {
353409 // cover the total fee.
354410
355411 let ( value_to_self, value_to_remote) = if channel_parameters. is_outbound_from_holder {
356- ( ( local_balance_before_fee_msat / 1000 ) . saturating_sub ( commit_tx_fee_sat) , remote_balance_before_fee_msat / 1000 )
412+ (
413+ ( local_balance_before_fee_msat / 1000 ) . saturating_sub ( commit_tx_fee_sat) ,
414+ remote_balance_before_fee_msat / 1000 ,
415+ )
357416 } else {
358- ( local_balance_before_fee_msat / 1000 , ( remote_balance_before_fee_msat / 1000 ) . saturating_sub ( commit_tx_fee_sat) )
417+ (
418+ local_balance_before_fee_msat / 1000 ,
419+ ( remote_balance_before_fee_msat / 1000 ) . saturating_sub ( commit_tx_fee_sat) ,
420+ )
359421 } ;
360422
361423 let mut to_broadcaster_value_sat = if local { value_to_self } else { value_to_remote } ;
362424 let mut to_countersignatory_value_sat = if local { value_to_remote } else { value_to_self } ;
363425
364426 if to_broadcaster_value_sat >= broadcaster_dust_limit_satoshis {
365- log_trace ! ( logger, " ...including {} output with value {}" , if local { "to_local" } else { "to_remote" } , to_broadcaster_value_sat) ;
427+ log_trace ! (
428+ logger,
429+ " ...including {} output with value {}" ,
430+ if local { "to_local" } else { "to_remote" } ,
431+ to_broadcaster_value_sat
432+ ) ;
366433 } else {
367434 to_broadcaster_value_sat = 0 ;
368435 }
369436
370437 if to_countersignatory_value_sat >= broadcaster_dust_limit_satoshis {
371- log_trace ! ( logger, " ...including {} output with value {}" , if local { "to_remote" } else { "to_local" } , to_countersignatory_value_sat) ;
438+ log_trace ! (
439+ logger,
440+ " ...including {} output with value {}" ,
441+ if local { "to_remote" } else { "to_local" } ,
442+ to_countersignatory_value_sat
443+ ) ;
372444 } else {
373445 to_countersignatory_value_sat = 0 ;
374446 }
375447
376- let directed_parameters =
377- if local { channel_parameters. as_holder_broadcastable ( ) }
378- else { channel_parameters. as_counterparty_broadcastable ( ) } ;
448+ let directed_parameters = if local {
449+ channel_parameters. as_holder_broadcastable ( )
450+ } else {
451+ channel_parameters. as_counterparty_broadcastable ( )
452+ } ;
379453 let tx = CommitmentTransaction :: new (
380454 commitment_number,
381455 per_commitment_point,
0 commit comments