Skip to content

Commit 9978224

Browse files
author
Antoine Riard
committed
Add user configurable csv delay encumbering channel refund output,
within reasonable lower or upper bound Add our_to_self_delay in Channel, to cache user config field at channel construction.
1 parent 87a9991 commit 9978224

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/ln/channel.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ pub(super) struct Channel {
317317
their_htlc_minimum_msat: u64,
318318
our_htlc_minimum_msat: u64,
319319
their_to_self_delay: u16,
320-
//implied by BREAKDOWN_TIMEOUT: our_to_self_delay: u16,
320+
our_to_self_delay: u16,
321321
#[cfg(test)]
322322
pub their_max_accepted_htlcs: u16,
323323
#[cfg(not(test))]
@@ -413,6 +413,9 @@ impl Channel {
413413
if push_msat > channel_value_satoshis * 1000 {
414414
return Err(APIError::APIMisuseError{err: "push value > channel value"});
415415
}
416+
if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
417+
return Err(APIError::APIMisuseError{err: "Configured with an unreasonable our_to_self_delay putting user funds at risks"});
418+
}
416419

417420

418421
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
@@ -481,6 +484,7 @@ impl Channel {
481484
their_htlc_minimum_msat: 0,
482485
our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(feerate),
483486
their_to_self_delay: 0,
487+
our_to_self_delay: config.own_channel_config.our_to_self_delay,
484488
their_max_accepted_htlcs: 0,
485489
minimum_depth: 0, // Filled in in accept_channel
486490

@@ -518,6 +522,10 @@ impl Channel {
518522
let chan_keys = keys_provider.get_channel_keys(true);
519523
let mut local_config = (*config).channel_options.clone();
520524

525+
if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
526+
return Err(ChannelError::Close("Configured with an unreasonable our_to_self_delay putting user funds at risks"));
527+
}
528+
521529
// Check sanity of message fields:
522530
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
523531
return Err(ChannelError::Close("funding value > 2^24"));
@@ -539,7 +547,7 @@ impl Channel {
539547
}
540548
Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
541549

542-
if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
550+
if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
543551
return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
544552
}
545553
if msg.max_accepted_htlcs < 1 {
@@ -671,6 +679,7 @@ impl Channel {
671679
their_htlc_minimum_msat: msg.htlc_minimum_msat,
672680
our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(msg.feerate_per_kw as u64),
673681
their_to_self_delay: msg.to_self_delay,
682+
our_to_self_delay: config.own_channel_config.our_to_self_delay,
674683
their_max_accepted_htlcs: msg.max_accepted_htlcs,
675684
minimum_depth: config.own_channel_config.minimum_depth,
676685

@@ -1359,7 +1368,7 @@ impl Channel {
13591368
if msg.htlc_minimum_msat >= (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000 {
13601369
return Err(ChannelError::Close("Minimum htlc value is full channel value"));
13611370
}
1362-
if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
1371+
if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
13631372
return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
13641373
}
13651374
if msg.max_accepted_htlcs < 1 {
@@ -3021,7 +3030,7 @@ impl Channel {
30213030
channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
30223031
htlc_minimum_msat: self.our_htlc_minimum_msat,
30233032
feerate_per_kw: fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u32,
3024-
to_self_delay: BREAKDOWN_TIMEOUT,
3033+
to_self_delay: self.our_to_self_delay,
30253034
max_accepted_htlcs: OUR_MAX_HTLCS,
30263035
funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
30273036
revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
@@ -3054,7 +3063,7 @@ impl Channel {
30543063
channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
30553064
htlc_minimum_msat: self.our_htlc_minimum_msat,
30563065
minimum_depth: self.minimum_depth,
3057-
to_self_delay: BREAKDOWN_TIMEOUT,
3066+
to_self_delay: self.our_to_self_delay,
30583067
max_accepted_htlcs: OUR_MAX_HTLCS,
30593068
funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
30603069
revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
@@ -3703,6 +3712,7 @@ impl Writeable for Channel {
37033712
self.their_htlc_minimum_msat.write(writer)?;
37043713
self.our_htlc_minimum_msat.write(writer)?;
37053714
self.their_to_self_delay.write(writer)?;
3715+
self.our_to_self_delay.write(writer)?;
37063716
self.their_max_accepted_htlcs.write(writer)?;
37073717
self.minimum_depth.write(writer)?;
37083718

@@ -3864,6 +3874,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
38643874
let their_htlc_minimum_msat = Readable::read(reader)?;
38653875
let our_htlc_minimum_msat = Readable::read(reader)?;
38663876
let their_to_self_delay = Readable::read(reader)?;
3877+
let our_to_self_delay = Readable::read(reader)?;
38673878
let their_max_accepted_htlcs = Readable::read(reader)?;
38683879
let minimum_depth = Readable::read(reader)?;
38693880

@@ -3941,6 +3952,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
39413952
their_htlc_minimum_msat,
39423953
our_htlc_minimum_msat,
39433954
their_to_self_delay,
3955+
our_to_self_delay,
39443956
their_max_accepted_htlcs,
39453957
minimum_depth,
39463958

src/util/config.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Various user-configurable channel limits and settings which ChannelManager
22
//! applies for you.
33
4+
use ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
5+
46
/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
57
#[derive(Clone, Debug)]
68
pub struct UserConfig {
@@ -30,13 +32,25 @@ pub struct ChannelHandshakeConfig {
3032
/// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
3133
/// equivalent limit applied to outbound channels).
3234
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 us funds.
39+
/// Default is BREAKDOWN_TIMEOUT, we enforce it as a minimum at channel opening so you can
40+
/// tweak config to ask for more security, not less.
41+
///
42+
/// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
43+
/// case of an honest unilateral channel close, which implicitly decrease the economic value of
44+
/// our channel.
45+
pub our_to_self_delay: u16,
3346
}
3447

3548
impl ChannelHandshakeConfig {
3649
/// Provides sane defaults for `ChannelHandshakeConfig`
3750
pub fn new() -> ChannelHandshakeConfig {
3851
ChannelHandshakeConfig {
3952
minimum_depth: 6,
53+
our_to_self_delay: BREAKDOWN_TIMEOUT,
4054
}
4155
}
4256
}
@@ -88,6 +102,13 @@ pub struct ChannelHandshakeLimits {
88102
/// Defaults to true to make the default that no announced channels are possible (which is
89103
/// appropriate for any nodes which are not online very reliably).
90104
pub force_announced_channel_preference: bool,
105+
/// Set to the amount of time we're willing to wait to claim money back to us.
106+
///
107+
/// Not checking this value would be also a security issue, as our peer would be able to set it to
108+
/// max relative lock-time (a year) and us losing money by useless locked funds.
109+
/// Default is MAX_LOCAL_BREAKDOWN_TIMEOUT, we enforce it as a maximum at channel opening offer
110+
/// so you can tweak config to reduce the loss of having useless locked funds (if you peer accept)
111+
pub their_to_self_delay: u16
91112
}
92113

93114
impl ChannelHandshakeLimits {
@@ -107,6 +128,7 @@ impl ChannelHandshakeLimits {
107128
max_dust_limit_satoshis: <u64>::max_value(),
108129
max_minimum_depth: 144,
109130
force_announced_channel_preference: true,
131+
their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
110132
}
111133
}
112134
}

0 commit comments

Comments
 (0)