Skip to content

Commit 1f8a241

Browse files
author
Antoine Riard
committed
Add new config setting max_outbound_dusted_htlc_msat
1 parent 222e545 commit 1f8a241

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,23 +1759,32 @@ impl<Signer: Sign> Channel<Signer> {
17591759
(self.pending_inbound_htlcs.len() as u32, htlc_inbound_value_msat)
17601760
}
17611761

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) {
17651765
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;
17661769
for ref htlc in self.pending_outbound_htlcs.iter() {
17671770
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+
}
17681774
}
17691775

17701776
let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
17711777
for update in self.holding_cell_htlc_updates.iter() {
17721778
if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
17731779
htlc_outbound_count += 1;
17741780
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+
}
17751784
}
17761785
}
17771786

1778-
(htlc_outbound_count as u32, htlc_outbound_value_msat)
1787+
(htlc_outbound_count as u32, htlc_outbound_value_msat, outbound_dusted_htlc_msat)
17791788
}
17801789

17811790
/// Get the available (ie not including pending HTLCs) inbound and outbound balance in msat.
@@ -3432,6 +3441,10 @@ impl<Signer: Sign> Channel<Signer> {
34323441
cmp::max(self.config.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
34333442
}
34343443

3444+
pub fn get_max_outbound_dusted_htlc_msat(&self) -> u64 {
3445+
self.config.max_outbound_dusted_htlc_msat
3446+
}
3447+
34353448
#[cfg(test)]
34363449
pub fn get_feerate(&self) -> u32 {
34373450
self.feerate_per_kw
@@ -4075,7 +4088,7 @@ impl<Signer: Sign> Channel<Signer> {
40754088
return Err(ChannelError::Ignore("Cannot send an HTLC while disconnected from channel counterparty".to_owned()));
40764089
}
40774090

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();
40794092
if outbound_htlc_count + 1 > self.counterparty_max_accepted_htlcs as u32 {
40804093
return Err(ChannelError::Ignore(format!("Cannot push more than their max accepted HTLCs ({})", self.counterparty_max_accepted_htlcs)));
40814094
}
@@ -4095,6 +4108,10 @@ impl<Signer: Sign> Channel<Signer> {
40954108
}
40964109
}
40974110

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+
40984115
let pending_value_to_self_msat = self.value_to_self_msat - htlc_outbound_value_msat;
40994116
if pending_value_to_self_msat < amount_msat {
41004117
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)));

lightning/src/util/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,18 @@ pub struct ChannelConfig {
206206
/// This cannot be changed after a channel has been initialized.
207207
///
208208
/// Default value: true.
209-
pub commit_upfront_shutdown_pubkey: bool
209+
pub commit_upfront_shutdown_pubkey: bool,
210+
/// Limit on the maximum value of dusted offered HTLCs to the counterparty at any given time
211+
/// to limit holder balance exposure to HTLCs. Dusted HTLCs are defined as any HTLC lower
212+
/// than counterparty's `dust_limit_satoshis` sumed up with their expexted onchain claim
213+
/// cost at minimal mempools congestion.
214+
///
215+
/// Note, to support reading serialized configs before 0.0.100, the default value will be
216+
/// always yielded at deser. A user-selected value must override such value before the
217+
/// config is passed to ChannelManager.
218+
///
219+
/// Default value: 5_000_000 msat.
220+
pub max_outbound_dusted_htlc_msat: u64,
210221
}
211222

212223
impl Default for ChannelConfig {
@@ -218,6 +229,7 @@ impl Default for ChannelConfig {
218229
cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
219230
announced_channel: false,
220231
commit_upfront_shutdown_pubkey: true,
232+
max_outbound_dusted_htlc_msat: 10_000_000,
221233
}
222234
}
223235
}
@@ -228,6 +240,7 @@ impl_writeable_tlv_based!(ChannelConfig, {
228240
(4, announced_channel, required),
229241
(6, commit_upfront_shutdown_pubkey, required),
230242
(8, forwarding_fee_base_msat, required),
243+
(default_value, 10_000_000),
231244
});
232245

233246
/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.

0 commit comments

Comments
 (0)