Skip to content

Commit

Permalink
Fix potential overflow in fee estimation conversion
Browse files Browse the repository at this point in the history
Previously, we would cast `FeeRate::to_sat_per_kwu` to `u32`, which
however might be unsafe if our fee estimation source delivers bogus
data. Here, we make sure to use the fallback rate if we would overflow
otherwise.
  • Loading branch information
tnull committed Jan 16, 2025
1 parent c39bfba commit c52904a
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/fee_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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,
}
}

Expand Down

0 comments on commit c52904a

Please sign in to comment.