From c14297b399427ff9783cd2189efd9c5e1d504497 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 15 Jan 2025 09:36:33 +0100 Subject: [PATCH 1/2] Default to fallbacks for huge bogus values in fee estimation conversion Previously, we would cast `FeeRate::to_sat_per_kwu` to `u32`, which however might result in `u32::max_value` being used if our fee estimation source delivers huge bogus data. Here, we make sure to use the fallback rate if we would do so otherwise. --- src/fee_estimator.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/fee_estimator.rs b/src/fee_estimator.rs index 8db6a6050..5c7fa1af7 100644 --- a/src/fee_estimator.rs +++ b/src/fee_estimator.rs @@ -77,7 +77,10 @@ impl FeeEstimator for OnchainFeeEstimator { impl LdkFeeEstimator for OnchainFeeEstimator { fn get_est_sat_per_1000_weight(&self, confirmation_target: LdkConfirmationTarget) -> u32 { - self.estimate_fee_rate(confirmation_target.into()).to_sat_per_kwu() as u32 + self.estimate_fee_rate(confirmation_target.into()) + .to_sat_per_kwu() + .try_into() + .unwrap_or_else(|_| get_fallback_rate_for_ldk_target(confirmation_target)) } } @@ -102,16 +105,20 @@ pub(crate) fn get_fallback_rate_for_target(target: ConfirmationTarget) -> u32 { match target { ConfirmationTarget::OnchainPayment => 5000, ConfirmationTarget::ChannelFunding => 1000, - ConfirmationTarget::Lightning(ldk_target) => match ldk_target { - LdkConfirmationTarget::MaximumFeeEstimate => 8000, - LdkConfirmationTarget::UrgentOnChainSweep => 5000, - LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, - LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, - LdkConfirmationTarget::AnchorChannelFee => 500, - LdkConfirmationTarget::NonAnchorChannelFee => 1000, - LdkConfirmationTarget::ChannelCloseMinimum => 500, - LdkConfirmationTarget::OutputSpendingFee => 1000, - }, + ConfirmationTarget::Lightning(ldk_target) => get_fallback_rate_for_ldk_target(ldk_target), + } +} + +pub(crate) fn get_fallback_rate_for_ldk_target(target: LdkConfirmationTarget) -> u32 { + match target { + LdkConfirmationTarget::MaximumFeeEstimate => 8000, + LdkConfirmationTarget::UrgentOnChainSweep => 5000, + LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, + LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, + LdkConfirmationTarget::AnchorChannelFee => 500, + LdkConfirmationTarget::NonAnchorChannelFee => 1000, + LdkConfirmationTarget::ChannelCloseMinimum => 500, + LdkConfirmationTarget::OutputSpendingFee => 1000, } } From 15c3e0e2ffcec064c0f8889043c9bfb3c5ff6c87 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 15 Jan 2025 11:27:37 +0100 Subject: [PATCH 2/2] Also ensure post-estimation fee rate tweaks can never undeflow --- src/fee_estimator.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fee_estimator.rs b/src/fee_estimator.rs index 5c7fa1af7..f000245aa 100644 --- a/src/fee_estimator.rs +++ b/src/fee_estimator.rs @@ -144,7 +144,10 @@ pub(crate) fn apply_post_estimation_adjustments( ConfirmationTarget::Lightning( LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, ) => { - let slightly_less_than_background = estimated_rate.to_sat_per_kwu() - 250; + let slightly_less_than_background = estimated_rate + .to_sat_per_kwu() + .saturating_sub(250) + .max(FEERATE_FLOOR_SATS_PER_KW as u64); FeeRate::from_sat_per_kwu(slightly_less_than_background) }, _ => estimated_rate,