Skip to content

Commit 86e2039

Browse files
committed
Expose confirmations via ChannelDetails
We expose the current number of confirmations in `ChannelDetails`.
1 parent 86ab095 commit 86e2039

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
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

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use crate::io;
4646
use crate::prelude::*;
4747
use core::{cmp,mem,fmt};
4848
use core::ops::Deref;
49+
use core::convert::TryInto;
4950
#[cfg(any(test, fuzzing, debug_assertions))]
5051
use crate::sync::Mutex;
5152
use bitcoin::hashes::hex::ToHex;
@@ -4525,6 +4526,16 @@ impl<Signer: Sign> Channel<Signer> {
45254526
self.funding_tx_confirmed_in
45264527
}
45274528

4529+
/// Returns the current number of confirmations on the funding transaction.
4530+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
4531+
if self.funding_tx_confirmation_height == 0 {
4532+
// We either haven't seen any confirmation yet, or observed a reorg.
4533+
return 0;
4534+
}
4535+
let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
4536+
funding_tx_confirmations.try_into().unwrap_or(0)
4537+
}
4538+
45284539
fn get_holder_selected_contest_delay(&self) -> u16 {
45294540
self.channel_transaction_parameters.holder_selected_contest_delay
45304541
}

lightning/src/ln/channelmanager.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,10 @@ pub struct ChannelDetails {
11411141
/// [`ChannelHandshakeConfig::minimum_depth`]: crate::util::config::ChannelHandshakeConfig::minimum_depth
11421142
/// [`ChannelHandshakeLimits::max_minimum_depth`]: crate::util::config::ChannelHandshakeLimits::max_minimum_depth
11431143
pub confirmations_required: Option<u32>,
1144+
/// The current number of confirmations on the funding transaction.
1145+
///
1146+
/// This value will be `None` for objects serialized with LDK versions prior to 0.0.113.
1147+
pub confirmations: Option<u32>,
11441148
/// The number of blocks (after our commitment transaction confirms) that we will need to wait
11451149
/// until we can claim our funds after we force-close the channel. During this time our
11461150
/// counterparty is allowed to punish us if we broadcasted a stale state. If our counterparty
@@ -1817,6 +1821,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18171821
let mut res = Vec::new();
18181822
{
18191823
let channel_state = self.channel_state.lock().unwrap();
1824+
let best_block_height = self.best_block.read().unwrap().height();
18201825
res.reserve(channel_state.by_id.len());
18211826
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
18221827
let balance = channel.get_available_balances();
@@ -1853,6 +1858,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18531858
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
18541859
user_channel_id: channel.get_user_id(),
18551860
confirmations_required: channel.minimum_depth(),
1861+
confirmations: Some(channel.get_funding_tx_confirmations(best_block_height)),
18561862
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
18571863
is_outbound: channel.is_outbound(),
18581864
is_channel_ready: channel.is_usable(),
@@ -6510,6 +6516,7 @@ impl Writeable for ChannelDetails {
65106516
(6, self.funding_txo, option),
65116517
(7, self.config, option),
65126518
(8, self.short_channel_id, option),
6519+
(9, self.confirmations, option),
65136520
(10, self.channel_value_satoshis, required),
65146521
(12, self.unspendable_punishment_reserve, option),
65156522
(14, user_channel_id_low, required),
@@ -6544,6 +6551,7 @@ impl Readable for ChannelDetails {
65446551
(6, funding_txo, option),
65456552
(7, config, option),
65466553
(8, short_channel_id, option),
6554+
(9, confirmations, option),
65476555
(10, channel_value_satoshis, required),
65486556
(12, unspendable_punishment_reserve, option),
65496557
(14, user_channel_id_low, required),
@@ -6587,6 +6595,7 @@ impl Readable for ChannelDetails {
65876595
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
65886596
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
65896597
confirmations_required,
6598+
confirmations,
65906599
force_close_spend_delay,
65916600
is_outbound: is_outbound.0.unwrap(),
65926601
is_channel_ready: is_channel_ready.0.unwrap(),

lightning/src/ln/reorg_tests.rs

+11-2
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,15 @@ 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);
292-
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
296+
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
293297
disconnect_blocks(&nodes[0], 1);
298+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
294299
} else {
295300
disconnect_all_blocks(&nodes[0]);
301+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
296302
}
297303

298304
let relevant_txids = nodes[0].node.get_relevant_txids();
@@ -334,12 +340,15 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
334340
let txid = relevant_txids[0].0;
335341
assert_eq!(txid, chan.3.txid());
336342
nodes[0].node.transaction_unconfirmed(&txid);
343+
assert_eq!(nodes[0].node.list_channels().len(), 0);
337344
} else if connect_style == ConnectStyle::FullBlockViaListen {
338345
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
339-
assert_eq!(nodes[0].node.list_channels().len(), 1);
346+
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
340347
disconnect_blocks(&nodes[0], 1);
348+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
341349
} else {
342350
disconnect_all_blocks(&nodes[0]);
351+
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
343352
}
344353

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

lightning/src/routing/router.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,7 @@ mod tests {
20252025
inbound_capacity_msat: 42,
20262026
unspendable_punishment_reserve: None,
20272027
confirmations_required: None,
2028+
confirmations: None,
20282029
force_close_spend_delay: None,
20292030
is_outbound: true, is_channel_ready: true,
20302031
is_usable: true, is_public: true,
@@ -5539,6 +5540,7 @@ mod benches {
55395540
inbound_capacity_msat: 0,
55405541
unspendable_punishment_reserve: None,
55415542
confirmations_required: None,
5543+
confirmations: None,
55425544
force_close_spend_delay: None,
55435545
is_outbound: true,
55445546
is_channel_ready: true,

0 commit comments

Comments
 (0)