Skip to content

Commit fa5b252

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 d80770b commit fa5b252

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
@@ -1963,6 +1963,7 @@ pub(super) struct FundingScope {
19631963
/// The hash of the block in which the funding transaction was included.
19641964
funding_tx_confirmed_in: Option<BlockHash>,
19651965
funding_tx_confirmation_height: u32,
1966+
short_channel_id: Option<u64>,
19661967
}
19671968

19681969
impl Writeable for FundingScope {
@@ -1975,6 +1976,7 @@ impl Writeable for FundingScope {
19751976
(9, self.funding_transaction, option),
19761977
(11, self.funding_tx_confirmed_in, option),
19771978
(13, self.funding_tx_confirmation_height, required),
1979+
(15, self.short_channel_id, option),
19781980
});
19791981
Ok(())
19801982
}
@@ -1990,6 +1992,7 @@ impl Readable for FundingScope {
19901992
let mut funding_transaction = None;
19911993
let mut funding_tx_confirmed_in = None;
19921994
let mut funding_tx_confirmation_height = RequiredWrapper(None);
1995+
let mut short_channel_id = None;
19931996

19941997
read_tlv_fields!(reader, {
19951998
(1, value_to_self_msat, required),
@@ -1999,6 +2002,7 @@ impl Readable for FundingScope {
19992002
(9, funding_transaction, option),
20002003
(11, funding_tx_confirmed_in, option),
20012004
(13, funding_tx_confirmation_height, required),
2005+
(15, short_channel_id, option),
20022006
});
20032007

20042008
Ok(Self {
@@ -2013,6 +2017,7 @@ impl Readable for FundingScope {
20132017
funding_transaction,
20142018
funding_tx_confirmed_in,
20152019
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
2020+
short_channel_id,
20162021
#[cfg(any(test, fuzzing))]
20172022
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
20182023
#[cfg(any(test, fuzzing))]
@@ -2109,6 +2114,13 @@ impl FundingScope {
21092114

21102115
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
21112116
}
2117+
2118+
/// Gets the channel's `short_channel_id`.
2119+
///
2120+
/// Will return `None` if the funding hasn't been confirmed yet.
2121+
pub fn get_short_channel_id(&self) -> Option<u64> {
2122+
self.short_channel_id
2123+
}
21122124
}
21132125

21142126
/// Info about a pending splice, used in the pre-splice channel
@@ -2270,7 +2282,6 @@ where
22702282
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
22712283
expecting_peer_commitment_signed: bool,
22722284

2273-
short_channel_id: Option<u64>,
22742285
/// Either the height at which this channel was created or the height at which it was last
22752286
/// serialized if it was serialized by versions prior to 0.0.103.
22762287
/// We use this to close if funding is never broadcasted.
@@ -3118,6 +3129,7 @@ where
31183129
funding_transaction: None,
31193130
funding_tx_confirmed_in: None,
31203131
funding_tx_confirmation_height: 0,
3132+
short_channel_id: None,
31213133
};
31223134
let channel_context = ChannelContext {
31233135
user_id,
@@ -3181,7 +3193,6 @@ where
31813193
closing_fee_limits: None,
31823194
target_closing_feerate_sats_per_kw: None,
31833195

3184-
short_channel_id: None,
31853196
channel_creation_height: current_chain_height,
31863197

31873198
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3359,6 +3370,7 @@ where
33593370
funding_transaction: None,
33603371
funding_tx_confirmed_in: None,
33613372
funding_tx_confirmation_height: 0,
3373+
short_channel_id: None,
33623374
};
33633375
let channel_context = Self {
33643376
user_id,
@@ -3420,7 +3432,6 @@ where
34203432
closing_fee_limits: None,
34213433
target_closing_feerate_sats_per_kw: None,
34223434

3423-
short_channel_id: None,
34243435
channel_creation_height: current_chain_height,
34253436

34263437
feerate_per_kw: commitment_feerate,
@@ -3644,13 +3655,6 @@ where
36443655
self.user_id
36453656
}
36463657

3647-
/// Gets the channel's `short_channel_id`.
3648-
///
3649-
/// Will return `None` if the channel hasn't been confirmed yet.
3650-
pub fn get_short_channel_id(&self) -> Option<u64> {
3651-
self.short_channel_id
3652-
}
3653-
36543658
/// Allowed in any state (including after shutdown)
36553659
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
36563660
self.latest_inbound_scid_alias
@@ -6192,7 +6196,7 @@ where
61926196
}
61936197

61946198
if let Some(scid_alias) = msg.short_channel_id_alias {
6195-
if Some(scid_alias) != self.context.short_channel_id {
6199+
if Some(scid_alias) != self.funding.short_channel_id {
61966200
// The scid alias provided can be used to route payments *from* our counterparty,
61976201
// i.e. can be used for inbound payments and provided in invoices, but is not used
61986202
// when routing outbound payments.
@@ -8904,7 +8908,7 @@ where
89048908

89058909
self.funding.funding_tx_confirmation_height = height;
89068910
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8907-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8911+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
89088912
Ok(scid) => Some(scid),
89098913
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
89108914
}
@@ -9092,7 +9096,7 @@ where
90929096
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
90939097
}
90949098

9095-
let short_channel_id = self.context.get_short_channel_id()
9099+
let short_channel_id = self.funding.get_short_channel_id()
90969100
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
90979101
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
90989102
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -9165,7 +9169,7 @@ where
91659169
},
91669170
Ok(v) => v
91679171
};
9168-
let short_channel_id = match self.context.get_short_channel_id() {
9172+
let short_channel_id = match self.funding.get_short_channel_id() {
91699173
Some(scid) => scid,
91709174
None => return None,
91719175
};
@@ -11512,7 +11516,7 @@ where
1151211516

1151311517
self.funding.funding_tx_confirmed_in.write(writer)?;
1151411518
self.funding.funding_tx_confirmation_height.write(writer)?;
11515-
self.context.short_channel_id.write(writer)?;
11519+
self.funding.short_channel_id.write(writer)?;
1151611520

1151711521
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1151811522
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -12171,6 +12175,7 @@ where
1217112175
funding_transaction,
1217212176
funding_tx_confirmed_in,
1217312177
funding_tx_confirmation_height,
12178+
short_channel_id,
1217412179
},
1217512180
pending_funding: pending_funding.unwrap(),
1217612181
context: ChannelContext {
@@ -12234,7 +12239,6 @@ where
1223412239
closing_fee_limits: None,
1223512240
target_closing_feerate_sats_per_kw,
1223612241

12237-
short_channel_id,
1223812242
channel_creation_height,
1223912243

1224012244
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
@@ -3179,7 +3179,7 @@ macro_rules! locked_close_channel {
31793179
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
31803180
}
31813181
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3182-
if let Some(short_id) = $channel_context.get_short_channel_id() {
3182+
if let Some(short_id) = $channel_funding.get_short_channel_id() {
31833183
short_to_chan_info.remove(&short_id);
31843184
} else {
31853185
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3302,7 +3302,7 @@ macro_rules! send_channel_ready {
33023302
let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
33033303
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
33043304
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3305-
if let Some(real_scid) = $channel.context.get_short_channel_id() {
3305+
if let Some(real_scid) = $channel.funding.get_short_channel_id() {
33063306
let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
33073307
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
33083308
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
@@ -4797,7 +4797,7 @@ where
47974797
action: msgs::ErrorAction::IgnoreError,
47984798
});
47994799
}
4800-
if chan.context.get_short_channel_id().is_none() {
4800+
if chan.funding.get_short_channel_id().is_none() {
48014801
return Err(LightningError {
48024802
err: "Channel not yet established".to_owned(),
48034803
action: msgs::ErrorAction::IgnoreError,
@@ -4827,7 +4827,7 @@ where
48274827
fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
48284828
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
48294829
log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
4830-
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
4830+
let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
48314831
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
48324832
Some(id) => id,
48334833
};
@@ -6017,7 +6017,7 @@ where
60176017
err: format!("Channel with id {} not fully established", next_hop_channel_id)
60186018
})
60196019
}
6020-
funded_chan.context.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
6020+
funded_chan.funding.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
60216021
} else {
60226022
return Err(APIError::ChannelUnavailable {
60236023
err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
@@ -6466,7 +6466,7 @@ where
64666466
};
64676467

64686468
let logger = WithChannelContext::from(&self.logger, &optimal_channel.context, Some(payment_hash));
6469-
let channel_description = if optimal_channel.context.get_short_channel_id() == Some(short_chan_id) {
6469+
let channel_description = if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
64706470
"specified"
64716471
} else {
64726472
"alternate"
@@ -8058,7 +8058,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80588058
);
80598059

80608060
let counterparty_node_id = channel.context.get_counterparty_node_id();
8061-
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
8061+
let short_channel_id = channel.funding.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
80628062

80638063
let mut htlc_forwards = None;
80648064
if !pending_forwards.is_empty() {
@@ -11302,7 +11302,7 @@ where
1130211302
.iter()
1130311303
.filter(|(_, channel)| channel.context().is_usable())
1130411304
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
11305-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
11305+
.and_then(|(_, channel)| channel.funding().get_short_channel_id()),
1130611306
})
1130711307
.collect::<Vec<_>>()
1130811308
}
@@ -12249,7 +12249,7 @@ where
1224912249
});
1225012250
}
1225112251
if funded_channel.is_our_channel_ready() {
12252-
if let Some(real_scid) = funded_channel.context.get_short_channel_id() {
12252+
if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
1225312253
// If we sent a 0conf channel_ready, and now have an SCID, we add it
1225412254
// to the short_to_chan_info map here. Note that we check whether we
1225512255
// can relay using the real SCID at relay-time (i.e.
@@ -14431,7 +14431,7 @@ where
1443114431
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1443214432
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
1443314433
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
14434-
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
14434+
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1443514435
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1443614436
}
1443714437
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)