Skip to content

Commit 75ecaeb

Browse files
committed
Correct spliced-stale SCID expiry for upgrades from pre-0.2 HTLC
If an HTLC was forwarded in 0.1, but waiting to be failed back, it will ultimately be failed by adding it to the `ChannelManager::pending_forwards` map with the channel's original SCID. If that channel is spliced between when the HTLC was forwarded (on 0.1) and when the HTLC is failed back (on 0.2), that SCID may no longer exist, causing the HTLC fail-back to be lost. Luckily, delaying when an SCID is expired is cheap - its just storing an extra `u64` or two and generating one requires an on-chain splice, so we simply delay removal of SCIDs for two months at which point any incoming HTLCs should have been expired for six weeks and the counterparty should have force-closed anyway.
1 parent 6b4a240 commit 75ecaeb

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,27 @@ pub(crate) const COINBASE_MATURITY: u32 = 100;
13731373
/// The number of blocks to wait for a channel_announcement to propagate such that payments using an
13741374
/// older SCID can still be relayed. Once the spend of the previous funding transaction has reached
13751375
/// this number of confirmations, the corresponding SCID will be forgotten.
1376+
///
1377+
/// Because HTLCs added prior to 0.1 which were waiting to be failed may reference a channel's
1378+
/// pre-splice SCID, we need to ensure this is at least the maximum number of blocks before an HTLC
1379+
/// gets failed-back due to a time-out. Luckily, in LDK prior to 0.2, this is enforced directly
1380+
/// when checking the incoming HTLC, and compared against `CLTV_FAR_FAR_AWAY` (which prior to LDK
1381+
/// 0.2, and still at the time of writing, is 14 * 24 * 6, i.e. two weeks).
1382+
///
1383+
/// Here we use four times that value to give us more time to fail an HTLC back (which does require
1384+
/// the user call [`ChannelManager::process_pending_htlc_forwards`]) just in case (if an HTLC has
1385+
/// been expired for 3 * 2 weeks our counterparty really should have closed the channel by now).
1386+
/// Holding on to stale SCIDs doesn't really cost us much as each one costs an on-chain splice to
1387+
/// generate anyway, so we might as well make this nearly arbitrarily long.
1388+
///
1389+
/// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
1390+
#[cfg(not(test))]
1391+
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 14 * 24 * 6 * 4;
1392+
1393+
/// In test (not `_test_utils`, though, since that tests actual upgrading), we deliberately break
1394+
/// the above condition so that we can ensure that HTLCs forwarded in 0.2 or later are handled
1395+
/// correctly even if this constant is reduced and an HTLC can outlive the original channel's SCID.
1396+
#[cfg(test)]
13761397
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 144;
13771398

13781399
struct PendingChannelMonitorUpdate {
@@ -11960,8 +11981,9 @@ where
1196011981
debug_assert!(false);
1196111982
self.funding.get_holder_pubkeys().funding_pubkey
1196211983
},
11963-
(Some(prev_funding_txid), ChannelSignerType::Ecdsa(ecdsa)) =>
11964-
ecdsa.new_funding_pubkey(prev_funding_txid, &self.context.secp_ctx),
11984+
(Some(prev_funding_txid), ChannelSignerType::Ecdsa(ecdsa)) => {
11985+
ecdsa.new_funding_pubkey(prev_funding_txid, &self.context.secp_ctx)
11986+
},
1196511987
#[cfg(taproot)]
1196611988
_ => todo!(),
1196711989
};
@@ -12064,8 +12086,9 @@ where
1206412086
debug_assert!(false);
1206512087
self.funding.get_holder_pubkeys().funding_pubkey
1206612088
},
12067-
(Some(prev_funding_txid), ChannelSignerType::Ecdsa(ecdsa)) =>
12068-
ecdsa.new_funding_pubkey(prev_funding_txid, &self.context.secp_ctx),
12089+
(Some(prev_funding_txid), ChannelSignerType::Ecdsa(ecdsa)) => {
12090+
ecdsa.new_funding_pubkey(prev_funding_txid, &self.context.secp_ctx)
12091+
},
1206912092
#[cfg(taproot)]
1207012093
_ => todo!(),
1207112094
};

0 commit comments

Comments
 (0)