|
13 | 13 | //! Includes traits for monitoring and receiving notifications of new blocks and block
|
14 | 14 | //! disconnections, transaction broadcasting, and feerate information requests.
|
15 | 15 |
|
| 16 | +use core::{cmp, ops::Deref}; |
| 17 | + |
16 | 18 | use bitcoin::blockdata::transaction::Transaction;
|
17 | 19 |
|
18 | 20 | /// An interface to send a transaction to the Bitcoin network.
|
@@ -41,14 +43,75 @@ pub enum ConfirmationTarget {
|
41 | 43 | pub trait FeeEstimator {
|
42 | 44 | /// Gets estimated satoshis of fee required per 1000 Weight-Units.
|
43 | 45 | ///
|
44 |
| - /// Must return a value no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later |
45 |
| - /// round-downs don't put us below 1 satoshi-per-byte). |
46 |
| - /// |
47 |
| - /// This method can be implemented with the following unit conversions: |
48 |
| - /// * max(satoshis-per-byte * 250, 253) |
49 |
| - /// * max(satoshis-per-kbyte / 4, 253) |
| 46 | + /// LDK will wrap this method and ensure that the value returned is no smaller than 253 |
| 47 | + /// (ie 1 satoshi-per-byte rounded up to ensure later round-downs don't put us below 1 satoshi-per-byte). |
50 | 48 | fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32;
|
51 | 49 | }
|
52 | 50 |
|
53 | 51 | /// Minimum relay fee as required by bitcoin network mempool policy.
|
54 | 52 | pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
|
| 53 | +/// Minimum feerate that takes a sane approach to rounding |
| 54 | +pub const FEERATE_FLOOR_SATS_PER_KW: u32 = 253; |
| 55 | + |
| 56 | +/// Wraps a `FeeEstimator` so that any fee estimations provided by it |
| 57 | +/// are bounded below by `FEERATE_FLOOR_SATS_PER_KW` (253 sats/KW) |
| 58 | +pub(crate) struct LowerBoundedFeeEstimator<'a, F: Deref>(pub(crate) &'a F) |
| 59 | +where |
| 60 | + F::Target: FeeEstimator; |
| 61 | + |
| 62 | +impl<'a, F: Deref> LowerBoundedFeeEstimator<'a, F> |
| 63 | +where |
| 64 | + F::Target: FeeEstimator, |
| 65 | +{ |
| 66 | + pub(crate) fn new(fee_estimator: &'a F) -> Self |
| 67 | + where |
| 68 | + F::Target: FeeEstimator, |
| 69 | + { |
| 70 | + LowerBoundedFeeEstimator(fee_estimator) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<F: Deref> FeeEstimator for LowerBoundedFeeEstimator<'_, F> |
| 75 | +where |
| 76 | + F::Target: FeeEstimator, |
| 77 | +{ |
| 78 | + fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 { |
| 79 | + cmp::max( |
| 80 | + self.0.get_est_sat_per_1000_weight(confirmation_target), |
| 81 | + FEERATE_FLOOR_SATS_PER_KW, |
| 82 | + ) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::{FEERATE_FLOOR_SATS_PER_KW, LowerBoundedFeeEstimator, ConfirmationTarget, FeeEstimator}; |
| 89 | + |
| 90 | + struct TestFeeEstimator { |
| 91 | + sat_per_kw: u32, |
| 92 | + } |
| 93 | + |
| 94 | + impl FeeEstimator for TestFeeEstimator { |
| 95 | + fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 { |
| 96 | + self.sat_per_kw |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_fee_estimator_less_than_floor() { |
| 102 | + let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW - 1; |
| 103 | + let test_fee_estimator = &TestFeeEstimator { sat_per_kw }; |
| 104 | + let fee_estimator = LowerBoundedFeeEstimator::new(&test_fee_estimator); |
| 105 | + |
| 106 | + assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), FEERATE_FLOOR_SATS_PER_KW); |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_fee_estimator_greater_than_floor() { |
| 111 | + let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW + 1; |
| 112 | + let test_fee_estimator = &TestFeeEstimator { sat_per_kw }; |
| 113 | + let fee_estimator = LowerBoundedFeeEstimator::new(&test_fee_estimator); |
| 114 | + |
| 115 | + assert_eq!(fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background), sat_per_kw); |
| 116 | + } |
| 117 | +} |
0 commit comments