Skip to content

Commit 1818c4a

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 35d4ebb commit 1818c4a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 2 deletions
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>) {

lightning/src/ln/functional_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8101,6 +8101,43 @@ fn test_bump_txn_sanitize_tracking_maps() {
81018101
}
81028102
}
81038103

8104+
#[test]
8105+
fn test_pending_claimed_htlc_no_balance_underflow() {
8106+
// Tests that if we have a pending outbound HTLC as well as a claimed-but-not-fully-removed
8107+
// HTLC we will not underflow when we call `Channel::get_balance_msat()`.
8108+
let chanmon_cfgs = create_chanmon_cfgs(2);
8109+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8110+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8111+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8112+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, InitFeatures::known(), InitFeatures::known());
8113+
8114+
let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1_010_000).0;
8115+
nodes[1].node.claim_funds(payment_preimage);
8116+
check_added_monitors!(nodes[1], 1);
8117+
let fulfill_ev = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8118+
8119+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &fulfill_ev.update_fulfill_htlcs[0]);
8120+
expect_payment_sent_without_paths!(nodes[0], payment_preimage);
8121+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &fulfill_ev.commitment_signed);
8122+
check_added_monitors!(nodes[0], 1);
8123+
let (_raa, _cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
8124+
8125+
// At this point nodes[1] has received 1,010k msat (10k msat more than their reserve) and can
8126+
// send an HTLC back (though it will go in the holding cell). Send an HTLC back and check we
8127+
// can get our balance.
8128+
8129+
// Get a route from nodes[1] to nodes[0] by getting a route going the other way and then flip
8130+
// the public key of the only hop. This works around ChannelDetails not showing the
8131+
// almost-claimed HTLC as available balance.
8132+
let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
8133+
route.payment_params = None; // This is all wrong, but unnecessary
8134+
route.paths[0][0].pubkey = nodes[0].node.get_our_node_id();
8135+
let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[0]);
8136+
nodes[1].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
8137+
8138+
assert_eq!(nodes[1].node.list_channels()[0].balance_msat, 1_000_000);
8139+
}
8140+
81048141
#[test]
81058142
fn test_channel_conf_timeout() {
81068143
// Tests that, for inbound channels, we give up on them if the funding transaction does not

0 commit comments

Comments
 (0)