@@ -10537,9 +10537,10 @@ where
10537
10537
Ok(splice_ack_msg)
10538
10538
}
10539
10539
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.
10541
10542
/// 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
10543
10544
#[cfg(splicing)]
10544
10545
fn compute_balances_less_fees(
10545
10546
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
@@ -10555,12 +10556,12 @@ where
10555
10556
((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
10556
10557
debug_assert!(value_to_remote_msat >= 0);
10557
10558
10558
- let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(
10559
+ let total_fee_sats = SpecTxBuilder {}.commit_tx_fee_sat(
10559
10560
feerate_per_kw,
10560
10561
0,
10561
10562
&self.funding.channel_transaction_parameters.channel_type_features,
10562
10563
);
10563
- let anchors_val = if self
10564
+ let anchors_val_sats = if self
10564
10565
.funding
10565
10566
.channel_transaction_parameters
10566
10567
.channel_type_features
@@ -10572,34 +10573,37 @@ where
10572
10573
} as i64;
10573
10574
10574
10575
// 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
+ {
10576
10580
(
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,
10579
10583
)
10580
10584
} else {
10581
10585
(
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 ,
10584
10588
)
10585
10589
};
10586
10590
10587
10591
// consider dust limit
10588
- let broadcaster_dust_limit_satoshis = if is_local {
10592
+ let broadcaster_dust_limit_sats = if is_local {
10589
10593
self.context.holder_dust_limit_satoshis
10590
10594
} else {
10591
10595
self.context.counterparty_dust_limit_satoshis
10592
10596
} 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;
10595
10599
}
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;
10599
10603
}
10600
- debug_assert!(value_to_remote >= 0);
10604
+ debug_assert!(new_value_to_remote_msat >= 0);
10601
10605
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)
10603
10607
}
10604
10608
10605
10609
/// Handle splice_ack
@@ -10614,31 +10618,31 @@ where
10614
10618
10615
10619
// Pre-check for reserve requirement
10616
10620
// (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 ,
10625
10629
);
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);
10633
10637
// Pre-check for reserve requirement
10634
10638
// This will also be checked later at tx_complete
10635
10639
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 ,
10642
10646
)?;
10643
10647
Ok(())
10644
10648
}
@@ -10695,59 +10699,58 @@ where
10695
10699
))
10696
10700
}
10697
10701
10698
- /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
10699
10702
/// 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)
10701
10704
#[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,
10705
10708
) -> Result<(), u64> {
10706
10709
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) {
10709
10712
return Ok(());
10710
10713
}
10711
10714
// We're not allowed to dip below the reserve once we've been above,
10712
10715
// check differently for originally v1 and v2 channels
10713
10716
if self.is_v2_established() {
10714
10717
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) {
10717
10720
return Err(post_channel_reserve_sats);
10718
10721
}
10719
10722
} 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) {
10721
10724
return Err(post_channel_reserve_sats);
10722
10725
}
10723
10726
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) {
10725
10728
return Err(post_channel_reserve_sats);
10726
10729
}
10727
10730
}
10728
10731
}
10729
10732
// 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 {
10731
10734
Ok(())
10732
10735
} else {
10733
10736
Err(post_channel_reserve_sats)
10734
10737
}
10735
10738
}
10736
10739
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).
10738
10741
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
10739
10742
/// to check with new channel value (before being committed to it).
10740
10743
#[cfg(splicing)]
10741
10744
pub fn check_splice_balances_meet_v2_reserve_requirements(
10742
10745
&self, self_balance_pre_msat: u64, self_balance_post_msat: u64,
10743
10746
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,
10745
10748
) -> 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 (
10747
10750
self_balance_pre_msat,
10748
10751
self_balance_post_msat,
10749
- channel_value_pre ,
10750
- channel_value_post ,
10752
+ channel_value_pre_sats ,
10753
+ channel_value_post_sats ,
10751
10754
self.context.holder_dust_limit_satoshis,
10752
10755
);
10753
10756
if let Err(channel_reserve_self) = is_ok_self {
@@ -10756,11 +10759,11 @@ where
10756
10759
self_balance_post_msat, channel_reserve_self,
10757
10760
)));
10758
10761
}
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 (
10760
10763
counterparty_balance_pre_msat,
10761
10764
counterparty_balance_post_msat,
10762
- channel_value_pre ,
10763
- channel_value_post ,
10765
+ channel_value_pre_sats ,
10766
+ channel_value_post_sats ,
10764
10767
self.context.counterparty_dust_limit_satoshis,
10765
10768
);
10766
10769
if let Err(channel_reserve_cp) = is_ok_cp {
0 commit comments