@@ -10537,9 +10537,10 @@ where
1053710537 Ok(splice_ack_msg)
1053810538 }
1053910539
10540- /// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
10540+ /// Compute the channel balances (local & remote, in msats) by taking into account fees,
10541+ // anchor values, and dust limits.
1054110542 /// Pending HTLCs are not taken into account, this method should be used when there is no such,
10542- /// e.g. in quiscence state
10543+ /// e.g. in quiescence state
1054310544 #[cfg(splicing)]
1054410545 fn compute_balances_less_fees(
1054510546 &self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
@@ -10555,12 +10556,12 @@ where
1055510556 ((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
1055610557 debug_assert!(value_to_remote_msat >= 0);
1055710558
10558- let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(
10559+ let total_fee_sats = SpecTxBuilder {}.commit_tx_fee_sat(
1055910560 feerate_per_kw,
1056010561 0,
1056110562 &self.funding.channel_transaction_parameters.channel_type_features,
1056210563 );
10563- let anchors_val = if self
10564+ let anchors_val_sats = if self
1056410565 .funding
1056510566 .channel_transaction_parameters
1056610567 .channel_type_features
@@ -10572,34 +10573,37 @@ where
1057210573 } as i64;
1057310574
1057410575 // consider fees and anchor values
10575- let (mut value_to_self, mut value_to_remote) = if self.funding.is_outbound() {
10576+ let (mut new_value_to_self_msat, mut new_value_to_remote_msat) = if self
10577+ .funding
10578+ .is_outbound()
10579+ {
1057610580 (
10577- ( value_to_self_msat as i64) / 1000 - anchors_val - total_fee_sat as i64,
10578- value_to_remote_msat / 1000 ,
10581+ value_to_self_msat as i64 - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
10582+ value_to_remote_msat,
1057910583 )
1058010584 } else {
1058110585 (
10582- ( value_to_self_msat as i64) / 1000 ,
10583- value_to_remote_msat / 1000 - anchors_val - total_fee_sat as i64,
10586+ value_to_self_msat as i64,
10587+ value_to_remote_msat - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
1058410588 )
1058510589 };
1058610590
1058710591 // consider dust limit
10588- let broadcaster_dust_limit_satoshis = if is_local {
10592+ let broadcaster_dust_limit_sats = if is_local {
1058910593 self.context.holder_dust_limit_satoshis
1059010594 } else {
1059110595 self.context.counterparty_dust_limit_satoshis
1059210596 } as i64;
10593- if value_to_self < broadcaster_dust_limit_satoshis {
10594- value_to_self = 0;
10597+ if new_value_to_self_msat < (broadcaster_dust_limit_sats * 1000) {
10598+ new_value_to_self_msat = 0;
1059510599 }
10596- debug_assert!(value_to_self >= 0);
10597- if value_to_remote < broadcaster_dust_limit_satoshis {
10598- value_to_remote = 0;
10600+ debug_assert!(new_value_to_self_msat >= 0);
10601+ if new_value_to_remote_msat < (broadcaster_dust_limit_sats * 1000) {
10602+ new_value_to_remote_msat = 0;
1059910603 }
10600- debug_assert!(value_to_remote >= 0);
10604+ debug_assert!(new_value_to_remote_msat >= 0);
1060110605
10602- (value_to_self as u64, value_to_remote as u64)
10606+ (new_value_to_self_msat as u64, new_value_to_remote_msat as u64)
1060310607 }
1060410608
1060510609 /// Handle splice_ack
@@ -10614,31 +10618,31 @@ where
1061410618
1061510619 // Pre-check for reserve requirement
1061610620 // (Note: It should also be checked later at tx_complete)
10617- let our_funding_contribution = pending_splice.our_funding_contribution;
10618- let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
10619-
10620- let pre_channel_value = self.funding.get_value_satoshis();
10621- let post_channel_value = PendingSplice::compute_post_value(
10622- pre_channel_value ,
10623- our_funding_contribution ,
10624- their_funding_contribution_satoshis ,
10621+ let our_funding_contribution_sats = pending_splice.our_funding_contribution;
10622+ let their_funding_contribution_sats = msg.funding_contribution_satoshis;
10623+
10624+ let pre_channel_value_sats = self.funding.get_value_satoshis();
10625+ let post_channel_value_sats = PendingSplice::compute_post_value(
10626+ pre_channel_value_sats ,
10627+ our_funding_contribution_sats ,
10628+ their_funding_contribution_sats ,
1062510629 );
10626- let pre_balance_self = self.funding.value_to_self_msat;
10627- let post_balance_self =
10628- PendingSplice::add_checked(pre_balance_self, our_funding_contribution );
10629- let (pre_balance_self_less_fees, pre_balance_counterparty_less_fees ) =
10630- self.compute_balances_less_fees(pre_channel_value, pre_balance_self , true);
10631- let (post_balance_self_less_fees, post_balance_counterparty_less_fees ) =
10632- self.compute_balances_less_fees(post_channel_value, post_balance_self , true);
10630+ let pre_balance_self_msat = self.funding.value_to_self_msat;
10631+ let post_balance_self_msat =
10632+ PendingSplice::add_checked(pre_balance_self_msat, our_funding_contribution_sats * 1000 );
10633+ let (pre_balance_self_less_fees_msat, pre_balance_counterparty_less_fees_msat ) =
10634+ self.compute_balances_less_fees(pre_channel_value_sats, pre_balance_self_msat , true);
10635+ let (post_balance_self_less_fees_msat, post_balance_counterparty_less_fees_msat ) =
10636+ self.compute_balances_less_fees(post_channel_value_sats, post_balance_self_msat , true);
1063310637 // Pre-check for reserve requirement
1063410638 // This will also be checked later at tx_complete
1063510639 let _res = self.check_splice_balances_meet_v2_reserve_requirements(
10636- pre_balance_self_less_fees ,
10637- post_balance_self_less_fees ,
10638- pre_balance_counterparty_less_fees ,
10639- post_balance_counterparty_less_fees ,
10640- pre_channel_value ,
10641- post_channel_value ,
10640+ pre_balance_self_less_fees_msat ,
10641+ post_balance_self_less_fees_msat ,
10642+ pre_balance_counterparty_less_fees_msat ,
10643+ post_balance_counterparty_less_fees_msat ,
10644+ pre_channel_value_sats ,
10645+ post_channel_value_sats ,
1064210646 )?;
1064310647 Ok(())
1064410648 }
@@ -10695,59 +10699,58 @@ where
1069510699 ))
1069610700 }
1069710701
10698- /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
1069910702 /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
10700- /// Returns the minimum channel reserve ( sats)
10703+ /// In case of error, it returns the minimum channel reserve that was violated (in sats)
1070110704 #[cfg(splicing)]
10702- pub fn check_splice_balance_meets_v2_reserve_requirement_noerr (
10703- &self, pre_balance : u64, post_balance : u64, pre_channel_value : u64,
10704- post_channel_value : u64, dust_limit : u64,
10705+ pub fn check_splice_balance_meets_v2_reserve_requirement (
10706+ &self, pre_balance_msat : u64, post_balance_msat : u64, pre_channel_value_sats : u64,
10707+ post_channel_value_sats : u64, dust_limit_sats : u64,
1070510708 ) -> Result<(), u64> {
1070610709 let post_channel_reserve_sats =
10707- get_v2_channel_reserve_satoshis(post_channel_value, dust_limit );
10708- if post_balance >= post_channel_reserve_sats * 1000 {
10710+ get_v2_channel_reserve_satoshis(post_channel_value_sats, dust_limit_sats );
10711+ if post_balance_msat >= ( post_channel_reserve_sats * 1000) {
1070910712 return Ok(());
1071010713 }
1071110714 // We're not allowed to dip below the reserve once we've been above,
1071210715 // check differently for originally v1 and v2 channels
1071310716 if self.is_v2_established() {
1071410717 let pre_channel_reserve_sats =
10715- get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit );
10716- if pre_balance >= pre_channel_reserve_sats * 1000 {
10718+ get_v2_channel_reserve_satoshis(pre_channel_value_sats, dust_limit_sats );
10719+ if pre_balance_msat >= ( pre_channel_reserve_sats * 1000) {
1071710720 return Err(post_channel_reserve_sats);
1071810721 }
1071910722 } else {
10720- if pre_balance >= self.funding.holder_selected_channel_reserve_satoshis * 1000 {
10723+ if pre_balance_msat >= ( self.funding.holder_selected_channel_reserve_satoshis * 1000) {
1072110724 return Err(post_channel_reserve_sats);
1072210725 }
1072310726 if let Some(cp_reserve) = self.funding.counterparty_selected_channel_reserve_satoshis {
10724- if pre_balance >= cp_reserve * 1000 {
10727+ if pre_balance_msat >= ( cp_reserve * 1000) {
1072510728 return Err(post_channel_reserve_sats);
1072610729 }
1072710730 }
1072810731 }
1072910732 // Make sure we either remain with the same balance or move towards the reserve.
10730- if post_balance >= pre_balance {
10733+ if post_balance_msat >= pre_balance_msat {
1073110734 Ok(())
1073210735 } else {
1073310736 Err(post_channel_reserve_sats)
1073410737 }
1073510738 }
1073610739
10737- /// Check that balances meet the channel reserve requirements or violates them (below reserve).
10740+ /// Check that balances (self and counterparty) meet the channel reserve requirements or violates them (below reserve).
1073810741 /// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
1073910742 /// to check with new channel value (before being committed to it).
1074010743 #[cfg(splicing)]
1074110744 pub fn check_splice_balances_meet_v2_reserve_requirements(
1074210745 &self, self_balance_pre_msat: u64, self_balance_post_msat: u64,
1074310746 counterparty_balance_pre_msat: u64, counterparty_balance_post_msat: u64,
10744- channel_value_pre : u64, channel_value_post : u64,
10747+ channel_value_pre_sats : u64, channel_value_post_sats : u64,
1074510748 ) -> Result<(), ChannelError> {
10746- let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
10749+ let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement (
1074710750 self_balance_pre_msat,
1074810751 self_balance_post_msat,
10749- channel_value_pre ,
10750- channel_value_post ,
10752+ channel_value_pre_sats ,
10753+ channel_value_post_sats ,
1075110754 self.context.holder_dust_limit_satoshis,
1075210755 );
1075310756 if let Err(channel_reserve_self) = is_ok_self {
@@ -10756,11 +10759,11 @@ where
1075610759 self_balance_post_msat, channel_reserve_self,
1075710760 )));
1075810761 }
10759- let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
10762+ let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement (
1076010763 counterparty_balance_pre_msat,
1076110764 counterparty_balance_post_msat,
10762- channel_value_pre ,
10763- channel_value_post ,
10765+ channel_value_pre_sats ,
10766+ channel_value_post_sats ,
1076410767 self.context.counterparty_dust_limit_satoshis,
1076510768 );
1076610769 if let Err(channel_reserve_cp) = is_ok_cp {
0 commit comments