Skip to content

Commit 8a22ec2

Browse files
committed
fix Clarify units (msat&sats), other minor
1 parent a0fe395 commit 8a22ec2

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

lightning/src/ln/channel.rs

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10354,9 +10354,10 @@ where
1035410354
Ok(splice_ack_msg)
1035510355
}
1035610356

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.
1035810359
/// 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
1036010361
#[cfg(splicing)]
1036110362
fn compute_balances_less_fees(
1036210363
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
@@ -10372,12 +10373,12 @@ where
1037210373
((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
1037310374
debug_assert!(value_to_remote_msat >= 0);
1037410375

10375-
let total_fee_sat = commit_tx_fee_sat(
10376+
let total_fee_sats = commit_tx_fee_sat(
1037610377
feerate_per_kw,
1037710378
0,
1037810379
&self.funding.channel_transaction_parameters.channel_type_features,
1037910380
);
10380-
let anchors_val = if self
10381+
let anchors_val_sats = if self
1038110382
.funding
1038210383
.channel_transaction_parameters
1038310384
.channel_type_features
@@ -10389,34 +10390,37 @@ where
1038910390
} as i64;
1039010391

1039110392
// 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+
{
1039310397
(
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,
1039610400
)
1039710401
} else {
1039810402
(
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,
1040110405
)
1040210406
};
1040310407

1040410408
// consider dust limit
10405-
let broadcaster_dust_limit_satoshis = if is_local {
10409+
let broadcaster_dust_limit_sats = if is_local {
1040610410
self.context.holder_dust_limit_satoshis
1040710411
} else {
1040810412
self.context.counterparty_dust_limit_satoshis
1040910413
} 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;
1041210416
}
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;
1041610420
}
10417-
debug_assert!(value_to_remote >= 0);
10421+
debug_assert!(new_value_to_remote_msat >= 0);
1041810422

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)
1042010424
}
1042110425

1042210426
/// Handle splice_ack
@@ -10431,31 +10435,31 @@ where
1043110435

1043210436
// Pre-check for reserve requirement
1043310437
// (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,
1044210446
);
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);
1045010454
// Pre-check for reserve requirement
1045110455
// This will also be checked later at tx_complete
1045210456
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,
1045910463
)?;
1046010464
Ok(())
1046110465
}
@@ -10535,59 +10539,58 @@ where
1053510539
Ok((None, None))
1053610540
}
1053710541

10538-
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
1053910542
/// 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)
1054110544
#[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,
1054510548
) -> Result<(), u64> {
1054610549
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) {
1054910552
return Ok(());
1055010553
}
1055110554
// We're not allowed to dip below the reserve once we've been above,
1055210555
// check differently for originally v1 and v2 channels
1055310556
if self.is_v2_established() {
1055410557
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) {
1055710560
return Err(post_channel_reserve_sats);
1055810561
}
1055910562
} 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) {
1056110564
return Err(post_channel_reserve_sats);
1056210565
}
1056310566
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) {
1056510568
return Err(post_channel_reserve_sats);
1056610569
}
1056710570
}
1056810571
}
1056910572
// 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 {
1057110574
Ok(())
1057210575
} else {
1057310576
Err(post_channel_reserve_sats)
1057410577
}
1057510578
}
1057610579

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).
1057810581
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
1057910582
/// to check with new channel value (before being committed to it).
1058010583
#[cfg(splicing)]
1058110584
pub fn check_splice_balances_meet_v2_reserve_requirements(
1058210585
&self, self_balance_pre_msat: u64, self_balance_post_msat: u64,
1058310586
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,
1058510588
) -> 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(
1058710590
self_balance_pre_msat,
1058810591
self_balance_post_msat,
10589-
channel_value_pre,
10590-
channel_value_post,
10592+
channel_value_pre_sats,
10593+
channel_value_post_sats,
1059110594
self.context.holder_dust_limit_satoshis,
1059210595
);
1059310596
if let Err(channel_reserve_self) = is_ok_self {
@@ -10596,11 +10599,11 @@ where
1059610599
self_balance_post_msat, channel_reserve_self,
1059710600
)));
1059810601
}
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(
1060010603
counterparty_balance_pre_msat,
1060110604
counterparty_balance_post_msat,
10602-
channel_value_pre,
10603-
channel_value_post,
10605+
channel_value_pre_sats,
10606+
channel_value_post_sats,
1060410607
self.context.counterparty_dust_limit_satoshis,
1060510608
);
1060610609
if let Err(channel_reserve_cp) = is_ok_cp {

0 commit comments

Comments
 (0)