Skip to content

Commit fc88b9f

Browse files
committed
Move ChannelContext::short_channel_id to FundingScope
When processing confirmed transactions, if the funding transaction is found then information about it in the ChannelContext is updated. In preparation for splicing, move this data to FundingScope.
1 parent 3969264 commit fc88b9f

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,7 @@ pub(super) struct FundingScope {
18191819
/// The hash of the block in which the funding transaction was included.
18201820
funding_tx_confirmed_in: Option<BlockHash>,
18211821
funding_tx_confirmation_height: u32,
1822+
short_channel_id: Option<u64>,
18221823
}
18231824

18241825
impl Writeable for FundingScope {
@@ -1831,6 +1832,7 @@ impl Writeable for FundingScope {
18311832
(9, self.funding_transaction, option),
18321833
(11, self.funding_tx_confirmed_in, option),
18331834
(13, self.funding_tx_confirmation_height, required),
1835+
(15, self.short_channel_id, option),
18341836
});
18351837
Ok(())
18361838
}
@@ -1845,6 +1847,7 @@ impl Readable for FundingScope {
18451847
let mut funding_transaction = None;
18461848
let mut funding_tx_confirmed_in = None;
18471849
let mut funding_tx_confirmation_height = RequiredWrapper(None);
1850+
let mut short_channel_id = None;
18481851

18491852
read_tlv_fields!(reader, {
18501853
(1, value_to_self_msat, required),
@@ -1854,6 +1857,7 @@ impl Readable for FundingScope {
18541857
(9, funding_transaction, option),
18551858
(11, funding_tx_confirmed_in, option),
18561859
(13, funding_tx_confirmation_height, required),
1860+
(15, short_channel_id, option),
18571861
});
18581862

18591863
Ok(Self {
@@ -1868,6 +1872,7 @@ impl Readable for FundingScope {
18681872
funding_transaction,
18691873
funding_tx_confirmed_in,
18701874
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
1875+
short_channel_id,
18711876
#[cfg(any(test, fuzzing))]
18721877
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
18731878
#[cfg(any(test, fuzzing))]
@@ -1961,6 +1966,13 @@ impl FundingScope {
19611966

19621967
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
19631968
}
1969+
1970+
/// Gets the channel's `short_channel_id`.
1971+
///
1972+
/// Will return `None` if the funding hasn't been confirmed yet.
1973+
pub fn get_short_channel_id(&self) -> Option<u64> {
1974+
self.short_channel_id
1975+
}
19641976
}
19651977

19661978
/// Info about a pending splice, used in the pre-splice channel
@@ -2120,7 +2132,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
21202132
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
21212133
expecting_peer_commitment_signed: bool,
21222134

2123-
short_channel_id: Option<u64>,
21242135
/// Either the height at which this channel was created or the height at which it was last
21252136
/// serialized if it was serialized by versions prior to 0.0.103.
21262137
/// We use this to close if funding is never broadcasted.
@@ -2935,6 +2946,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29352946
funding_transaction: None,
29362947
funding_tx_confirmed_in: None,
29372948
funding_tx_confirmation_height: 0,
2949+
short_channel_id: None,
29382950
};
29392951
let channel_context = ChannelContext {
29402952
user_id,
@@ -2998,7 +3010,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29983010
closing_fee_limits: None,
29993011
target_closing_feerate_sats_per_kw: None,
30003012

3001-
short_channel_id: None,
30023013
channel_creation_height: current_chain_height,
30033014

30043015
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3169,6 +3180,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
31693180
funding_transaction: None,
31703181
funding_tx_confirmed_in: None,
31713182
funding_tx_confirmation_height: 0,
3183+
short_channel_id: None,
31723184
};
31733185
let channel_context = Self {
31743186
user_id,
@@ -3230,7 +3242,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32303242
closing_fee_limits: None,
32313243
target_closing_feerate_sats_per_kw: None,
32323244

3233-
short_channel_id: None,
32343245
channel_creation_height: current_chain_height,
32353246

32363247
feerate_per_kw: commitment_feerate,
@@ -3449,13 +3460,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34493460
self.user_id
34503461
}
34513462

3452-
/// Gets the channel's `short_channel_id`.
3453-
///
3454-
/// Will return `None` if the channel hasn't been confirmed yet.
3455-
pub fn get_short_channel_id(&self) -> Option<u64> {
3456-
self.short_channel_id
3457-
}
3458-
34593463
/// Allowed in any state (including after shutdown)
34603464
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
34613465
self.latest_inbound_scid_alias
@@ -5684,7 +5688,7 @@ impl<SP: Deref> FundedChannel<SP> where
56845688
}
56855689

56865690
if let Some(scid_alias) = msg.short_channel_id_alias {
5687-
if Some(scid_alias) != self.context.short_channel_id {
5691+
if Some(scid_alias) != self.funding.short_channel_id {
56885692
// The scid alias provided can be used to route payments *from* our counterparty,
56895693
// i.e. can be used for inbound payments and provided in invoices, but is not used
56905694
// when routing outbound payments.
@@ -8463,7 +8467,7 @@ impl<SP: Deref> FundedChannel<SP> where
84638467

84648468
self.funding.funding_tx_confirmation_height = height;
84658469
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8466-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8470+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
84678471
Ok(scid) => Some(scid),
84688472
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
84698473
}
@@ -8643,7 +8647,7 @@ impl<SP: Deref> FundedChannel<SP> where
86438647
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
86448648
}
86458649

8646-
let short_channel_id = self.context.get_short_channel_id()
8650+
let short_channel_id = self.funding.get_short_channel_id()
86478651
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
86488652
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
86498653
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -8715,7 +8719,7 @@ impl<SP: Deref> FundedChannel<SP> where
87158719
},
87168720
Ok(v) => v
87178721
};
8718-
let short_channel_id = match self.context.get_short_channel_id() {
8722+
let short_channel_id = match self.funding.get_short_channel_id() {
87198723
Some(scid) => scid,
87208724
None => return None,
87218725
};
@@ -10982,7 +10986,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1098210986

1098310987
self.funding.funding_tx_confirmed_in.write(writer)?;
1098410988
self.funding.funding_tx_confirmation_height.write(writer)?;
10985-
self.context.short_channel_id.write(writer)?;
10989+
self.funding.short_channel_id.write(writer)?;
1098610990

1098710991
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1098810992
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -11639,6 +11643,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1163911643
funding_transaction,
1164011644
funding_tx_confirmed_in,
1164111645
funding_tx_confirmation_height,
11646+
short_channel_id,
1164211647
},
1164311648
pending_funding: pending_funding.unwrap(),
1164411649
context: ChannelContext {
@@ -11702,7 +11707,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1170211707
closing_fee_limits: None,
1170311708
target_closing_feerate_sats_per_kw,
1170411709

11705-
short_channel_id,
1170611710
channel_creation_height,
1170711711

1170811712
counterparty_dust_limit_satoshis,

lightning/src/ln/channel_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl ChannelDetails {
516516
} else {
517517
None
518518
},
519-
short_channel_id: context.get_short_channel_id(),
519+
short_channel_id: funding.get_short_channel_id(),
520520
outbound_scid_alias: if context.is_usable() {
521521
Some(context.outbound_scid_alias())
522522
} else {

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,7 +3057,7 @@ macro_rules! locked_close_channel {
30573057
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
30583058
}
30593059
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3060-
if let Some(short_id) = $channel_context.get_short_channel_id() {
3060+
if let Some(short_id) = $channel_funding.get_short_channel_id() {
30613061
short_to_chan_info.remove(&short_id);
30623062
} else {
30633063
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3173,7 +3173,7 @@ macro_rules! send_channel_ready {
31733173
let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
31743174
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
31753175
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3176-
if let Some(real_scid) = $channel.context.get_short_channel_id() {
3176+
if let Some(real_scid) = $channel.funding.get_short_channel_id() {
31773177
let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
31783178
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
31793179
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
@@ -4594,7 +4594,7 @@ where
45944594
action: msgs::ErrorAction::IgnoreError
45954595
});
45964596
}
4597-
if chan.context.get_short_channel_id().is_none() {
4597+
if chan.funding.get_short_channel_id().is_none() {
45984598
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
45994599
}
46004600
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
@@ -4616,7 +4616,7 @@ where
46164616
fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
46174617
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
46184618
log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
4619-
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
4619+
let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
46204620
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
46214621
Some(id) => id,
46224622
};
@@ -5680,7 +5680,7 @@ where
56805680
err: format!("Channel with id {} not fully established", next_hop_channel_id)
56815681
})
56825682
}
5683-
funded_chan.context.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
5683+
funded_chan.funding.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
56845684
} else {
56855685
return Err(APIError::ChannelUnavailable {
56865686
err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
@@ -6126,7 +6126,7 @@ where
61266126
};
61276127

61286128
let logger = WithChannelContext::from(&self.logger, &optimal_channel.context, Some(payment_hash));
6129-
let channel_description = if optimal_channel.context.get_short_channel_id() == Some(short_chan_id) {
6129+
let channel_description = if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
61306130
"specified"
61316131
} else {
61326132
"alternate"
@@ -7683,7 +7683,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
76837683
);
76847684

76857685
let counterparty_node_id = channel.context.get_counterparty_node_id();
7686-
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
7686+
let short_channel_id = channel.funding.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
76877687

76887688
let mut htlc_forwards = None;
76897689
if !pending_forwards.is_empty() {
@@ -10941,7 +10941,7 @@ where
1094110941
.iter()
1094210942
.filter(|(_, channel)| channel.context().is_usable())
1094310943
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10944-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10944+
.and_then(|(_, channel)| channel.funding().get_short_channel_id()),
1094510945
})
1094610946
.collect::<Vec<_>>();
1094710947

@@ -11815,7 +11815,7 @@ where
1181511815
});
1181611816
}
1181711817
if funded_channel.is_our_channel_ready() {
11818-
if let Some(real_scid) = funded_channel.context.get_short_channel_id() {
11818+
if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
1181911819
// If we sent a 0conf channel_ready, and now have an SCID, we add it
1182011820
// to the short_to_chan_info map here. Note that we check whether we
1182111821
// can relay using the real SCID at relay-time (i.e.
@@ -13924,7 +13924,7 @@ where
1392413924
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1392513925
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
1392613926
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
13927-
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
13927+
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1392813928
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1392913929
}
1393013930
per_peer_state.entry(channel.context.get_counterparty_node_id())

lightning/src/ln/payment_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ fn test_trivial_inflight_htlc_tracking() {
19271927
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
19281928
&NodeId::from_pubkey(&node_a_id),
19291929
&NodeId::from_pubkey(&node_b_id),
1930-
channel_1.context().get_short_channel_id().unwrap(),
1930+
channel_1.funding().get_short_channel_id().unwrap(),
19311931
);
19321932
assert_eq!(chan_1_used_liquidity, None);
19331933
}
@@ -1940,7 +1940,7 @@ fn test_trivial_inflight_htlc_tracking() {
19401940
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
19411941
&NodeId::from_pubkey(&node_b_id),
19421942
&NodeId::from_pubkey(&node_c_id),
1943-
channel_2.context().get_short_channel_id().unwrap(),
1943+
channel_2.funding().get_short_channel_id().unwrap(),
19441944
);
19451945

19461946
assert_eq!(chan_2_used_liquidity, None);
@@ -1968,7 +1968,7 @@ fn test_trivial_inflight_htlc_tracking() {
19681968
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
19691969
&NodeId::from_pubkey(&node_a_id),
19701970
&NodeId::from_pubkey(&node_b_id),
1971-
channel_1.context().get_short_channel_id().unwrap(),
1971+
channel_1.funding().get_short_channel_id().unwrap(),
19721972
);
19731973
// First hop accounts for expected 1000 msat fee
19741974
assert_eq!(chan_1_used_liquidity, Some(501000));
@@ -1982,7 +1982,7 @@ fn test_trivial_inflight_htlc_tracking() {
19821982
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
19831983
&NodeId::from_pubkey(&node_b_id),
19841984
&NodeId::from_pubkey(&node_c_id),
1985-
channel_2.context().get_short_channel_id().unwrap(),
1985+
channel_2.funding().get_short_channel_id().unwrap(),
19861986
);
19871987

19881988
assert_eq!(chan_2_used_liquidity, Some(500000));
@@ -2010,7 +2010,7 @@ fn test_trivial_inflight_htlc_tracking() {
20102010
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
20112011
&NodeId::from_pubkey(&node_a_id),
20122012
&NodeId::from_pubkey(&node_b_id),
2013-
channel_1.context().get_short_channel_id().unwrap(),
2013+
channel_1.funding().get_short_channel_id().unwrap(),
20142014
);
20152015
assert_eq!(chan_1_used_liquidity, None);
20162016
}
@@ -2023,7 +2023,7 @@ fn test_trivial_inflight_htlc_tracking() {
20232023
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
20242024
&NodeId::from_pubkey(&node_b_id),
20252025
&NodeId::from_pubkey(&node_c_id),
2026-
channel_2.context().get_short_channel_id().unwrap(),
2026+
channel_2.funding().get_short_channel_id().unwrap(),
20272027
);
20282028
assert_eq!(chan_2_used_liquidity, None);
20292029
}
@@ -2073,7 +2073,7 @@ fn test_holding_cell_inflight_htlcs() {
20732073
let used_liquidity = inflight_htlcs.used_liquidity_msat(
20742074
&NodeId::from_pubkey(&node_a_id),
20752075
&NodeId::from_pubkey(&node_b_id),
2076-
channel.context().get_short_channel_id().unwrap(),
2076+
channel.funding().get_short_channel_id().unwrap(),
20772077
);
20782078

20792079
assert_eq!(used_liquidity, Some(2000000));

0 commit comments

Comments
 (0)