Skip to content

Commit 80b851e

Browse files
committed
Check pending funding in send_update_fee
If there are any pending splices when an sending an update_fee message, the new fee rate must be validated against each pending FundingScope. Otherwise, it may be invalid once the splice is locked.
1 parent c973cdb commit 80b851e

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

lightning/src/ln/channel.rs

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6736,31 +6736,11 @@ impl<SP: Deref> FundedChannel<SP> where
67366736
panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
67376737
}
67386738

6739-
// Before proposing a feerate update, check that we can actually afford the new fee.
6740-
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6741-
let htlc_stats = self.context.get_pending_htlc_stats(&self.funding, Some(feerate_per_kw), dust_exposure_limiting_feerate);
6742-
let commitment_data = self.context.build_commitment_transaction(
6743-
&self.funding, self.holder_commitment_point.transaction_number(),
6744-
&self.holder_commitment_point.current_point(), true, true, logger,
6745-
);
6746-
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_data.tx.nondust_htlcs().len() + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.funding.get_channel_type()) * 1000;
6747-
let holder_balance_msat = commitment_data.stats.local_balance_before_fee_anchors_msat - htlc_stats.outbound_holding_cell_msat;
6748-
if holder_balance_msat < buffer_fee_msat + commitment_data.stats.total_anchors_sat * 1000 + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
6749-
//TODO: auto-close after a number of failures?
6750-
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
6751-
return None;
6752-
}
6753-
6754-
// Note, we evaluate pending htlc "preemptive" trimmed-to-dust threshold at the proposed `feerate_per_kw`.
6755-
let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
6756-
if htlc_stats.on_holder_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
6757-
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
6758-
return None;
6759-
}
6760-
if htlc_stats.on_counterparty_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
6761-
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
6762-
return None;
6763-
}
6739+
core::iter::once(&self.funding)
6740+
.chain(self.pending_funding.iter())
6741+
.map(|funding| self.validate_send_update_fee(funding, feerate_per_kw, fee_estimator, logger))
6742+
.collect::<Result<(), ()>>()
6743+
.ok()?;
67646744

67656745
// Some of the checks of `can_generate_new_commitment` have already been done above, but
67666746
// it's much more brittle to not use it in favor of checking the remaining flags left, as it
@@ -6783,6 +6763,43 @@ impl<SP: Deref> FundedChannel<SP> where
67836763
})
67846764
}
67856765

6766+
fn validate_send_update_fee<F: Deref, L: Deref>(
6767+
&self, funding: &FundingScope, feerate_per_kw: u32,
6768+
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
6769+
) -> Result<(), ()>
6770+
where
6771+
F::Target: FeeEstimator,
6772+
L::Target: Logger,
6773+
{
6774+
// Before proposing a feerate update, check that we can actually afford the new fee.
6775+
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6776+
let htlc_stats = self.context.get_pending_htlc_stats(funding, Some(feerate_per_kw), dust_exposure_limiting_feerate);
6777+
let commitment_data = self.context.build_commitment_transaction(
6778+
funding, self.holder_commitment_point.transaction_number(),
6779+
&self.holder_commitment_point.current_point(), true, true, logger,
6780+
);
6781+
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_data.tx.nondust_htlcs().len() + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, funding.get_channel_type()) * 1000;
6782+
let holder_balance_msat = commitment_data.stats.local_balance_before_fee_anchors_msat - htlc_stats.outbound_holding_cell_msat;
6783+
if holder_balance_msat < buffer_fee_msat + commitment_data.stats.total_anchors_sat * 1000 + funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
6784+
//TODO: auto-close after a number of failures?
6785+
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
6786+
return Err(());
6787+
}
6788+
6789+
// Note, we evaluate pending htlc "preemptive" trimmed-to-dust threshold at the proposed `feerate_per_kw`.
6790+
let max_dust_htlc_exposure_msat = self.context.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
6791+
if htlc_stats.on_holder_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
6792+
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
6793+
return Err(());
6794+
}
6795+
if htlc_stats.on_counterparty_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
6796+
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
6797+
return Err(());
6798+
}
6799+
6800+
Ok(())
6801+
}
6802+
67866803
/// Removes any uncommitted inbound HTLCs and resets the state of uncommitted outbound HTLC
67876804
/// updates, to be used on peer disconnection. After this, update_*_htlc messages need to be
67886805
/// resent.

0 commit comments

Comments
 (0)