Skip to content

Commit 0c7ea78

Browse files
committed
fixup: Add TxBuilder::get_next_commitment_stats
So far we have always panicked if the total value of HTLCs is greater than the payer's balance; we continue to do so here. Also adjust `subtract_addl_outputs` for clarity
1 parent 32dcbc7 commit 0c7ea78

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

lightning/src/sign/tx_builder.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,33 @@ fn excess_fees_on_counterparty_tx_dust_exposure_msat(
7979
}
8080

8181
fn subtract_addl_outputs(
82-
is_outbound_from_holder: bool, value_to_self_after_htlcs: Option<u64>,
83-
value_to_remote_after_htlcs: Option<u64>, channel_type: &ChannelTypeFeatures,
82+
is_outbound_from_holder: bool, value_to_self_after_htlcs: u64,
83+
value_to_remote_after_htlcs: u64, channel_type: &ChannelTypeFeatures,
8484
) -> (Option<u64>, Option<u64>) {
8585
let total_anchors_sat = if channel_type.supports_anchors_zero_fee_htlc_tx() {
8686
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
8787
} else {
8888
0
8989
};
9090

91-
let mut local_balance_before_fee_msat = value_to_self_after_htlcs;
92-
let mut remote_balance_before_fee_msat = value_to_remote_after_htlcs;
93-
9491
// We MUST use checked subs here, as the funder's balance is not guaranteed to be greater
9592
// than or equal to `total_anchors_sat`.
9693
//
9794
// This is because when the remote party sends an `update_fee` message, we build the new
9895
// commitment transaction *before* checking whether the remote party's balance is enough to
9996
// cover the total anchor sum.
10097

101-
if is_outbound_from_holder {
102-
local_balance_before_fee_msat = local_balance_before_fee_msat
103-
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000));
98+
let local_balance_before_fee_msat = if is_outbound_from_holder {
99+
value_to_self_after_htlcs.checked_sub(total_anchors_sat * 1000)
104100
} else {
105-
remote_balance_before_fee_msat = remote_balance_before_fee_msat
106-
.and_then(|balance_msat| balance_msat.checked_sub(total_anchors_sat * 1000));
107-
}
101+
Some(value_to_self_after_htlcs)
102+
};
103+
104+
let remote_balance_before_fee_msat = if !is_outbound_from_holder {
105+
value_to_remote_after_htlcs.checked_sub(total_anchors_sat * 1000)
106+
} else {
107+
Some(value_to_remote_after_htlcs)
108+
};
108109

109110
(local_balance_before_fee_msat, remote_balance_before_fee_msat)
110111
}
@@ -181,10 +182,12 @@ impl TxBuilder for SpecTxBuilder {
181182
.iter()
182183
.filter_map(|htlc| (!htlc.outbound).then_some(htlc.amount_msat))
183184
.sum();
184-
let value_to_holder_after_htlcs =
185-
value_to_holder_msat.checked_sub(outbound_htlcs_value_msat);
186-
let value_to_counterparty_after_htlcs =
187-
value_to_counterparty_msat.checked_sub(inbound_htlcs_value_msat);
185+
let value_to_holder_after_htlcs = value_to_holder_msat
186+
.checked_sub(outbound_htlcs_value_msat)
187+
.expect("outbound_htlcs_value_msat is greater than value_to_holder_msat");
188+
let value_to_counterparty_after_htlcs = value_to_counterparty_msat
189+
.checked_sub(inbound_htlcs_value_msat)
190+
.expect("inbound_htlcs_value_msat is greater than value_to_counterparty_msat");
188191

189192
// Subtract the anchors from the channel funder
190193
let (holder_balance_msat, counterparty_balance_msat) = subtract_addl_outputs(

0 commit comments

Comments
 (0)