Skip to content

Commit 80ee1da

Browse files
authored
Merge pull request #849 from TheBlueMatt/2021-03-config-cltv-delta
Make cltv_expiry_delta configurable and reduce the min/default some
2 parents fba204b + e640b93 commit 80ee1da

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

codecov.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ coverage:
33
project:
44
default:
55
target: auto
6-
threshold: 2%
6+
threshold: 1%
77
base: auto
88
informational: false
9+
patch:
10+
default:
11+
target: auto
12+
threshold: 100%
13+
base: auto

lightning/src/ln/channel.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use bitcoin::secp256k1;
2525
use ln::features::{ChannelFeatures, InitFeatures};
2626
use ln::msgs;
2727
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
28-
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
28+
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
2929
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor};
3030
use ln::chan_utils;
3131
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -3359,6 +3359,10 @@ impl<Signer: Sign> Channel<Signer> {
33593359
self.config.fee_proportional_millionths
33603360
}
33613361

3362+
pub fn get_cltv_expiry_delta(&self) -> u16 {
3363+
cmp::max(self.config.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
3364+
}
3365+
33623366
#[cfg(test)]
33633367
pub fn get_feerate(&self) -> u32 {
33643368
self.feerate_per_kw

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,16 @@ pub const BREAKDOWN_TIMEOUT: u16 = 6 * 24;
523523
pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 2 * 6 * 24 * 7;
524524

525525
/// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
526-
/// HTLC's CLTV. This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
527-
/// ie the node we forwarded the payment on to should always have enough room to reliably time out
528-
/// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
529-
/// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
530-
const CLTV_EXPIRY_DELTA: u16 = 6 * 12; //TODO?
526+
/// HTLC's CLTV. The current default represents roughly six hours of blocks at six blocks/hour.
527+
///
528+
/// This can be increased (but not decreased) through [`ChannelConfig::cltv_expiry_delta`]
529+
///
530+
/// [`ChannelConfig::cltv_expiry_delta`]: crate::util::config::ChannelConfig::cltv_expiry_delta
531+
// This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
532+
// i.e. the node we forwarded the payment on to should always have enough room to reliably time out
533+
// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
534+
// CLTV_CLAIM_BUFFER point (we static assert that it's at least 3 blocks more).
535+
pub const MIN_CLTV_EXPIRY_DELTA: u16 = 6 * 6;
531536
pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
532537

533538
// Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
@@ -538,13 +543,13 @@ pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
538543
// LATENCY_GRACE_PERIOD_BLOCKS.
539544
#[deny(const_err)]
540545
#[allow(dead_code)]
541-
const CHECK_CLTV_EXPIRY_SANITY: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - CLTV_CLAIM_BUFFER - ANTI_REORG_DELAY - LATENCY_GRACE_PERIOD_BLOCKS;
546+
const CHECK_CLTV_EXPIRY_SANITY: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - CLTV_CLAIM_BUFFER - ANTI_REORG_DELAY - LATENCY_GRACE_PERIOD_BLOCKS;
542547

543548
// Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
544549
// ChannelMontior::would_broadcast_at_height for a description of why this is needed.
545550
#[deny(const_err)]
546551
#[allow(dead_code)]
547-
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
552+
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
548553

549554
/// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
550555
#[derive(Clone)]
@@ -1271,7 +1276,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
12711276
if fee.is_none() || msg.amount_msat < fee.unwrap() || (msg.amount_msat - fee.unwrap()) < *amt_to_forward { // fee_insufficient
12721277
break Some(("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, Some(self.get_channel_update(chan).unwrap())));
12731278
}
1274-
if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + CLTV_EXPIRY_DELTA as u64 { // incorrect_cltv_expiry
1279+
if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + chan.get_cltv_expiry_delta() as u64 { // incorrect_cltv_expiry
12751280
break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update(chan).unwrap())));
12761281
}
12771282
let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
@@ -1329,7 +1334,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13291334
short_channel_id,
13301335
timestamp: chan.get_update_time_counter(),
13311336
flags: (!were_node_one) as u8 | ((!chan.is_live() as u8) << 1),
1332-
cltv_expiry_delta: CLTV_EXPIRY_DELTA,
1337+
cltv_expiry_delta: chan.get_cltv_expiry_delta(),
13331338
htlc_minimum_msat: chan.get_counterparty_htlc_minimum_msat(),
13341339
htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()),
13351340
fee_base_msat: chan.get_holder_fee_base_msat(&self.fee_estimator),

lightning/src/util/config.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@ pub struct ChannelHandshakeConfig {
2323
///
2424
/// Default value: 6.
2525
pub minimum_depth: u32,
26-
/// Set to the amount of time we require our counterparty to wait to claim their money.
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
28+
/// transaction).
2729
///
28-
/// It's one of the main parameter of our security model. We (or one of our watchtowers) MUST
29-
/// be online to check for peer having broadcast a revoked transaction to steal our funds
30-
/// at least once every our_to_self_delay blocks.
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).
3134
///
3235
/// Meanwhile, asking for a too high delay, we bother peer to freeze funds for nothing in
3336
/// case of an honest unilateral channel close, which implicitly decrease the economic value of
3437
/// our channel.
3538
///
36-
/// Default value: [`BREAKDOWN_TIMEOUT`] (currently 144), we enforce it as a minimum at channel
37-
/// opening so you can tweak config to ask for more security, not less.
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.
3841
pub our_to_self_delay: u16,
3942
/// Set to the smallest value HTLC we will accept to process.
4043
///
@@ -161,6 +164,26 @@ pub struct ChannelConfig {
161164
///
162165
/// Default value: 0.
163166
pub fee_proportional_millionths: u32,
167+
/// The difference in the CLTV value between incoming HTLCs and an outbound HTLC forwarded over
168+
/// the channel this config applies to.
169+
///
170+
/// This is analogous to [`ChannelHandshakeConfig::our_to_self_delay`] but applies to in-flight
171+
/// HTLC balance when a channel appears on-chain whereas
172+
/// [`ChannelHandshakeConfig::our_to_self_delay`] applies to the remaining
173+
/// (non-HTLC-encumbered) balance.
174+
///
175+
/// Thus, for HTLC-encumbered balances to be enforced on-chain when a channel is force-closed,
176+
/// we (or one of our watchtowers) MUST be online to check for broadcast of the current
177+
/// commitment transaction at least once per this many blocks (minus some margin to allow us
178+
/// enough time to broadcast and confirm a transaction, possibly with time in between to RBF
179+
/// the spending transaction).
180+
///
181+
/// Default value: 72 (12 hours at an average of 6 blocks/hour).
182+
/// Minimum value: [`MIN_CLTV_EXPIRY_DELTA`], any values less than this will be treated as
183+
/// [`MIN_CLTV_EXPIRY_DELTA`] instead.
184+
///
185+
/// [`MIN_CLTV_EXPIRY_DELTA`]: crate::ln::channelmanager::MIN_CLTV_EXPIRY_DELTA
186+
pub cltv_expiry_delta: u16,
164187
/// Set to announce the channel publicly and notify all nodes that they can route via this
165188
/// channel.
166189
///
@@ -192,15 +215,17 @@ impl Default for ChannelConfig {
192215
fn default() -> Self {
193216
ChannelConfig {
194217
fee_proportional_millionths: 0,
218+
cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
195219
announced_channel: false,
196220
commit_upfront_shutdown_pubkey: true,
197221
}
198222
}
199223
}
200224

201225
//Add write and readable traits to channelconfig
202-
impl_writeable!(ChannelConfig, 8+1+1, {
226+
impl_writeable!(ChannelConfig, 8+2+1+1, {
203227
fee_proportional_millionths,
228+
cltv_expiry_delta,
204229
announced_channel,
205230
commit_upfront_shutdown_pubkey
206231
});

0 commit comments

Comments
 (0)