Skip to content

Commit 58ff9c1

Browse files
committed
Consider fees and anchor in balances when checking reserve requirements
1 parent 4509590 commit 58ff9c1

File tree

1 file changed

+73
-6
lines changed

1 file changed

+73
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10851,6 +10851,71 @@ where
1085110851
})
1085210852
}
1085310853

10854+
/// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
10855+
/// Pending HTLCs are not taken into account, this method should be used when there is no such,
10856+
/// e.g. in quiscence state
10857+
#[cfg(splicing)]
10858+
fn compute_balances_less_fees(
10859+
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
10860+
) -> (u64, u64) {
10861+
// We should get here only when there are no pending HTLCs, as they are not taken into account
10862+
debug_assert!(self.context.pending_inbound_htlcs.is_empty());
10863+
debug_assert!(self.context.pending_outbound_htlcs.is_empty());
10864+
10865+
let feerate_per_kw = self.context.feerate_per_kw;
10866+
10867+
// compute 'raw' counterparty balance
10868+
let value_to_remote_msat: i64 =
10869+
((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
10870+
debug_assert!(value_to_remote_msat >= 0);
10871+
10872+
let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(
10873+
feerate_per_kw,
10874+
0,
10875+
&self.funding.channel_transaction_parameters.channel_type_features,
10876+
);
10877+
let anchors_val = if self
10878+
.funding
10879+
.channel_transaction_parameters
10880+
.channel_type_features
10881+
.supports_anchors_zero_fee_htlc_tx()
10882+
{
10883+
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
10884+
} else {
10885+
0
10886+
} as i64;
10887+
10888+
// consider fees and anchor values
10889+
let (mut value_to_self, mut value_to_remote) = if self.funding.is_outbound() {
10890+
(
10891+
(value_to_self_msat as i64) / 1000 - anchors_val - total_fee_sat as i64,
10892+
value_to_remote_msat / 1000,
10893+
)
10894+
} else {
10895+
(
10896+
(value_to_self_msat as i64) / 1000,
10897+
value_to_remote_msat / 1000 - anchors_val - total_fee_sat as i64,
10898+
)
10899+
};
10900+
10901+
// consider dust limit
10902+
let broadcaster_dust_limit_satoshis = if is_local {
10903+
self.context.holder_dust_limit_satoshis
10904+
} else {
10905+
self.context.counterparty_dust_limit_satoshis
10906+
} as i64;
10907+
if value_to_self < broadcaster_dust_limit_satoshis {
10908+
value_to_self = 0;
10909+
}
10910+
debug_assert!(value_to_self >= 0);
10911+
if value_to_remote < broadcaster_dust_limit_satoshis {
10912+
value_to_remote = 0;
10913+
}
10914+
debug_assert!(value_to_remote >= 0);
10915+
10916+
(value_to_self as u64, value_to_remote as u64)
10917+
}
10918+
1085410919
/// Handle splice_ack
1085510920
#[cfg(splicing)]
1085610921
pub(crate) fn splice_ack<ES: Deref, L: Deref>(
@@ -10916,15 +10981,17 @@ where
1091610981
let pre_balance_self = self.funding.value_to_self_msat;
1091710982
let post_balance_self =
1091810983
PendingSplice::add_checked(pre_balance_self, our_funding_contribution_satoshis);
10919-
let pre_balance_counterparty = pre_channel_value.saturating_sub(pre_balance_self);
10920-
let post_balance_counterparty = post_channel_value.saturating_sub(post_balance_self);
10984+
let (pre_balance_self_less_fees, pre_balance_counterparty_less_fees) =
10985+
self.compute_balances_less_fees(pre_channel_value, pre_balance_self, true);
10986+
let (post_balance_self_less_fees, post_balance_counterparty_less_fees) =
10987+
self.compute_balances_less_fees(post_channel_value, post_balance_self, true);
1092110988
// Pre-check for reserve requirement
1092210989
// This will also be checked later at tx_complete
1092310990
let _res = self.check_splice_balances_meet_v2_reserve_requirements(
10924-
pre_balance_self,
10925-
post_balance_self,
10926-
pre_balance_counterparty,
10927-
post_balance_counterparty,
10991+
pre_balance_self_less_fees,
10992+
post_balance_self_less_fees,
10993+
pre_balance_counterparty_less_fees,
10994+
post_balance_counterparty_less_fees,
1092810995
pre_channel_value,
1092910996
post_channel_value,
1093010997
)?;

0 commit comments

Comments
 (0)