Skip to content

Commit

Permalink
Expose confirmations via ChannelDetails
Browse files Browse the repository at this point in the history
We expose the current number of confirmations in `ChannelDetails`.
  • Loading branch information
tnull committed Nov 17, 2022
1 parent 86ab095 commit 86e2039
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
user_channel_id: 0, inbound_capacity_msat: 0,
unspendable_punishment_reserve: None,
confirmations_required: None,
confirmations: None,
force_close_spend_delay: None,
is_outbound: true, is_channel_ready: true,
is_usable: true, is_public: true,
Expand Down
11 changes: 11 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use crate::io;
use crate::prelude::*;
use core::{cmp,mem,fmt};
use core::ops::Deref;
use core::convert::TryInto;
#[cfg(any(test, fuzzing, debug_assertions))]
use crate::sync::Mutex;
use bitcoin::hashes::hex::ToHex;
Expand Down Expand Up @@ -4525,6 +4526,16 @@ impl<Signer: Sign> Channel<Signer> {
self.funding_tx_confirmed_in
}

/// Returns the current number of confirmations on the funding transaction.
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
if self.funding_tx_confirmation_height == 0 {
// We either haven't seen any confirmation yet, or observed a reorg.
return 0;
}
let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
funding_tx_confirmations.try_into().unwrap_or(0)
}

fn get_holder_selected_contest_delay(&self) -> u16 {
self.channel_transaction_parameters.holder_selected_contest_delay
}
Expand Down
9 changes: 9 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,10 @@ pub struct ChannelDetails {
/// [`ChannelHandshakeConfig::minimum_depth`]: crate::util::config::ChannelHandshakeConfig::minimum_depth
/// [`ChannelHandshakeLimits::max_minimum_depth`]: crate::util::config::ChannelHandshakeLimits::max_minimum_depth
pub confirmations_required: Option<u32>,
/// The current number of confirmations on the funding transaction.
///
/// This value will be `None` for objects serialized with LDK versions prior to 0.0.113.
pub confirmations: Option<u32>,
/// The number of blocks (after our commitment transaction confirms) that we will need to wait
/// until we can claim our funds after we force-close the channel. During this time our
/// counterparty is allowed to punish us if we broadcasted a stale state. If our counterparty
Expand Down Expand Up @@ -1817,6 +1821,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
let mut res = Vec::new();
{
let channel_state = self.channel_state.lock().unwrap();
let best_block_height = self.best_block.read().unwrap().height();
res.reserve(channel_state.by_id.len());
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
let balance = channel.get_available_balances();
Expand Down Expand Up @@ -1853,6 +1858,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
user_channel_id: channel.get_user_id(),
confirmations_required: channel.minimum_depth(),
confirmations: Some(channel.get_funding_tx_confirmations(best_block_height)),
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
is_outbound: channel.is_outbound(),
is_channel_ready: channel.is_usable(),
Expand Down Expand Up @@ -6510,6 +6516,7 @@ impl Writeable for ChannelDetails {
(6, self.funding_txo, option),
(7, self.config, option),
(8, self.short_channel_id, option),
(9, self.confirmations, option),
(10, self.channel_value_satoshis, required),
(12, self.unspendable_punishment_reserve, option),
(14, user_channel_id_low, required),
Expand Down Expand Up @@ -6544,6 +6551,7 @@ impl Readable for ChannelDetails {
(6, funding_txo, option),
(7, config, option),
(8, short_channel_id, option),
(9, confirmations, option),
(10, channel_value_satoshis, required),
(12, unspendable_punishment_reserve, option),
(14, user_channel_id_low, required),
Expand Down Expand Up @@ -6587,6 +6595,7 @@ impl Readable for ChannelDetails {
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
confirmations_required,
confirmations,
force_close_spend_delay,
is_outbound: is_outbound.0.unwrap(),
is_channel_ready: is_channel_ready.0.unwrap(),
Expand Down
13 changes: 11 additions & 2 deletions lightning/src/ln/reorg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
assert_eq!(nodes[0].node.short_to_chan_info.read().unwrap().len(), 2);
mem::drop(channel_state);

assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(10));
assert_eq!(nodes[1].node.list_channels()[0].confirmations, Some(10));

if !reorg_after_reload {
if use_funding_unconfirmed {
let relevant_txids = nodes[0].node.get_relevant_txids();
Expand All @@ -287,12 +290,15 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
disconnect_blocks(&nodes[0], 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
} else {
disconnect_all_blocks(&nodes[0]);
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
Expand Down Expand Up @@ -334,12 +340,15 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
let txid = relevant_txids[0].0;
assert_eq!(txid, chan.3.txid());
nodes[0].node.transaction_unconfirmed(&txid);
assert_eq!(nodes[0].node.list_channels().len(), 0);
} else if connect_style == ConnectStyle::FullBlockViaListen {
disconnect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
assert_eq!(nodes[0].node.list_channels().len(), 1);
assert_eq!(nodes[0].node.list_channels()[0].confirmations, Some(1));
disconnect_blocks(&nodes[0], 1);
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
} else {
disconnect_all_blocks(&nodes[0]);
assert_eq!(nodes[0].node.list_usable_channels().len(), 0);
}

let relevant_txids = nodes[0].node.get_relevant_txids();
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,7 @@ mod tests {
inbound_capacity_msat: 42,
unspendable_punishment_reserve: None,
confirmations_required: None,
confirmations: None,
force_close_spend_delay: None,
is_outbound: true, is_channel_ready: true,
is_usable: true, is_public: true,
Expand Down Expand Up @@ -5539,6 +5540,7 @@ mod benches {
inbound_capacity_msat: 0,
unspendable_punishment_reserve: None,
confirmations_required: None,
confirmations: None,
force_close_spend_delay: None,
is_outbound: true,
is_channel_ready: true,
Expand Down

0 comments on commit 86e2039

Please sign in to comment.