Skip to content

Commit 874dd69

Browse files
committed
Adjust balance check, cleanup (review)
1 parent a9720ef commit 874dd69

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

lightning/src/ln/channel.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -1186,12 +1186,6 @@ impl UnfundedChannelContext {
11861186
#[derive(Clone)]
11871187
pub(crate) struct PendingSpliceInfoPre {
11881188
pub our_funding_contribution: i64,
1189-
// TODO(splicing): Enable below fields
1190-
// pub funding_feerate_perkw: u32,
1191-
// pub locktime: u32,
1192-
// /// The funding inputs we will be contributing to the splice.
1193-
// /// TODO(splice): will be changed to TransactionU16LenLimited
1194-
// pub our_funding_inputs: Vec<(TxIn, Transaction)>,
11951189
}
11961190

11971191
#[cfg(splicing)]
@@ -3653,23 +3647,24 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36533647
(context.holder_selected_channel_reserve_satoshis, context.counterparty_selected_channel_reserve_satoshis)
36543648
}
36553649

3656-
/// Check that a proposed channel value meets the channel reserve requirements or violates them (below reserve)
3650+
/// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
3651+
/// The channel value is an input, so that this can be used for checks with new planned channel value.
36573652
#[cfg(any(dual_funding, splicing))]
3658-
pub fn check_channel_value_meets_reserve_requirements(&self, proposed_channel_value: u64) -> Result<(), ChannelError> {
3653+
pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> {
36593654
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3660-
proposed_channel_value, self.holder_dust_limit_satoshis);
3661-
if proposed_channel_value < holder_selected_channel_reserve_satoshis {
3655+
channel_value, self.holder_dust_limit_satoshis);
3656+
if balance < holder_selected_channel_reserve_satoshis {
36623657
return Err(ChannelError::Warn(format!(
3663-
"Proposed channel value below reserve mandated by holder, {} vs {}",
3664-
proposed_channel_value, holder_selected_channel_reserve_satoshis,
3658+
"Balance below reserve mandated by holder, {} vs {}",
3659+
balance, holder_selected_channel_reserve_satoshis,
36653660
)));
36663661
}
36673662
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3668-
proposed_channel_value, self.counterparty_dust_limit_satoshis);
3669-
if proposed_channel_value < counterparty_selected_channel_reserve_satoshis {
3663+
channel_value, self.counterparty_dust_limit_satoshis);
3664+
if balance < counterparty_selected_channel_reserve_satoshis {
36703665
return Err(ChannelError::Warn(format!(
3671-
"Proposed channel value below reserve mandated by counterparty, {} vs {}",
3672-
proposed_channel_value, counterparty_selected_channel_reserve_satoshis,
3666+
"Balance below reserve mandated by counterparty, {} vs {}",
3667+
balance, counterparty_selected_channel_reserve_satoshis,
36733668
)));
36743669
}
36753670
Ok(())
@@ -7922,14 +7917,10 @@ impl<SP: Deref> Channel<SP> where
79227917
where ES::Target: EntropySource, L::Target: Logger
79237918
{
79247919
if !self.context.is_outbound() {
7925-
// TODO(splicing): Enable starting in the line below
7926-
// Apply start of splice change in the state
7927-
// self.context.splice_start(false, logger);
7920+
// TODO(splicing): Apply start of splice (splice_start)
79287921

79297922
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis)?;
79307923
// TODO(splicing): start interactive funding negotiation
7931-
// let _msg = post_chan.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
7932-
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
79337924
Ok(splice_ack_msg)
79347925
} else {
79357926
Err(ChannelError::Warn("Internal consistency error: splice_init on inbound channel".into()))

lightning/src/ln/channelmanager.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -4146,8 +4146,8 @@ where
41464146
if (pre_channel_value as i64).saturating_add(our_funding_contribution_satoshis) < 0 {
41474147
return Err(APIError::APIMisuseError {
41484148
err: format!(
4149-
"Post-splicing channel value cannot be negative. It was {} - {}",
4150-
pre_channel_value, -our_funding_contribution_satoshis
4149+
"Post-splicing channel value cannot be negative. It was {} - -{}",
4150+
pre_channel_value, our_funding_contribution_satoshis
41514151
)
41524152
});
41534153
}
@@ -9408,7 +9408,7 @@ where
94089408
// Sanity check: capacity cannot decrease below 0
94099409
if (pre_channel_value as i64).saturating_add(msg.funding_contribution_satoshis) < 0 {
94109410
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
9411-
"Post-splicing channel value cannot be negative. It was {} - {}", pre_channel_value, -msg.funding_contribution_satoshis,
9411+
"Post-splicing channel value cannot be negative. It was {} - -{}", pre_channel_value, msg.funding_contribution_satoshis,
94129412
), msg.channel_id));
94139413
}
94149414

@@ -9420,8 +9420,9 @@ where
94209420

94219421
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, msg.funding_contribution_satoshis, our_funding_contribution);
94229422

9423-
// Check for reserve requirement, it will also be checked later at tx_complete
9424-
let _res = chan.context.check_channel_value_meets_reserve_requirements(post_channel_value)
9423+
// Early check for reserve requirement, assuming maximum balance of full channel value
9424+
// This will also be checked later at tx_complete
9425+
let _res = chan.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)
94259426
.map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.channel_id))?;
94269427

94279428
// Check if a splice has been initiated already.
@@ -9493,8 +9494,9 @@ where
94939494
let pre_channel_value = chan.context.get_value_satoshis();
94949495
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, pending_splice.our_funding_contribution, msg.funding_contribution_satoshis);
94959496

9496-
// Check for reserve requirement, it will also be checked later at tx_complete
9497-
let _res = chan.context.check_channel_value_meets_reserve_requirements(post_channel_value)
9497+
// Early check for reserve requirement, assuming maximum balance of full channel value
9498+
// This will also be checked later at tx_complete
9499+
let _res = chan.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)
94989500
.map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.channel_id))?;
94999501
} else {
95009502
return Err(MsgHandleErrInternal::send_err_msg_no_close("Channel is not funded, cannot splice".to_owned(), msg.channel_id));

0 commit comments

Comments
 (0)