Skip to content

Commit 654ce40

Browse files
committed
Consider fees and anchor in balances when checking reserve requirements
1 parent aeaf392 commit 654ce40

File tree

1 file changed

+85
-13
lines changed

1 file changed

+85
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10851,6 +10851,74 @@ 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+
/// e.g. in quiescence 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_sats = 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_sats = 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 new_value_to_self_msat, mut new_value_to_remote_msat) = if self
10890+
.funding
10891+
.is_outbound()
10892+
{
10893+
(
10894+
value_to_self_msat as i64 - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000,
10895+
value_to_remote_msat,
10896+
)
10897+
} else {
10898+
(
10899+
value_to_self_msat as i64,
10900+
value_to_remote_msat - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000,
10901+
)
10902+
};
10903+
10904+
// consider dust limit
10905+
let broadcaster_dust_limit_sats = if is_local {
10906+
self.context.holder_dust_limit_satoshis
10907+
} else {
10908+
self.context.counterparty_dust_limit_satoshis
10909+
} as i64;
10910+
if new_value_to_self_msat < (broadcaster_dust_limit_sats * 1000) {
10911+
new_value_to_self_msat = 0;
10912+
}
10913+
debug_assert!(new_value_to_self_msat >= 0);
10914+
if new_value_to_remote_msat < (broadcaster_dust_limit_sats * 1000) {
10915+
new_value_to_remote_msat = 0;
10916+
}
10917+
debug_assert!(new_value_to_remote_msat >= 0);
10918+
10919+
(new_value_to_self_msat as u64, new_value_to_remote_msat as u64)
10920+
}
10921+
1085410922
/// Handle splice_ack
1085510923
#[cfg(splicing)]
1085610924
pub(crate) fn splice_ack<ES: Deref, L: Deref>(
@@ -10908,25 +10976,29 @@ where
1090810976

1090910977
// Pre-check for reserve requirement
1091010978
// (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(
10979+
let pre_channel_value_sats = self.funding.get_value_satoshis();
10980+
let post_channel_value_sats = self.funding.compute_post_splice_value(
1091310981
our_funding_contribution_satoshis,
1091410982
their_funding_contribution_satoshis,
1091510983
);
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);
10984+
let pre_balance_self_msat = self.funding.value_to_self_msat;
10985+
let post_balance_self_msat = PendingSplice::add_checked(
10986+
pre_balance_self_msat,
10987+
our_funding_contribution_satoshis * 1000,
10988+
);
10989+
let (pre_balance_self_less_fees_msat, pre_balance_counterparty_less_fees_msat) =
10990+
self.compute_balances_less_fees(pre_channel_value_sats, pre_balance_self_msat, true);
10991+
let (post_balance_self_less_fees_msat, post_balance_counterparty_less_fees_msat) =
10992+
self.compute_balances_less_fees(post_channel_value_sats, post_balance_self_msat, true);
1092110993
// Pre-check for reserve requirement
1092210994
// This will also be checked later at tx_complete
1092310995
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,
10996+
pre_balance_self_less_fees_msat,
10997+
post_balance_self_less_fees_msat,
10998+
pre_balance_counterparty_less_fees_msat,
10999+
post_balance_counterparty_less_fees_msat,
11000+
pre_channel_value_sats,
11001+
post_channel_value_sats,
1093011002
)?;
1093111003

1093211004
log_info!(

0 commit comments

Comments
 (0)