Pending balance counts force-close sweeps twice while the sweep is unconfirmed
Follow-up to #2167 / #4515, reported by @myxmaster in https://github.com/ZeusLN/zeus/pull/4515#issuecomment-5832940713.
LND
After a force close's commitment transaction confirms, the channel sits in pending_force_closing_channels and its limbo_balance is counted in total_limbo_balance. lnd broadcasts the sweep one block before maturity. The sweep pays a wallet address, so while it is unconfirmed it is also counted as external unconfirmed funds. Both land on the pending line until the sweep confirms.
Regtest vector from the report (local and remote force close both reproduce):
pendingchannels: pending_force_closing_channels, limbo_balance 96,860
walletbalance: confirmed 991,415 unconfirmed 96,395 total 1,087,810
listchaintxns: sweep 739695d3… amount 96,395 is_our_address true
ZEUS: total 991,415 + pending 193,255 = 1,184,670
overcount 96,860 = exactly the limbo
This is pre-existing on master. #4515 fixes the same overlap for cooperative closes by joining waiting_close_channels[].closing_txid to the external unconfirmed txids. The CSV phase before the sweep displays correctly because the commitment transaction has no wallet outputs.
Proposed join: an unconfirmed external tx whose previous_outpoints[].outpoint spends a force-closed channel's closing_txid. Requirements:
- Deduplicate by txid before summing. lnd's sweeper batches inputs from several channels into one sweep transaction, so summing per channel subtracts the same tx more than once.
- Second-level HTLC sweeps spend the HTLC transaction, not the commitment. Include the txids from
pending_htlcs[].outpointin the join set. - The sweep amount is net of the sweep fee (96,395 vs 96,860 above). Dropping the whole limbo is exact only when the channel has no
pending_htlcs. Otherwise subtractmin(limbo, swept amount), which leaves the fee on the pending line until confirmation.
LDK Node
The #4515 overlap correction does not apply on LDK. ChannelsStore passes only waiting_close_channels to getCooperativeCloses, and LdkNode.getPendingChannels reports cooperative closes in pending_closing_channels, with closing_txid taken from a sweep's latestSpendingTxid (usually '' for a cooperative close, which has no sweep entry).
total_limbo_balance on LDK includes cooperative closes awaiting confirmation. If totalOnchainBalanceSats includes the unconfirmed closing output (expected, since it pays the wallet's shutdown script), the overcount equals the limbo. LDK also appears to keep reporting the close as ClaimableAwaitingConfirmations until about 6 confirmations, which would extend the overcount past the first confirmation, when the output is no longer unconfirmed. Both points need a regtest check.
Force-close sweeps on LDK likely overlap the same way as on LND while the sweep is broadcastAwaitingConfirmation.
Unrelated cleanup in the same area: LdkNode.getLightningBalance computes pendingCloseBalance (backends/LdkNode.ts:387) and never returns it. The pending-close amount comes from getPendingChannels total_limbo_balance instead, so the block can be removed.
I'd like to take the LND half of this, following the #4515 pattern in BalanceUtils: join unconfirmed external txs on previous_outpoints against each force close's closing_txid plus the pending_htlcs outpoint txids, dedupe by txid, drop the whole limbo when the channel has no pending HTLCs, and min(limbo, swept) otherwise. I'll leave the LDK part alone, since it needs the regtest checks you listed. Happy to step back if @myxmaster is already on it.