1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Various user-configurable channel limits and settings which ChannelManager
13 use ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
15 /// Configuration we set when applicable.
17 /// Default::default() provides sane defaults.
18 #[derive(Copy, Clone, Debug)]
19 pub struct ChannelHandshakeConfig {
20 /// Confirmations we will wait for before considering the channel locked in.
21 /// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
22 /// equivalent limit applied to outbound channels).
25 pub minimum_depth: u32,
26 /// Set to the number of blocks we require our counterparty to wait to claim their money (ie
27 /// the number of blocks we have to punish our counterparty if they broadcast a revoked
30 /// This is one of the main parameters of our security model. We (or one of our watchtowers) MUST
31 /// be online to check for revoked transactions on-chain at least once every our_to_self_delay
32 /// blocks (minus some margin to allow us enough time to broadcast and confirm a transaction,
33 /// possibly with time in between to RBF the spending transaction).
35 /// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
36 /// case of an honest unilateral channel close, which implicitly decrease the economic value of
39 /// Default value: [`BREAKDOWN_TIMEOUT`], we enforce it as a minimum at channel opening so you
40 /// can tweak config to ask for more security, not less.
41 pub our_to_self_delay: u16,
42 /// Set to the smallest value HTLC we will accept to process.
44 /// This value is sent to our counterparty on channel-open and we close the channel any time
45 /// our counterparty misbehaves by sending us an HTLC with a value smaller than this.
47 /// Default value: 1. If the value is less than 1, it is ignored and set to 1, as is required
49 pub our_htlc_minimum_msat: u64,
52 impl Default for ChannelHandshakeConfig {
53 fn default() -> ChannelHandshakeConfig {
54 ChannelHandshakeConfig {
56 our_to_self_delay: BREAKDOWN_TIMEOUT,
57 our_htlc_minimum_msat: 1,
62 /// Optional channel limits which are applied during channel creation.
64 /// These limits are only applied to our counterparty's limits, not our own.
66 /// Use 0/<type>::max_value() as appropriate to skip checking.
68 /// Provides sane defaults for most configurations.
70 /// Most additional limits are disabled except those with which specify a default in individual
71 /// field documentation. Note that this may result in barely-usable channels, but since they
72 /// are applied mostly only to incoming channels that's not much of a problem.
73 #[derive(Copy, Clone, Debug)]
74 pub struct ChannelHandshakeLimits {
75 /// Minimum allowed satoshis when a channel is funded, this is supplied by the sender and so
76 /// only applies to inbound channels.
79 pub min_funding_satoshis: u64,
80 /// The remote node sets a limit on the minimum size of HTLCs we can send to them. This allows
81 /// you to limit the maximum minimum-size they can require.
83 /// Default value: u64::max_value.
84 pub max_htlc_minimum_msat: u64,
85 /// The remote node sets a limit on the maximum value of pending HTLCs to them at any given
86 /// time to limit their funds exposure to HTLCs. This allows you to set a minimum such value.
89 pub min_max_htlc_value_in_flight_msat: u64,
90 /// The remote node will require we keep a certain amount in direct payment to ourselves at all
91 /// time, ensuring that we are able to be punished if we broadcast an old state. This allows to
92 /// you limit the amount which we will have to keep to ourselves (and cannot use for HTLCs).
94 /// Default value: u64::max_value.
95 pub max_channel_reserve_satoshis: u64,
96 /// The remote node sets a limit on the maximum number of pending HTLCs to them at any given
97 /// time. This allows you to set a minimum such value.
100 pub min_max_accepted_htlcs: u16,
101 /// Before a channel is usable the funding transaction will need to be confirmed by at least a
102 /// certain number of blocks, specified by the node which is not the funder (as the funder can
103 /// assume they aren't going to double-spend themselves).
104 /// This config allows you to set a limit on the maximum amount of time to wait.
106 /// Default value: 144, or roughly one day and only applies to outbound channels.
107 pub max_minimum_depth: u32,
108 /// Set to force the incoming channel to match our announced channel preference in
111 /// Default value: true, to make the default that no announced channels are possible (which is
112 /// appropriate for any nodes which are not online very reliably).
113 pub force_announced_channel_preference: bool,
114 /// Set to the amount of time we're willing to wait to claim money back to us.
116 /// Not checking this value would be a security issue, as our peer would be able to set it to
117 /// max relative lock-time (a year) and we would "lose" money as it would be locked for a long time.
119 /// Default value: 2016, which we also enforce as a maximum value so you can tweak config to
120 /// reduce the loss of having useless locked funds (if your peer accepts)
121 pub their_to_self_delay: u16
124 impl Default for ChannelHandshakeLimits {
125 fn default() -> Self {
126 ChannelHandshakeLimits {
127 min_funding_satoshis: 0,
128 max_htlc_minimum_msat: <u64>::max_value(),
129 min_max_htlc_value_in_flight_msat: 0,
130 max_channel_reserve_satoshis: <u64>::max_value(),
131 min_max_accepted_htlcs: 0,
132 max_minimum_depth: 144,
133 force_announced_channel_preference: true,
134 their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
139 /// Options which apply on a per-channel basis and may change at runtime or based on negotiation
140 /// with our counterparty.
141 #[derive(Copy, Clone, Debug)]
142 pub struct ChannelConfig {
143 /// Amount (in millionths of a satoshi) the channel will charge per transferred satoshi.
144 /// This may be allowed to change at runtime in a later update, however doing so must result in
145 /// update messages sent to notify all nodes of our updated relay fee.
147 /// Default value: 0.
148 pub fee_proportional_millionths: u32,
149 /// The difference in the CLTV value between incoming HTLCs and an outbound HTLC forwarded over
150 /// the channel this config applies to.
152 /// This is analogous to [`ChannelHandshakeConfig::our_to_self_delay`] but applies to in-flight
153 /// HTLC balance when a channel appears on-chain whereas
154 /// [`ChannelHandshakeConfig::our_to_self_delay`] applies to the remaining
155 /// (non-HTLC-encumbered) balance.
157 /// Thus, for HTLC-encumbered balances to be enforced on-chain when a channel is force-closed,
158 /// we (or one of our watchtowers) MUST be online to check for broadcast of the current
159 /// commitment transaction at least once per this many blocks (minus some margin to allow us
160 /// enough time to broadcast and confirm a transaction, possibly with time in between to RBF
161 /// the spending transaction).
163 /// Default value: 72 (12 hours at an average of 6 blocks/hour).
164 /// Minimum value: [`MIN_CLTV_EXPIRY_DELTA`], any values less than this will be treated as
165 /// [`MIN_CLTV_EXPIRY_DELTA`] instead.
167 /// [`MIN_CLTV_EXPIRY_DELTA`]: crate::ln::channelmanager::MIN_CLTV_EXPIRY_DELTA
168 pub cltv_expiry_delta: u16,
169 /// Set to announce the channel publicly and notify all nodes that they can route via this
172 /// This should only be set to true for nodes which expect to be online reliably.
174 /// As the node which funds a channel picks this value this will only apply for new outbound
175 /// channels unless ChannelHandshakeLimits::force_announced_channel_preferences is set.
177 /// This cannot be changed after the initial channel handshake.
179 /// Default value: false.
180 pub announced_channel: bool,
181 /// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
182 /// supports it, they will then enforce the mutual-close output to us matches what we provided
183 /// at intialization, preventing us from closing to an alternate pubkey.
185 /// This is set to true by default to provide a slight increase in security, though ultimately
186 /// any attacker who is able to take control of a channel can just as easily send the funds via
187 /// lightning payments, so we never require that our counterparties support this option.
189 /// This cannot be changed after a channel has been initialized.
191 /// Default value: true.
192 pub commit_upfront_shutdown_pubkey: bool
195 impl Default for ChannelConfig {
196 /// Provides sane defaults for most configurations (but with zero relay fees!).
197 fn default() -> Self {
199 fee_proportional_millionths: 0,
200 cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
201 announced_channel: false,
202 commit_upfront_shutdown_pubkey: true,
207 //Add write and readable traits to channelconfig
208 impl_writeable!(ChannelConfig, 4+2+1+1, {
209 fee_proportional_millionths,
212 commit_upfront_shutdown_pubkey
215 /// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
217 /// Default::default() provides sane defaults for most configurations
218 /// (but currently with 0 relay fees!)
219 #[derive(Copy, Clone, Debug)]
220 pub struct UserConfig {
221 /// Channel config that we propose to our counterparty.
222 pub own_channel_config: ChannelHandshakeConfig,
223 /// Limits applied to our counterparty's proposed channel config settings.
224 pub peer_channel_config_limits: ChannelHandshakeLimits,
225 /// Channel config which affects behavior during channel lifetime.
226 pub channel_options: ChannelConfig,
229 impl Default for UserConfig {
230 fn default() -> Self {
232 own_channel_config: ChannelHandshakeConfig::default(),
233 peer_channel_config_limits: ChannelHandshakeLimits::default(),
234 channel_options: ChannelConfig::default(),