Skip to content

Commit bbf593a

Browse files
committed
can_accept_incoming_htlc
1 parent c759b47 commit bbf593a

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,78 +4489,49 @@ where
44894489

44904490
#[rustfmt::skip]
44914491
fn can_accept_incoming_htlc<L: Deref>(
4492-
&self, funding: &FundingScope, msg: &msgs::UpdateAddHTLC,
4492+
&self, funding: &FundingScope,
44934493
dust_exposure_limiting_feerate: Option<u32>, logger: &L,
44944494
) -> Result<(), LocalHTLCFailureReason>
44954495
where
44964496
L::Target: Logger,
44974497
{
4498-
let htlc_stats = self.get_pending_htlc_stats(funding, None, dust_exposure_limiting_feerate);
4498+
// `Some(())` is for the fee spike buffer we keep for the remote if the channel is not zero fee. This deviates from the spec because the fee spike buffer requirement
4499+
// doesn't exist on the receiver's side, only on the sender's. Note that with anchor
4500+
// outputs we are no longer as sensitive to fee spikes, so we need to account for them.
4501+
//
4502+
// A `None` `HTLCCandidate` is used as in this case because we're already accounting for
4503+
// the incoming HTLC as it has been fully committed by both sides.
4504+
let fee_spike_buffer_htlc = if funding.get_channel_type().supports_anchor_zero_fee_commitments() {
4505+
0
4506+
} else {
4507+
1
4508+
};
4509+
// 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)
4510+
let include_counterparty_unknown_htlcs = false;
4511+
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);
4512+
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);
4513+
44994514
let max_dust_htlc_exposure_msat = self.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
4500-
let on_counterparty_tx_dust_htlc_exposure_msat = htlc_stats.on_counterparty_tx_dust_exposure_msat;
4501-
if on_counterparty_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4515+
if next_remote_commitment_stats.dust_exposure_msat > max_dust_htlc_exposure_msat {
45024516
// Note that the total dust exposure includes both the dust HTLCs and the excess mining fees of the counterparty commitment transaction
45034517
log_info!(logger, "Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
4504-
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4518+
next_remote_commitment_stats.dust_exposure_msat, max_dust_htlc_exposure_msat);
45054519
return Err(LocalHTLCFailureReason::DustLimitCounterparty)
45064520
}
4507-
let dust_buffer_feerate = self.get_dust_buffer_feerate(None);
4508-
let (htlc_success_tx_fee_sat, _) = second_stage_tx_fees_sat(
4509-
&funding.get_channel_type(), dust_buffer_feerate,
4510-
);
4511-
let exposure_dust_limit_success_sats = htlc_success_tx_fee_sat + self.holder_dust_limit_satoshis;
4512-
if msg.amount_msat / 1000 < exposure_dust_limit_success_sats {
4513-
let on_holder_tx_dust_htlc_exposure_msat = htlc_stats.on_holder_tx_dust_exposure_msat;
4514-
if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
4515-
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4516-
on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
4517-
return Err(LocalHTLCFailureReason::DustLimitHolder)
4518-
}
4521+
if next_local_commitment_stats.dust_exposure_msat > max_dust_htlc_exposure_msat {
4522+
// Note: We now always check holder dust exposure, whereas we previously would only
4523+
// do it if the incoming HTLC was dust on our own commitment transaction
4524+
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
4525+
next_local_commitment_stats.dust_exposure_msat, max_dust_htlc_exposure_msat);
4526+
return Err(LocalHTLCFailureReason::DustLimitHolder)
45194527
}
45204528

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

95389509
core::iter::once(&self.funding)
95399510
.chain(self.pending_funding.iter())
9540-
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
9511+
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, dust_exposure_limiting_feerate, &logger))
95419512
}
95429513

95439514
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)