Add user configurable csv delay encumbering channel refund output,
[rust-lightning] / src / util / config.rs
1 //! Various user-configurable channel limits and settings which ChannelManager
2 //! applies for you.
3
4 use ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
5
6 /// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
7 #[derive(Clone, Debug)]
8 pub struct UserConfig {
9         /// Channel config that we propose to our counterparty.
10         pub own_channel_config: ChannelHandshakeConfig,
11         /// Limits applied to our counterparty's proposed channel config settings.
12         pub peer_channel_config_limits: ChannelHandshakeLimits,
13         /// Channel config which affects behavior during channel lifetime.
14         pub channel_options: ChannelConfig,
15 }
16
17 impl UserConfig {
18         /// Provides sane defaults for most configurations (but with 0 relay fees!)
19         pub fn new() -> Self{
20                 UserConfig {
21                         own_channel_config: ChannelHandshakeConfig::new(),
22                         peer_channel_config_limits: ChannelHandshakeLimits::new(),
23                         channel_options: ChannelConfig::new(),
24                 }
25         }
26 }
27
28 /// Configuration we set when applicable.
29 #[derive(Clone, Debug)]
30 pub struct ChannelHandshakeConfig {
31         /// Confirmations we will wait for before considering the channel locked in.
32         /// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
33         /// equivalent limit applied to outbound channels).
34         pub minimum_depth: u32,
35         /// Set to the amount of time we require our counterparty to wait to claim their money.
36         ///
37         /// It's one of the main parameter of our security model. We (or one of our watchtowers) MUST
38         /// be online to check for peer having broadcast a revoked transaction to steal our funds
39         /// at least once every our_to_self_delay blocks.
40         /// Default is BREAKDOWN_TIMEOUT, we enforce it as a minimum at channel opening so you can
41         /// tweak config to ask for more security, not less.
42         ///
43         /// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
44         /// case of an honest unilateral channel close, which implicitly decrease the economic value of
45         /// our channel.
46         pub our_to_self_delay: u16,
47 }
48
49 impl ChannelHandshakeConfig {
50         /// Provides sane defaults for `ChannelHandshakeConfig`
51         pub fn new() -> ChannelHandshakeConfig {
52                 ChannelHandshakeConfig {
53                         minimum_depth: 6,
54                         our_to_self_delay: BREAKDOWN_TIMEOUT,
55                 }
56         }
57 }
58
59 /// Optional channel limits which are applied during channel creation.
60 ///
61 /// These limits are only applied to our counterparty's limits, not our own.
62 ///
63 /// Use 0/<type>::max_value() as appropriate to skip checking.
64 #[derive(Copy, Clone, Debug)]
65 pub struct ChannelHandshakeLimits {
66         /// Minimum allowed satoshis when a channel is funded, this is supplied by the sender and so
67         /// only applies to inbound channels.
68         pub min_funding_satoshis: u64,
69         /// The remote node sets a limit on the minimum size of HTLCs we can send to them. This allows
70         /// you to limit the maximum minimum-size they can require.
71         pub max_htlc_minimum_msat: u64,
72         /// The remote node sets a limit on the maximum value of pending HTLCs to them at any given
73         /// time to limit their funds exposure to HTLCs. This allows you to set a minimum such value.
74         pub min_max_htlc_value_in_flight_msat: u64,
75         /// The remote node will require we keep a certain amount in direct payment to ourselves at all
76         /// time, ensuring that we are able to be punished if we broadcast an old state. This allows to
77         /// you limit the amount which we will have to keep to ourselves (and cannot use for HTLCs).
78         pub max_channel_reserve_satoshis: u64,
79         /// The remote node sets a limit on the maximum number of pending HTLCs to them at any given
80         /// time. This allows you to set a minimum such value.
81         pub min_max_accepted_htlcs: u16,
82         /// Outputs below a certain value will not be added to on-chain transactions. The dust value is
83         /// required to always be higher than this value so this only applies to HTLC outputs (and
84         /// potentially to-self outputs before any payments have been made).
85         /// Thus, HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
86         /// This setting allows you to set a minimum dust limit for their commitment transactions,
87         /// reflecting the reality that tiny outputs are not considered standard transactions and will
88         /// not propagate through the Bitcoin network.
89         /// Defaults to 546, or the current dust limit on the Bitcoin network.
90         pub min_dust_limit_satoshis: u64,
91         /// Maximum allowed threshold above which outputs will not be generated in their commitment
92         /// transactions.
93         /// HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
94         pub max_dust_limit_satoshis: u64,
95         /// Before a channel is usable the funding transaction will need to be confirmed by at least a
96         /// certain number of blocks, specified by the node which is not the funder (as the funder can
97         /// assume they aren't going to double-spend themselves).
98         /// This config allows you to set a limit on the maximum amount of time to wait. Defaults to
99         /// 144 blocks or roughly one day and only applies to outbound channels.
100         pub max_minimum_depth: u32,
101         /// Set to force the incoming channel to match our announced channel preference in
102         /// ChannelConfig.
103         /// Defaults to true to make the default that no announced channels are possible (which is
104         /// appropriate for any nodes which are not online very reliably).
105         pub force_announced_channel_preference: bool,
106         /// Set to the amount of time we're willing to wait to claim money back to us.
107         ///
108         /// Not checking this value would be a security issue, as our peer would be able to set it to
109         /// max relative lock-time (a year) and we would "lose" money as it would be locked for a long time.
110         /// Default is MAX_LOCAL_BREAKDOWN_TIMEOUT, which we also enforce as a maximum value
111         /// so you can tweak config to reduce the loss of having useless locked funds (if your peer accepts)
112         pub their_to_self_delay: u16
113 }
114
115 impl ChannelHandshakeLimits {
116         /// Provides sane defaults for most configurations.
117         ///
118         /// Most additional limits are disabled except those with which specify a default in individual
119         /// field documentation. Note that this may result in barely-usable channels, but since they
120         /// are applied mostly only to incoming channels that's not much of a problem.
121         pub fn new() -> Self {
122                 ChannelHandshakeLimits {
123                         min_funding_satoshis: 0,
124                         max_htlc_minimum_msat: <u64>::max_value(),
125                         min_max_htlc_value_in_flight_msat: 0,
126                         max_channel_reserve_satoshis: <u64>::max_value(),
127                         min_max_accepted_htlcs: 0,
128                         min_dust_limit_satoshis: 546,
129                         max_dust_limit_satoshis: <u64>::max_value(),
130                         max_minimum_depth: 144,
131                         force_announced_channel_preference: true,
132                         their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
133                 }
134         }
135 }
136
137 /// Options which apply on a per-channel basis and may change at runtime or based on negotiation
138 /// with our counterparty.
139 #[derive(Copy, Clone, Debug)]
140 pub struct ChannelConfig {
141         /// Amount (in millionths of a satoshi) the channel will charge per transferred satoshi.
142         /// This may be allowed to change at runtime in a later update, however doing so must result in
143         /// update messages sent to notify all nodes of our updated relay fee.
144         pub fee_proportional_millionths: u32,
145         /// Set to announce the channel publicly and notify all nodes that they can route via this
146         /// channel.
147         ///
148         /// This should only be set to true for nodes which expect to be online reliably.
149         ///
150         /// As the node which funds a channel picks this value this will only apply for new outbound
151         /// channels unless ChannelHandshakeLimits::force_announced_channel_preferences is set.
152         ///
153         /// This cannot be changed after the initial channel handshake.
154         pub announced_channel: bool,
155         /// Set to commit to an upfront shutdown_pubkey at channel opening. In case of mutual
156         /// closing, the other peer will check that our closing transction output is encumbered
157         /// by the provided script.
158         ///
159         /// We set it by default as this ensure greater security to the user funds.
160         ///
161         /// This cannot be changed after channel opening.
162         pub commit_upfront_shutdown_pubkey: bool
163 }
164
165 impl ChannelConfig {
166         /// Provides sane defaults for most configurations (but with zero relay fees!).
167         pub fn new() -> Self {
168                 ChannelConfig {
169                         fee_proportional_millionths: 0,
170                         announced_channel: false,
171                         commit_upfront_shutdown_pubkey: true,
172                 }
173         }
174 }
175
176 //Add write and readable traits to channelconfig
177 impl_writeable!(ChannelConfig, 8+1+1, {
178         fee_proportional_millionths,
179         announced_channel,
180         commit_upfront_shutdown_pubkey
181 });