Skip to content

Commit 6ffa569

Browse files
committed
Move splice reserve checks to FundedChannel
1 parent 0051744 commit 6ffa569

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

lightning/src/ln/channel.rs

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,66 +5078,6 @@ where
50785078
}
50795079
}
50805080

5081-
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
5082-
#[cfg(splicing)]
5083-
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(
5084-
pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64,
5085-
dust_limit: u64,
5086-
) -> Result<(), u64> {
5087-
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
5088-
if post_balance >= post_channel_reserve {
5089-
return Ok(());
5090-
}
5091-
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
5092-
if pre_balance >= pre_channel_reserve {
5093-
// We're not allowed to dip below the reserve once we've been above.
5094-
return Err(post_channel_reserve);
5095-
}
5096-
// Make sure we either remain with the same balance or move towards the reserve.
5097-
if post_balance >= pre_balance {
5098-
Ok(())
5099-
} else {
5100-
Err(post_channel_reserve)
5101-
}
5102-
}
5103-
5104-
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
5105-
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
5106-
/// to check with new channel value (before being committed to it).
5107-
#[cfg(splicing)]
5108-
pub fn check_splice_balances_meet_v2_reserve_requirements(
5109-
&self, self_balance_pre: u64, self_balance_post: u64, counterparty_balance_pre: u64,
5110-
counterparty_balance_post: u64, channel_value_pre: u64, channel_value_post: u64,
5111-
) -> Result<(), ChannelError> {
5112-
let is_ok_self = Self::check_splice_balance_meets_v2_reserve_requirement_noerr(
5113-
self_balance_pre,
5114-
self_balance_post,
5115-
channel_value_pre,
5116-
channel_value_post,
5117-
self.holder_dust_limit_satoshis,
5118-
);
5119-
if let Err(channel_reserve_self) = is_ok_self {
5120-
return Err(ChannelError::Warn(format!(
5121-
"Balance below reserve, mandated by holder, {} vs {}",
5122-
self_balance_post, channel_reserve_self,
5123-
)));
5124-
}
5125-
let is_ok_cp = Self::check_splice_balance_meets_v2_reserve_requirement_noerr(
5126-
counterparty_balance_pre,
5127-
counterparty_balance_post,
5128-
channel_value_pre,
5129-
channel_value_post,
5130-
self.counterparty_dust_limit_satoshis,
5131-
);
5132-
if let Err(channel_reserve_cp) = is_ok_cp {
5133-
return Err(ChannelError::Warn(format!(
5134-
"Balance below reserve mandated by counterparty, {} vs {}",
5135-
counterparty_balance_post, channel_reserve_cp,
5136-
)));
5137-
}
5138-
Ok(())
5139-
}
5140-
51415081
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
51425082
/// number of pending HTLCs that are on track to be in our next commitment tx.
51435083
///
@@ -11047,7 +10987,7 @@ where
1104710987
self.compute_balances_less_fees(post_channel_value, post_balance_self, true);
1104810988
// Pre-check for reserve requirement
1104910989
// This will also be checked later at tx_complete
11050-
let _res = self.context.check_splice_balances_meet_v2_reserve_requirements(
10990+
let _res = self.check_splice_balances_meet_v2_reserve_requirements(
1105110991
pre_balance_self_less_fees,
1105210992
post_balance_self_less_fees,
1105310993
pre_balance_counterparty_less_fees,
@@ -11146,6 +11086,68 @@ where
1114611086
))
1114711087
}
1114811088

11089+
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
11090+
#[cfg(splicing)]
11091+
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(
11092+
&self, pre_balance: u64, post_balance: u64, pre_channel_value: u64,
11093+
post_channel_value: u64, dust_limit: u64,
11094+
) -> Result<(), u64> {
11095+
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
11096+
if post_balance >= post_channel_reserve {
11097+
return Ok(());
11098+
}
11099+
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
11100+
if pre_balance >= pre_channel_reserve {
11101+
// We're not allowed to dip below the reserve once we've been above.
11102+
return Err(post_channel_reserve);
11103+
}
11104+
// Make sure we either remain with the same balance or move towards the reserve.
11105+
if post_balance >= pre_balance {
11106+
Ok(())
11107+
} else {
11108+
Err(post_channel_reserve)
11109+
}
11110+
}
11111+
11112+
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
11113+
/// The channel value is an input as opposed to using from the FundingScope, so that this can be used in case of splicing
11114+
/// to check with new channel value (before being committed to it).
11115+
#[cfg(splicing)]
11116+
pub fn check_splice_balances_meet_v2_reserve_requirements(
11117+
&self, self_balance_pre: u64, self_balance_post: u64, counterparty_balance_pre: u64,
11118+
counterparty_balance_post: u64, channel_value_pre: u64, channel_value_post: u64,
11119+
) -> Result<(), ChannelError> {
11120+
let is_ok_self = self.check_splice_balance_meets_v2_reserve_requirement_noerr(
11121+
self_balance_pre,
11122+
self_balance_post,
11123+
channel_value_pre,
11124+
channel_value_post,
11125+
self.context.holder_dust_limit_satoshis,
11126+
);
11127+
if let Err(channel_reserve_self) = is_ok_self {
11128+
return Err(ChannelError::Warn(format!(
11129+
"Balance below reserve, mandated by holder, {} vs {}",
11130+
self_balance_post, channel_reserve_self,
11131+
)));
11132+
}
11133+
let is_ok_cp = self.check_splice_balance_meets_v2_reserve_requirement_noerr(
11134+
counterparty_balance_pre,
11135+
counterparty_balance_post,
11136+
channel_value_pre,
11137+
channel_value_post,
11138+
self.context.counterparty_dust_limit_satoshis,
11139+
);
11140+
if let Err(channel_reserve_cp) = is_ok_cp {
11141+
return Err(ChannelError::Warn(format!(
11142+
"Balance below reserve mandated by counterparty, {} vs {}",
11143+
counterparty_balance_post, channel_reserve_cp,
11144+
)));
11145+
}
11146+
Ok(())
11147+
}
11148+
11149+
// Send stuff to our remote peers:
11150+
1114911151
/// Queues up an outbound HTLC to send by placing it in the holding cell. You should call
1115011152
/// [`Self::maybe_free_holding_cell_htlcs`] in order to actually generate and send the
1115111153
/// commitment update.

0 commit comments

Comments
 (0)