Skip to content

Commit 7ebc0a9

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 3b09db8 commit 7ebc0a9

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/ln/channel.rs

Lines changed: 21 additions & 9 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);
@@ -424,7 +427,7 @@ impl Channel {
424427

425428
let secp_ctx = Secp256k1::new();
426429
let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
427-
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
430+
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
428431
keys_provider.get_destination_script(), logger.clone());
429432

430433
Ok(Channel {
@@ -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 {
@@ -612,7 +620,7 @@ impl Channel {
612620

613621
let secp_ctx = Secp256k1::new();
614622
let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
615-
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
623+
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
616624
keys_provider.get_destination_script(), logger.clone());
617625
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
618626
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
@@ -692,6 +700,7 @@ impl Channel {
692700
their_htlc_minimum_msat: msg.htlc_minimum_msat,
693701
our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(msg.feerate_per_kw as u64),
694702
their_to_self_delay: msg.to_self_delay,
703+
our_to_self_delay: config.own_channel_config.our_to_self_delay,
695704
their_max_accepted_htlcs: msg.max_accepted_htlcs,
696705
minimum_depth: config.own_channel_config.minimum_depth,
697706

@@ -927,7 +936,7 @@ impl Channel {
927936
log_trace!(self, " ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
928937
txouts.push((TxOut {
929938
script_pubkey: chan_utils::get_revokeable_redeemscript(&keys.revocation_key,
930-
if local { self.their_to_self_delay } else { BREAKDOWN_TIMEOUT },
939+
if local { self.their_to_self_delay } else { self.our_to_self_delay },
931940
&keys.a_delayed_payment_key).to_v0_p2wsh(),
932941
value: value_to_a as u64
933942
}, None));
@@ -1126,7 +1135,7 @@ impl Channel {
11261135
/// @local is used only to convert relevant internal structures which refer to remote vs local
11271136
/// to decide value of outputs and direction of HTLCs.
11281137
fn build_htlc_transaction(&self, prev_hash: &Sha256dHash, htlc: &HTLCOutputInCommitment, local: bool, keys: &TxCreationKeys, feerate_per_kw: u64) -> Transaction {
1129-
chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.their_to_self_delay } else { BREAKDOWN_TIMEOUT }, htlc, &keys.a_delayed_payment_key, &keys.revocation_key)
1138+
chan_utils::build_htlc_transaction(prev_hash, feerate_per_kw, if local { self.their_to_self_delay } else { self.our_to_self_delay }, htlc, &keys.a_delayed_payment_key, &keys.revocation_key)
11301139
}
11311140

11321141
fn create_htlc_tx_signature(&self, tx: &Transaction, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<(Script, Signature, bool), ChannelError> {
@@ -1380,7 +1389,7 @@ impl Channel {
13801389
if msg.htlc_minimum_msat >= (self.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000 {
13811390
return Err(ChannelError::Close("Minimum htlc value is full channel value"));
13821391
}
1383-
if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
1392+
if msg.to_self_delay > config.peer_channel_config_limits.their_to_self_delay || msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
13841393
return Err(ChannelError::Close("They wanted our payments to be delayed by a needlessly long period"));
13851394
}
13861395
if msg.max_accepted_htlcs < 1 {
@@ -3064,7 +3073,7 @@ impl Channel {
30643073
channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
30653074
htlc_minimum_msat: self.our_htlc_minimum_msat,
30663075
feerate_per_kw: fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u32,
3067-
to_self_delay: BREAKDOWN_TIMEOUT,
3076+
to_self_delay: self.our_to_self_delay,
30683077
max_accepted_htlcs: OUR_MAX_HTLCS,
30693078
funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
30703079
revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
@@ -3097,7 +3106,7 @@ impl Channel {
30973106
channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
30983107
htlc_minimum_msat: self.our_htlc_minimum_msat,
30993108
minimum_depth: self.minimum_depth,
3100-
to_self_delay: BREAKDOWN_TIMEOUT,
3109+
to_self_delay: self.our_to_self_delay,
31013110
max_accepted_htlcs: OUR_MAX_HTLCS,
31023111
funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key),
31033112
revocation_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key),
@@ -3746,6 +3755,7 @@ impl Writeable for Channel {
37463755
self.their_htlc_minimum_msat.write(writer)?;
37473756
self.our_htlc_minimum_msat.write(writer)?;
37483757
self.their_to_self_delay.write(writer)?;
3758+
self.our_to_self_delay.write(writer)?;
37493759
self.their_max_accepted_htlcs.write(writer)?;
37503760
self.minimum_depth.write(writer)?;
37513761

@@ -3907,6 +3917,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
39073917
let their_htlc_minimum_msat = Readable::read(reader)?;
39083918
let our_htlc_minimum_msat = Readable::read(reader)?;
39093919
let their_to_self_delay = Readable::read(reader)?;
3920+
let our_to_self_delay = Readable::read(reader)?;
39103921
let their_max_accepted_htlcs = Readable::read(reader)?;
39113922
let minimum_depth = Readable::read(reader)?;
39123923

@@ -3984,6 +3995,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
39843995
their_htlc_minimum_msat,
39853996
our_htlc_minimum_msat,
39863997
their_to_self_delay,
3998+
our_to_self_delay,
39873999
their_max_accepted_htlcs,
39884000
minimum_depth,
39894001

src/util/config.rs

Lines changed: 23 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,26 @@ 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 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,
3347
}
3448

3549
impl ChannelHandshakeConfig {
3650
/// Provides sane defaults for `ChannelHandshakeConfig`
3751
pub fn new() -> ChannelHandshakeConfig {
3852
ChannelHandshakeConfig {
3953
minimum_depth: 6,
54+
our_to_self_delay: BREAKDOWN_TIMEOUT,
4055
}
4156
}
4257
}
@@ -88,6 +103,13 @@ pub struct ChannelHandshakeLimits {
88103
/// Defaults to true to make the default that no announced channels are possible (which is
89104
/// appropriate for any nodes which are not online very reliably).
90105
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
91113
}
92114

93115
impl ChannelHandshakeLimits {
@@ -107,6 +129,7 @@ impl ChannelHandshakeLimits {
107129
max_dust_limit_satoshis: <u64>::max_value(),
108130
max_minimum_depth: 144,
109131
force_announced_channel_preference: true,
132+
their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,
110133
}
111134
}
112135
}

0 commit comments

Comments
 (0)