Skip to content

Commit b65c073

Browse files
committed
Track historical SCIDs from previous funding
When a splice is locked, the SCID from the previous funding transaction needs to be remembered so that pending HTLCs can be handled properly. Additionally, when they need to be cleaned up once they should no longer be used. Track these SCIDs as splices are locked and clean any up as blocks are connected.
1 parent 98f2ea6 commit b65c073

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,10 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
22672267
// blinded paths instead of simple scid+node_id aliases.
22682268
outbound_scid_alias: u64,
22692269

2270+
/// Short channel ids used by any prior FundingScope. These are maintained such that
2271+
/// ChannelManager can look up the channel for any pending HTLCs.
2272+
pub historical_scids: Vec<u64>,
2273+
22702274
// We track whether we already emitted a `ChannelPending` event.
22712275
channel_pending_event_emitted: bool,
22722276

@@ -3083,6 +3087,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30833087

30843088
latest_inbound_scid_alias: None,
30853089
outbound_scid_alias: 0,
3090+
historical_scids: Vec::new(),
30863091

30873092
channel_pending_event_emitted: false,
30883093
funding_tx_broadcast_safe_event_emitted: false,
@@ -3317,6 +3322,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
33173322

33183323
latest_inbound_scid_alias: None,
33193324
outbound_scid_alias,
3325+
historical_scids: Vec::new(),
33203326

33213327
channel_pending_event_emitted: false,
33223328
funding_tx_broadcast_safe_event_emitted: false,
@@ -5338,6 +5344,9 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
53385344
#[cfg(splicing)]
53395345
macro_rules! promote_splice_funding {
53405346
($self: expr, $funding: expr) => {
5347+
if let Some(scid) = $self.funding.short_channel_id {
5348+
$self.context.historical_scids.push(scid);
5349+
}
53415350
core::mem::swap(&mut $self.funding, $funding);
53425351
$self.pending_splice = None;
53435352
$self.pending_funding.clear();
@@ -11448,7 +11457,8 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1144811457
(54, self.pending_funding, optional_vec), // Added in 0.2
1144911458
(55, removed_htlc_failure_attribution_data, optional_vec), // Added in 0.2
1145011459
(57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
11451-
(58, self.interactive_tx_signing_session, option) // Added in 0.2
11460+
(58, self.interactive_tx_signing_session, option), // Added in 0.2
11461+
(60, self.context.historical_scids, optional_vec), // Added in 0.2
1145211462
});
1145311463

1145411464
Ok(())
@@ -11764,6 +11774,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1176411774
let mut is_manual_broadcast = None;
1176511775

1176611776
let mut pending_funding = Some(Vec::new());
11777+
let mut historical_scids = Some(Vec::new());
1176711778

1176811779
let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
1176911780

@@ -11806,6 +11817,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1180611817
(55, removed_htlc_failure_attribution_data, optional_vec),
1180711818
(57, holding_cell_failure_attribution_data, optional_vec),
1180811819
(58, interactive_tx_signing_session, option), // Added in 0.2
11820+
(60, historical_scids, optional_vec), // Added in 0.2
1180911821
});
1181011822

1181111823
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -12078,6 +12090,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1207812090
latest_inbound_scid_alias,
1207912091
// Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
1208012092
outbound_scid_alias,
12093+
historical_scids: historical_scids.unwrap(),
1208112094

1208212095
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
1208312096
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8484
use crate::sign::ecdsa::EcdsaChannelSigner;
8585
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelConfigOverrides, UserConfig};
8686
use crate::util::wakers::{Future, Notifier};
87-
use crate::util::scid_utils::fake_scid;
87+
use crate::util::scid_utils::{block_from_scid, fake_scid};
8888
use crate::util::string::UntrustedString;
8989
use crate::util::ser::{BigSize, FixedLengthReader, LengthReadable, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
9090
use crate::util::logger::{Level, Logger, WithContext};
@@ -2912,6 +2912,11 @@ const MAX_NO_CHANNEL_PEERS: usize = 250;
29122912
/// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
29132913
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
29142914

2915+
/// The number of blocks to wait for a channel_announcement to propagate such that payments using an
2916+
/// older SCID can still be relayed. Once the spend of the previous funding transaction has reached
2917+
/// this number of confirmations, the corresponding SCID will be forgotten.
2918+
const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 12;
2919+
29152920
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
29162921
/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
29172922
#[derive(Debug, PartialEq)]
@@ -3070,6 +3075,9 @@ macro_rules! locked_close_channel {
30703075
debug_assert!(alias_removed);
30713076
}
30723077
short_to_chan_info.remove(&$channel_context.outbound_scid_alias());
3078+
for scid in &$channel_context.historical_scids {
3079+
short_to_chan_info.remove(scid);
3080+
}
30733081
}}
30743082
}
30753083

@@ -11696,6 +11704,29 @@ where
1169611704
channel.check_for_stale_feerate(&logger, feerate)?;
1169711705
}
1169811706
}
11707+
11708+
// Remove any scids used by older funding transactions
11709+
if let Some(current_scid) = channel.funding.get_short_channel_id() {
11710+
let historical_scids = &mut channel.context.historical_scids;
11711+
if !historical_scids.is_empty() {
11712+
let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
11713+
11714+
// Remove an older SCID if the next funding has enough confirmations
11715+
for (scid, next_scid) in historical_scids
11716+
.iter()
11717+
.zip(historical_scids.iter().skip(1).chain(core::iter::once(&current_scid)))
11718+
{
11719+
let funding_height = block_from_scid(*next_scid);
11720+
let retain_scid = funding_height + CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY > height;
11721+
if !retain_scid {
11722+
short_to_chan_info.remove(scid);
11723+
}
11724+
}
11725+
11726+
historical_scids.retain(|scid| short_to_chan_info.contains_key(scid));
11727+
}
11728+
}
11729+
1169911730
channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None))
1170011731
});
1170111732

@@ -13994,6 +14025,11 @@ where
1399414025
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1399514026
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1399614027
}
14028+
14029+
for short_channel_id in &channel.context.historical_scids {
14030+
short_to_chan_info.insert(*short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
14031+
}
14032+
1399714033
per_peer_state.entry(channel.context.get_counterparty_node_id())
1399814034
.or_insert_with(|| Mutex::new(empty_peer_state()))
1399914035
.get_mut().unwrap()

0 commit comments

Comments
 (0)