|
| 1 | +use crate::logger::{log_error, log_trace, Logger}; |
| 2 | +use crate::Error; |
| 3 | + |
| 4 | +use lightning::chain::chaininterface::{ |
| 5 | + ConfirmationTarget, FeeEstimator, FEERATE_FLOOR_SATS_PER_KW, |
| 6 | +}; |
| 7 | + |
| 8 | +use bdk::FeeRate; |
| 9 | +use esplora_client::AsyncClient as EsploraClient; |
| 10 | + |
| 11 | +use std::collections::HashMap; |
| 12 | +use std::ops::Deref; |
| 13 | +use std::sync::RwLock; |
| 14 | + |
| 15 | +pub(crate) struct OnchainFeeEstimator<L: Deref> |
| 16 | +where |
| 17 | + L::Target: Logger, |
| 18 | +{ |
| 19 | + fee_rate_cache: RwLock<HashMap<ConfirmationTarget, FeeRate>>, |
| 20 | + esplora_client: EsploraClient, |
| 21 | + logger: L, |
| 22 | +} |
| 23 | + |
| 24 | +impl<L: Deref> OnchainFeeEstimator<L> |
| 25 | +where |
| 26 | + L::Target: Logger, |
| 27 | +{ |
| 28 | + pub(crate) fn new(esplora_client: EsploraClient, logger: L) -> Self { |
| 29 | + let fee_rate_cache = RwLock::new(HashMap::new()); |
| 30 | + Self { fee_rate_cache, esplora_client, logger } |
| 31 | + } |
| 32 | + |
| 33 | + pub(crate) async fn update_fee_estimates(&self) -> Result<(), Error> { |
| 34 | + let confirmation_targets = vec![ |
| 35 | + ConfirmationTarget::OnChainSweep, |
| 36 | + ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee, |
| 37 | + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee, |
| 38 | + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, |
| 39 | + ConfirmationTarget::AnchorChannelFee, |
| 40 | + ConfirmationTarget::NonAnchorChannelFee, |
| 41 | + ConfirmationTarget::ChannelCloseMinimum, |
| 42 | + ]; |
| 43 | + for target in confirmation_targets { |
| 44 | + let num_blocks = match target { |
| 45 | + ConfirmationTarget::OnChainSweep => 6, |
| 46 | + ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => 1, |
| 47 | + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008, |
| 48 | + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 144, |
| 49 | + ConfirmationTarget::AnchorChannelFee => 1008, |
| 50 | + ConfirmationTarget::NonAnchorChannelFee => 12, |
| 51 | + ConfirmationTarget::ChannelCloseMinimum => 144, |
| 52 | + }; |
| 53 | + |
| 54 | + let estimates = self.esplora_client.get_fee_estimates().await.map_err(|e| { |
| 55 | + log_error!( |
| 56 | + self.logger, |
| 57 | + "Failed to retrieve fee rate estimates for {:?}: {}", |
| 58 | + target, |
| 59 | + e |
| 60 | + ); |
| 61 | + Error::FeerateEstimationUpdateFailed |
| 62 | + })?; |
| 63 | + |
| 64 | + let converted_estimates = esplora_client::convert_fee_rate(num_blocks, estimates) |
| 65 | + .map_err(|e| { |
| 66 | + log_error!( |
| 67 | + self.logger, |
| 68 | + "Failed to convert fee rate estimates for {:?}: {}", |
| 69 | + target, |
| 70 | + e |
| 71 | + ); |
| 72 | + Error::FeerateEstimationUpdateFailed |
| 73 | + })?; |
| 74 | + |
| 75 | + let fee_rate = FeeRate::from_sat_per_vb(converted_estimates); |
| 76 | + |
| 77 | + // LDK 0.0.118 introduced changes to the `ConfirmationTarget` semantics that |
| 78 | + // require some post-estimation adjustments to the fee rates, which we do here. |
| 79 | + let adjusted_fee_rate = match target { |
| 80 | + ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => { |
| 81 | + let really_high_prio = fee_rate.as_sat_per_vb() * 10.0; |
| 82 | + FeeRate::from_sat_per_vb(really_high_prio) |
| 83 | + } |
| 84 | + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => { |
| 85 | + let slightly_less_than_background = fee_rate.fee_wu(1000) - 250; |
| 86 | + FeeRate::from_sat_per_kwu(slightly_less_than_background as f32) |
| 87 | + } |
| 88 | + _ => fee_rate, |
| 89 | + }; |
| 90 | + |
| 91 | + let mut locked_fee_rate_cache = self.fee_rate_cache.write().unwrap(); |
| 92 | + locked_fee_rate_cache.insert(target, adjusted_fee_rate); |
| 93 | + log_trace!( |
| 94 | + self.logger, |
| 95 | + "Fee rate estimation updated for {:?}: {} sats/kwu", |
| 96 | + target, |
| 97 | + adjusted_fee_rate.fee_wu(1000) |
| 98 | + ); |
| 99 | + } |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + pub(crate) fn estimate_fee_rate(&self, confirmation_target: ConfirmationTarget) -> FeeRate { |
| 104 | + let locked_fee_rate_cache = self.fee_rate_cache.read().unwrap(); |
| 105 | + |
| 106 | + let fallback_sats_kwu = match confirmation_target { |
| 107 | + ConfirmationTarget::OnChainSweep => 5000, |
| 108 | + ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => 25 * 250, |
| 109 | + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, |
| 110 | + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW, |
| 111 | + ConfirmationTarget::AnchorChannelFee => 500, |
| 112 | + ConfirmationTarget::NonAnchorChannelFee => 1000, |
| 113 | + ConfirmationTarget::ChannelCloseMinimum => 500, |
| 114 | + }; |
| 115 | + |
| 116 | + // We'll fall back on this, if we really don't have any other information. |
| 117 | + let fallback_rate = FeeRate::from_sat_per_kwu(fallback_sats_kwu as f32); |
| 118 | + |
| 119 | + *locked_fee_rate_cache.get(&confirmation_target).unwrap_or(&fallback_rate) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl<L: Deref> FeeEstimator for OnchainFeeEstimator<L> |
| 124 | +where |
| 125 | + L::Target: Logger, |
| 126 | +{ |
| 127 | + fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 { |
| 128 | + (self.estimate_fee_rate(confirmation_target).fee_wu(1000) as u32) |
| 129 | + .max(FEERATE_FLOOR_SATS_PER_KW) |
| 130 | + } |
| 131 | +} |
0 commit comments