Skip to content

Commit 8a02254

Browse files
author
Antoine Riard
committed
Enforce max_balance_dust_htlc_msat at HTLC reception/forward
At `update_add_htlc()`/`send_htlc()`, we verify that the inbound/ outbound dust or the sum of both, on either sides of the link isn't above new config setting `max_balance_dust_htlc_msat`. A dust HTLC is hence defined as a trimmed-to-dust one, i.e including the fee cost to publish its claiming transaction.
1 parent 23deefc commit 8a02254

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,20 @@ impl<Signer: Sign> Channel<Signer> {
20612061
}
20622062
}
20632063

2064+
if msg.amount_msat / 1000 < (self.feerate_per_kw as u64 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + self.counterparty_dust_limit_satoshis {
2065+
if on_counterparty_tx_dust_inbound + on_counterparty_tx_dust_outbound + msg.amount_msat > self.get_max_balance_dust_htlc_msat() {
2066+
log_info!(logger, "Cannot accept value that would put holder dusted balance {} on counterparty commitment over limit {}", on_counterparty_tx_dust_inbound + on_counterparty_tx_dust_outbound + msg.amount_msat, self.get_max_balance_dust_htlc_msat());
2067+
pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
2068+
}
2069+
}
2070+
2071+
if msg.amount_msat / 1000 < (self.feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000) + self.holder_dust_limit_satoshis {
2072+
if on_holder_tx_dust_inbound + on_holder_tx_dust_outbound + msg.amount_msat > self.get_max_balance_dust_htlc_msat() {
2073+
log_info!(logger, "Cannot accept value that would put holder dusted balance {} on holder commitment over limit {}", on_holder_tx_dust_inbound + on_holder_tx_dust_outbound + msg.amount_msat, self.get_max_balance_dust_htlc_msat());
2074+
pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x1000|7);
2075+
}
2076+
}
2077+
20642078
let pending_value_to_self_msat =
20652079
self.value_to_self_msat + htlc_inbound_value_msat - removed_outbound_total_msat;
20662080
let pending_remote_value_msat =
@@ -3461,6 +3475,10 @@ impl<Signer: Sign> Channel<Signer> {
34613475
cmp::max(self.config.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
34623476
}
34633477

3478+
pub fn get_max_balance_dust_htlc_msat(&self) -> u64 {
3479+
self.config.max_balance_dust_htlc_msat
3480+
}
3481+
34643482
#[cfg(test)]
34653483
pub fn get_feerate(&self) -> u32 {
34663484
self.feerate_per_kw
@@ -4125,6 +4143,18 @@ impl<Signer: Sign> Channel<Signer> {
41254143
}
41264144
}
41274145

4146+
if amount_msat / 1000 < (self.feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000) + self.counterparty_dust_limit_satoshis {
4147+
if on_counterparty_tx_dust_inbound + on_counterparty_tx_dust_outbound + amount_msat > self.get_max_balance_dust_htlc_msat() {
4148+
return Err(ChannelError::Ignore(format!("Cannot send value that would put holder dusted balance {} on counterparty commitment over limit {}", on_counterparty_tx_dust_inbound + on_counterparty_tx_dust_outbound + amount_msat, self.get_max_balance_dust_htlc_msat())));
4149+
}
4150+
}
4151+
4152+
if amount_msat / 1000 < (self.feerate_per_kw as u64 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + self.holder_dust_limit_satoshis {
4153+
if on_holder_tx_dust_inbound + on_holder_tx_dust_outbound + amount_msat > self.get_max_balance_dust_htlc_msat() {
4154+
return Err(ChannelError::Ignore(format!("Cannot send value that would put holder dusted balance {} on holder commitment over limit {}", on_holder_tx_dust_inbound + on_holder_tx_dust_outbound + amount_msat, self.get_max_balance_dust_htlc_msat())));
4155+
}
4156+
}
4157+
41284158
let pending_value_to_self_msat = self.value_to_self_msat - htlc_outbound_value_msat;
41294159
if pending_value_to_self_msat < amount_msat {
41304160
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/ln/functional_test_utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,9 @@ pub fn test_default_channel_config() -> UserConfig {
13541354
// When most of our tests were written, the default HTLC minimum was fixed at 1000.
13551355
// It now defaults to 1, so we simply set it to the expected value here.
13561356
default_config.own_channel_config.our_htlc_minimum_msat = 1000;
1357+
// When most of our tests were written, we didn't have notion of a `max_balance_dust_htlc_msat`,
1358+
// It now defaults to 5_000_000 msat, to avoid interferring with tests we bump it to 50_000_000 msat.
1359+
default_config.channel_options.max_balance_dust_htlc_msat = 50_000_000;
13571360
default_config
13581361
}
13591362

0 commit comments

Comments
 (0)