Skip to content

Commit

Permalink
Adjust balance check, cleanup (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Nov 19, 2024
1 parent 4ee3735 commit fd61390
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
45 changes: 12 additions & 33 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,12 +1183,6 @@ impl UnfundedChannelContext {
#[derive(Clone)]
pub(crate) struct PendingSpliceInfoPre {
pub our_funding_contribution: i64,
// TODO(splicing): Enable below fields
// pub funding_feerate_perkw: u32,
// pub locktime: u32,
// /// The funding inputs we will be contributing to the splice.
// /// TODO(splice): will be changed to TransactionU16LenLimited
// pub our_funding_inputs: Vec<(TxIn, Transaction)>,
}

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

/// Check that a proposed channel value meets the channel reserve requirements or violates them (below reserve)
/// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
/// The channel value is an input, so that this can be used for checks with new planned channel value.
#[cfg(any(dual_funding, splicing))]
pub fn check_channel_value_meets_reserve_requirements(&self, proposed_channel_value: u64) -> Result<(), ChannelError> {
pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> {
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
proposed_channel_value, self.holder_dust_limit_satoshis);
if proposed_channel_value < holder_selected_channel_reserve_satoshis {
channel_value, self.holder_dust_limit_satoshis);
if balance < holder_selected_channel_reserve_satoshis {
return Err(ChannelError::Warn(format!(
"Proposed channel value below reserve mandated by holder, {} vs {}",
proposed_channel_value, holder_selected_channel_reserve_satoshis,
"Balance below reserve mandated by holder, {} vs {}",
balance, holder_selected_channel_reserve_satoshis,
)));
}
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
proposed_channel_value, self.counterparty_dust_limit_satoshis);
if proposed_channel_value < counterparty_selected_channel_reserve_satoshis {
channel_value, self.counterparty_dust_limit_satoshis);
if balance < counterparty_selected_channel_reserve_satoshis {
return Err(ChannelError::Warn(format!(
"Proposed channel value below reserve mandated by counterparty, {} vs {}",
proposed_channel_value, counterparty_selected_channel_reserve_satoshis,
"Balance below reserve mandated by counterparty, {} vs {}",
balance, counterparty_selected_channel_reserve_satoshis,
)));
}
Ok(())
Expand Down Expand Up @@ -3832,18 +3827,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
Ok(())
}

// /// Splice process starting; update state; update capacity, state, reset funding tx
// #[cfg(splicing)]
// pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
// // Set state, by this point handshake is complete
// self.channel_state = ChannelState::NegotiatingFunding(NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT);

// log_info!(logger,
// "Splicing process started, new channel value {}, outgoing {}, channel_id {}",
// self.channel_value_satoshis, is_outgoing, self.channel_id,
// );
// }

/// Get the splice message that can be sent during splice initiation.
#[cfg(splicing)]
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
Expand Down Expand Up @@ -7465,14 +7448,10 @@ impl<SP: Deref> Channel<SP> where
where ES::Target: EntropySource, L::Target: Logger
{
if !self.context.is_outbound() {
// TODO(splicing): Enable starting in the line below
// Apply start of splice change in the state
// self.context.splice_start(false, logger);
// TODO(splicing): Apply start of splice (splice_start)

let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis)?;
// TODO(splicing): start interactive funding negotiation
// let _msg = post_chan.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
Ok(splice_ack_msg)
} else {
Err(ChannelError::Warn("Internal consistency error: splice_init on inbound channel".into()))
Expand Down
10 changes: 6 additions & 4 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9054,8 +9054,9 @@ where

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

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

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

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

0 comments on commit fd61390

Please sign in to comment.