@@ -10851,9 +10851,10 @@ where
1085110851 })
1085210852 }
1085310853
10854- /// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
10854+ /// Compute the channel balances (local & remote, in msats) by taking into account fees,
10855+ // anchor values, and dust limits.
1085510856 /// Pending HTLCs are not taken into account, this method should be used when there is no such,
10856- /// e.g. in quiscence state
10857+ /// e.g. in quiescence state
1085710858 #[cfg(splicing)]
1085810859 fn compute_balances_less_fees(
1085910860 &self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
@@ -10869,12 +10870,12 @@ where
1086910870 ((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
1087010871 debug_assert!(value_to_remote_msat >= 0);
1087110872
10872- let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(
10873+ let total_fee_sats = SpecTxBuilder {}.commit_tx_fee_sat(
1087310874 feerate_per_kw,
1087410875 0,
1087510876 &self.funding.channel_transaction_parameters.channel_type_features,
1087610877 );
10877- let anchors_val = if self
10878+ let anchors_val_sats = if self
1087810879 .funding
1087910880 .channel_transaction_parameters
1088010881 .channel_type_features
@@ -10886,34 +10887,37 @@ where
1088610887 } as i64;
1088710888
1088810889 // consider fees and anchor values
10889- let (mut value_to_self, mut value_to_remote) = if self.funding.is_outbound() {
10890+ let (mut new_value_to_self_msat, mut new_value_to_remote_msat) = if self
10891+ .funding
10892+ .is_outbound()
10893+ {
1089010894 (
10891- ( value_to_self_msat as i64) / 1000 - anchors_val - total_fee_sat as i64,
10892- value_to_remote_msat / 1000 ,
10895+ value_to_self_msat as i64 - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
10896+ value_to_remote_msat,
1089310897 )
1089410898 } else {
1089510899 (
10896- ( value_to_self_msat as i64) / 1000 ,
10897- value_to_remote_msat / 1000 - anchors_val - total_fee_sat as i64,
10900+ value_to_self_msat as i64,
10901+ value_to_remote_msat - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
1089810902 )
1089910903 };
1090010904
1090110905 // consider dust limit
10902- let broadcaster_dust_limit_satoshis = if is_local {
10906+ let broadcaster_dust_limit_sats = if is_local {
1090310907 self.context.holder_dust_limit_satoshis
1090410908 } else {
1090510909 self.context.counterparty_dust_limit_satoshis
1090610910 } as i64;
10907- if value_to_self < broadcaster_dust_limit_satoshis {
10908- value_to_self = 0;
10911+ if new_value_to_self_msat < (broadcaster_dust_limit_sats * 1000) {
10912+ new_value_to_self_msat = 0;
1090910913 }
10910- debug_assert!(value_to_self >= 0);
10911- if value_to_remote < broadcaster_dust_limit_satoshis {
10912- value_to_remote = 0;
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;
1091310917 }
10914- debug_assert!(value_to_remote >= 0);
10918+ debug_assert!(new_value_to_remote_msat >= 0);
1091510919
10916- (value_to_self as u64, value_to_remote as u64)
10920+ (new_value_to_self_msat as u64, new_value_to_remote_msat as u64)
1091710921 }
1091810922
1091910923 /// Handle splice_ack
@@ -10973,27 +10977,29 @@ where
1097310977
1097410978 // Pre-check for reserve requirement
1097510979 // (Note: It should also be checked later at tx_complete)
10976- let pre_channel_value = self.funding.get_value_satoshis();
10977- 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(
1097810982 our_funding_contribution_satoshis,
1097910983 their_funding_contribution_satoshis,
1098010984 );
10981- let pre_balance_self = self.funding.value_to_self_msat;
10982- let post_balance_self =
10983- PendingSplice::add_checked(pre_balance_self, our_funding_contribution_satoshis);
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);
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);
1098810994 // Pre-check for reserve requirement
1098910995 // This will also be checked later at tx_complete
1099010996 let _res = self.check_splice_balances_meet_v2_reserve_requirements(
10991- pre_balance_self_less_fees ,
10992- post_balance_self_less_fees ,
10993- pre_balance_counterparty_less_fees ,
10994- post_balance_counterparty_less_fees ,
10995- pre_channel_value ,
10996- 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 ,
1099711003 )?;
1099811004
1099911005 log_info!(
@@ -11086,59 +11092,58 @@ where
1108611092 ))
1108711093 }
1108811094
11089- /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
1109011095 /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
11091- /// Returns the minimum channel reserve ( sats)
11096+ /// In case of error, it returns the minimum channel reserve that was violated (in sats)
1109211097 #[cfg(splicing)]
11093- pub fn check_splice_balance_meets_v2_reserve_requirement_noerr (
11094- &self, pre_balance : u64, post_balance : u64, pre_channel_value : u64,
11095- post_channel_value : u64, dust_limit : u64,
11098+ pub fn check_splice_balance_meets_v2_reserve_requirement (
11099+ &self, pre_balance_msat : u64, post_balance_msat : u64, pre_channel_value_sats : u64,
11100+ post_channel_value_sats : u64, dust_limit_sats : u64,
1109611101 ) -> Result<(), u64> {
1109711102 let post_channel_reserve_sats =
11098- get_v2_channel_reserve_satoshis(post_channel_value, dust_limit );
11099- if post_balance >= post_channel_reserve_sats * 1000 {
11103+ get_v2_channel_reserve_satoshis(post_channel_value_sats, dust_limit_sats );
11104+ if post_balance_msat >= ( post_channel_reserve_sats * 1000) {
1110011105 return Ok(());
1110111106 }
1110211107 // We're not allowed to dip below the reserve once we've been above,
1110311108 // check differently for originally v1 and v2 channels
1110411109 if self.is_v2_established() {
1110511110 let pre_channel_reserve_sats =
11106- get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit );
11107- if pre_balance >= pre_channel_reserve_sats * 1000 {
11111+ get_v2_channel_reserve_satoshis(pre_channel_value_sats, dust_limit_sats );
11112+ if pre_balance_msat >= ( pre_channel_reserve_sats * 1000) {
1110811113 return Err(post_channel_reserve_sats);
1110911114 }
1111011115 } else {
11111- if pre_balance >= self.funding.holder_selected_channel_reserve_satoshis * 1000 {
11116+ if pre_balance_msat >= ( self.funding.holder_selected_channel_reserve_satoshis * 1000) {
1111211117 return Err(post_channel_reserve_sats);
1111311118 }
1111411119 if let Some(cp_reserve) = self.funding.counterparty_selected_channel_reserve_satoshis {
11115- if pre_balance >= cp_reserve * 1000 {
11120+ if pre_balance_msat >= ( cp_reserve * 1000) {
1111611121 return Err(post_channel_reserve_sats);
1111711122 }
1111811123 }
1111911124 }
1112011125 // Make sure we either remain with the same balance or move towards the reserve.
11121- if post_balance >= pre_balance {
11126+ if post_balance_msat >= pre_balance_msat {
1112211127 Ok(())
1112311128 } else {
1112411129 Err(post_channel_reserve_sats)
1112511130 }
1112611131 }
1112711132
11128- /// Check that balances meet the channel reserve requirements or violates them (below reserve).
11133+ /// Check that balances (self and counterparty) meet the channel reserve requirements or violates them (below reserve).
1112911134 /// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
1113011135 /// to check with new channel value (before being committed to it).
1113111136 #[cfg(splicing)]
1113211137 pub fn check_splice_balances_meet_v2_reserve_requirements(
1113311138 &self, self_balance_pre_msat: u64, self_balance_post_msat: u64,
1113411139 counterparty_balance_pre_msat: u64, counterparty_balance_post_msat: u64,
11135- channel_value_pre : u64, channel_value_post : u64,
11140+ channel_value_pre_sats : u64, channel_value_post_sats : u64,
1113611141 ) -> Result<(), ChannelError> {
11137- let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
11142+ let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement (
1113811143 self_balance_pre_msat,
1113911144 self_balance_post_msat,
11140- channel_value_pre ,
11141- channel_value_post ,
11145+ channel_value_pre_sats ,
11146+ channel_value_post_sats ,
1114211147 self.context.holder_dust_limit_satoshis,
1114311148 );
1114411149 if let Err(channel_reserve_self) = is_ok_self {
@@ -11147,11 +11152,11 @@ where
1114711152 self_balance_post_msat, channel_reserve_self,
1114811153 )));
1114911154 }
11150- let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
11155+ let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement (
1115111156 counterparty_balance_pre_msat,
1115211157 counterparty_balance_post_msat,
11153- channel_value_pre ,
11154- channel_value_post ,
11158+ channel_value_pre_sats ,
11159+ channel_value_post_sats ,
1115511160 self.context.counterparty_dust_limit_satoshis,
1115611161 );
1115711162 if let Err(channel_reserve_cp) = is_ok_cp {
0 commit comments