From bb3b4de36728f70f7cd957295362bb5adff746d9 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:00:56 +0100 Subject: [PATCH] Finetune balance check, use expected balance --- lightning/src/ln/channel.rs | 32 +++++++++++++++++++----------- lightning/src/ln/channelmanager.rs | 6 ++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 8892bbf1bff..7848eca4123 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -3423,7 +3423,10 @@ impl ChannelContext where SP::Target: SignerProvider { /// The channel value is an input as opposed to using from self, so that this can be used in case of splicing /// to checks with new channel value (before being comitted to it). #[cfg(any(dual_funding, splicing))] - pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> { + pub fn check_balance_meets_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> { + if balance == 0 { + return Ok(()); + } let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis( channel_value, self.holder_dust_limit_satoshis); if balance < holder_selected_channel_reserve_satoshis { @@ -7485,9 +7488,11 @@ impl Channel where /// Handle splice_init #[cfg(splicing)] - pub fn splice_init( - &mut self, their_funding_contribution_satoshis: i64, our_funding_contribution_satoshis: i64, - ) -> Result { + pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result { + let their_funding_contribution_satoshis = msg.funding_contribution_satoshis; + // TODO(splicing): Currently not possible to contribute on the splicing-acceptor side + let our_funding_contribution_satoshis = 0i64; + // Check if a splice has been initiated already. // Note: this could be handled more nicely, and support multiple outstanding splice's, the incoming splice_ack matters anyways. if let Some(splice_info) = &self.context.pending_splice_pre { @@ -7520,11 +7525,12 @@ impl Channel where } let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, their_funding_contribution_satoshis, our_funding_contribution_satoshis); - + let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution_satoshis); // Early check for reserve requirement, assuming maximum balance of full channel value // This will also be checked later at tx_complete - let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?; + let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?; + // TODO(splicing): Store msg.funding_pubkey // TODO(splicing): Apply start of splice (splice_start) let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis); @@ -7534,9 +7540,9 @@ impl Channel where /// Handle splice_ack #[cfg(splicing)] - pub fn splice_ack( - &mut self, their_funding_contribution_satoshis: i64, - ) -> Result<(), ChannelError> { + pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> { + let their_funding_contribution_satoshis = msg.funding_contribution_satoshis; + // check if splice is pending let pending_splice = if let Some(pending_splice) = &self.context.pending_splice_pre { pending_splice @@ -7544,12 +7550,14 @@ impl Channel where return Err(ChannelError::Warn(format!("Channel is not in pending splice"))); }; - let pre_channel_value = self.context.get_value_satoshis(); - let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, pending_splice.our_funding_contribution, their_funding_contribution_satoshis); + let our_funding_contribution = pending_splice.our_funding_contribution; + let pre_channel_value = self.context.get_value_satoshis(); + let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis); + let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution); // Early check for reserve requirement, assuming maximum balance of full channel value // This will also be checked later at tx_complete - let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?; + let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?; Ok(()) } diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 926fce51cd1..dac0a486a51 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -9002,8 +9002,6 @@ where let mut peer_state_lock = peer_state_mutex.lock().unwrap(); let peer_state = &mut *peer_state_lock; - let our_funding_contribution = 0i64; - // Look for the channel match peer_state.channel_by_id.entry(msg.channel_id) { hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!( @@ -9012,7 +9010,7 @@ where ), msg.channel_id)), hash_map::Entry::Occupied(mut chan_entry) => { if let ChannelPhase::Funded(chan) = chan_entry.get_mut() { - match chan.splice_init(msg.funding_contribution_satoshis, our_funding_contribution) { + match chan.splice_init(msg) { Ok(splice_ack_msg) => { peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck { node_id: *counterparty_node_id, @@ -9060,7 +9058,7 @@ where ), msg.channel_id)), hash_map::Entry::Occupied(mut chan) => { if let ChannelPhase::Funded(chan) = chan.get_mut() { - match chan.splice_ack(msg.funding_contribution_satoshis) { + match chan.splice_ack(msg) { Ok(_) => {} Err(err) => { return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));