Add `NextCommitmentStats::get_balances_including_fee`
What changed, and why it matters
This commit adds a small helper function to include transaction fees when calculating Lightning channel balances. It also makes an existing internal function's code style match the new helper. There is no obvious security bug being fixed; it appears to be a routine code cleanup and convenience addition.
No immediate action required. Review as part of normal code maintenance. If this commit is part of a larger PR, consider whether the new helper is used correctly in subsequent commits.
Security signals we found
No security-relevant keywords in commit title or message
No bug-fix language or CVE references
Change is additive (new helper) and stylistic (refactor)
Uses checked_sub to avoid underflow, which is defensive but not a fix for a known bug
Evidence from the diff
The patch introduces NextCommitmentStats::get_balances_including_fee_msat, which returns (holder_balance, counterparty_balance) after subtracting the commitment transaction fee from the party responsible for paying it. It stores is_outbound_from_holder in NextCommitmentStats to decide which side pays the fee. It also refactors subtract_addl_outputs to use the same tuple-return style. The change is purely additive and stylistic; no vulnerability or security fix is described or evident in the diff.
Changed components
lightning/src/sign/tx_builder.rsNextCommitmentStats structsubtract_addl_outputs functionInspect captured patch +34 / −14
diff --git a/lightning/src/sign/tx_builder.rs b/lightning/src/sign/tx_builder.rs
index f9c871d..ed3f474 100644
--- a/lightning/src/sign/tx_builder.rs
+++ b/lightning/src/sign/tx_builder.rs
@@ -35,6 +35,7 @@ impl HTLCAmountDirection {
}
pub(crate) struct NextCommitmentStats {
+ pub is_outbound_from_holder: bool,
pub inbound_htlcs_count: usize,
pub inbound_htlcs_value_msat: u64,
pub holder_balance_before_fee_msat: Option<u64>,
@@ -48,6 +49,26 @@ pub(crate) struct NextCommitmentStats {
pub extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat: Option<u64>,
}
+impl NextCommitmentStats {
+ pub(crate) fn get_balances_including_fee_msat(&self) -> (Option<u64>, Option<u64>) {
+ if self.is_outbound_from_holder {
+ (
+ self.holder_balance_before_fee_msat.and_then(|balance_msat| {
+ balance_msat.checked_sub(self.commit_tx_fee_sat * 1000)
+ }),
+ self.counterparty_balance_before_fee_msat,
+ )
+ } else {
+ (
+ self.holder_balance_before_fee_msat,
+ self.counterparty_balance_before_fee_msat.and_then(|balance_msat| {
+ balance_msat.checked_sub(self.commit_tx_fee_sat * 1000)
+ }),
+ )
+ }
+ }
+}
+
fn excess_fees_on_counterparty_tx_dust_exposure_msat(
next_commitment_htlcs: &[HTLCAmountDirection], dust_buffer_feerate: u32, excess_feerate: u32,
counterparty_dust_limit_satoshis: u64, dust_htlc_exposure_msat: u64,
@@ -126,21 +147,19 @@ fn subtract_addl_outputs(
// commitment transaction *before* checking whether the remote party's balance is enough to
// cover the total anchor sum.
- let local_balance_before_fee_msat = if is_outbound_from_holder {
- value_to_self_after_htlcs_msat
- .and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000))
- } else {
- value_to_self_after_htlcs_msat
- };
-
- let remote_balance_before_fee_msat = if !is_outbound_from_holder {
- value_to_remote_after_htlcs_msat
- .and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000))
+ if is_outbound_from_holder {
+ (
+ value_to_self_after_htlcs_msat
+ .and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000)),
+ value_to_remote_after_htlcs_msat,
+ )
} else {
- value_to_remote_after_htlcs_msat
- };
-
- (local_balance_before_fee_msat, remote_balance_before_fee_msat)
+ (
+ value_to_self_after_htlcs_msat,
+ value_to_remote_after_htlcs_msat
+ .and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000)),
+ )
+ }
}
fn get_dust_buffer_feerate(feerate_per_kw: u32) -> u32 {
@@ -280,6 +299,7 @@ impl TxBuilder for SpecTxBuilder {
};
NextCommitmentStats {
+ is_outbound_from_holder,
inbound_htlcs_count,
inbound_htlcs_value_msat,
holder_balance_before_fee_msat,
Why this scored 17/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.