Skip to content

Commit

Permalink
Simplify reserve requirement check (merge two methods)
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Mar 4, 2025
1 parent 1d9ea18 commit cd3c40b
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4131,39 +4131,32 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
}
}

/// Check a balance against a channel reserve requirement
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
#[cfg(splicing)]
pub fn check_balance_meets_reserve_requirement(balance: u64, channel_value: u64, dust_limit: u64) -> Result<(), u64> {
if balance == 0 {
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64, dust_limit: u64) -> Result<(), u64> {
if post_balance == 0 {
// 0 balance is fine
Ok(())
} else {
let channel_reserve = get_v2_channel_reserve_satoshis(channel_value, dust_limit);
if balance >= channel_reserve {
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
if post_balance >= post_channel_reserve {
Ok(())
} else {
Err(channel_reserve)
}
}
}

/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
#[cfg(splicing)]
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64, dust_limit: u64) -> Result<(), u64> {
match Self::check_balance_meets_reserve_requirement(
post_balance, post_channel_value, dust_limit
) {
Ok(_) => Ok(()),
Err(post_channel_reserve) =>
// post is not OK, check pre
match Self::check_balance_meets_reserve_requirement(
pre_balance, pre_channel_value, dust_limit
) {
if pre_balance == 0 {
// pre OK, post not -> not
Ok(_) => Err(post_channel_reserve),
// post not OK, but so was pre -> OK
Err(_) => Ok(()),
Err(post_channel_reserve)
} else {
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
if pre_balance >= pre_channel_reserve {
// pre OK, post not -> not
Err(post_channel_reserve)
} else {
// post not OK, but so was pre -> OK
Ok(())
}
}
}
}
}

Expand Down

0 comments on commit cd3c40b

Please sign in to comment.