@@ -43,10 +43,7 @@ pub(crate) struct NextCommitmentStats {
43
43
pub nondust_htlc_count : usize ,
44
44
pub commit_tx_fee_sat : u64 ,
45
45
pub dust_exposure_msat : u64 ,
46
- // If the counterparty sets a feerate on the channel in excess of our dust_exposure_limiting_feerate,
47
- // this should be set to the dust exposure that would result from us adding an additional nondust outbound
48
- // htlc on the counterparty's commitment transaction.
49
- pub extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat : Option < u64 > ,
46
+ pub extra_accepted_htlc_dust_exposure_msat : u64 ,
50
47
}
51
48
52
49
impl NextCommitmentStats {
@@ -69,65 +66,52 @@ impl NextCommitmentStats {
69
66
}
70
67
}
71
68
72
- fn excess_fees_on_counterparty_tx_dust_exposure_msat (
73
- next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 , excess_feerate : u32 ,
74
- counterparty_dust_limit_satoshis : u64 , dust_htlc_exposure_msat : u64 ,
75
- channel_type : & ChannelTypeFeatures ,
69
+ fn commit_plus_htlc_tx_fees_msat (
70
+ local : bool , next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 ,
71
+ feerate : u32 , broadcaster_dust_limit_satoshis : u64 , channel_type : & ChannelTypeFeatures ,
76
72
) -> ( u64 , u64 ) {
77
- let on_counterparty_tx_accepted_nondust_htlcs = next_commitment_htlcs
73
+ let accepted_nondust_htlcs = next_commitment_htlcs
78
74
. iter ( )
79
75
. filter ( |htlc| {
80
- htlc. outbound
76
+ htlc. outbound != local
81
77
&& !htlc. is_dust (
82
- false ,
78
+ local ,
83
79
dust_buffer_feerate,
84
- counterparty_dust_limit_satoshis ,
80
+ broadcaster_dust_limit_satoshis ,
85
81
channel_type,
86
82
)
87
83
} )
88
84
. count ( ) ;
89
- let on_counterparty_tx_offered_nondust_htlcs = next_commitment_htlcs
85
+ let offered_nondust_htlcs = next_commitment_htlcs
90
86
. iter ( )
91
87
. filter ( |htlc| {
92
- ! htlc. outbound
88
+ htlc. outbound == local
93
89
&& !htlc. is_dust (
94
- false ,
90
+ local ,
95
91
dust_buffer_feerate,
96
- counterparty_dust_limit_satoshis ,
92
+ broadcaster_dust_limit_satoshis ,
97
93
channel_type,
98
94
)
99
95
} )
100
96
. count ( ) ;
101
97
102
- let commitment_fee_sat = commit_tx_fee_sat (
103
- excess_feerate,
104
- on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs,
105
- channel_type,
106
- ) ;
107
- let second_stage_fees_sat = htlc_tx_fees_sat (
108
- excess_feerate,
109
- on_counterparty_tx_accepted_nondust_htlcs,
110
- on_counterparty_tx_offered_nondust_htlcs,
111
- channel_type,
112
- ) ;
113
- let on_counterparty_tx_dust_exposure_msat =
114
- dust_htlc_exposure_msat + ( commitment_fee_sat + second_stage_fees_sat) * 1000 ;
98
+ let commitment_fee_sat =
99
+ commit_tx_fee_sat ( feerate, accepted_nondust_htlcs + offered_nondust_htlcs, channel_type) ;
100
+ let second_stage_fees_sat =
101
+ htlc_tx_fees_sat ( feerate, accepted_nondust_htlcs, offered_nondust_htlcs, channel_type) ;
102
+ let total_fees_msat = ( commitment_fee_sat + second_stage_fees_sat) * 1000 ;
115
103
116
- let extra_htlc_commitment_fee_sat = commit_tx_fee_sat (
117
- excess_feerate ,
118
- on_counterparty_tx_accepted_nondust_htlcs + 1 + on_counterparty_tx_offered_nondust_htlcs ,
104
+ let extra_accepted_htlc_commitment_fee_sat = commit_tx_fee_sat (
105
+ feerate ,
106
+ accepted_nondust_htlcs + 1 + offered_nondust_htlcs ,
119
107
channel_type,
120
108
) ;
121
- let extra_htlc_second_stage_fees_sat = htlc_tx_fees_sat (
122
- excess_feerate,
123
- on_counterparty_tx_accepted_nondust_htlcs + 1 ,
124
- on_counterparty_tx_offered_nondust_htlcs,
125
- channel_type,
126
- ) ;
127
- let extra_htlc_dust_exposure_msat = dust_htlc_exposure_msat
128
- + ( extra_htlc_commitment_fee_sat + extra_htlc_second_stage_fees_sat) * 1000 ;
109
+ let extra_accepted_htlc_second_stage_fees_sat =
110
+ htlc_tx_fees_sat ( feerate, accepted_nondust_htlcs + 1 , offered_nondust_htlcs, channel_type) ;
111
+ let extra_accepted_htlc_total_fees_msat =
112
+ ( extra_accepted_htlc_commitment_fee_sat + extra_accepted_htlc_second_stage_fees_sat) * 1000 ;
129
113
130
- ( on_counterparty_tx_dust_exposure_msat , extra_htlc_dust_exposure_msat )
114
+ ( total_fees_msat , extra_accepted_htlc_total_fees_msat )
131
115
}
132
116
133
117
fn subtract_addl_outputs (
@@ -205,11 +189,11 @@ impl TxBuilder for SpecTxBuilder {
205
189
dust_exposure_limiting_feerate : Option < u32 > , broadcaster_dust_limit_satoshis : u64 ,
206
190
channel_type : & ChannelTypeFeatures ,
207
191
) -> Result < NextCommitmentStats , ( ) > {
208
- let excess_feerate_opt =
209
- feerate_per_kw. checked_sub ( dust_exposure_limiting_feerate. unwrap_or ( feerate_per_kw) ) ;
192
+ let excess_feerate =
193
+ feerate_per_kw. saturating_sub ( dust_exposure_limiting_feerate. unwrap_or ( feerate_per_kw) ) ;
210
194
if channel_type. supports_anchor_zero_fee_commitments ( ) {
211
195
debug_assert_eq ! ( feerate_per_kw, 0 ) ;
212
- debug_assert_eq ! ( excess_feerate_opt , Some ( 0 ) ) ;
196
+ debug_assert_eq ! ( excess_feerate , 0 ) ;
213
197
debug_assert_eq ! ( addl_nondust_htlc_count, 0 ) ;
214
198
}
215
199
@@ -272,22 +256,24 @@ impl TxBuilder for SpecTxBuilder {
272
256
} )
273
257
. sum ( ) ;
274
258
275
- // Count the excess fees on the counterparty's transaction as dust
276
- let ( dust_exposure_msat, extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat) =
277
- if let ( Some ( excess_feerate) , false ) = ( excess_feerate_opt, local) {
278
- let ( dust_exposure_msat, extra_nondust_htlc_exposure_msat) =
279
- excess_fees_on_counterparty_tx_dust_exposure_msat (
280
- & next_commitment_htlcs,
281
- dust_buffer_feerate,
282
- excess_feerate,
283
- broadcaster_dust_limit_satoshis,
284
- dust_exposure_msat,
285
- channel_type,
286
- ) ;
287
- ( dust_exposure_msat, Some ( extra_nondust_htlc_exposure_msat) )
288
- } else {
289
- ( dust_exposure_msat, None )
290
- } ;
259
+ // Add any excess fees to dust exposure on counterparty transactions
260
+ let ( dust_exposure_msat, extra_accepted_htlc_dust_exposure_msat) = if local {
261
+ ( dust_exposure_msat, dust_exposure_msat)
262
+ } else {
263
+ let ( excess_fees_msat, extra_accepted_htlc_excess_fees_msat) =
264
+ commit_plus_htlc_tx_fees_msat (
265
+ local,
266
+ & next_commitment_htlcs,
267
+ dust_buffer_feerate,
268
+ excess_feerate,
269
+ broadcaster_dust_limit_satoshis,
270
+ channel_type,
271
+ ) ;
272
+ (
273
+ dust_exposure_msat + excess_fees_msat,
274
+ dust_exposure_msat + extra_accepted_htlc_excess_fees_msat,
275
+ )
276
+ } ;
291
277
292
278
Ok ( NextCommitmentStats {
293
279
is_outbound_from_holder,
@@ -298,7 +284,7 @@ impl TxBuilder for SpecTxBuilder {
298
284
nondust_htlc_count : nondust_htlc_count + addl_nondust_htlc_count,
299
285
commit_tx_fee_sat,
300
286
dust_exposure_msat,
301
- extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat ,
287
+ extra_accepted_htlc_dust_exposure_msat ,
302
288
} )
303
289
}
304
290
fn commit_tx_fee_sat (
0 commit comments