@@ -42,71 +42,55 @@ pub(crate) struct NextCommitmentStats {
42
42
pub nondust_htlc_count : usize ,
43
43
pub commit_tx_fee_sat : u64 ,
44
44
pub dust_exposure_msat : u64 ,
45
- // If the counterparty sets a feerate on the channel in excess of our dust_exposure_limiting_feerate,
46
- // this should be set to the dust exposure that would result from us adding an additional nondust outbound
47
- // htlc on the counterparty's commitment transaction.
48
- pub extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat : Option < u64 > ,
45
+ pub extra_accepted_htlc_dust_exposure_msat : u64 ,
49
46
}
50
47
51
- fn excess_fees_on_counterparty_tx_dust_exposure_msat (
52
- next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 , excess_feerate : u32 ,
53
- counterparty_dust_limit_satoshis : u64 , dust_htlc_exposure_msat : u64 ,
54
- channel_type : & ChannelTypeFeatures ,
48
+ fn commit_plus_htlc_tx_fees_msat (
49
+ local : bool , next_commitment_htlcs : & [ HTLCAmountDirection ] , dust_buffer_feerate : u32 ,
50
+ feerate : u32 , broadcaster_dust_limit_satoshis : u64 , channel_type : & ChannelTypeFeatures ,
55
51
) -> ( u64 , u64 ) {
56
- let on_counterparty_tx_accepted_nondust_htlcs = next_commitment_htlcs
52
+ let accepted_nondust_htlcs = next_commitment_htlcs
57
53
. iter ( )
58
54
. filter ( |htlc| {
59
- htlc. outbound
55
+ htlc. outbound != local
60
56
&& !htlc. is_dust (
61
- false ,
57
+ local ,
62
58
dust_buffer_feerate,
63
- counterparty_dust_limit_satoshis ,
59
+ broadcaster_dust_limit_satoshis ,
64
60
channel_type,
65
61
)
66
62
} )
67
63
. count ( ) ;
68
- let on_counterparty_tx_offered_nondust_htlcs = next_commitment_htlcs
64
+ let offered_nondust_htlcs = next_commitment_htlcs
69
65
. iter ( )
70
66
. filter ( |htlc| {
71
- ! htlc. outbound
67
+ htlc. outbound == local
72
68
&& !htlc. is_dust (
73
- false ,
69
+ local ,
74
70
dust_buffer_feerate,
75
- counterparty_dust_limit_satoshis ,
71
+ broadcaster_dust_limit_satoshis ,
76
72
channel_type,
77
73
)
78
74
} )
79
75
. count ( ) ;
80
76
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 ;
77
+ let commitment_fee_sat =
78
+ commit_tx_fee_sat ( feerate, accepted_nondust_htlcs + offered_nondust_htlcs, channel_type) ;
79
+ let second_stage_fees_sat =
80
+ htlc_tx_fees_sat ( feerate, accepted_nondust_htlcs, offered_nondust_htlcs, channel_type) ;
81
+ let total_fees_msat = ( commitment_fee_sat + second_stage_fees_sat) * 1000 ;
94
82
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 ,
83
+ let extra_accepted_htlc_commitment_fee_sat = commit_tx_fee_sat (
84
+ feerate ,
85
+ accepted_nondust_htlcs + 1 + offered_nondust_htlcs ,
98
86
channel_type,
99
87
) ;
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 ;
88
+ let extra_accepted_htlc_second_stage_fees_sat =
89
+ htlc_tx_fees_sat ( feerate, accepted_nondust_htlcs + 1 , offered_nondust_htlcs, channel_type) ;
90
+ let extra_accepted_htlc_total_fees_msat =
91
+ ( extra_accepted_htlc_commitment_fee_sat + extra_accepted_htlc_second_stage_fees_sat) * 1000 ;
108
92
109
- ( on_counterparty_tx_dust_exposure_msat , extra_htlc_dust_exposure_msat )
93
+ ( total_fees_msat , extra_accepted_htlc_total_fees_msat )
110
94
}
111
95
112
96
fn subtract_addl_outputs (
@@ -186,11 +170,11 @@ impl TxBuilder for SpecTxBuilder {
186
170
dust_exposure_limiting_feerate : Option < u32 > , broadcaster_dust_limit_satoshis : u64 ,
187
171
channel_type : & ChannelTypeFeatures ,
188
172
) -> Result < NextCommitmentStats , ( ) > {
189
- let excess_feerate_opt =
190
- feerate_per_kw. checked_sub ( dust_exposure_limiting_feerate. unwrap_or ( feerate_per_kw) ) ;
173
+ let excess_feerate =
174
+ feerate_per_kw. saturating_sub ( dust_exposure_limiting_feerate. unwrap_or ( feerate_per_kw) ) ;
191
175
if channel_type. supports_anchor_zero_fee_commitments ( ) {
192
176
debug_assert_eq ! ( feerate_per_kw, 0 ) ;
193
- debug_assert_eq ! ( excess_feerate_opt , Some ( 0 ) ) ;
177
+ debug_assert_eq ! ( excess_feerate , 0 ) ;
194
178
debug_assert_eq ! ( addl_nondust_htlc_count, 0 ) ;
195
179
}
196
180
@@ -253,22 +237,24 @@ impl TxBuilder for SpecTxBuilder {
253
237
} )
254
238
. sum ( ) ;
255
239
256
- // Count the excess fees on the counterparty's transaction as dust
257
- let ( dust_exposure_msat, extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat) =
258
- if let ( Some ( excess_feerate) , false ) = ( excess_feerate_opt, local) {
259
- let ( dust_exposure_msat, extra_nondust_htlc_exposure_msat) =
260
- excess_fees_on_counterparty_tx_dust_exposure_msat (
261
- & next_commitment_htlcs,
262
- dust_buffer_feerate,
263
- excess_feerate,
264
- broadcaster_dust_limit_satoshis,
265
- dust_exposure_msat,
266
- channel_type,
267
- ) ;
268
- ( dust_exposure_msat, Some ( extra_nondust_htlc_exposure_msat) )
269
- } else {
270
- ( dust_exposure_msat, None )
271
- } ;
240
+ // Add any excess fees to dust exposure on counterparty transactions
241
+ let ( dust_exposure_msat, extra_accepted_htlc_dust_exposure_msat) = if local {
242
+ ( dust_exposure_msat, dust_exposure_msat)
243
+ } else {
244
+ let ( excess_fees_msat, extra_accepted_htlc_excess_fees_msat) =
245
+ commit_plus_htlc_tx_fees_msat (
246
+ local,
247
+ & next_commitment_htlcs,
248
+ dust_buffer_feerate,
249
+ excess_feerate,
250
+ broadcaster_dust_limit_satoshis,
251
+ channel_type,
252
+ ) ;
253
+ (
254
+ dust_exposure_msat + excess_fees_msat,
255
+ dust_exposure_msat + extra_accepted_htlc_excess_fees_msat,
256
+ )
257
+ } ;
272
258
273
259
Ok ( NextCommitmentStats {
274
260
inbound_htlcs_count,
@@ -278,7 +264,7 @@ impl TxBuilder for SpecTxBuilder {
278
264
nondust_htlc_count : nondust_htlc_count + addl_nondust_htlc_count,
279
265
commit_tx_fee_sat,
280
266
dust_exposure_msat,
281
- extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat ,
267
+ extra_accepted_htlc_dust_exposure_msat ,
282
268
} )
283
269
}
284
270
fn commit_tx_fee_sat (
0 commit comments