Skip to content

Commit 6c6361d

Browse files
committed
Cleanup dust exposure due to excess fees in get_next_commitment_stats
1 parent ec13990 commit 6c6361d

File tree

1 file changed

+47
-61
lines changed

1 file changed

+47
-61
lines changed

lightning/src/sign/tx_builder.rs

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ pub(crate) struct NextCommitmentStats {
4343
pub nondust_htlc_count: usize,
4444
pub commit_tx_fee_sat: u64,
4545
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,
5047
}
5148

5249
impl NextCommitmentStats {
@@ -69,65 +66,52 @@ impl NextCommitmentStats {
6966
}
7067
}
7168

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,
7672
) -> (u64, u64) {
77-
let on_counterparty_tx_accepted_nondust_htlcs = next_commitment_htlcs
73+
let accepted_nondust_htlcs = next_commitment_htlcs
7874
.iter()
7975
.filter(|htlc| {
80-
htlc.outbound
76+
htlc.outbound != local
8177
&& !htlc.is_dust(
82-
false,
78+
local,
8379
dust_buffer_feerate,
84-
counterparty_dust_limit_satoshis,
80+
broadcaster_dust_limit_satoshis,
8581
channel_type,
8682
)
8783
})
8884
.count();
89-
let on_counterparty_tx_offered_nondust_htlcs = next_commitment_htlcs
85+
let offered_nondust_htlcs = next_commitment_htlcs
9086
.iter()
9187
.filter(|htlc| {
92-
!htlc.outbound
88+
htlc.outbound == local
9389
&& !htlc.is_dust(
94-
false,
90+
local,
9591
dust_buffer_feerate,
96-
counterparty_dust_limit_satoshis,
92+
broadcaster_dust_limit_satoshis,
9793
channel_type,
9894
)
9995
})
10096
.count();
10197

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;
115103

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,
119107
channel_type,
120108
);
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;
129113

130-
(on_counterparty_tx_dust_exposure_msat, extra_htlc_dust_exposure_msat)
114+
(total_fees_msat, extra_accepted_htlc_total_fees_msat)
131115
}
132116

133117
fn subtract_addl_outputs(
@@ -205,11 +189,11 @@ impl TxBuilder for SpecTxBuilder {
205189
dust_exposure_limiting_feerate: Option<u32>, broadcaster_dust_limit_satoshis: u64,
206190
channel_type: &ChannelTypeFeatures,
207191
) -> 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));
210194
if channel_type.supports_anchor_zero_fee_commitments() {
211195
debug_assert_eq!(feerate_per_kw, 0);
212-
debug_assert_eq!(excess_feerate_opt, Some(0));
196+
debug_assert_eq!(excess_feerate, 0);
213197
debug_assert_eq!(addl_nondust_htlc_count, 0);
214198
}
215199

@@ -272,22 +256,24 @@ impl TxBuilder for SpecTxBuilder {
272256
})
273257
.sum();
274258

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+
};
291277

292278
Ok(NextCommitmentStats {
293279
is_outbound_from_holder,
@@ -298,7 +284,7 @@ impl TxBuilder for SpecTxBuilder {
298284
nondust_htlc_count: nondust_htlc_count + addl_nondust_htlc_count,
299285
commit_tx_fee_sat,
300286
dust_exposure_msat,
301-
extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat,
287+
extra_accepted_htlc_dust_exposure_msat,
302288
})
303289
}
304290
fn commit_tx_fee_sat(

0 commit comments

Comments
 (0)