Skip to content

Commit a79f28e

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 c0f5571 commit a79f28e

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

lightning/src/ln/channel.rs

+20-16
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))]
@@ -1835,6 +1840,13 @@ impl FundingScope {
18351840

18361841
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
18371842
}
1843+
1844+
/// Gets the channel's `short_channel_id`.
1845+
///
1846+
/// Will return `None` if the funding hasn't been confirmed yet.
1847+
pub fn get_short_channel_id(&self) -> Option<u64> {
1848+
self.short_channel_id
1849+
}
18381850
}
18391851

18401852
/// Info about a pending splice, used in the pre-splice channel
@@ -1994,7 +2006,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
19942006
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
19952007
expecting_peer_commitment_signed: bool,
19962008

1997-
short_channel_id: Option<u64>,
19982009
/// Either the height at which this channel was created or the height at which it was last
19992010
/// serialized if it was serialized by versions prior to 0.0.103.
20002011
/// We use this to close if funding is never broadcasted.
@@ -2805,6 +2816,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28052816
funding_transaction: None,
28062817
funding_tx_confirmed_in: None,
28072818
funding_tx_confirmation_height: 0,
2819+
short_channel_id: None,
28082820
};
28092821
let channel_context = ChannelContext {
28102822
user_id,
@@ -2868,7 +2880,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28682880
closing_fee_limits: None,
28692881
target_closing_feerate_sats_per_kw: None,
28702882

2871-
short_channel_id: None,
28722883
channel_creation_height: current_chain_height,
28732884

28742885
feerate_per_kw: open_channel_fields.commitment_feerate_sat_per_1000_weight,
@@ -3041,6 +3052,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30413052
funding_transaction: None,
30423053
funding_tx_confirmed_in: None,
30433054
funding_tx_confirmation_height: 0,
3055+
short_channel_id: None,
30443056
};
30453057
let channel_context = Self {
30463058
user_id,
@@ -3102,7 +3114,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
31023114
closing_fee_limits: None,
31033115
target_closing_feerate_sats_per_kw: None,
31043116

3105-
short_channel_id: None,
31063117
channel_creation_height: current_chain_height,
31073118

31083119
feerate_per_kw: commitment_feerate,
@@ -3312,13 +3323,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
33123323
self.user_id
33133324
}
33143325

3315-
/// Gets the channel's `short_channel_id`.
3316-
///
3317-
/// Will return `None` if the channel hasn't been confirmed yet.
3318-
pub fn get_short_channel_id(&self) -> Option<u64> {
3319-
self.short_channel_id
3320-
}
3321-
33223326
/// Allowed in any state (including after shutdown)
33233327
pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
33243328
self.latest_inbound_scid_alias
@@ -4862,7 +4866,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48624866
return Err(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel is not currently usable".to_owned()));
48634867
}
48644868

4865-
let short_channel_id = self.get_short_channel_id()
4869+
let short_channel_id = funding.get_short_channel_id()
48664870
.ok_or(ChannelError::Ignore("Cannot get a ChannelAnnouncement if the channel has not been confirmed yet".to_owned()))?;
48674871
let node_id = NodeId::from_pubkey(&node_signer.get_node_id(Recipient::Node)
48684872
.map_err(|_| ChannelError::Ignore("Failed to retrieve own public key".to_owned()))?);
@@ -4934,7 +4938,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
49344938
},
49354939
Ok(v) => v
49364940
};
4937-
let short_channel_id = match self.get_short_channel_id() {
4941+
let short_channel_id = match funding.get_short_channel_id() {
49384942
Some(scid) => scid,
49394943
None => return None,
49404944
};
@@ -5634,7 +5638,7 @@ impl<SP: Deref> FundedChannel<SP> where
56345638
}
56355639

56365640
if let Some(scid_alias) = msg.short_channel_id_alias {
5637-
if Some(scid_alias) != self.context.short_channel_id {
5641+
if Some(scid_alias) != self.funding.short_channel_id {
56385642
// The scid alias provided can be used to route payments *from* our counterparty,
56395643
// i.e. can be used for inbound payments and provided in invoices, but is not used
56405644
// when routing outbound payments.
@@ -8314,7 +8318,7 @@ impl<SP: Deref> FundedChannel<SP> where
83148318

83158319
self.funding.funding_tx_confirmation_height = height;
83168320
self.funding.funding_tx_confirmed_in = Some(*block_hash);
8317-
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
8321+
self.funding.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
83188322
Ok(scid) => Some(scid),
83198323
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
83208324
};
@@ -10730,7 +10734,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1073010734

1073110735
self.funding.funding_tx_confirmed_in.write(writer)?;
1073210736
self.funding.funding_tx_confirmation_height.write(writer)?;
10733-
self.context.short_channel_id.write(writer)?;
10737+
self.funding.short_channel_id.write(writer)?;
1073410738

1073510739
self.context.counterparty_dust_limit_satoshis.write(writer)?;
1073610740
self.context.holder_dust_limit_satoshis.write(writer)?;
@@ -11366,6 +11370,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1136611370
funding_transaction,
1136711371
funding_tx_confirmed_in,
1136811372
funding_tx_confirmation_height,
11373+
short_channel_id,
1136911374
},
1137011375
pending_funding: pending_funding.unwrap(),
1137111376
context: ChannelContext {
@@ -11429,7 +11434,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1142911434
closing_fee_limits: None,
1143011435
target_closing_feerate_sats_per_kw,
1143111436

11432-
short_channel_id,
1143311437
channel_creation_height,
1143411438

1143511439
counterparty_dust_limit_satoshis,

lightning/src/ln/channel_state.rs

+1-1
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

+10-10
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

+7-7
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)