Skip to content

Commit 5f3411a

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 1526b2b commit 5f3411a

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
@@ -1688,6 +1688,7 @@ pub(super) struct FundingScope {
16881688
/// The hash of the block in which the funding transaction was included.
16891689
funding_tx_confirmed_in: Option<BlockHash>,
16901690
funding_tx_confirmation_height: u32,
1691+
short_channel_id: Option<u64>,
16911692
}
16921693

16931694
impl Writeable for FundingScope {
@@ -1700,6 +1701,7 @@ impl Writeable for FundingScope {
17001701
(9, self.funding_transaction, option),
17011702
(11, self.funding_tx_confirmed_in, option),
17021703
(13, self.funding_tx_confirmation_height, required),
1704+
(15, self.short_channel_id, option),
17031705
});
17041706
Ok(())
17051707
}
@@ -1714,6 +1716,7 @@ impl Readable for FundingScope {
17141716
let mut funding_transaction = None;
17151717
let mut funding_tx_confirmed_in = None;
17161718
let mut funding_tx_confirmation_height = RequiredWrapper(None);
1719+
let mut short_channel_id = None;
17171720

17181721
read_tlv_fields!(reader, {
17191722
(1, value_to_self_msat, required),
@@ -1723,6 +1726,7 @@ impl Readable for FundingScope {
17231726
(9, funding_transaction, option),
17241727
(11, funding_tx_confirmed_in, option),
17251728
(13, funding_tx_confirmation_height, required),
1729+
(15, short_channel_id, option),
17261730
});
17271731

17281732
Ok(Self {
@@ -1737,6 +1741,7 @@ impl Readable for FundingScope {
17371741
funding_transaction,
17381742
funding_tx_confirmed_in,
17391743
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
1744+
short_channel_id,
17401745
#[cfg(any(test, fuzzing))]
17411746
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
17421747
#[cfg(any(test, fuzzing))]
@@ -1830,6 +1835,13 @@ impl FundingScope {
18301835

18311836
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
18321837
}
1838+
1839+
/// Gets the channel's `short_channel_id`.
1840+
///
1841+
/// Will return `None` if the funding hasn't been confirmed yet.
1842+
pub fn get_short_channel_id(&self) -> Option<u64> {
1843+
self.short_channel_id
1844+
}
18331845
}
18341846

18351847
/// Info about a pending splice, used in the pre-splice channel
@@ -1989,7 +2001,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
19892001
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
19902002
expecting_peer_commitment_signed: bool,
19912003

1992-
short_channel_id: Option<u64>,
19932004
/// Either the height at which this channel was created or the height at which it was last
19942005
/// serialized if it was serialized by versions prior to 0.0.103.
19952006
/// We use this to close if funding is never broadcasted.
@@ -2800,6 +2811,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28002811
funding_transaction: None,
28012812
funding_tx_confirmed_in: None,
28022813
funding_tx_confirmation_height: 0,
2814+
short_channel_id: None,
28032815
};
28042816
let channel_context = ChannelContext {
28052817
user_id,
@@ -2863,7 +2875,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28632875
closing_fee_limits: None,
28642876
target_closing_feerate_sats_per_kw: None,
28652877

2866-
short_channel_id: None,
28672878
channel_creation_height: current_chain_height,
28682879

28692880
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3036,6 +3047,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30363047
funding_transaction: None,
30373048
funding_tx_confirmed_in: None,
30383049
funding_tx_confirmation_height: 0,
3050+
short_channel_id: None,
30393051
};
30403052
let channel_context = Self {
30413053
user_id,
@@ -3097,7 +3109,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30973109
closing_fee_limits: None,
30983110
target_closing_feerate_sats_per_kw: None,
30993111

3100-
short_channel_id: None,
31013112
channel_creation_height: current_chain_height,
31023113

31033114
feerate_per_kw: commitment_feerate,
@@ -3307,13 +3318,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
33073318
self.user_id
33083319
}
33093320

3310-
/// Gets the channel's `short_channel_id`.
3311-
///
3312-
/// Will return `None` if the channel hasn't been confirmed yet.
3313-
pub fn get_short_channel_id(&self) -> Option<u64> {
3314-
self.short_channel_id
3315-
}
3316-
33173321
/// Allowed in any state (including after shutdown)
33183322
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
33193323
self.latest_inbound_scid_alias
@@ -5526,7 +5530,7 @@ impl<SP: Deref> FundedChannel<SP> where
55265530
}
55275531

55285532
if let Some(scid_alias) = msg.short_channel_id_alias {
5529-
if Some(scid_alias) != self.context.short_channel_id {
5533+
if Some(scid_alias) != self.funding.short_channel_id {
55305534
// The scid alias provided can be used to route payments *from* our counterparty,
55315535
// i.e. can be used for inbound payments and provided in invoices, but is not used
55325536
// when routing outbound payments.
@@ -8202,7 +8206,7 @@ impl<SP: Deref> FundedChannel<SP> where
82028206

82038207
self.funding.funding_tx_confirmation_height = height;
82048208
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8205-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8209+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
82068210
Ok(scid) => Some(scid),
82078211
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
82088212
}
@@ -8382,7 +8386,7 @@ impl<SP: Deref> FundedChannel<SP> where
83828386
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
83838387
}
83848388

8385-
let short_channel_id = self.context.get_short_channel_id()
8389+
let short_channel_id = self.funding.get_short_channel_id()
83868390
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
83878391
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
83888392
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -8454,7 +8458,7 @@ impl<SP: Deref> FundedChannel<SP> where
84548458
},
84558459
Ok(v) => v
84568460
};
8457-
let short_channel_id = match self.context.get_short_channel_id() {
8461+
let short_channel_id = match self.funding.get_short_channel_id() {
84588462
Some(scid) => scid,
84598463
None => return None,
84608464
};
@@ -10701,7 +10705,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1070110705

1070210706
self.funding.funding_tx_confirmed_in.write(writer)?;
1070310707
self.funding.funding_tx_confirmation_height.write(writer)?;
10704-
self.context.short_channel_id.write(writer)?;
10708+
self.funding.short_channel_id.write(writer)?;
1070510709

1070610710
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1070710711
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -11337,6 +11341,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1133711341
funding_transaction,
1133811342
funding_tx_confirmed_in,
1133911343
funding_tx_confirmation_height,
11344+
short_channel_id,
1134011345
},
1134111346
pending_funding: pending_funding.unwrap(),
1134211347
context: ChannelContext {
@@ -11400,7 +11405,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1140011405
closing_fee_limits: None,
1140111406
target_closing_feerate_sats_per_kw,
1140211407

11403-
short_channel_id,
1140411408
channel_creation_height,
1140511409

1140611410
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
@@ -3031,7 +3031,7 @@ macro_rules! locked_close_channel {
30313031
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
30323032
}
30333033
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3034-
if let Some(short_id) = $channel_context.get_short_channel_id() {
3034+
if let Some(short_id) = $channel_funding.get_short_channel_id() {
30353035
short_to_chan_info.remove(&short_id);
30363036
} else {
30373037
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3147,7 +3147,7 @@ macro_rules! send_channel_ready {
31473147
let outbound_alias_insert = short_to_chan_info.insert($channel.context.outbound_scid_alias(), ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
31483148
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
31493149
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
3150-
if let Some(real_scid) = $channel.context.get_short_channel_id() {
3150+
if let Some(real_scid) = $channel.funding.get_short_channel_id() {
31513151
let scid_insert = short_to_chan_info.insert(real_scid, ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()));
31523152
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.context.get_counterparty_node_id(), $channel.context.channel_id()),
31533153
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
@@ -4572,7 +4572,7 @@ where
45724572
action: msgs::ErrorAction::IgnoreError
45734573
});
45744574
}
4575-
if chan.context.get_short_channel_id().is_none() {
4575+
if chan.funding.get_short_channel_id().is_none() {
45764576
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
45774577
}
45784578
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
@@ -4594,7 +4594,7 @@ where
45944594
fn get_channel_update_for_unicast(&self, chan: &FundedChannel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
45954595
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
45964596
log_trace!(logger, "Attempting to generate channel update for channel {}", chan.context.channel_id());
4597-
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
4597+
let short_channel_id = match chan.funding.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
45984598
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
45994599
Some(id) => id,
46004600
};
@@ -5657,7 +5657,7 @@ where
56575657
err: format!("Channel with id {} not fully established", next_hop_channel_id)
56585658
})
56595659
}
5660-
funded_chan.context.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
5660+
funded_chan.funding.get_short_channel_id().unwrap_or(funded_chan.context.outbound_scid_alias())
56615661
} else {
56625662
return Err(APIError::ChannelUnavailable {
56635663
err: format!("Channel with id {} for the passed counterparty node_id {} is still opening.",
@@ -6099,7 +6099,7 @@ where
60996099
};
61006100

61016101
let logger = WithChannelContext::from(&self.logger, &optimal_channel.context, Some(payment_hash));
6102-
let channel_description = if optimal_channel.context.get_short_channel_id() == Some(short_chan_id) {
6102+
let channel_description = if optimal_channel.funding.get_short_channel_id() == Some(short_chan_id) {
61036103
"specified"
61046104
} else {
61056105
"alternate"
@@ -7663,7 +7663,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
76637663
);
76647664

76657665
let counterparty_node_id = channel.context.get_counterparty_node_id();
7666-
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
7666+
let short_channel_id = channel.funding.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
76677667

76687668
let mut htlc_forwards = None;
76697669
if !pending_forwards.is_empty() {
@@ -10913,7 +10913,7 @@ where
1091310913
.iter()
1091410914
.filter(|(_, channel)| channel.context().is_usable())
1091510915
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10916-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10916+
.and_then(|(_, channel)| channel.funding().get_short_channel_id()),
1091710917
})
1091810918
.collect::<Vec<_>>();
1091910919

@@ -11776,7 +11776,7 @@ where
1177611776
});
1177711777
}
1177811778
if funded_channel.is_our_channel_ready() {
11779-
if let Some(real_scid) = funded_channel.context.get_short_channel_id() {
11779+
if let Some(real_scid) = funded_channel.funding.get_short_channel_id() {
1178011780
// If we sent a 0conf channel_ready, and now have an SCID, we add it
1178111781
// to the short_to_chan_info map here. Note that we check whether we
1178211782
// can relay using the real SCID at relay-time (i.e.
@@ -13870,7 +13870,7 @@ where
1387013870
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1387113871
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
1387213872
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
13873-
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
13873+
if let Some(short_channel_id) = channel.funding.get_short_channel_id() {
1387413874
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1387513875
}
1387613876
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
@@ -1709,7 +1709,7 @@ fn test_trivial_inflight_htlc_tracking(){
17091709
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
17101710
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
17111711
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1712-
channel_1.context().get_short_channel_id().unwrap()
1712+
channel_1.funding().get_short_channel_id().unwrap()
17131713
);
17141714
assert_eq!(chan_1_used_liquidity, None);
17151715
}
@@ -1721,7 +1721,7 @@ fn test_trivial_inflight_htlc_tracking(){
17211721
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
17221722
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
17231723
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1724-
channel_2.context().get_short_channel_id().unwrap()
1724+
channel_2.funding().get_short_channel_id().unwrap()
17251725
);
17261726

17271727
assert_eq!(chan_2_used_liquidity, None);
@@ -1746,7 +1746,7 @@ fn test_trivial_inflight_htlc_tracking(){
17461746
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
17471747
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
17481748
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1749-
channel_1.context().get_short_channel_id().unwrap()
1749+
channel_1.funding().get_short_channel_id().unwrap()
17501750
);
17511751
// First hop accounts for expected 1000 msat fee
17521752
assert_eq!(chan_1_used_liquidity, Some(501000));
@@ -1759,7 +1759,7 @@ fn test_trivial_inflight_htlc_tracking(){
17591759
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
17601760
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
17611761
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1762-
channel_2.context().get_short_channel_id().unwrap()
1762+
channel_2.funding().get_short_channel_id().unwrap()
17631763
);
17641764

17651765
assert_eq!(chan_2_used_liquidity, Some(500000));
@@ -1785,7 +1785,7 @@ fn test_trivial_inflight_htlc_tracking(){
17851785
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
17861786
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
17871787
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1788-
channel_1.context().get_short_channel_id().unwrap()
1788+
channel_1.funding().get_short_channel_id().unwrap()
17891789
);
17901790
assert_eq!(chan_1_used_liquidity, None);
17911791
}
@@ -1797,7 +1797,7 @@ fn test_trivial_inflight_htlc_tracking(){
17971797
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
17981798
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
17991799
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
1800-
channel_2.context().get_short_channel_id().unwrap()
1800+
channel_2.funding().get_short_channel_id().unwrap()
18011801
);
18021802
assert_eq!(chan_2_used_liquidity, None);
18031803
}
@@ -1838,7 +1838,7 @@ fn test_holding_cell_inflight_htlcs() {
18381838
let used_liquidity = inflight_htlcs.used_liquidity_msat(
18391839
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
18401840
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1841-
channel.context().get_short_channel_id().unwrap()
1841+
channel.funding().get_short_channel_id().unwrap()
18421842
);
18431843

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

0 commit comments

Comments
 (0)