@@ -10354,9 +10354,10 @@ where
10354
10354
Ok(splice_ack_msg)
10355
10355
}
10356
10356
10357
- /// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
10357
+ /// Compute the channel balances (local & remote, in msats) by taking into account fees,
10358
+ // anchor values, and dust limits.
10358
10359
/// Pending HTLCs are not taken into account, this method should be used when there is no such,
10359
- /// e.g. in quiscence state
10360
+ /// e.g. in quiescence state
10360
10361
#[cfg(splicing)]
10361
10362
fn compute_balances_less_fees(
10362
10363
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
@@ -10372,12 +10373,12 @@ where
10372
10373
((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
10373
10374
debug_assert!(value_to_remote_msat >= 0);
10374
10375
10375
- let total_fee_sat = commit_tx_fee_sat(
10376
+ let total_fee_sats = commit_tx_fee_sat(
10376
10377
feerate_per_kw,
10377
10378
0,
10378
10379
&self.funding.channel_transaction_parameters.channel_type_features,
10379
10380
);
10380
- let anchors_val = if self
10381
+ let anchors_val_sats = if self
10381
10382
.funding
10382
10383
.channel_transaction_parameters
10383
10384
.channel_type_features
@@ -10389,34 +10390,37 @@ where
10389
10390
} as i64;
10390
10391
10391
10392
// consider fees and anchor values
10392
- let (mut value_to_self, mut value_to_remote) = if self.funding.is_outbound() {
10393
+ let (mut new_value_to_self_msat, mut new_value_to_remote_msat) = if self
10394
+ .funding
10395
+ .is_outbound()
10396
+ {
10393
10397
(
10394
- ( value_to_self_msat as i64) / 1000 - anchors_val - total_fee_sat as i64,
10395
- value_to_remote_msat / 1000 ,
10398
+ value_to_self_msat as i64 - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
10399
+ value_to_remote_msat,
10396
10400
)
10397
10401
} else {
10398
10402
(
10399
- ( value_to_self_msat as i64) / 1000 ,
10400
- value_to_remote_msat / 1000 - anchors_val - total_fee_sat as i64,
10403
+ value_to_self_msat as i64,
10404
+ value_to_remote_msat - anchors_val_sats * 1000 - total_fee_sats as i64 * 1000 ,
10401
10405
)
10402
10406
};
10403
10407
10404
10408
// consider dust limit
10405
- let broadcaster_dust_limit_satoshis = if is_local {
10409
+ let broadcaster_dust_limit_sats = if is_local {
10406
10410
self.context.holder_dust_limit_satoshis
10407
10411
} else {
10408
10412
self.context.counterparty_dust_limit_satoshis
10409
10413
} as i64;
10410
- if value_to_self < broadcaster_dust_limit_satoshis {
10411
- value_to_self = 0;
10414
+ if new_value_to_self_msat < (broadcaster_dust_limit_sats * 1000) {
10415
+ new_value_to_self_msat = 0;
10412
10416
}
10413
- debug_assert!(value_to_self >= 0);
10414
- if value_to_remote < broadcaster_dust_limit_satoshis {
10415
- value_to_remote = 0;
10417
+ debug_assert!(new_value_to_self_msat >= 0);
10418
+ if new_value_to_remote_msat < (broadcaster_dust_limit_sats * 1000) {
10419
+ new_value_to_remote_msat = 0;
10416
10420
}
10417
- debug_assert!(value_to_remote >= 0);
10421
+ debug_assert!(new_value_to_remote_msat >= 0);
10418
10422
10419
- (value_to_self as u64, value_to_remote as u64)
10423
+ (new_value_to_self_msat as u64, new_value_to_remote_msat as u64)
10420
10424
}
10421
10425
10422
10426
/// Handle splice_ack
@@ -10431,31 +10435,31 @@ where
10431
10435
10432
10436
// Pre-check for reserve requirement
10433
10437
// (Note: It should also be checked later at tx_complete)
10434
- let our_funding_contribution = pending_splice.our_funding_contribution;
10435
- let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
10436
-
10437
- let pre_channel_value = self.funding.get_value_satoshis();
10438
- let post_channel_value = PendingSplice::compute_post_value(
10439
- pre_channel_value ,
10440
- our_funding_contribution ,
10441
- their_funding_contribution_satoshis ,
10438
+ let our_funding_contribution_sats = pending_splice.our_funding_contribution;
10439
+ let their_funding_contribution_sats = msg.funding_contribution_satoshis;
10440
+
10441
+ let pre_channel_value_sats = self.funding.get_value_satoshis();
10442
+ let post_channel_value_sats = PendingSplice::compute_post_value(
10443
+ pre_channel_value_sats ,
10444
+ our_funding_contribution_sats ,
10445
+ their_funding_contribution_sats ,
10442
10446
);
10443
- let pre_balance_self = self.funding.value_to_self_msat;
10444
- let post_balance_self =
10445
- PendingSplice::add_checked(pre_balance_self, our_funding_contribution );
10446
- let (pre_balance_self_less_fees, pre_balance_counterparty_less_fees ) =
10447
- self.compute_balances_less_fees(pre_channel_value, pre_balance_self , true);
10448
- let (post_balance_self_less_fees, post_balance_counterparty_less_fees ) =
10449
- self.compute_balances_less_fees(post_channel_value, post_balance_self , true);
10447
+ let pre_balance_self_msat = self.funding.value_to_self_msat;
10448
+ let post_balance_self_msat =
10449
+ PendingSplice::add_checked(pre_balance_self_msat, our_funding_contribution_sats * 1000 );
10450
+ let (pre_balance_self_less_fees_msat, pre_balance_counterparty_less_fees_msat ) =
10451
+ self.compute_balances_less_fees(pre_channel_value_sats, pre_balance_self_msat , true);
10452
+ let (post_balance_self_less_fees_msat, post_balance_counterparty_less_fees_msat ) =
10453
+ self.compute_balances_less_fees(post_channel_value_sats, post_balance_self_msat , true);
10450
10454
// Pre-check for reserve requirement
10451
10455
// This will also be checked later at tx_complete
10452
10456
let _res = self.check_splice_balances_meet_v2_reserve_requirements(
10453
- pre_balance_self_less_fees ,
10454
- post_balance_self_less_fees ,
10455
- pre_balance_counterparty_less_fees ,
10456
- post_balance_counterparty_less_fees ,
10457
- pre_channel_value ,
10458
- post_channel_value ,
10457
+ pre_balance_self_less_fees_msat ,
10458
+ post_balance_self_less_fees_msat ,
10459
+ pre_balance_counterparty_less_fees_msat ,
10460
+ post_balance_counterparty_less_fees_msat ,
10461
+ pre_channel_value_sats ,
10462
+ post_channel_value_sats ,
10459
10463
)?;
10460
10464
Ok(())
10461
10465
}
@@ -10535,59 +10539,58 @@ where
10535
10539
Ok((None, None))
10536
10540
}
10537
10541
10538
- /// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
10539
10542
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
10540
- /// Returns the minimum channel reserve ( sats)
10543
+ /// In case of error, it returns the minimum channel reserve that was violated (in sats)
10541
10544
#[cfg(splicing)]
10542
- pub fn check_splice_balance_meets_v2_reserve_requirement_noerr (
10543
- &self, pre_balance : u64, post_balance : u64, pre_channel_value : u64,
10544
- post_channel_value : u64, dust_limit : u64,
10545
+ pub fn check_splice_balance_meets_v2_reserve_requirement (
10546
+ &self, pre_balance_msat : u64, post_balance_msat : u64, pre_channel_value_sats : u64,
10547
+ post_channel_value_sats : u64, dust_limit_sats : u64,
10545
10548
) -> Result<(), u64> {
10546
10549
let post_channel_reserve_sats =
10547
- get_v2_channel_reserve_satoshis(post_channel_value, dust_limit );
10548
- if post_balance >= post_channel_reserve_sats * 1000 {
10550
+ get_v2_channel_reserve_satoshis(post_channel_value_sats, dust_limit_sats );
10551
+ if post_balance_msat >= ( post_channel_reserve_sats * 1000) {
10549
10552
return Ok(());
10550
10553
}
10551
10554
// We're not allowed to dip below the reserve once we've been above,
10552
10555
// check differently for originally v1 and v2 channels
10553
10556
if self.is_v2_established() {
10554
10557
let pre_channel_reserve_sats =
10555
- get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit );
10556
- if pre_balance >= pre_channel_reserve_sats * 1000 {
10558
+ get_v2_channel_reserve_satoshis(pre_channel_value_sats, dust_limit_sats );
10559
+ if pre_balance_msat >= ( pre_channel_reserve_sats * 1000) {
10557
10560
return Err(post_channel_reserve_sats);
10558
10561
}
10559
10562
} else {
10560
- if pre_balance >= self.funding.holder_selected_channel_reserve_satoshis * 1000 {
10563
+ if pre_balance_msat >= ( self.funding.holder_selected_channel_reserve_satoshis * 1000) {
10561
10564
return Err(post_channel_reserve_sats);
10562
10565
}
10563
10566
if let Some(cp_reserve) = self.funding.counterparty_selected_channel_reserve_satoshis {
10564
- if pre_balance >= cp_reserve * 1000 {
10567
+ if pre_balance_msat >= ( cp_reserve * 1000) {
10565
10568
return Err(post_channel_reserve_sats);
10566
10569
}
10567
10570
}
10568
10571
}
10569
10572
// Make sure we either remain with the same balance or move towards the reserve.
10570
- if post_balance >= pre_balance {
10573
+ if post_balance_msat >= pre_balance_msat {
10571
10574
Ok(())
10572
10575
} else {
10573
10576
Err(post_channel_reserve_sats)
10574
10577
}
10575
10578
}
10576
10579
10577
- /// Check that balances meet the channel reserve requirements or violates them (below reserve).
10580
+ /// Check that balances (self and counterparty) meet the channel reserve requirements or violates them (below reserve).
10578
10581
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
10579
10582
/// to check with new channel value (before being committed to it).
10580
10583
#[cfg(splicing)]
10581
10584
pub fn check_splice_balances_meet_v2_reserve_requirements(
10582
10585
&self, self_balance_pre_msat: u64, self_balance_post_msat: u64,
10583
10586
counterparty_balance_pre_msat: u64, counterparty_balance_post_msat: u64,
10584
- channel_value_pre : u64, channel_value_post : u64,
10587
+ channel_value_pre_sats : u64, channel_value_post_sats : u64,
10585
10588
) -> Result<(), ChannelError> {
10586
- let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
10589
+ let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement (
10587
10590
self_balance_pre_msat,
10588
10591
self_balance_post_msat,
10589
- channel_value_pre ,
10590
- channel_value_post ,
10592
+ channel_value_pre_sats ,
10593
+ channel_value_post_sats ,
10591
10594
self.context.holder_dust_limit_satoshis,
10592
10595
);
10593
10596
if let Err(channel_reserve_self) = is_ok_self {
@@ -10596,11 +10599,11 @@ where
10596
10599
self_balance_post_msat, channel_reserve_self,
10597
10600
)));
10598
10601
}
10599
- let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement_noerr (
10602
+ let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement (
10600
10603
counterparty_balance_pre_msat,
10601
10604
counterparty_balance_post_msat,
10602
- channel_value_pre ,
10603
- channel_value_post ,
10605
+ channel_value_pre_sats ,
10606
+ channel_value_post_sats ,
10604
10607
self.context.counterparty_dust_limit_satoshis,
10605
10608
);
10606
10609
if let Err(channel_reserve_cp) = is_ok_cp {
0 commit comments