Skip to content

Commit 83588a3

Browse files
committed
Include inbound-claimed-HTLCs in reported channel balances
Given the balance is reported as "total balance if we went to chain ignoring fees", it seems reasonable to include claimed HTLCs - if we went to chain we'd get those funds, less on-chain fees. Further, if we do not include them, its possible to have pending outbound holding-cell HTLCs underflow the balance calculation, causing a panic in debug mode, and bogus values in release. This resolves a subtraction underflow bug found by the `chanmon_consistency` fuzz target.
1 parent 7b6a7bb commit 83588a3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lightning/src/ln/channel.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2155,8 +2155,15 @@ impl<Signer: Sign> Channel<Signer> {
21552155
/// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
21562156
/// See also [`Channel::get_inbound_outbound_available_balance_msat`]
21572157
pub fn get_balance_msat(&self) -> u64 {
2158-
self.value_to_self_msat
2159-
- self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
2158+
// Include our local balance, plus any inbound HTLCs we know the preimage for, minus any
2159+
// HTLCs sent or which will be sent after commitment signed's are exchanged.
2160+
let mut balance_msat = self.value_to_self_msat;
2161+
for ref htlc in self.pending_inbound_htlcs.iter() {
2162+
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
2163+
balance_msat += htlc.amount_msat;
2164+
}
2165+
}
2166+
balance_msat - self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
21602167
}
21612168

21622169
pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {

0 commit comments

Comments
 (0)