@@ -53,8 +53,7 @@ impl<S: MutinyStorage> MutinyFeeEstimator<S> {
5353 sats_per_kw : Option < u32 > ,
5454 ) -> u64 {
5555 // if no fee rate is provided, use the normal confirmation target
56- let sats_per_kw = sats_per_kw
57- . unwrap_or_else ( || self . get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ) ;
56+ let sats_per_kw = sats_per_kw. unwrap_or_else ( || self . get_normal_fee_rate ( ) ) ;
5857 let expected_weight = {
5958 // Calculate the non-witness and witness data sizes
6059 let non_witness_size = TX_OVERHEAD
@@ -144,14 +143,29 @@ impl<S: MutinyStorage> MutinyFeeEstimator<S> {
144143
145144 Ok ( ( ) )
146145 }
146+
147+ pub fn get_low_fee_rate ( & self ) -> u32 {
148+ // MinAllowedNonAnchorChannelRemoteFee is a fee rate we expect to get slowly
149+ self . get_est_sat_per_1000_weight ( ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee )
150+ }
151+
152+ pub fn get_normal_fee_rate ( & self ) -> u32 {
153+ // NonAnchorChannelFee is a fee rate we expect to be confirmed in 6 blocks
154+ self . get_est_sat_per_1000_weight ( ConfirmationTarget :: NonAnchorChannelFee )
155+ }
156+
157+ pub fn get_high_fee_rate ( & self ) -> u32 {
158+ // OnChainSweep is the highest fee rate we have, so use that
159+ self . get_est_sat_per_1000_weight ( ConfirmationTarget :: OnChainSweep )
160+ }
147161}
148162
149163impl < S : MutinyStorage > FeeEstimator for MutinyFeeEstimator < S > {
150164 fn get_est_sat_per_1000_weight ( & self , confirmation_target : ConfirmationTarget ) -> u32 {
151165 let num_blocks = num_blocks_from_conf_target ( confirmation_target) ;
152166 let fallback_fee = fallback_fee_from_conf_target ( confirmation_target) ;
153167
154- match self . storage . get_fee_estimates ( ) {
168+ let fee = match self . storage . get_fee_estimates ( ) {
155169 Err ( _) | Ok ( None ) => fallback_fee,
156170 Ok ( Some ( estimates) ) => {
157171 let found = estimates. get ( & num_blocks. to_string ( ) ) ;
@@ -160,43 +174,46 @@ impl<S: MutinyStorage> FeeEstimator for MutinyFeeEstimator<S> {
160174 log_trace ! ( self . logger, "Got fee rate from saved cache!" ) ;
161175 let sats_vbyte = num. to_owned ( ) ;
162176 // convert to sats per kw
163- let mut fee_rate = sats_vbyte * 250.0 ;
164-
165- // if we're using the high priority target, multiply by 3
166- // this should help prevent force closures from fee disputes
167- if confirmation_target == ConfirmationTarget :: HighPriority {
168- fee_rate *= 3.0 ;
169- }
177+ let fee_rate = sats_vbyte * 250.0 ;
170178
171179 // return the fee rate, but make sure it's not lower than the floor
172180 ( fee_rate as u32 ) . max ( FEERATE_FLOOR_SATS_PER_KW )
173181 }
174182 None => fallback_fee,
175183 }
176184 }
185+ } ;
186+
187+ // any post processing we do after the we get the fee rate from the cache
188+ match confirmation_target {
189+ ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee => fee * 30 , // multiply by 30 to help prevent force closes
190+ ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => fee - 250 , // helps with rounding errors
191+ _ => fee,
177192 }
178193 }
179194}
180195
181196fn num_blocks_from_conf_target ( confirmation_target : ConfirmationTarget ) -> usize {
182197 match confirmation_target {
183- // MempoolMinimum is only used for anchor channels, we just set the target to 1008
184- // as that is esplora's highest block target available
185- ConfirmationTarget :: MempoolMinimum => 1008 ,
186- // Background is VERY lax and may never confirm if used directly
187- // it is only meant for lower ranges of transaction to enter mempool
188- ConfirmationTarget :: Background => 1008 ,
189- ConfirmationTarget :: Normal => 6 ,
190- ConfirmationTarget :: HighPriority => 1 ,
198+ ConfirmationTarget :: AnchorChannelFee => 1008 ,
199+ ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => 1008 ,
200+ ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => 1008 ,
201+ ConfirmationTarget :: ChannelCloseMinimum => 1008 ,
202+ ConfirmationTarget :: NonAnchorChannelFee => 6 ,
203+ ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee => 1 ,
204+ ConfirmationTarget :: OnChainSweep => 1 ,
191205 }
192206}
193207
194208fn fallback_fee_from_conf_target ( confirmation_target : ConfirmationTarget ) -> u32 {
195209 match confirmation_target {
196- ConfirmationTarget :: MempoolMinimum => 3 * 250 ,
197- ConfirmationTarget :: Background => 10 * 250 ,
198- ConfirmationTarget :: Normal => 20 * 250 ,
199- ConfirmationTarget :: HighPriority => 50 * 250 ,
210+ ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee => 3 * 250 ,
211+ ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => 3 * 250 ,
212+ ConfirmationTarget :: ChannelCloseMinimum => 10 * 250 ,
213+ ConfirmationTarget :: AnchorChannelFee => 10 * 250 ,
214+ ConfirmationTarget :: NonAnchorChannelFee => 20 * 250 ,
215+ ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee => 50 * 250 ,
216+ ConfirmationTarget :: OnChainSweep => 50 * 250 ,
200217 }
201218}
202219
@@ -229,28 +246,31 @@ mod test {
229246 #[ test]
230247 fn test_num_blocks_from_conf_target ( ) {
231248 assert_eq ! (
232- num_blocks_from_conf_target( ConfirmationTarget :: Background ) ,
249+ num_blocks_from_conf_target( ConfirmationTarget :: ChannelCloseMinimum ) ,
233250 1008
234251 ) ;
235- assert_eq ! ( num_blocks_from_conf_target( ConfirmationTarget :: Normal ) , 6 ) ;
236252 assert_eq ! (
237- num_blocks_from_conf_target( ConfirmationTarget :: HighPriority ) ,
253+ num_blocks_from_conf_target( ConfirmationTarget :: NonAnchorChannelFee ) ,
254+ 6
255+ ) ;
256+ assert_eq ! (
257+ num_blocks_from_conf_target( ConfirmationTarget :: OnChainSweep ) ,
238258 1
239259 ) ;
240260 }
241261
242262 #[ test]
243263 fn test_fallback_fee_from_conf_target ( ) {
244264 assert_eq ! (
245- fallback_fee_from_conf_target( ConfirmationTarget :: Background ) ,
265+ fallback_fee_from_conf_target( ConfirmationTarget :: ChannelCloseMinimum ) ,
246266 2_500
247267 ) ;
248268 assert_eq ! (
249- fallback_fee_from_conf_target( ConfirmationTarget :: Normal ) ,
269+ fallback_fee_from_conf_target( ConfirmationTarget :: NonAnchorChannelFee ) ,
250270 5_000
251271 ) ;
252272 assert_eq ! (
253- fallback_fee_from_conf_target( ConfirmationTarget :: HighPriority ) ,
273+ fallback_fee_from_conf_target( ConfirmationTarget :: OnChainSweep ) ,
254274 12_500
255275 ) ;
256276 }
@@ -288,17 +308,17 @@ mod test {
288308
289309 // test that we get the fee rate from the cache
290310 assert_eq ! (
291- fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: Normal ) ,
311+ fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: NonAnchorChannelFee ) ,
292312 2500
293313 ) ;
294314
295315 // test that we get the fallback fee rate
296316 assert_eq ! (
297- fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: Background ) ,
317+ fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: ChannelCloseMinimum ) ,
298318 2_500
299319 ) ;
300320 assert_eq ! (
301- fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: HighPriority ) ,
321+ fee_estimator. get_est_sat_per_1000_weight( ConfirmationTarget :: OnChainSweep ) ,
302322 12_500
303323 ) ;
304324 }
0 commit comments