forked from getAlby/ldk-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfee_estimator.rs
More file actions
166 lines (147 loc) · 5.92 KB
/
fee_estimator.rs
File metadata and controls
166 lines (147 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::collections::HashMap;
use std::sync::RwLock;
use bitcoin::FeeRate;
use lightning::chain::chaininterface::{
ConfirmationTarget as LdkConfirmationTarget, FeeEstimator as LdkFeeEstimator,
FEERATE_FLOOR_SATS_PER_KW,
};
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub(crate) enum ConfirmationTarget {
/// The default target for onchain payments.
OnchainPayment,
/// The target used for funding transactions.
ChannelFunding,
/// Targets used by LDK.
Lightning(LdkConfirmationTarget),
}
pub(crate) trait FeeEstimator {
fn estimate_fee_rate(&self, confirmation_target: ConfirmationTarget) -> FeeRate;
}
impl From<LdkConfirmationTarget> for ConfirmationTarget {
fn from(value: LdkConfirmationTarget) -> Self {
Self::Lightning(value)
}
}
pub(crate) struct OnchainFeeEstimator {
fee_rate_cache: RwLock<HashMap<ConfirmationTarget, FeeRate>>,
}
impl OnchainFeeEstimator {
pub(crate) fn new() -> Self {
let fee_rate_cache = RwLock::new(HashMap::new());
Self { fee_rate_cache }
}
// Updates the fee rate cache and returns if the new values changed.
pub(crate) fn set_fee_rate_cache(
&self, fee_rate_cache_update: HashMap<ConfirmationTarget, FeeRate>,
) -> bool {
let mut locked_fee_rate_cache = self.fee_rate_cache.write().unwrap();
if fee_rate_cache_update != *locked_fee_rate_cache {
*locked_fee_rate_cache = fee_rate_cache_update;
true
} else {
false
}
}
}
impl FeeEstimator for OnchainFeeEstimator {
fn estimate_fee_rate(&self, confirmation_target: ConfirmationTarget) -> FeeRate {
let locked_fee_rate_cache = self.fee_rate_cache.read().unwrap();
let fallback_sats_kwu = get_fallback_rate_for_target(confirmation_target);
// We'll fall back on this, if we really don't have any other information.
let fallback_rate = FeeRate::from_sat_per_kwu(fallback_sats_kwu as u64);
let estimate = *locked_fee_rate_cache.get(&confirmation_target).unwrap_or(&fallback_rate);
// Currently we assume every transaction needs to at least be relayable, which is why we
// enforce a lower bound of `FEERATE_FLOOR_SATS_PER_KW`.
FeeRate::from_sat_per_kwu(estimate.to_sat_per_kwu().max(FEERATE_FLOOR_SATS_PER_KW as u64))
}
}
impl LdkFeeEstimator for OnchainFeeEstimator {
fn get_est_sat_per_1000_weight(&self, confirmation_target: LdkConfirmationTarget) -> u32 {
self.estimate_fee_rate(confirmation_target.into())
.to_sat_per_kwu()
.try_into()
.unwrap_or_else(|_| get_fallback_rate_for_ldk_target(confirmation_target))
}
}
pub(crate) fn get_num_block_defaults_for_target(target: ConfirmationTarget) -> usize {
match target {
ConfirmationTarget::OnchainPayment => 6,
ConfirmationTarget::ChannelFunding => 12,
ConfirmationTarget::Lightning(ldk_target) => match ldk_target {
LdkConfirmationTarget::MaximumFeeEstimate => 1,
LdkConfirmationTarget::UrgentOnChainSweep => 6,
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008,
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 144,
LdkConfirmationTarget::AnchorChannelFee => 1008,
LdkConfirmationTarget::NonAnchorChannelFee => 12,
LdkConfirmationTarget::ChannelCloseMinimum => 144,
LdkConfirmationTarget::OutputSpendingFee => 12,
},
}
}
pub(crate) fn get_fallback_rate_for_target(target: ConfirmationTarget) -> u32 {
match target {
ConfirmationTarget::OnchainPayment => 5000,
ConfirmationTarget::ChannelFunding => 1000,
ConfirmationTarget::Lightning(ldk_target) => get_fallback_rate_for_ldk_target(ldk_target),
}
}
pub(crate) fn get_fallback_rate_for_ldk_target(target: LdkConfirmationTarget) -> u32 {
match target {
LdkConfirmationTarget::MaximumFeeEstimate => 8000,
LdkConfirmationTarget::UrgentOnChainSweep => 5000,
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => FEERATE_FLOOR_SATS_PER_KW,
LdkConfirmationTarget::AnchorChannelFee => 500,
LdkConfirmationTarget::NonAnchorChannelFee => 1000,
LdkConfirmationTarget::ChannelCloseMinimum => 500,
LdkConfirmationTarget::OutputSpendingFee => 1000,
}
}
pub(crate) fn get_all_conf_targets() -> [ConfirmationTarget; 10] {
[
ConfirmationTarget::OnchainPayment,
ConfirmationTarget::ChannelFunding,
LdkConfirmationTarget::MaximumFeeEstimate.into(),
LdkConfirmationTarget::UrgentOnChainSweep.into(),
LdkConfirmationTarget::MinAllowedAnchorChannelRemoteFee.into(),
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee.into(),
LdkConfirmationTarget::AnchorChannelFee.into(),
LdkConfirmationTarget::NonAnchorChannelFee.into(),
LdkConfirmationTarget::ChannelCloseMinimum.into(),
LdkConfirmationTarget::OutputSpendingFee.into(),
]
}
pub(crate) fn apply_post_estimation_adjustments(
target: ConfirmationTarget, estimated_rate: FeeRate,
) -> FeeRate {
match target {
ConfirmationTarget::Lightning(
LdkConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee,
) => {
let slightly_less_than_background = estimated_rate
.to_sat_per_kwu()
.saturating_sub(250)
.max(FEERATE_FLOOR_SATS_PER_KW as u64);
FeeRate::from_sat_per_kwu(slightly_less_than_background)
},
ConfirmationTarget::Lightning(LdkConfirmationTarget::MaximumFeeEstimate) => {
// MaximumFeeEstimate is mostly used for protection against fee-inflation attacks. As
// users were previously impacted by this limit being too restrictive (read: too low),
// we bump it here a bit to give them some leeway.
let slightly_bump = estimated_rate
.to_sat_per_kwu()
.saturating_mul(11)
.saturating_div(10)
.saturating_add(2500);
FeeRate::from_sat_per_kwu(slightly_bump)
},
_ => estimated_rate,
}
}