Skip to content

Commit 34553af

Browse files
committed
can_accept_incoming_htlc
1 parent 1a1e097 commit 34553af

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
@@ -4492,78 +4492,47 @@ where
44924492

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

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

95419510
core::iter::once(&self.funding)
95429511
.chain(self.pending_funding.iter())
9543-
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
9512+
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, dust_exposure_limiting_feerate, &logger))
95449513
}
95459514

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