Skip to content

Commit 1f1b6bf

Browse files
committed
Move splice reserve checks to FundedChannel
1 parent e145119 commit 1f1b6bf

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

lightning/src/ln/channel.rs

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5120,66 +5120,6 @@ where
51205120
}
51215121
}
51225122

5123-
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
5124-
#[cfg(splicing)]
5125-
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(
5126-
pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64,
5127-
dust_limit: u64,
5128-
) -> Result<(), u64> {
5129-
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
5130-
if post_balance >= post_channel_reserve {
5131-
return Ok(());
5132-
}
5133-
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
5134-
if pre_balance >= pre_channel_reserve {
5135-
// We're not allowed to dip below the reserve once we've been above.
5136-
return Err(post_channel_reserve);
5137-
}
5138-
// Make sure we either remain with the same balance or move towards the reserve.
5139-
if post_balance >= pre_balance {
5140-
Ok(())
5141-
} else {
5142-
Err(post_channel_reserve)
5143-
}
5144-
}
5145-
5146-
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
5147-
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
5148-
/// to check with new channel value (before being committed to it).
5149-
#[cfg(splicing)]
5150-
pub fn check_splice_balances_meet_v2_reserve_requirements(
5151-
&self, self_balance_pre: u64, self_balance_post: u64, counterparty_balance_pre: u64,
5152-
counterparty_balance_post: u64, channel_value_pre: u64, channel_value_post: u64,
5153-
) -> Result<(), ChannelError> {
5154-
let is_ok_self = Self::check_splice_balance_meets_v2_reserve_requirement_noerr(
5155-
self_balance_pre,
5156-
self_balance_post,
5157-
channel_value_pre,
5158-
channel_value_post,
5159-
self.holder_dust_limit_satoshis,
5160-
);
5161-
if let Err(channel_reserve_self) = is_ok_self {
5162-
return Err(ChannelError::Warn(format!(
5163-
"Balance below reserve, mandated by holder, {} vs {}",
5164-
self_balance_post, channel_reserve_self,
5165-
)));
5166-
}
5167-
let is_ok_cp = Self::check_splice_balance_meets_v2_reserve_requirement_noerr(
5168-
counterparty_balance_pre,
5169-
counterparty_balance_post,
5170-
channel_value_pre,
5171-
channel_value_post,
5172-
self.counterparty_dust_limit_satoshis,
5173-
);
5174-
if let Err(channel_reserve_cp) = is_ok_cp {
5175-
return Err(ChannelError::Warn(format!(
5176-
"Balance below reserve mandated by counterparty, {} vs {}",
5177-
counterparty_balance_post, channel_reserve_cp,
5178-
)));
5179-
}
5180-
Ok(())
5181-
}
5182-
51835123
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
51845124
/// number of pending HTLCs that are on track to be in our next commitment tx.
51855125
///
@@ -10509,7 +10449,7 @@ where
1050910449
self.compute_balances_less_fees(post_channel_value, post_balance_self, true);
1051010450
// Pre-check for reserve requirement
1051110451
// This will also be checked later at tx_complete
10512-
let _res = self.context.check_splice_balances_meet_v2_reserve_requirements(
10452+
let _res = self.check_splice_balances_meet_v2_reserve_requirements(
1051310453
pre_balance_self_less_fees,
1051410454
post_balance_self_less_fees,
1051510455
pre_balance_counterparty_less_fees,
@@ -10595,6 +10535,66 @@ where
1059510535
Ok((None, None))
1059610536
}
1059710537

10538+
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
10539+
#[cfg(splicing)]
10540+
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(
10541+
&self, pre_balance: u64, post_balance: u64, pre_channel_value: u64,
10542+
post_channel_value: u64, dust_limit: u64,
10543+
) -> Result<(), u64> {
10544+
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
10545+
if post_balance >= post_channel_reserve {
10546+
return Ok(());
10547+
}
10548+
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
10549+
if pre_balance >= pre_channel_reserve {
10550+
// We're not allowed to dip below the reserve once we've been above.
10551+
return Err(post_channel_reserve);
10552+
}
10553+
// Make sure we either remain with the same balance or move towards the reserve.
10554+
if post_balance >= pre_balance {
10555+
Ok(())
10556+
} else {
10557+
Err(post_channel_reserve)
10558+
}
10559+
}
10560+
10561+
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
10562+
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
10563+
/// to check with new channel value (before being committed to it).
10564+
#[cfg(splicing)]
10565+
pub fn check_splice_balances_meet_v2_reserve_requirements(
10566+
&self, self_balance_pre: u64, self_balance_post: u64, counterparty_balance_pre: u64,
10567+
counterparty_balance_post: u64, channel_value_pre: u64, channel_value_post: u64,
10568+
) -> Result<(), ChannelError> {
10569+
let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement_noerr(
10570+
self_balance_pre,
10571+
self_balance_post,
10572+
channel_value_pre,
10573+
channel_value_post,
10574+
self.context.holder_dust_limit_satoshis,
10575+
);
10576+
if let Err(channel_reserve_self) = is_ok_self {
10577+
return Err(ChannelError::Warn(format!(
10578+
"Balance below reserve, mandated by holder, {} vs {}",
10579+
self_balance_post, channel_reserve_self,
10580+
)));
10581+
}
10582+
let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement_noerr(
10583+
counterparty_balance_pre,
10584+
counterparty_balance_post,
10585+
channel_value_pre,
10586+
channel_value_post,
10587+
self.context.counterparty_dust_limit_satoshis,
10588+
);
10589+
if let Err(channel_reserve_cp) = is_ok_cp {
10590+
return Err(ChannelError::Warn(format!(
10591+
"Balance below reserve mandated by counterparty, {} vs {}",
10592+
counterparty_balance_post, channel_reserve_cp,
10593+
)));
10594+
}
10595+
Ok(())
10596+
}
10597+
1059810598
// Send stuff to our remote peers:
1059910599

1060010600
/// Queues up an outbound HTLC to send by placing it in the holding cell. You should call

0 commit comments

Comments
 (0)