Skip to content

Commit 195a753

Browse files
committed
Add NextCommitmentStats::get_balances_including_fee
`NextCommitmentStats` provides the commitment transaction fee as a separate value to assist with applying a multiplier on it in `can_accept_incoming_htlc`. Nonetheless in most cases, we want the balances to include the commitment transaction fee, so here we add a helper that gives us these balances.
1 parent e63b416 commit 195a753

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lightning/src/sign/tx_builder.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl HTLCAmountDirection {
3535
}
3636

3737
pub(crate) struct NextCommitmentStats {
38+
pub is_outbound_from_holder: bool,
3839
pub inbound_htlcs_count: usize,
3940
pub inbound_htlcs_value_msat: u64,
4041
pub holder_balance_before_fee_msat: Option<u64>,
@@ -48,6 +49,26 @@ pub(crate) struct NextCommitmentStats {
4849
pub extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat: Option<u64>,
4950
}
5051

52+
impl NextCommitmentStats {
53+
pub(crate) fn get_balances_including_fee_msat(&self) -> (Option<u64>, Option<u64>) {
54+
let holder_balance_incl_fee_msat = if self.is_outbound_from_holder {
55+
self.holder_balance_before_fee_msat
56+
.and_then(|balance| balance.checked_sub(self.commit_tx_fee_sat * 1000))
57+
} else {
58+
self.holder_balance_before_fee_msat
59+
};
60+
61+
let counterparty_balance_incl_fee_msat = if self.is_outbound_from_holder {
62+
self.counterparty_balance_before_fee_msat
63+
} else {
64+
self.counterparty_balance_before_fee_msat
65+
.and_then(|balance| balance.checked_sub(self.commit_tx_fee_sat * 1000))
66+
};
67+
68+
(holder_balance_incl_fee_msat, counterparty_balance_incl_fee_msat)
69+
}
70+
}
71+
5172
#[rustfmt::skip]
5273
fn excess_fees_on_counterparty_tx_dust_exposure_msat(
5374
next_commitment_htlcs: &[HTLCAmountDirection], dust_buffer_feerate: u32,
@@ -243,6 +264,7 @@ impl TxBuilder for SpecTxBuilder {
243264
};
244265

245266
NextCommitmentStats {
267+
is_outbound_from_holder,
246268
inbound_htlcs_count,
247269
inbound_htlcs_value_msat,
248270
holder_balance_before_fee_msat,

0 commit comments

Comments
 (0)