@@ -2,9 +2,9 @@ use crate::config::FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS;
22use crate :: logger:: { log_error, log_trace, Logger } ;
33use crate :: { Config , Error } ;
44
5- use lightning:: chain:: chaininterface:: {
6- ConfirmationTarget , FeeEstimator , FEERATE_FLOOR_SATS_PER_KW ,
7- } ;
5+ use lightning:: chain:: chaininterface:: ConfirmationTarget as LdkConfirmationTarget ;
6+ use lightning :: chain :: chaininterface :: FeeEstimator as LdkFeeEstimator ;
7+ use lightning :: chain :: chaininterface :: FEERATE_FLOOR_SATS_PER_KW ;
88
99use bdk:: FeeRate ;
1010use esplora_client:: AsyncClient as EsploraClient ;
@@ -17,6 +17,26 @@ use std::ops::Deref;
1717use std:: sync:: { Arc , RwLock } ;
1818use std:: time:: Duration ;
1919
20+ #[ derive( Clone , Copy , Debug , Hash , PartialEq , Eq ) ]
21+ pub ( crate ) enum ConfirmationTarget {
22+ /// The default target for onchain payments.
23+ OnchainPayment ,
24+ /// The target which we use for funding transactions.
25+ ChannelFunding ,
26+ /// Targets used by LDK.
27+ Lightning ( LdkConfirmationTarget ) ,
28+ }
29+
30+ pub ( crate ) trait FeeEstimator {
31+ fn estimate_fee_rate ( & self , confirmation_target : ConfirmationTarget ) -> FeeRate ;
32+ }
33+
34+ impl From < LdkConfirmationTarget > for ConfirmationTarget {
35+ fn from ( value : LdkConfirmationTarget ) -> Self {
36+ Self :: Lightning ( value)
37+ }
38+ }
39+
2040pub ( crate ) struct OnchainFeeEstimator < L : Deref >
2141where
2242 L :: Target : Logger ,
@@ -61,23 +81,30 @@ where
6181 }
6282
6383 let confirmation_targets = vec ! [
64- ConfirmationTarget :: OnChainSweep ,
65- ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee ,
66- ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee ,
67- ConfirmationTarget :: AnchorChannelFee ,
68- ConfirmationTarget :: NonAnchorChannelFee ,
69- ConfirmationTarget :: ChannelCloseMinimum ,
70- ConfirmationTarget :: OutputSpendingFee ,
84+ ConfirmationTarget :: OnchainPayment ,
85+ ConfirmationTarget :: ChannelFunding ,
86+ LdkConfirmationTarget :: OnChainSweep . into( ) ,
87+ LdkConfirmationTarget :: MinAllowedAnchorChannelRemoteFee . into( ) ,
88+ LdkConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee . into( ) ,
89+ LdkConfirmationTarget :: AnchorChannelFee . into( ) ,
90+ LdkConfirmationTarget :: NonAnchorChannelFee . into( ) ,
91+ LdkConfirmationTarget :: ChannelCloseMinimum . into( ) ,
92+ LdkConfirmationTarget :: OutputSpendingFee . into( ) ,
7193 ] ;
94+
7295 for target in confirmation_targets {
7396 let num_blocks = match target {
74- ConfirmationTarget :: OnChainSweep => 6 ,
75- ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => 1008 ,
76- ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => 144 ,
77- ConfirmationTarget :: AnchorChannelFee => 1008 ,
78- ConfirmationTarget :: NonAnchorChannelFee => 12 ,
79- ConfirmationTarget :: ChannelCloseMinimum => 144 ,
80- ConfirmationTarget :: OutputSpendingFee => 12 ,
97+ ConfirmationTarget :: OnchainPayment => 6 ,
98+ ConfirmationTarget :: ChannelFunding => 12 ,
99+ ConfirmationTarget :: Lightning ( ldk_target) => match ldk_target {
100+ LdkConfirmationTarget :: OnChainSweep => 6 ,
101+ LdkConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => 1008 ,
102+ LdkConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => 144 ,
103+ LdkConfirmationTarget :: AnchorChannelFee => 1008 ,
104+ LdkConfirmationTarget :: NonAnchorChannelFee => 12 ,
105+ LdkConfirmationTarget :: ChannelCloseMinimum => 144 ,
106+ LdkConfirmationTarget :: OutputSpendingFee => 12 ,
107+ } ,
81108 } ;
82109
83110 let converted_estimates =
96123 // LDK 0.0.118 introduced changes to the `ConfirmationTarget` semantics that
97124 // require some post-estimation adjustments to the fee rates, which we do here.
98125 let adjusted_fee_rate = match target {
99- ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => {
126+ ConfirmationTarget :: Lightning (
127+ LdkConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee ,
128+ ) => {
100129 let slightly_less_than_background =
101130 fee_rate. fee_wu ( Weight :: from_wu ( 1000 ) ) - 250 ;
102131 FeeRate :: from_sat_per_kwu ( slightly_less_than_background as f32 )
@@ -115,33 +144,53 @@ where
115144 }
116145 Ok ( ( ) )
117146 }
147+ }
118148
119- pub ( crate ) fn estimate_fee_rate ( & self , confirmation_target : ConfirmationTarget ) -> FeeRate {
149+ impl < L : Deref > FeeEstimator for OnchainFeeEstimator < L >
150+ where
151+ L :: Target : Logger ,
152+ {
153+ fn estimate_fee_rate ( & self , confirmation_target : ConfirmationTarget ) -> FeeRate {
120154 let locked_fee_rate_cache = self . fee_rate_cache . read ( ) . unwrap ( ) ;
121155
122156 let fallback_sats_kwu = match confirmation_target {
123- ConfirmationTarget :: OnChainSweep => 5000 ,
124- ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW ,
125- ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW ,
126- ConfirmationTarget :: AnchorChannelFee => 500 ,
127- ConfirmationTarget :: NonAnchorChannelFee => 1000 ,
128- ConfirmationTarget :: ChannelCloseMinimum => 500 ,
129- ConfirmationTarget :: OutputSpendingFee => 1000 ,
157+ ConfirmationTarget :: OnchainPayment => 5000 ,
158+ ConfirmationTarget :: ChannelFunding => 1000 ,
159+ ConfirmationTarget :: Lightning ( ldk_target) => match ldk_target {
160+ LdkConfirmationTarget :: OnChainSweep => 5000 ,
161+ LdkConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => {
162+ FEERATE_FLOOR_SATS_PER_KW
163+ } ,
164+ LdkConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => {
165+ FEERATE_FLOOR_SATS_PER_KW
166+ } ,
167+ LdkConfirmationTarget :: AnchorChannelFee => 500 ,
168+ LdkConfirmationTarget :: NonAnchorChannelFee => 1000 ,
169+ LdkConfirmationTarget :: ChannelCloseMinimum => 500 ,
170+ LdkConfirmationTarget :: OutputSpendingFee => 1000 ,
171+ } ,
130172 } ;
131173
132174 // We'll fall back on this, if we really don't have any other information.
133175 let fallback_rate = FeeRate :: from_sat_per_kwu ( fallback_sats_kwu as f32 ) ;
134176
135- * locked_fee_rate_cache. get ( & confirmation_target) . unwrap_or ( & fallback_rate)
177+ let estimate = * locked_fee_rate_cache. get ( & confirmation_target) . unwrap_or ( & fallback_rate) ;
178+
179+ // Currently we assume every transaction needs to at least be relayable, which is why we
180+ // enforce a lower bound of `FEERATE_FLOOR_SATS_PER_KW`.
181+ let weight_units = Weight :: from_wu ( 1000 ) ;
182+ FeeRate :: from_wu (
183+ estimate. fee_wu ( weight_units) . max ( FEERATE_FLOOR_SATS_PER_KW as u64 ) ,
184+ weight_units,
185+ )
136186 }
137187}
138188
139- impl < L : Deref > FeeEstimator for OnchainFeeEstimator < L >
189+ impl < L : Deref > LdkFeeEstimator for OnchainFeeEstimator < L >
140190where
141191 L :: Target : Logger ,
142192{
143- fn get_est_sat_per_1000_weight ( & self , confirmation_target : ConfirmationTarget ) -> u32 {
144- ( self . estimate_fee_rate ( confirmation_target) . fee_wu ( Weight :: from_wu ( 1000 ) ) as u32 )
145- . max ( FEERATE_FLOOR_SATS_PER_KW )
193+ fn get_est_sat_per_1000_weight ( & self , confirmation_target : LdkConfirmationTarget ) -> u32 {
194+ self . estimate_fee_rate ( confirmation_target. into ( ) ) . fee_wu ( Weight :: from_wu ( 1000 ) ) as u32
146195 }
147196}
0 commit comments