@@ -2,9 +2,9 @@ use crate::config::FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS;
2
2
use crate :: logger:: { log_error, log_trace, Logger } ;
3
3
use crate :: { Config , Error } ;
4
4
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 ;
8
8
9
9
use bdk:: FeeRate ;
10
10
use esplora_client:: AsyncClient as EsploraClient ;
@@ -17,6 +17,26 @@ use std::ops::Deref;
17
17
use std:: sync:: { Arc , RwLock } ;
18
18
use std:: time:: Duration ;
19
19
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 used 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
+
20
40
pub ( crate ) struct OnchainFeeEstimator < L : Deref >
21
41
where
22
42
L :: Target : Logger ,
@@ -61,23 +81,30 @@ where
61
81
}
62
82
63
83
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( ) ,
71
93
] ;
94
+
72
95
for target in confirmation_targets {
73
96
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
+ } ,
81
108
} ;
82
109
83
110
let converted_estimates =
96
123
// LDK 0.0.118 introduced changes to the `ConfirmationTarget` semantics that
97
124
// require some post-estimation adjustments to the fee rates, which we do here.
98
125
let adjusted_fee_rate = match target {
99
- ConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee => {
126
+ ConfirmationTarget :: Lightning (
127
+ LdkConfirmationTarget :: MinAllowedNonAnchorChannelRemoteFee ,
128
+ ) => {
100
129
let slightly_less_than_background =
101
130
fee_rate. fee_wu ( Weight :: from_wu ( 1000 ) ) - 250 ;
102
131
FeeRate :: from_sat_per_kwu ( slightly_less_than_background as f32 )
@@ -115,33 +144,53 @@ where
115
144
}
116
145
Ok ( ( ) )
117
146
}
147
+ }
118
148
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 {
120
154
let locked_fee_rate_cache = self . fee_rate_cache . read ( ) . unwrap ( ) ;
121
155
122
156
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
+ } ,
130
172
} ;
131
173
132
174
// We'll fall back on this, if we really don't have any other information.
133
175
let fallback_rate = FeeRate :: from_sat_per_kwu ( fallback_sats_kwu as f32 ) ;
134
176
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
+ )
136
186
}
137
187
}
138
188
139
- impl < L : Deref > FeeEstimator for OnchainFeeEstimator < L >
189
+ impl < L : Deref > LdkFeeEstimator for OnchainFeeEstimator < L >
140
190
where
141
191
L :: Target : Logger ,
142
192
{
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
146
195
}
147
196
}
0 commit comments