Skip to content

Commit db6dd94

Browse files
committed
Improve prediction of commitment stats in can_accept_incoming_htlc
`ChannelContext::get_pending_htlc_stats` predicts that the set of HTLCs on the next commitment will be all the HTLCs in `ChannelContext.pending_inbound_htlcs`, and `ChannelContext.pending_outbound_htlcs`, as well as all the outbound HTLC adds in the holding cell. This is an overestimate: * Outbound HTLC removals which have been ACK'ed by the counterparty will certainly not be present in any *next* commitment, even though they remain in `pending_outbound_htlcs`. * Outbound HTLCs in the `RemoteRemoved` state, will not be present in the next *local* commitment. * Outbound HTLCs in the `LocalAnnounced` state have no guarantee that they were yet received by the counterparty. * Outbound `update_add_htlc`'s in the holding cell are certainly not known by the counterparty, and we will reevaluate their addition to the channel when freeing the holding cell. * Inbound HTLCs in the `LocalRemoved` state will not be present in the next *remote* commitment. This commit stops using `get_pending_htlc_stats` in favor of the newly added `ChannelContext::get_next_{local, remote}_commitment_stats` methods, and fixes the issues described above. `ChannelContext::next_remote_commit_tx_fee_msat` counts inbound HTLCs in the `LocalRemoved` state, as well as outbound HTLCs in the `LocalAnnounced` state. We now do not count them for the same reasons described above. Inbound `LocalRemoved` HTLCs that were **not** successful are now credited to `remote_balance_before_fee_msat` as they will certainly not be on the next remote commitment. We previously debited these from the remote balance to arrive at `remote_balance_before_fee_msat`. We now always check holder dust exposure, whereas we previously would only do it if the incoming HTLC was dust on our own commitment transaction. Furthermore, dust exposure calculations now take a buffer from the currently committed feerate, and ignore any fee updates in `ChannelContext.pending_update_fee`.
1 parent 4bfdb35 commit db6dd94

File tree

2 files changed

+28
-59
lines changed

2 files changed

+28
-59
lines changed

lightning/src/ln/channel.rs

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,78 +4486,47 @@ where
44864486

44874487
#[rustfmt::skip]
44884488
fn can_accept_incoming_htlc<L: Deref>(
4489-
&self, funding: &FundingScope, msg: &msgs::UpdateAddHTLC,
4489+
&self, funding: &FundingScope,
44904490
dust_exposure_limiting_feerate: Option<u32>, logger: &L,
44914491
) -> Result<(), LocalHTLCFailureReason>
44924492
where
44934493
L::Target: Logger,
44944494
{
4495-
let htlc_stats = self.get_pending_htlc_stats(funding, None, dust_exposure_limiting_feerate);
4495+
// The fee spike buffer (an additional nondust HTLC) we keep for the remote if the channel
4496+
// is not zero fee. This deviates from the spec because the fee spike buffer requirement
4497+
// doesn't exist on the receiver's side, only on the sender's. Note that with anchor
4498+
// outputs we are no longer as sensitive to fee spikes, so we need to account for them.
4499+
let fee_spike_buffer_htlc = if funding.get_channel_type().supports_anchor_zero_fee_commitments() {
4500+
0
4501+
} else {
4502+
1
4503+
};
4504+
// Do not include outbound update_add_htlc's in the holding cell, or those which haven't yet been ACK'ed by the counterparty (ie. LocalAnnounced HTLCs)
4505+
let include_counterparty_unknown_htlcs = false;
4506+
// A `None` `HTLCCandidate` is used as in this case because we're already accounting for
4507+
// the incoming HTLC as it has been fully committed by both sides.
4508+
let next_local_commitment_stats = self.get_next_local_commitment_stats(funding, None, include_counterparty_unknown_htlcs, fee_spike_buffer_htlc, self.feerate_per_kw, dust_exposure_limiting_feerate);
4509+
let next_remote_commitment_stats = self.get_next_remote_commitment_stats(funding, None, include_counterparty_unknown_htlcs, fee_spike_buffer_htlc, self.feerate_per_kw, dust_exposure_limiting_feerate);
4510+
44964511
let max_dust_htlc_exposure_msat = self.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
4497-
let on_counterparty_tx_dust_htlc_exposure_msat = htlc_stats.on_counterparty_tx_dust_exposure_msat;
4498-
if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4512+
if next_remote_commitment_stats.dust_exposure_msat > max_dust_htlc_exposure_msat {
44994513
// Note that the total dust exposure includes both the dust HTLCs and the excess mining fees of the counterparty commitment transaction
45004514
log_info!(logger, "Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
4501-
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4515+
next_remote_commitment_stats.dust_exposure_msat, max_dust_htlc_exposure_msat);
45024516
return Err(LocalHTLCFailureReason::DustLimitCounterparty)
45034517
}
4504-
let dust_buffer_feerate = self.get_dust_buffer_feerate(None);
4505-
let (htlc_success_tx_fee_sat, _) = second_stage_tx_fees_sat(
4506-
&funding.get_channel_type(), dust_buffer_feerate,
4507-
);
4508-
let exposure_dust_limit_success_sats = htlc_success_tx_fee_sat + self.holder_dust_limit_satoshis;
4509-
if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
4510-
let on_holder_tx_dust_htlc_exposure_msat = htlc_stats.on_holder_tx_dust_exposure_msat;
4511-
if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4512-
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4513-
on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4514-
return Err(LocalHTLCFailureReason::DustLimitHolder)
4515-
}
4518+
if next_local_commitment_stats.dust_exposure_msat > max_dust_htlc_exposure_msat {
4519+
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4520+
next_local_commitment_stats.dust_exposure_msat, max_dust_htlc_exposure_msat);
4521+
return Err(LocalHTLCFailureReason::DustLimitHolder)
45164522
}
45174523

45184524
if !funding.is_outbound() {
4519-
let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs
4520-
.iter()
4521-
.filter_map(|htlc| {
4522-
matches!(
4523-
htlc.state,
4524-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _))
4525-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _))
4526-
)
4527-
.then_some(htlc.amount_msat)
4528-
})
4529-
.sum();
4530-
let pending_value_to_self_msat =
4531-
funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
4532-
let pending_remote_value_msat =
4533-
funding.get_value_satoshis() * 1000 - pending_value_to_self_msat;
4534-
// Subtract any non-HTLC outputs from the local and remote balances
4535-
let (_, remote_balance_before_fee_msat) = SpecTxBuilder {}.subtract_non_htlc_outputs(
4536-
funding.is_outbound(),
4537-
pending_value_to_self_msat,
4538-
pending_remote_value_msat,
4539-
funding.get_channel_type()
4540-
);
4541-
4542-
// `Some(())` is for the fee spike buffer we keep for the remote if the channel is
4543-
// not zero fee. This deviates from the spec because the fee spike buffer requirement
4544-
// doesn't exist on the receiver's side, only on the sender's. Note that with anchor
4545-
// outputs we are no longer as sensitive to fee spikes, so we need to account for them.
4546-
//
4547-
// A `None` `HTLCCandidate` is used as in this case because we're already accounting for
4548-
// the incoming HTLC as it has been fully committed by both sides.
4549-
let fee_spike_buffer_htlc = if funding.get_channel_type().supports_anchor_zero_fee_commitments() {
4550-
None
4551-
} else {
4552-
Some(())
4553-
};
4554-
4555-
let mut remote_fee_cost_incl_stuck_buffer_msat = self.next_remote_commit_tx_fee_msat(
4556-
funding, None, fee_spike_buffer_htlc,
4557-
);
4525+
let mut remote_fee_cost_incl_stuck_buffer_msat = next_remote_commitment_stats.commit_tx_fee_sat * 1000;
45584526
if !funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
45594527
remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
45604528
}
4529+
let remote_balance_before_fee_msat = next_remote_commitment_stats.counterparty_balance_msat.unwrap_or(0);
45614530
if remote_balance_before_fee_msat.saturating_sub(funding.holder_selected_channel_reserve_satoshis * 1000) < remote_fee_cost_incl_stuck_buffer_msat {
45624531
log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.channel_id());
45634532
return Err(LocalHTLCFailureReason::FeeSpikeBuffer);
@@ -9525,7 +9494,7 @@ where
95259494
/// this function determines whether to fail the HTLC, or forward / claim it.
95269495
#[rustfmt::skip]
95279496
pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
9528-
&self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
9497+
&self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
95299498
) -> Result<(), LocalHTLCFailureReason>
95309499
where
95319500
F::Target: FeeEstimator,
@@ -9541,7 +9510,7 @@ where
95419510

95429511
core::iter::once(&self.funding)
95439512
.chain(self.pending_funding.iter())
9544-
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
9513+
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, dust_exposure_limiting_feerate, &logger))
95459514
}
95469515

95479516
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6262,7 +6262,7 @@ where
62626262
&chan.context,
62636263
Some(update_add_htlc.payment_hash),
62646264
);
6265-
chan.can_accept_incoming_htlc(update_add_htlc, &self.fee_estimator, &logger)
6265+
chan.can_accept_incoming_htlc(&self.fee_estimator, &logger)
62666266
},
62676267
) {
62686268
Some(Ok(_)) => {},

0 commit comments

Comments
 (0)