Skip to content

Commit 9953d2b

Browse files
authored
Merge pull request #430 from tnull/2025-01-fix-potential-fee-rate-conversion-overflow
Default to fallbacks for huge bogus values in fee estimation conversion
2 parents 03c4ab0 + 15c3e0e commit 9953d2b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/fee_estimator.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ impl FeeEstimator for OnchainFeeEstimator {
7777

7878
impl LdkFeeEstimator for OnchainFeeEstimator {
7979
fn get_est_sat_per_1000_weight(&self, confirmation_target: LdkConfirmationTarget) -> u32 {
80-
self.estimate_fee_rate(confirmation_target.into()).to_sat_per_kwu() as u32
80+
self.estimate_fee_rate(confirmation_target.into())
81+
.to_sat_per_kwu()
82+
.try_into()
83+
.unwrap_or_else(|_| get_fallback_rate_for_ldk_target(confirmation_target))
8184
}
8285
}
8386

@@ -102,16 +105,20 @@ pub(crate) fn get_fallback_rate_for_target(target: ConfirmationTarget) -> u32 {
102105
match target {
103106
ConfirmationTarget::OnchainPayment => 5000,
104107
ConfirmationTarget::ChannelFunding => 1000,
105-
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
106-
LdkConfirmationTarget::MaximumFeeEstimate => 8000,
107-
LdkConfirmationTarget::UrgentOnChainSweep => 5000,
108-
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
109-
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
110-
LdkConfirmationTarget::AnchorChannelFee => 500,
111-
LdkConfirmationTarget::NonAnchorChannelFee => 1000,
112-
LdkConfirmationTarget::ChannelCloseMinimum => 500,
113-
LdkConfirmationTarget::OutputSpendingFee => 1000,
114-
},
108+
ConfirmationTarget::Lightning(ldk_target) => get_fallback_rate_for_ldk_target(ldk_target),
109+
}
110+
}
111+
112+
pub(crate) fn get_fallback_rate_for_ldk_target(target: LdkConfirmationTarget) -> u32 {
113+
match target {
114+
LdkConfirmationTarget::MaximumFeeEstimate => 8000,
115+
LdkConfirmationTarget::UrgentOnChainSweep => 5000,
116+
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
117+
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
118+
LdkConfirmationTarget::AnchorChannelFee => 500,
119+
LdkConfirmationTarget::NonAnchorChannelFee => 1000,
120+
LdkConfirmationTarget::ChannelCloseMinimum => 500,
121+
LdkConfirmationTarget::OutputSpendingFee => 1000,
115122
}
116123
}
117124

@@ -137,7 +144,10 @@ pub(crate) fn apply_post_estimation_adjustments(
137144
ConfirmationTarget::Lightning(
138145
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee,
139146
) => {
140-
let slightly_less_than_background = estimated_rate.to_sat_per_kwu() - 250;
147+
let slightly_less_than_background = estimated_rate
148+
.to_sat_per_kwu()
149+
.saturating_sub(250)
150+
.max(FEERATE_FLOOR_SATS_PER_KW as u64);
141151
FeeRate::from_sat_per_kwu(slightly_less_than_background)
142152
},
143153
_ => estimated_rate,

0 commit comments

Comments
 (0)