Skip to content

Commit 3c53384

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 d4ad337 commit 3c53384

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
@@ -1824,6 +1824,7 @@ pub(super) struct FundingScope {
18241824
/// The hash of the block in which the funding transaction was included.
18251825
funding_tx_confirmed_in: Option<BlockHash>,
18261826
funding_tx_confirmation_height: u32,
1827+
short_channel_id: Option<u64>,
18271828
}
18281829

18291830
impl Writeable for FundingScope {
@@ -1836,6 +1837,7 @@ impl Writeable for FundingScope {
18361837
(9, self.funding_transaction, option),
18371838
(11, self.funding_tx_confirmed_in, option),
18381839
(13, self.funding_tx_confirmation_height, required),
1840+
(15, self.short_channel_id, option),
18391841
});
18401842
Ok(())
18411843
}
@@ -1850,6 +1852,7 @@ impl Readable for FundingScope {
18501852
let mut funding_transaction = None;
18511853
let mut funding_tx_confirmed_in = None;
18521854
let mut funding_tx_confirmation_height = RequiredWrapper(None);
1855+
let mut short_channel_id = None;
18531856

18541857
read_tlv_fields!(reader, {
18551858
(1, value_to_self_msat, required),
@@ -1859,6 +1862,7 @@ impl Readable for FundingScope {
18591862
(9, funding_transaction, option),
18601863
(11, funding_tx_confirmed_in, option),
18611864
(13, funding_tx_confirmation_height, required),
1865+
(15, short_channel_id, option),
18621866
});
18631867

18641868
Ok(Self {
@@ -1873,6 +1877,7 @@ impl Readable for FundingScope {
18731877
funding_transaction,
18741878
funding_tx_confirmed_in,
18751879
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
1880+
short_channel_id,
18761881
#[cfg(any(test, fuzzing))]
18771882
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
18781883
#[cfg(any(test, fuzzing))]
@@ -1966,6 +1971,13 @@ impl FundingScope {
19661971

19671972
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
19681973
}
1974+
1975+
/// Gets the channel's `short_channel_id`.
1976+
///
1977+
/// Will return `None` if the funding hasn't been confirmed yet.
1978+
pub fn get_short_channel_id(&self) -> Option<u64> {
1979+
self.short_channel_id
1980+
}
19691981
}
19701982

19711983
/// Info about a pending splice, used in the pre-splice channel
@@ -2125,7 +2137,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
21252137
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
21262138
expecting_peer_commitment_signed: bool,
21272139

2128-
short_channel_id: Option<u64>,
21292140
/// Either the height at which this channel was created or the height at which it was last
21302141
/// serialized if it was serialized by versions prior to 0.0.103.
21312142
/// We use this to close if funding is never broadcasted.
@@ -2940,6 +2951,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29402951
funding_transaction: None,
29412952
funding_tx_confirmed_in: None,
29422953
funding_tx_confirmation_height: 0,
2954+
short_channel_id: None,
29432955
};
29442956
let channel_context = ChannelContext {
29452957
user_id,
@@ -3003,7 +3015,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30033015
closing_fee_limits: None,
30043016
target_closing_feerate_sats_per_kw: None,
30053017

3006-
short_channel_id: None,
30073018
channel_creation_height: current_chain_height,
30083019

30093020
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3180,6 +3191,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
31803191
funding_transaction: None,
31813192
funding_tx_confirmed_in: None,
31823193
funding_tx_confirmation_height: 0,
3194+
short_channel_id: None,
31833195
};
31843196
let channel_context = Self {
31853197
user_id,
@@ -3241,7 +3253,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32413253
closing_fee_limits: None,
32423254
target_closing_feerate_sats_per_kw: None,
32433255

3244-
short_channel_id: None,
32453256
channel_creation_height: current_chain_height,
32463257

32473258
feerate_per_kw: commitment_feerate,
@@ -3460,13 +3471,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34603471
self.user_id
34613472
}
34623473

3463-
/// Gets the channel's `short_channel_id`.
3464-
///
3465-
/// Will return `None` if the channel hasn't been confirmed yet.
3466-
pub fn get_short_channel_id(&self) -> Option<u64> {
3467-
self.short_channel_id
3468-
}
3469-
34703474
/// Allowed in any state (including after shutdown)
34713475
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
34723476
self.latest_inbound_scid_alias
@@ -5941,7 +5945,7 @@ impl<SP: Deref> FundedChannel<SP> where
59415945
}
59425946

59435947
if let Some(scid_alias) = msg.short_channel_id_alias {
5944-
if Some(scid_alias) != self.context.short_channel_id {
5948+
if Some(scid_alias) != self.funding.short_channel_id {
59455949
// The scid alias provided can be used to route payments *from* our counterparty,
59465950
// i.e. can be used for inbound payments and provided in invoices, but is not used
59475951
// when routing outbound payments.
@@ -8582,7 +8586,7 @@ impl<SP: Deref> FundedChannel<SP> where
85828586

85838587
self.funding.funding_tx_confirmation_height = height;
85848588
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8585-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8589+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
85868590
Ok(scid) => Some(scid),
85878591
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
85888592
}
@@ -8762,7 +8766,7 @@ impl<SP: Deref> FundedChannel<SP> where
87628766
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
87638767
}
87648768

8765-
let short_channel_id = self.context.get_short_channel_id()
8769+
let short_channel_id = self.funding.get_short_channel_id()
87668770
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
87678771
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
87688772
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -8834,7 +8838,7 @@ impl<SP: Deref> FundedChannel<SP> where
88348838
},
88358839
Ok(v) => v
88368840
};
8837-
let short_channel_id = match self.context.get_short_channel_id() {
8841+
let short_channel_id = match self.funding.get_short_channel_id() {
88388842
Some(scid) => scid,
88398843
None => return None,
88408844
};
@@ -11109,7 +11113,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1110911113

1111011114
self.funding.funding_tx_confirmed_in.write(writer)?;
1111111115
self.funding.funding_tx_confirmation_height.write(writer)?;
11112-
self.context.short_channel_id.write(writer)?;
11116+
self.funding.short_channel_id.write(writer)?;
1111311117

1111411118
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1111511119
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -11766,6 +11770,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1176611770
funding_transaction,
1176711771
funding_tx_confirmed_in,
1176811772
funding_tx_confirmation_height,
11773+
short_channel_id,
1176911774
},
1177011775
pending_funding: pending_funding.unwrap(),
1177111776
context: ChannelContext {
@@ -11829,7 +11834,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1182911834
closing_fee_limits: None,
1183011835
target_closing_feerate_sats_per_kw,
1183111836

11832-
short_channel_id,
1183311837
channel_creation_height,
1183411838

1183511839
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
@@ -3159,7 +3159,7 @@ macro_rules! locked_close_channel {
31593159
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
31603160
}
31613161
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3162-
if let Some(short_id) = $channel_context.get_short_channel_id() {
3162+
if let Some(short_id) = $channel_funding.get_short_channel_id() {
31633163
short_to_chan_info.remove(&short_id);
31643164
} else {
31653165
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3282,7 +3282,7 @@ macro_rules! send_channel_ready {
32823282
let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
32833283
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
32843284
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3285-
if let Some(real_scid) = $channel.context.get_short_channel_id() {
3285+
if let Some(real_scid) = $channel.funding.get_short_channel_id() {
32863286
let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
32873287
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
32883288
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
@@ -4759,7 +4759,7 @@ where
47594759
action: msgs::ErrorAction::IgnoreError
47604760
});
47614761
}
4762-
if chan.context.get_short_channel_id().is_none() {
4762+
if chan.funding.get_short_channel_id().is_none() {
47634763
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
47644764
}
47654765
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
@@ -4782,7 +4782,7 @@ where
47824782
fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
47834783
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
47844784
log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
4785-
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
4785+
let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
47864786
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
47874787
Some(id) => id,
47884788
};
@@ -5930,7 +5930,7 @@ where
59305930
err: format!("Channel with id {} not fully established", next_hop_channel_id)
59315931
})
59325932
}
5933-
funded_chan.context.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
5933+
funded_chan.funding.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
59345934
} else {
59355935
return Err(APIError::ChannelUnavailable {
59365936
err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
@@ -6379,7 +6379,7 @@ where
63796379
};
63806380

63816381
let logger = WithChannelContext::from(&self.logger, &optimal_channel.context, Some(payment_hash));
6382-
let channel_description = if optimal_channel.context.get_short_channel_id() == Some(short_chan_id) {
6382+
let channel_description = if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
63836383
"specified"
63846384
} else {
63856385
"alternate"
@@ -7965,7 +7965,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79657965
);
79667966

79677967
let counterparty_node_id = channel.context.get_counterparty_node_id();
7968-
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
7968+
let short_channel_id = channel.funding.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
79697969

79707970
let mut htlc_forwards = None;
79717971
if !pending_forwards.is_empty() {
@@ -11161,7 +11161,7 @@ where
1116111161
.iter()
1116211162
.filter(|(_, channel)| channel.context().is_usable())
1116311163
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
11164-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
11164+
.and_then(|(_, channel)| channel.funding().get_short_channel_id()),
1116511165
})
1116611166
.collect::<Vec<_>>()
1116711167
}
@@ -12084,7 +12084,7 @@ where
1208412084
});
1208512085
}
1208612086
if funded_channel.is_our_channel_ready() {
12087-
if let Some(real_scid) = funded_channel.context.get_short_channel_id() {
12087+
if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
1208812088
// If we sent a 0conf channel_ready, and now have an SCID, we add it
1208912089
// to the short_to_chan_info map here. Note that we check whether we
1209012090
// can relay using the real SCID at relay-time (i.e.
@@ -14259,7 +14259,7 @@ where
1425914259
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1426014260
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
1426114261
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
14262-
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
14262+
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1426314263
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1426414264
}
1426514265
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)