Skip to content

Commit 1d70ac2

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 688ac14 commit 1d70ac2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,10 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
22332233
// blinded paths instead of simple scid+node_id aliases.
22342234
outbound_scid_alias: u64,
22352235

2236+
/// Short channel ids used by any prior FundingScope. These are maintained such that
2237+
/// ChannelManager can look up the channel for any pending HTLCs.
2238+
pub historical_scids: Vec<u64>,
2239+
22362240
// We track whether we already emitted a `ChannelPending` event.
22372241
channel_pending_event_emitted: bool,
22382242

@@ -3045,6 +3049,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30453049

30463050
latest_inbound_scid_alias: None,
30473051
outbound_scid_alias: 0,
3052+
historical_scids: Vec::new(),
30483053

30493054
channel_pending_event_emitted: false,
30503055
funding_tx_broadcast_safe_event_emitted: false,
@@ -3281,6 +3286,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32813286

32823287
latest_inbound_scid_alias: None,
32833288
outbound_scid_alias,
3289+
historical_scids: Vec::new(),
32843290

32853291
channel_pending_event_emitted: false,
32863292
funding_tx_broadcast_safe_event_emitted: false,
@@ -5301,6 +5307,9 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
53015307
#[cfg(splicing)]
53025308
macro_rules! promote_splice_funding {
53035309
($self: expr, $funding: expr) => {
5310+
if let Some(scid) = $self.funding.short_channel_id {
5311+
$self.context.historical_scids.push(scid);
5312+
}
53045313
core::mem::swap(&mut $self.funding, $funding);
53055314
$self.pending_splice = None;
53065315
$self.pending_funding.clear();
@@ -11264,6 +11273,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1126411273
(54, self.pending_funding, optional_vec), // Added in 0.2
1126511274
(55, removed_htlc_failure_attribution_data, optional_vec), // Added in 0.2
1126611275
(57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
11276+
(58, self.context.historical_scids, optional_vec), // Added in 0.2
1126711277
});
1126811278

1126911279
Ok(())
@@ -11579,6 +11589,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1157911589
let mut is_manual_broadcast = None;
1158011590

1158111591
let mut pending_funding = Some(Vec::new());
11592+
let mut historical_scids = Some(Vec::new());
1158211593

1158311594
read_tlv_fields!(reader, {
1158411595
(0, announcement_sigs, option),
@@ -11618,6 +11629,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1161811629
(54, pending_funding, optional_vec), // Added in 0.2
1161911630
(55, removed_htlc_failure_attribution_data, optional_vec),
1162011631
(57, holding_cell_failure_attribution_data, optional_vec),
11632+
(58, historical_scids, optional_vec), // Added in 0.2
1162111633
});
1162211634

1162311635
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -11894,6 +11906,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1189411906
latest_inbound_scid_alias,
1189511907
// Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
1189611908
outbound_scid_alias,
11909+
historical_scids: historical_scids.unwrap(),
1189711910

1189811911
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
1189911912
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8282
use crate::sign::ecdsa::EcdsaChannelSigner;
8383
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelConfigOverrides, UserConfig};
8484
use crate::util::wakers::{Future, Notifier};
85-
use crate::util::scid_utils::fake_scid;
85+
use crate::util::scid_utils::{block_from_scid, fake_scid};
8686
use crate::util::string::UntrustedString;
8787
use crate::util::ser::{BigSize, FixedLengthReader, LengthReadable, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
8888
use crate::util::logger::{Level, Logger, WithContext};
@@ -3068,6 +3068,9 @@ macro_rules! locked_close_channel {
30683068
debug_assert!(alias_removed);
30693069
}
30703070
short_to_chan_info.remove(&$channel_context.outbound_scid_alias());
3071+
for scid in &$channel_context.historical_scids {
3072+
short_to_chan_info.remove(scid);
3073+
}
30713074
}}
30723075
}
30733076

@@ -11686,6 +11689,17 @@ where
1168611689
channel.check_for_stale_feerate(&logger, feerate)?;
1168711690
}
1168811691
}
11692+
11693+
// Remove any scids used by old splice funding transactions
11694+
let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
11695+
channel.context.historical_scids.retain(|scid| {
11696+
let retain_scid = block_from_scid(*scid) + 12 > height;
11697+
if !retain_scid {
11698+
short_to_chan_info.remove(scid);
11699+
}
11700+
retain_scid
11701+
});
11702+
1168911703
channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None))
1169011704
});
1169111705

@@ -13976,6 +13990,11 @@ where
1397613990
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1397713991
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1397813992
}
13993+
13994+
for short_channel_id in &channel.context.historical_scids {
13995+
short_to_chan_info.insert(*short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
13996+
}
13997+
1397913998
per_peer_state.entry(channel.context.get_counterparty_node_id())
1398013999
.or_insert_with(|| Mutex::new(empty_peer_state()))
1398114000
.get_mut().unwrap()

0 commit comments

Comments
 (0)