Skip to content

Commit 6ee1aee

Browse files
committed
Adjust balance check, cleanup (review)
1 parent 4ee3735 commit 6ee1aee

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

lightning/src/ln/channel.rs

+12-33
Original file line numberDiff line numberDiff line change
@@ -1183,12 +1183,6 @@ impl UnfundedChannelContext {
11831183
#[derive(Clone)]
11841184
pub(crate) struct PendingSpliceInfoPre {
11851185
pub our_funding_contribution: i64,
1186-
// TODO(splicing): Enable below fields
1187-
// pub funding_feerate_perkw: u32,
1188-
// pub locktime: u32,
1189-
// /// The funding inputs we will be contributing to the splice.
1190-
// /// TODO(splice): will be changed to TransactionU16LenLimited
1191-
// pub our_funding_inputs: Vec<(TxIn, Transaction)>,
11921186
}
11931187

11941188
#[cfg(splicing)]
@@ -3424,23 +3418,24 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34243418
(context.holder_selected_channel_reserve_satoshis, context.counterparty_selected_channel_reserve_satoshis)
34253419
}
34263420

3427-
/// Check that a proposed channel value meets the channel reserve requirements or violates them (below reserve)
3421+
/// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
3422+
/// The channel value is an input, so that this can be used for checks with new planned channel value.
34283423
#[cfg(any(dual_funding, splicing))]
3429-
pub fn check_channel_value_meets_reserve_requirements(&self, proposed_channel_value: u64) -> Result<(), ChannelError> {
3424+
pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> {
34303425
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3431-
proposed_channel_value, self.holder_dust_limit_satoshis);
3432-
if proposed_channel_value < holder_selected_channel_reserve_satoshis {
3426+
channel_value, self.holder_dust_limit_satoshis);
3427+
if balance < holder_selected_channel_reserve_satoshis {
34333428
return Err(ChannelError::Warn(format!(
3434-
"Proposed channel value below reserve mandated by holder, {} vs {}",
3435-
proposed_channel_value, holder_selected_channel_reserve_satoshis,
3429+
"Balance below reserve mandated by holder, {} vs {}",
3430+
balance, holder_selected_channel_reserve_satoshis,
34363431
)));
34373432
}
34383433
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3439-
proposed_channel_value, self.counterparty_dust_limit_satoshis);
3440-
if proposed_channel_value < counterparty_selected_channel_reserve_satoshis {
3434+
channel_value, self.counterparty_dust_limit_satoshis);
3435+
if balance < counterparty_selected_channel_reserve_satoshis {
34413436
return Err(ChannelError::Warn(format!(
3442-
"Proposed channel value below reserve mandated by counterparty, {} vs {}",
3443-
proposed_channel_value, counterparty_selected_channel_reserve_satoshis,
3437+
"Balance below reserve mandated by counterparty, {} vs {}",
3438+
balance, counterparty_selected_channel_reserve_satoshis,
34443439
)));
34453440
}
34463441
Ok(())
@@ -3832,18 +3827,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38323827
Ok(())
38333828
}
38343829

3835-
// /// Splice process starting; update state; update capacity, state, reset funding tx
3836-
// #[cfg(splicing)]
3837-
// pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
3838-
// // Set state, by this point handshake is complete
3839-
// self.channel_state = ChannelState::NegotiatingFunding(NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT);
3840-
3841-
// log_info!(logger,
3842-
// "Splicing process started, new channel value {}, outgoing {}, channel_id {}",
3843-
// self.channel_value_satoshis, is_outgoing, self.channel_id,
3844-
// );
3845-
// }
3846-
38473830
/// Get the splice message that can be sent during splice initiation.
38483831
#[cfg(splicing)]
38493832
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
@@ -7465,14 +7448,10 @@ impl<SP: Deref> Channel<SP> where
74657448
where ES::Target: EntropySource, L::Target: Logger
74667449
{
74677450
if !self.context.is_outbound() {
7468-
// TODO(splicing): Enable starting in the line below
7469-
// Apply start of splice change in the state
7470-
// self.context.splice_start(false, logger);
7451+
// TODO(splicing): Apply start of splice (splice_start)
74717452

74727453
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis)?;
74737454
// TODO(splicing): start interactive funding negotiation
7474-
// let _msg = post_chan.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
7475-
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
74767455
Ok(splice_ack_msg)
74777456
} else {
74787457
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
@@ -4134,8 +4134,8 @@ where
41344134
if (pre_channel_value as i64).saturating_add(our_funding_contribution_satoshis) < 0 {
41354135
return Err(APIError::APIMisuseError {
41364136
err: format!(
4137-
"Post-splicing channel value cannot be negative. It was {} - {}",
4138-
pre_channel_value, -our_funding_contribution_satoshis
4137+
"Post-splicing channel value cannot be negative. It was {} - -{}",
4138+
pre_channel_value, our_funding_contribution_satoshis
41394139
)
41404140
});
41414141
}
@@ -9042,7 +9042,7 @@ where
90429042
// Sanity check: capacity cannot decrease below 0
90439043
if (pre_channel_value as i64).saturating_add(msg.funding_contribution_satoshis) < 0 {
90449044
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
9045-
"Post-splicing channel value cannot be negative. It was {} - {}", pre_channel_value, -msg.funding_contribution_satoshis,
9045+
"Post-splicing channel value cannot be negative. It was {} - -{}", pre_channel_value, msg.funding_contribution_satoshis,
90469046
), msg.channel_id));
90479047
}
90489048

@@ -9054,8 +9054,9 @@ where
90549054

90559055
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, msg.funding_contribution_satoshis, our_funding_contribution);
90569056

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

90619062
// Check if a splice has been initiated already.
@@ -9127,8 +9128,9 @@ where
91279128
let pre_channel_value = chan.context.get_value_satoshis();
91289129
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, pending_splice.our_funding_contribution, msg.funding_contribution_satoshis);
91299130

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

0 commit comments

Comments
 (0)