Skip to content

Commit

Permalink
Finetune balance check, use expected balance
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Nov 20, 2024
1 parent 3bfcb23 commit bb3b4de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
32 changes: 20 additions & 12 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3423,7 +3423,10 @@ impl<SP: Deref> ChannelContext<SP> 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 {
Expand Down Expand Up @@ -7485,9 +7488,11 @@ impl<SP: Deref> Channel<SP> where

/// Handle splice_init
#[cfg(splicing)]
pub fn splice_init(
&mut self, their_funding_contribution_satoshis: i64, our_funding_contribution_satoshis: i64,
) -> Result<msgs::SpliceAck, ChannelError> {
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
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 {
Expand Down Expand Up @@ -7520,11 +7525,12 @@ impl<SP: Deref> Channel<SP> 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);
Expand All @@ -7534,22 +7540,24 @@ impl<SP: Deref> Channel<SP> 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
} else {
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(())
}

Expand Down
6 changes: 2 additions & 4 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit bb3b4de

Please sign in to comment.