Merge PR 'Fix initial debug commitment balances' (#5302)
What changed, and why it matters
This commit fixes an internal accounting check used only in debug builds. The balances recorded at channel creation did not subtract the small reserved funds used for anchor-style Lightning transactions, so the recorded counterparty balance could be slightly too high. In rare splice scenarios this mismatch could make a later sanity check fail and crash a debug build, but it would not affect production releases or allow an attacker to steal funds.
No urgent action. Treat as a normal bug fix. Ensure debug builds and tests pass after splicing operations. If running a fork, backport to keep debug assertions consistent, but production nodes are unaffected.
Security signals we found
Debug-only assertion variable mismatch fixed
Anchor output reserve now included in initial balance calculation
New regression test for splice-after-initial-push edge case
No change to release-build transaction construction or balance validation
Evidence from the diff
The patch corrects the initial values of holder_prev_commitment_tx_balance and counterparty_prev_commitment_tx_balance, which are #[cfg(debug_assertions)]-only assertion variables in FundingScope. Previously the code computed balances as raw channel value minus push amount or value_to_self_msat, ignoring total_anchors_sat (2 * ANCHOR_OUTPUT_VALUE_SATOSHI for anchor channels). The fix subtracts the anchor reserve from the holder balance and, where applicable, from the counterparty balance so the debug-only balance assertions stay consistent with real commitment transaction outputs. A new regression test, test_splice_out_after_initial_push, exercises a splice that would trip the old understatement. The change is purely defensive/debug instrumentation; no runtime funds-handling logic is altered.
Changed components
lightning/src/ln/channel.rslightning/src/ln/splicing_tests.rslightning/src/sign/tx_builder.rsInspect captured patch +56 / −11
### lightning/src/ln/channel.rs
@@ -77,6 +77,8 @@ use crate::ln::types::ChannelId;
use crate::offers::static_invoice::StaticInvoice;
use crate::routing::gossip::NodeId;
use crate::sign::ecdsa::EcdsaChannelSigner;
+#[cfg(debug_assertions)]
+use crate::sign::tx_builder::total_anchors_sat;
use crate::sign::tx_builder::{
ChannelConstraints, ChannelStats, HTLCAmountDirection, SpecTxBuilder, TxBuilder,
};
@@ -4440,6 +4442,12 @@ impl<SP: SignerProvider> ChannelContext<SP> {
let pubkeys = holder_signer.pubkeys(&secp_ctx);
+ #[cfg(debug_assertions)]
+ let counterparty_balance_msat = (channel_value_satoshis * 1000)
+ .checked_sub(value_to_self_msat)
+ .unwrap()
+ .saturating_sub(total_anchors_sat(&channel_type) * 1000);
+
let funding = FundingScope {
value_to_self_msat,
counterparty_selected_channel_reserve_satoshis: Some(msg_channel_reserve_satoshis),
@@ -4448,12 +4456,12 @@ impl<SP: SignerProvider> ChannelContext<SP> {
#[cfg(debug_assertions)]
holder_prev_commitment_tx_balance: Mutex::new((
value_to_self_msat,
- (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat),
+ counterparty_balance_msat,
)),
#[cfg(debug_assertions)]
counterparty_prev_commitment_tx_balance: Mutex::new((
value_to_self_msat,
- (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat),
+ counterparty_balance_msat,
)),
#[cfg(any(test, fuzzing))]
@@ -4756,6 +4764,10 @@ impl<SP: SignerProvider> ChannelContext<SP> {
.map(|f| f(&pubkeys))
.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
+ #[cfg(debug_assertions)]
+ let holder_balance_msat =
+ value_to_self_msat.saturating_sub(total_anchors_sat(&channel_type) * 1000);
+
let funding = FundingScope {
value_to_self_msat,
counterparty_selected_channel_reserve_satoshis: None, // Filled in in accept_channel
@@ -4764,15 +4776,9 @@ impl<SP: SignerProvider> ChannelContext<SP> {
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
// when we receive `accept_channel2`.
#[cfg(debug_assertions)]
- holder_prev_commitment_tx_balance: Mutex::new((
- channel_value_satoshis * 1000 - push_msat,
- push_msat,
- )),
+ holder_prev_commitment_tx_balance: Mutex::new((holder_balance_msat, push_msat)),
#[cfg(debug_assertions)]
- counterparty_prev_commitment_tx_balance: Mutex::new((
- channel_value_satoshis * 1000 - push_msat,
- push_msat,
- )),
+ counterparty_prev_commitment_tx_balance: Mutex::new((holder_balance_msat, push_msat)),
#[cfg(any(test, fuzzing))]
next_local_fee: Mutex::new(PredictedNextFee::default()),
@@ -6874,6 +6880,18 @@ impl<SP: SignerProvider> ChannelContext<SP> {
self.feerate_per_kw =
selected_commitment_sat_per_1000_weight(&fee_estimator, &next_channel_type);
funding.channel_transaction_parameters.channel_type_features = next_channel_type;
+ #[cfg(debug_assertions)]
+ {
+ let holder_balance_msat = funding
+ .value_to_self_msat
+ .saturating_sub(total_anchors_sat(funding.get_channel_type()) * 1000);
+ let counterparty_balance_msat = (funding.get_value_satoshis() * 1000)
+ .checked_sub(funding.value_to_self_msat)
+ .unwrap();
+ let initial_balances = (holder_balance_msat, counterparty_balance_msat);
+ *funding.holder_prev_commitment_tx_balance.lock().unwrap() = initial_balances;
+ *funding.counterparty_prev_commitment_tx_balance.lock().unwrap() = initial_balances;
+ }
Ok(())
}
### lightning/src/ln/splicing_tests.rs
@@ -1951,6 +1951,33 @@ fn test_splice_out() {
let _ = send_payment(&nodes[0], &[&nodes[1]], htlc_limit_msat);
}
+#[test]
+fn test_splice_out_after_initial_push() {
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+ let (_, _, channel_id, _) =
+ create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 500_000, 50_000_000);
+
+ // Splice before any HTLC updates, withdrawing more than the channel value minus twice the
+ // initial push so that an underestimated initial counterparty balance would fall below reserve.
+ let output = TxOut {
+ value: Amount::from_sat(410_000),
+ script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
+ };
+ let contribution =
+ initiate_splice_out(&nodes[0], &nodes[1], channel_id, vec![output.clone()]).unwrap();
+ let (splice_tx, _) = splice_channel(&nodes[0], &nodes[1], channel_id, contribution);
+ assert!(splice_tx.output.contains(&output));
+ mine_transaction(&nodes[0], &splice_tx);
+ mine_transaction(&nodes[1], &splice_tx);
+ lock_splice_after_blocks(&nodes[0], &nodes[1], ANTI_REORG_DELAY - 1);
+
+ send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
+ send_payment(&nodes[1], &[&nodes[0]], 1_000_000);
+}
+
#[test]
fn test_splice_in_and_out_funds_outputs_from_inputs() {
let chanmon_cfgs = create_chanmon_cfgs(2);
### lightning/src/sign/tx_builder.rs
@@ -98,7 +98,7 @@ fn commit_plus_htlc_tx_fees_msat(
(total_fees_msat, extra_accepted_htlc_total_fees_msat)
}
-fn total_anchors_sat(channel_type: &ChannelTypeFeatures) -> u64 {
+pub(crate) fn total_anchors_sat(channel_type: &ChannelTypeFeatures) -> u64 {
if channel_type.supports_anchors_zero_fee_htlc_tx() {
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
} else {Why this scored 27/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.