Merge pull request #1203 from lightning-signer/2021-12-value-to-self
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 8 Dec 2021 02:24:32 +0000 (02:24 +0000)
committerGitHub <noreply@github.com>
Wed, 8 Dec 2021 02:24:32 +0000 (02:24 +0000)
Getter for the total channel balance

fuzz/src/router.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/routing/router.rs

index 149d40134f5102165aaaa00440079352fcbcd737..d6aa97e42e7fc3939c327501017080b03e2552cb 100644 (file)
@@ -223,6 +223,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                                force_close_spend_delay: None,
                                                                is_outbound: true, is_funding_locked: true,
                                                                is_usable: true, is_public: true,
+                                                               balance_msat: 0,
                                                                outbound_capacity_msat: 0,
                                                        });
                                                }
index 0ab687521c3aa5fcff317b1c53ee65e58022dcda..a027e6c364734ded0b61ad1c24e1b72e26eaede0 100644 (file)
@@ -2122,6 +2122,8 @@ impl<Signer: Sign> Channel<Signer> {
        /// Doesn't bother handling the
        /// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
        /// corner case properly.
+       /// The channel reserve is subtracted from each balance.
+       /// See also [`Channel::get_balance_msat`]
        pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
                // Note that we have to handle overflow due to the above case.
                (
@@ -2137,6 +2139,14 @@ impl<Signer: Sign> Channel<Signer> {
                )
        }
 
+       /// Get our total balance in msat.
+       /// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
+       /// See also [`Channel::get_inbound_outbound_available_balance_msat`]
+       pub fn get_balance_msat(&self) -> u64 {
+               self.value_to_self_msat
+                       - self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
+       }
+
        pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {
                (self.holder_selected_channel_reserve_satoshis, self.counterparty_selected_channel_reserve_satoshis)
        }
index f9f7b04402202a3aa9a45f8036b9a48fd58ec631..9c344fe7621d0c7188493f5870aa0a96fa73ba8f 100644 (file)
@@ -868,17 +868,30 @@ pub struct ChannelDetails {
        pub unspendable_punishment_reserve: Option<u64>,
        /// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
        pub user_channel_id: u64,
+       /// Our total balance.  This is the amount we would get if we close the channel.
+       /// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
+       /// amount is not likely to be recoverable on close.
+       ///
+       /// This does not include any pending HTLCs which are not yet fully resolved (and, thus, whose
+       /// balance is not available for inclusion in new outbound HTLCs). This further does not include
+       /// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
+       /// This does not consider any on-chain fees.
+       ///
+       /// See also [`ChannelDetails::outbound_capacity_msat`]
+       pub balance_msat: u64,
        /// The available outbound capacity for sending HTLCs to the remote peer. This does not include
-       /// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
+       /// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
        /// available for inclusion in new outbound HTLCs). This further does not include any pending
        /// outgoing HTLCs which are awaiting some other resolution to be sent.
        ///
+       /// See also [`ChannelDetails::balance_msat`]
+       ///
        /// This value is not exact. Due to various in-flight changes, feerate changes, and our
        /// conflict-avoidance policy, exactly this amount is not likely to be spendable. However, we
        /// should be able to spend nearly this amount.
        pub outbound_capacity_msat: u64,
        /// The available inbound capacity for the remote peer to send HTLCs to us. This does not
-       /// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
+       /// include any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
        /// available for inclusion in new inbound HTLCs).
        /// Note that there are some corner cases not fully handled here, so the actual available
        /// inbound capacity may be slightly higher than this.
@@ -1449,6 +1462,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                        res.reserve(channel_state.by_id.len());
                        for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
                                let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
+                               let balance_msat = channel.get_balance_msat();
                                let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
                                        channel.get_holder_counterparty_selected_channel_reserve_satoshis();
                                res.push(ChannelDetails {
@@ -1463,6 +1477,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                        short_channel_id: channel.get_short_channel_id(),
                                        channel_value_satoshis: channel.get_value_satoshis(),
                                        unspendable_punishment_reserve: to_self_reserve_satoshis,
+                                       balance_msat,
                                        inbound_capacity_msat,
                                        outbound_capacity_msat,
                                        user_channel_id: channel.get_user_id(),
index cd7d62e6686ed9628d455f5b548ff17a1cfdf091..e49844383bbceee091e9d42b7262416109b2d484 100644 (file)
@@ -1521,6 +1521,7 @@ mod tests {
                        short_channel_id,
                        channel_value_satoshis: 0,
                        user_channel_id: 0,
+                       balance_msat: 0,
                        outbound_capacity_msat,
                        inbound_capacity_msat: 42,
                        unspendable_punishment_reserve: None,