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