Skip to content

Commit 73899ac

Browse files
author
Antoine Riard
committed
Document exactly our CLTV sanitization policy for final incoming HTLCs
We want to avoid a third-party channel closure, where a random node by sending us a payment expiring at current height, would trigger our onchain logic to close the channel due to a near-expiration. PaymentReceived and unknown HTLC cancellation must happen before LATENCY_GRACE_PERIOD_BLOCKS.
1 parent 795aff8 commit 73899ac

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,8 +1727,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
17271727
return Err(ChannelError::Close("Remote provided CLTV expiry in seconds instead of block height"));
17281728
}
17291729

1730-
//TODO: Check msg.cltv_expiry further? Do this in channel manager?
1731-
17321730
if self.channel_state & ChannelState::LocalShutdownSent as u32 != 0 {
17331731
if let PendingHTLCStatus::Forward(_) = pending_forward_state {
17341732
panic!("ChannelManager shouldn't be trying to add a forwardable HTLC after we've started closing");

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,11 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
10391039

10401040
// OUR PAYMENT!
10411041
// final_expiry_too_soon
1042-
if (msg.cltv_expiry as u64) < self.latest_block_height.load(Ordering::Acquire) as u64 + (CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
1042+
// We have to have some headroom to broadcast on chain if we have the preimage, so make sure we have at least
1043+
// HTLC_FAIL_BACK_BUFFER blocks to go.
1044+
// Also, ensure that, in the case of an unknown payment hash, our payment logic has enough time to fail the HTLC backward
1045+
// before our onchain logic triggers a channel closure (see HTLC_FAIL_BACK_BUFFER rational).
1046+
if (msg.cltv_expiry as u64) <= self.latest_block_height.load(Ordering::Acquire) as u64 + HTLC_FAIL_BACK_BUFFER as u64 + 1 {
10431047
return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
10441048
}
10451049
// final_incorrect_htlc_amount

lightning/src/util/events.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ pub enum Event {
5252
/// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
5353
/// ChannelManager::claim_funds to get it....
5454
/// Note that if the preimage is not known or the amount paid is incorrect, you must call
55-
/// ChannelManager::fail_htlc_backwards to free up resources for this HTLC.
55+
/// ChannelManager::fail_htlc_backwards to free up resources for this HTLC before
56+
/// LATENCY_GRACE_PERIOD_BLOCKS to avoid any channel-closure by our onchain monitoring due
57+
/// to expiration of an incoming HTLC for which we may know the preimage.
5658
/// The amount paid should be considered 'incorrect' when it is less than or more than twice
5759
/// the amount expected.
5860
/// If you fail to call either ChannelManager::claim_funds or

0 commit comments

Comments
 (0)