Skip to content

Commit b1b3666

Browse files
committed
Expose confirmations via ChannelDetails
We expose the current number of confirmations in `ChannelDetails`.
1 parent 0edb0e2 commit b1b3666

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

fuzz/src/router.rs

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
225225
user_channel_id: 0, inbound_capacity_msat: 0,
226226
unspendable_punishment_reserve: None,
227227
confirmations_required: None,
228+
confirmations: None,
228229
force_close_spend_delay: None,
229230
is_outbound: true, is_channel_ready: true,
230231
is_usable: true, is_public: true,

lightning/src/ln/channel.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4495,6 +4495,16 @@ impl<Signer: Sign> Channel<Signer> {
44954495
self.funding_tx_confirmed_in
44964496
}
44974497

4498+
/// Returns the current number of confirmations on the funding transaction.
4499+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
4500+
if self.funding_tx_confirmation_height == 0 {
4501+
// We either haven't seen any confirmation yet, or observed a reorg.
4502+
return 0;
4503+
}
4504+
4505+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
4506+
}
4507+
44984508
fn get_holder_selected_contest_delay(&self) -> u16 {
44994509
self.channel_transaction_parameters.holder_selected_contest_delay
45004510
}

lightning/src/ln/channelmanager.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,10 @@ pub struct ChannelDetails {
11461146
/// [`ChannelHandshakeConfig::minimum_depth`]: crate::util::config::ChannelHandshakeConfig::minimum_depth
11471147
/// [`ChannelHandshakeLimits::max_minimum_depth`]: crate::util::config::ChannelHandshakeLimits::max_minimum_depth
11481148
pub confirmations_required: Option<u32>,
1149+
/// The current number of confirmations on the funding transaction.
1150+
///
1151+
/// This value will be `None` for objects serialized with LDK versions prior to 0.0.113.
1152+
pub confirmations: Option<u32>,
11491153
/// The number of blocks (after our commitment transaction confirms) that we will need to wait
11501154
/// until we can claim our funds after we force-close the channel. During this time our
11511155
/// counterparty is allowed to punish us if we broadcasted a stale state. If our counterparty
@@ -1694,6 +1698,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
16941698
let mut res = Vec::new();
16951699
{
16961700
let channel_state = self.channel_state.lock().unwrap();
1701+
let best_block_height = self.best_block.read().unwrap().height();
16971702
res.reserve(channel_state.by_id.len());
16981703
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
16991704
let balance = channel.get_available_balances();
@@ -1730,6 +1735,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
17301735
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
17311736
user_channel_id: channel.get_user_id(),
17321737
confirmations_required: channel.minimum_depth(),
1738+
confirmations: Some(channel.get_funding_tx_confirmations(best_block_height)),
17331739
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
17341740
is_outbound: channel.is_outbound(),
17351741
is_channel_ready: channel.is_usable(),
@@ -6467,6 +6473,7 @@ impl Writeable for ChannelDetails {
64676473
(6, self.funding_txo, option),
64686474
(7, self.config, option),
64696475
(8, self.short_channel_id, option),
6476+
(9, self.confirmations, option),
64706477
(10, self.channel_value_satoshis, required),
64716478
(12, self.unspendable_punishment_reserve, option),
64726479
(14, user_channel_id_low, required),
@@ -6501,6 +6508,7 @@ impl Readable for ChannelDetails {
65016508
(6, funding_txo, option),
65026509
(7, config, option),
65036510
(8, short_channel_id, option),
6511+
(9, confirmations, option),
65046512
(10, channel_value_satoshis, required),
65056513
(12, unspendable_punishment_reserve, option),
65066514
(14, user_channel_id_low, required),
@@ -6544,6 +6552,7 @@ impl Readable for ChannelDetails {
65446552
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
65456553
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
65466554
confirmations_required,
6555+
confirmations,
65476556
force_close_spend_delay,
65486557
is_outbound: is_outbound.0.unwrap(),
65496558
is_channel_ready: is_channel_ready.0.unwrap(),

lightning/src/ln/reorg_tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
277277
assert_eq!(nodes[0].node.short_to_chan_info.read().unwrap().len(), 2);
278278
mem::drop(channel_state);
279279

280+
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(10));
281+
assert_eq!(nodes[1].node.list_channels()[0].confirmations, Some(10));
282+
280283
if !reorg_after_reload {
281284
if use_funding_unconfirmed {
282285
let relevant_txids = nodes[0].node.get_relevant_txids();
@@ -287,12 +290,16 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
287290
let txid = relevant_txids[0].0;
288291
assert_eq!(txid, chan.3.txid());
289292
nodes[0].node.transaction_unconfirmed(&txid);
293+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
290294
} else if connect_style == ConnectStyle::FullBlockViaListen {
291295
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
292296
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
297+
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
293298
disconnect_blocks(&nodes[0], 1);
299+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
294300
} else {
295301
disconnect_all_blocks(&nodes[0]);
302+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
296303
}
297304

298305
let relevant_txids = nodes[0].node.get_relevant_txids();
@@ -334,12 +341,16 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
334341
let txid = relevant_txids[0].0;
335342
assert_eq!(txid, chan.3.txid());
336343
nodes[0].node.transaction_unconfirmed(&txid);
344+
assert_eq!(nodes[0].node.list_channels().len(), 0);
337345
} else if connect_style == ConnectStyle::FullBlockViaListen {
338346
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
339347
assert_eq!(nodes[0].node.list_channels().len(), 1);
348+
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
340349
disconnect_blocks(&nodes[0], 1);
350+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
341351
} else {
342352
disconnect_all_blocks(&nodes[0]);
353+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
343354
}
344355

345356
let relevant_txids = nodes[0].node.get_relevant_txids();

lightning/src/routing/router.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,7 @@ mod tests {
20482048
inbound_capacity_msat: 42,
20492049
unspendable_punishment_reserve: None,
20502050
confirmations_required: None,
2051+
confirmations: None,
20512052
force_close_spend_delay: None,
20522053
is_outbound: true, is_channel_ready: true,
20532054
is_usable: true, is_public: true,
@@ -5562,6 +5563,7 @@ mod benches {
55625563
inbound_capacity_msat: 0,
55635564
unspendable_punishment_reserve: None,
55645565
confirmations_required: None,
5566+
confirmations: None,
55655567
force_close_spend_delay: None,
55665568
is_outbound: true,
55675569
is_channel_ready: true,

0 commit comments

Comments
 (0)