Skip to content

Commit d7672d4

Browse files
committed
Consider anchor outputs value in get_available_balances
This could lead us to sending/forwarding HTLCs that would put us below our reserve, forcing our counterparty to close the channel on us due to an invalid update.
1 parent 37a3a03 commit d7672d4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
724724

725725
cur_holder_commitment_transaction_number: u64,
726726
cur_counterparty_commitment_transaction_number: u64,
727-
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
727+
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
728728
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
729729
pending_outbound_htlcs: Vec<OutboundHTLCOutput>,
730730
holding_cell_htlc_updates: Vec<HTLCUpdateAwaitingACK>,
@@ -1673,6 +1673,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
16731673

16741674
let mut available_capacity_msat = outbound_capacity_msat;
16751675

1676+
let anchor_outputs_value_msat = if context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
1677+
ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
1678+
} else {
1679+
0
1680+
};
16761681
if context.is_outbound() {
16771682
// We should mind channel commit tx fee when computing how much of the available capacity
16781683
// can be used in the next htlc. Mirrors the logic in send_htlc.
@@ -1694,7 +1699,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
16941699
// We will first subtract the fee as if we were above-dust. Then, if the resulting
16951700
// value ends up being below dust, we have this fee available again. In that case,
16961701
// match the value to right-below-dust.
1697-
let mut capacity_minus_commitment_fee_msat: i64 = (available_capacity_msat as i64) - (max_reserved_commit_tx_fee_msat as i64);
1702+
let mut capacity_minus_commitment_fee_msat: i64 = available_capacity_msat as i64 -
1703+
max_reserved_commit_tx_fee_msat as i64 - anchor_outputs_value_msat as i64;
16981704
if capacity_minus_commitment_fee_msat < (real_dust_limit_timeout_sat as i64) * 1000 {
16991705
let one_htlc_difference_msat = max_reserved_commit_tx_fee_msat - min_reserved_commit_tx_fee_msat;
17001706
debug_assert!(one_htlc_difference_msat != 0);
@@ -1719,7 +1725,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17191725
let remote_balance_msat = (context.channel_value_satoshis * 1000 - context.value_to_self_msat)
17201726
.saturating_sub(inbound_stats.pending_htlcs_value_msat);
17211727

1722-
if remote_balance_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
1728+
if remote_balance_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat + anchor_outputs_value_msat {
17231729
// If another HTLC's fee would reduce the remote's balance below the reserve limit
17241730
// we've selected for them, we can only send dust HTLCs.
17251731
available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);

0 commit comments

Comments
 (0)