Skip to content

Commit 61518f9

Browse files
authored
Merge pull request #1203 from lightning-signer/2021-12-value-to-self
Getter for the total channel balance
2 parents 3cf1b15 + 02a9f92 commit 61518f9

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
223223
force_close_spend_delay: None,
224224
is_outbound: true, is_funding_locked: true,
225225
is_usable: true, is_public: true,
226+
balance_msat: 0,
226227
outbound_capacity_msat: 0,
227228
});
228229
}

lightning/src/ln/channel.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,8 @@ impl<Signer: Sign> Channel<Signer> {
21222122
/// Doesn't bother handling the
21232123
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
21242124
/// corner case properly.
2125+
/// The channel reserve is subtracted from each balance.
2126+
/// See also [`Channel::get_balance_msat`]
21252127
pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
21262128
// Note that we have to handle overflow due to the above case.
21272129
(
@@ -2137,6 +2139,14 @@ impl<Signer: Sign> Channel<Signer> {
21372139
)
21382140
}
21392141

2142+
/// Get our total balance in msat.
2143+
/// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
2144+
/// See also [`Channel::get_inbound_outbound_available_balance_msat`]
2145+
pub fn get_balance_msat(&self) -> u64 {
2146+
self.value_to_self_msat
2147+
- self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
2148+
}
2149+
21402150
pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {
21412151
(self.holder_selected_channel_reserve_satoshis, self.counterparty_selected_channel_reserve_satoshis)
21422152
}

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,17 +868,30 @@ pub struct ChannelDetails {
868868
pub unspendable_punishment_reserve: Option<u64>,
869869
/// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
870870
pub user_channel_id: u64,
871+
/// Our total balance. This is the amount we would get if we close the channel.
872+
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
873+
/// amount is not likely to be recoverable on close.
874+
///
875+
/// This does not include any pending HTLCs which are not yet fully resolved (and, thus, whose
876+
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
877+
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
878+
/// This does not consider any on-chain fees.
879+
///
880+
/// See also [`ChannelDetails::outbound_capacity_msat`]
881+
pub balance_msat: u64,
871882
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
872-
/// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
883+
/// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
873884
/// available for inclusion in new outbound HTLCs). This further does not include any pending
874885
/// outgoing HTLCs which are awaiting some other resolution to be sent.
875886
///
887+
/// See also [`ChannelDetails::balance_msat`]
888+
///
876889
/// This value is not exact. Due to various in-flight changes, feerate changes, and our
877890
/// conflict-avoidance policy, exactly this amount is not likely to be spendable. However, we
878891
/// should be able to spend nearly this amount.
879892
pub outbound_capacity_msat: u64,
880893
/// The available inbound capacity for the remote peer to send HTLCs to us. This does not
881-
/// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
894+
/// include any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
882895
/// available for inclusion in new inbound HTLCs).
883896
/// Note that there are some corner cases not fully handled here, so the actual available
884897
/// 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
14491462
res.reserve(channel_state.by_id.len());
14501463
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
14511464
let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
1465+
let balance_msat = channel.get_balance_msat();
14521466
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
14531467
channel.get_holder_counterparty_selected_channel_reserve_satoshis();
14541468
res.push(ChannelDetails {
@@ -1463,6 +1477,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14631477
short_channel_id: channel.get_short_channel_id(),
14641478
channel_value_satoshis: channel.get_value_satoshis(),
14651479
unspendable_punishment_reserve: to_self_reserve_satoshis,
1480+
balance_msat,
14661481
inbound_capacity_msat,
14671482
outbound_capacity_msat,
14681483
user_channel_id: channel.get_user_id(),

lightning/src/routing/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,7 @@ mod tests {
15211521
short_channel_id,
15221522
channel_value_satoshis: 0,
15231523
user_channel_id: 0,
1524+
balance_msat: 0,
15241525
outbound_capacity_msat,
15251526
inbound_capacity_msat: 42,
15261527
unspendable_punishment_reserve: None,

0 commit comments

Comments
 (0)