Skip to content

Commit 1b783ce

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 da9ddb2 commit 1b783ce

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
@@ -6737,31 +6737,11 @@ impl<SP: Deref> FundedChannel<SP> where
67376737
panic!("Cannot update fee while peer is disconnected/we're awaiting a monitor update (ChannelManager should have caught this)");
67386738
}
67396739

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

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

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

0 commit comments

Comments
 (0)