Skip to content

Commit 85f1b0c

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. Also make the style of `tx_builder::subtract_addl_outputs` consistent with `get_balances_including_fee`.
1 parent e55084c commit 85f1b0c

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

lightning/src/sign/tx_builder.rs

Lines changed: 34 additions & 14 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+
if self.is_outbound_from_holder {
55+
(
56+
self.holder_balance_before_fee_msat.and_then(|balance_msat| {
57+
balance_msat.checked_sub(self.commit_tx_fee_sat * 1000)
58+
}),
59+
self.counterparty_balance_before_fee_msat,
60+
)
61+
} else {
62+
(
63+
self.holder_balance_before_fee_msat,
64+
self.counterparty_balance_before_fee_msat.and_then(|balance_msat| {
65+
balance_msat.checked_sub(self.commit_tx_fee_sat * 1000)
66+
}),
67+
)
68+
}
69+
}
70+
}
71+
5172
fn excess_fees_on_counterparty_tx_dust_exposure_msat(
5273
next_commitment_htlcs: &[HTLCAmountDirection], dust_buffer_feerate: u32, excess_feerate: u32,
5374
counterparty_dust_limit_satoshis: u64, dust_htlc_exposure_msat: u64,
@@ -126,21 +147,19 @@ fn subtract_addl_outputs(
126147
// commitment transaction *before* checking whether the remote party's balance is enough to
127148
// cover the total anchor sum.
128149

129-
let local_balance_before_fee_msat = if is_outbound_from_holder {
130-
value_to_self_after_htlcs_msat
131-
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000))
132-
} else {
133-
value_to_self_after_htlcs_msat
134-
};
135-
136-
let remote_balance_before_fee_msat = if !is_outbound_from_holder {
137-
value_to_remote_after_htlcs_msat
138-
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000))
150+
if is_outbound_from_holder {
151+
(
152+
value_to_self_after_htlcs_msat
153+
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000)),
154+
value_to_remote_after_htlcs_msat,
155+
)
139156
} else {
140-
value_to_remote_after_htlcs_msat
141-
};
142-
143-
(local_balance_before_fee_msat, remote_balance_before_fee_msat)
157+
(
158+
value_to_self_after_htlcs_msat,
159+
value_to_remote_after_htlcs_msat
160+
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000)),
161+
)
162+
}
144163
}
145164

146165
fn get_dust_buffer_feerate(feerate_per_kw: u32) -> u32 {
@@ -280,6 +299,7 @@ impl TxBuilder for SpecTxBuilder {
280299
};
281300

282301
NextCommitmentStats {
302+
is_outbound_from_holder,
283303
inbound_htlcs_count,
284304
inbound_htlcs_value_msat,
285305
holder_balance_before_fee_msat,

0 commit comments

Comments
 (0)