Skip to content

Commit dbd677b

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 0eca1e5 commit dbd677b

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
@@ -1951,6 +1951,7 @@ pub(super) struct FundingScope {
19511951
/// The hash of the block in which the funding transaction was included.
19521952
funding_tx_confirmed_in: Option<BlockHash>,
19531953
funding_tx_confirmation_height: u32,
1954+
short_channel_id: Option<u64>,
19541955
}
19551956

19561957
impl Writeable for FundingScope {
@@ -1963,6 +1964,7 @@ impl Writeable for FundingScope {
19631964
(9, self.funding_transaction, option),
19641965
(11, self.funding_tx_confirmed_in, option),
19651966
(13, self.funding_tx_confirmation_height, required),
1967+
(15, self.short_channel_id, option),
19661968
});
19671969
Ok(())
19681970
}
@@ -1978,6 +1980,7 @@ impl Readable for FundingScope {
19781980
let mut funding_transaction = None;
19791981
let mut funding_tx_confirmed_in = None;
19801982
let mut funding_tx_confirmation_height = RequiredWrapper(None);
1983+
let mut short_channel_id = None;
19811984

19821985
read_tlv_fields!(reader, {
19831986
(1, value_to_self_msat, required),
@@ -1987,6 +1990,7 @@ impl Readable for FundingScope {
19871990
(9, funding_transaction, option),
19881991
(11, funding_tx_confirmed_in, option),
19891992
(13, funding_tx_confirmation_height, required),
1993+
(15, short_channel_id, option),
19901994
});
19911995

19921996
Ok(Self {
@@ -2001,6 +2005,7 @@ impl Readable for FundingScope {
20012005
funding_transaction,
20022006
funding_tx_confirmed_in,
20032007
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
2008+
short_channel_id,
20042009
#[cfg(any(test, fuzzing))]
20052010
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
20062011
#[cfg(any(test, fuzzing))]
@@ -2097,6 +2102,13 @@ impl FundingScope {
20972102

20982103
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
20992104
}
2105+
2106+
/// Gets the channel's `short_channel_id`.
2107+
///
2108+
/// Will return `None` if the funding hasn't been confirmed yet.
2109+
pub fn get_short_channel_id(&self) -> Option<u64> {
2110+
self.short_channel_id
2111+
}
21002112
}
21012113

21022114
/// Info about a pending splice, used in the pre-splice channel
@@ -2258,7 +2270,6 @@ where
22582270
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
22592271
expecting_peer_commitment_signed: bool,
22602272

2261-
short_channel_id: Option<u64>,
22622273
/// Either the height at which this channel was created or the height at which it was last
22632274
/// serialized if it was serialized by versions prior to 0.0.103.
22642275
/// We use this to close if funding is never broadcasted.
@@ -3102,6 +3113,7 @@ where
31023113
funding_transaction: None,
31033114
funding_tx_confirmed_in: None,
31043115
funding_tx_confirmation_height: 0,
3116+
short_channel_id: None,
31053117
};
31063118
let channel_context = ChannelContext {
31073119
user_id,
@@ -3165,7 +3177,6 @@ where
31653177
closing_fee_limits: None,
31663178
target_closing_feerate_sats_per_kw: None,
31673179

3168-
short_channel_id: None,
31693180
channel_creation_height: current_chain_height,
31703181

31713182
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3343,6 +3354,7 @@ where
33433354
funding_transaction: None,
33443355
funding_tx_confirmed_in: None,
33453356
funding_tx_confirmation_height: 0,
3357+
short_channel_id: None,
33463358
};
33473359
let channel_context = Self {
33483360
user_id,
@@ -3404,7 +3416,6 @@ where
34043416
closing_fee_limits: None,
34053417
target_closing_feerate_sats_per_kw: None,
34063418

3407-
short_channel_id: None,
34083419
channel_creation_height: current_chain_height,
34093420

34103421
feerate_per_kw: commitment_feerate,
@@ -3629,13 +3640,6 @@ where
36293640
self.user_id
36303641
}
36313642

3632-
/// Gets the channel's `short_channel_id`.
3633-
///
3634-
/// Will return `None` if the channel hasn't been confirmed yet.
3635-
pub fn get_short_channel_id(&self) -> Option<u64> {
3636-
self.short_channel_id
3637-
}
3638-
36393643
/// Allowed in any state (including after shutdown)
36403644
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
36413645
self.latest_inbound_scid_alias
@@ -6168,7 +6172,7 @@ where
61686172
}
61696173

61706174
if let Some(scid_alias) = msg.short_channel_id_alias {
6171-
if Some(scid_alias) != self.context.short_channel_id {
6175+
if Some(scid_alias) != self.funding.short_channel_id {
61726176
// The scid alias provided can be used to route payments *from* our counterparty,
61736177
// i.e. can be used for inbound payments and provided in invoices, but is not used
61746178
// when routing outbound payments.
@@ -8860,7 +8864,7 @@ where
88608864

88618865
self.funding.funding_tx_confirmation_height = height;
88628866
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8863-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8867+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
88648868
Ok(scid) => Some(scid),
88658869
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
88668870
}
@@ -9044,7 +9048,7 @@ where
90449048
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
90459049
}
90469050

9047-
let short_channel_id = self.context.get_short_channel_id()
9051+
let short_channel_id = self.funding.get_short_channel_id()
90489052
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
90499053
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
90509054
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -9117,7 +9121,7 @@ where
91179121
},
91189122
Ok(v) => v
91199123
};
9120-
let short_channel_id = match self.context.get_short_channel_id() {
9124+
let short_channel_id = match self.funding.get_short_channel_id() {
91219125
Some(scid) => scid,
91229126
None => return None,
91239127
};
@@ -11459,7 +11463,7 @@ where
1145911463

1146011464
self.funding.funding_tx_confirmed_in.write(writer)?;
1146111465
self.funding.funding_tx_confirmation_height.write(writer)?;
11462-
self.context.short_channel_id.write(writer)?;
11466+
self.funding.short_channel_id.write(writer)?;
1146311467

1146411468
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1146511469
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -12118,6 +12122,7 @@ where
1211812122
funding_transaction,
1211912123
funding_tx_confirmed_in,
1212012124
funding_tx_confirmation_height,
12125+
short_channel_id,
1212112126
},
1212212127
pending_funding: pending_funding.unwrap(),
1212312128
context: ChannelContext {
@@ -12181,7 +12186,6 @@ where
1218112186
closing_fee_limits: None,
1218212187
target_closing_feerate_sats_per_kw,
1218312188

12184-
short_channel_id,
1218512189
channel_creation_height,
1218612190

1218712191
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
@@ -3168,7 +3168,7 @@ macro_rules! locked_close_channel {
31683168
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
31693169
}
31703170
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3171-
if let Some(short_id) = $channel_context.get_short_channel_id() {
3171+
if let Some(short_id) = $channel_funding.get_short_channel_id() {
31723172
short_to_chan_info.remove(&short_id);
31733173
} else {
31743174
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3291,7 +3291,7 @@ macro_rules! send_channel_ready {
32913291
let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
32923292
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
32933293
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3294-
if let Some(real_scid) = $channel.context.get_short_channel_id() {
3294+
if let Some(real_scid) = $channel.funding.get_short_channel_id() {
32953295
let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
32963296
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
32973297
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
@@ -4768,7 +4768,7 @@ where
47684768
action: msgs::ErrorAction::IgnoreError
47694769
});
47704770
}
4771-
if chan.context.get_short_channel_id().is_none() {
4771+
if chan.funding.get_short_channel_id().is_none() {
47724772
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
47734773
}
47744774
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
@@ -4791,7 +4791,7 @@ where
47914791
fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
47924792
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
47934793
log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
4794-
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
4794+
let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
47954795
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
47964796
Some(id) => id,
47974797
};
@@ -5939,7 +5939,7 @@ where
59395939
err: format!("Channel with id {} not fully established", next_hop_channel_id)
59405940
})
59415941
}
5942-
funded_chan.context.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
5942+
funded_chan.funding.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
59435943
} else {
59445944
return Err(APIError::ChannelUnavailable {
59455945
err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
@@ -6388,7 +6388,7 @@ where
63886388
};
63896389

63906390
let logger = WithChannelContext::from(&self.logger, &optimal_channel.context, Some(payment_hash));
6391-
let channel_description = if optimal_channel.context.get_short_channel_id() == Some(short_chan_id) {
6391+
let channel_description = if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
63926392
"specified"
63936393
} else {
63946394
"alternate"
@@ -7974,7 +7974,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79747974
);
79757975

79767976
let counterparty_node_id = channel.context.get_counterparty_node_id();
7977-
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
7977+
let short_channel_id = channel.funding.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
79787978

79797979
let mut htlc_forwards = None;
79807980
if !pending_forwards.is_empty() {
@@ -11170,7 +11170,7 @@ where
1117011170
.iter()
1117111171
.filter(|(_, channel)| channel.context().is_usable())
1117211172
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
11173-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
11173+
.and_then(|(_, channel)| channel.funding().get_short_channel_id()),
1117411174
})
1117511175
.collect::<Vec<_>>()
1117611176
}
@@ -12093,7 +12093,7 @@ where
1209312093
});
1209412094
}
1209512095
if funded_channel.is_our_channel_ready() {
12096-
if let Some(real_scid) = funded_channel.context.get_short_channel_id() {
12096+
if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
1209712097
// If we sent a 0conf channel_ready, and now have an SCID, we add it
1209812098
// to the short_to_chan_info map here. Note that we check whether we
1209912099
// can relay using the real SCID at relay-time (i.e.
@@ -14268,7 +14268,7 @@ where
1426814268
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1426914269
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
1427014270
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
14271-
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
14271+
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1427214272
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1427314273
}
1427414274
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)