Skip to content

Commit a5fa952

Browse files
committed
Consider fees and anchor in balances when checking reserve requirements
1 parent ba195ce commit a5fa952

File tree

1 file changed

+86
-13
lines changed

1 file changed

+86
-13
lines changed

lightning/src/ln/channel.rs

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

10854+
/// Compute the channel balances (local & remote, in msats) by taking into account fees,
10855+
/// anchor values, and dust limits.
10856+
/// Pending HTLCs are not taken into account, this method should be used when there is no such,
10857+
/// e.g. in quiescence state
10858+
#[cfg(splicing)]
10859+
fn compute_balances_less_fees(
10860+
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
10861+
) -> (u64, u64) {
10862+
// We should get here only when there are no pending HTLCs, as they are not taken into account
10863+
debug_assert!(self.context.pending_inbound_htlcs.is_empty());
10864+
debug_assert!(self.context.pending_outbound_htlcs.is_empty());
10865+
10866+
let feerate_per_kw = self.context.feerate_per_kw;
10867+
10868+
// compute 'raw' counterparty balance
10869+
let value_to_remote_msat: i64 =
10870+
((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
10871+
debug_assert!(value_to_remote_msat >= 0);
10872+
10873+
let total_fee_sats = SpecTxBuilder {}.commit_tx_fee_sat(
10874+
feerate_per_kw,
10875+
0,
10876+
&self.funding.channel_transaction_parameters.channel_type_features,
10877+
);
10878+
let anchors_val_sats = if self
10879+
.funding
10880+
.channel_transaction_parameters
10881+
.channel_type_features
10882+
.supports_anchors_zero_fee_htlc_tx()
10883+
{
10884+
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
10885+
} else {
10886+
0
10887+
} as i64;
10888+
10889+
// consider fees and anchor values
10890+
let (mut new_value_to_self_msat, mut new_value_to_remote_msat) = if self
10891+
.funding
10892+
.is_outbound()
10893+
{
10894+
(
10895+
value_to_self_msat as i64 - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000,
10896+
value_to_remote_msat,
10897+
)
10898+
} else {
10899+
(
10900+
value_to_self_msat as i64,
10901+
value_to_remote_msat - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000,
10902+
)
10903+
};
10904+
10905+
// consider dust limit
10906+
let broadcaster_dust_limit_sats = if is_local {
10907+
self.context.holder_dust_limit_satoshis
10908+
} else {
10909+
self.context.counterparty_dust_limit_satoshis
10910+
} as i64;
10911+
if new_value_to_self_msat < (broadcaster_dust_limit_sats * 1000) {
10912+
new_value_to_self_msat = 0;
10913+
}
10914+
debug_assert!(new_value_to_self_msat >= 0);
10915+
if new_value_to_remote_msat < (broadcaster_dust_limit_sats * 1000) {
10916+
new_value_to_remote_msat = 0;
10917+
}
10918+
debug_assert!(new_value_to_remote_msat >= 0);
10919+
10920+
(new_value_to_self_msat as u64, new_value_to_remote_msat as u64)
10921+
}
10922+
1085410923
/// Handle splice_ack
1085510924
#[cfg(splicing)]
1085610925
pub(crate) fn splice_ack<ES: Deref, L: Deref>(
@@ -10908,25 +10977,29 @@ where
1090810977

1090910978
// Pre-check for reserve requirement
1091010979
// (Note: It should also be checked later at tx_complete)
10911-
let pre_channel_value = self.funding.get_value_satoshis();
10912-
let post_channel_value = self.funding.compute_post_splice_value(
10980+
let pre_channel_value_sats = self.funding.get_value_satoshis();
10981+
let post_channel_value_sats = self.funding.compute_post_splice_value(
1091310982
our_funding_contribution_satoshis,
1091410983
their_funding_contribution_satoshis,
1091510984
);
10916-
let pre_balance_self = self.funding.value_to_self_msat;
10917-
let post_balance_self =
10918-
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);
10985+
let pre_balance_self_msat = self.funding.value_to_self_msat;
10986+
let post_balance_self_msat = PendingSplice::add_checked(
10987+
pre_balance_self_msat,
10988+
our_funding_contribution_satoshis * 1000,
10989+
);
10990+
let (pre_balance_self_less_fees_msat, pre_balance_counterparty_less_fees_msat) =
10991+
self.compute_balances_less_fees(pre_channel_value_sats, pre_balance_self_msat, true);
10992+
let (post_balance_self_less_fees_msat, post_balance_counterparty_less_fees_msat) =
10993+
self.compute_balances_less_fees(post_channel_value_sats, post_balance_self_msat, true);
1092110994
// Pre-check for reserve requirement
1092210995
// This will also be checked later at tx_complete
1092310996
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,
10928-
pre_channel_value,
10929-
post_channel_value,
10997+
pre_balance_self_less_fees_msat,
10998+
post_balance_self_less_fees_msat,
10999+
pre_balance_counterparty_less_fees_msat,
11000+
post_balance_counterparty_less_fees_msat,
11001+
pre_channel_value_sats,
11002+
post_channel_value_sats,
1093011003
)?;
1093111004

1093211005
log_info!(

0 commit comments

Comments
 (0)