Skip to content

Commit cd3c40b

Browse files
committed
Simplify reserve requirement check (merge two methods)
1 parent 1d9ea18 commit cd3c40b

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

lightning/src/ln/channel.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,39 +4131,32 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41314131
}
41324132
}
41334133

4134-
/// Check a balance against a channel reserve requirement
4134+
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
41354135
#[cfg(splicing)]
4136-
pub fn check_balance_meets_reserve_requirement(balance: u64, channel_value: u64, dust_limit: u64) -> Result<(), u64> {
4137-
if balance == 0 {
4136+
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> {
4137+
if post_balance == 0 {
41384138
// 0 balance is fine
41394139
Ok(())
41404140
} else {
4141-
let channel_reserve = get_v2_channel_reserve_satoshis(channel_value, dust_limit);
4142-
if balance >= channel_reserve {
4141+
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
4142+
if post_balance >= post_channel_reserve {
41434143
Ok(())
41444144
} else {
4145-
Err(channel_reserve)
4146-
}
4147-
}
4148-
}
4149-
4150-
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
4151-
#[cfg(splicing)]
4152-
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> {
4153-
match Self::check_balance_meets_reserve_requirement(
4154-
post_balance, post_channel_value, dust_limit
4155-
) {
4156-
Ok(_) => Ok(()),
4157-
Err(post_channel_reserve) =>
41584145
// post is not OK, check pre
4159-
match Self::check_balance_meets_reserve_requirement(
4160-
pre_balance, pre_channel_value, dust_limit
4161-
) {
4146+
if pre_balance == 0 {
41624147
// pre OK, post not -> not
4163-
Ok(_) => Err(post_channel_reserve),
4164-
// post not OK, but so was pre -> OK
4165-
Err(_) => Ok(()),
4148+
Err(post_channel_reserve)
4149+
} else {
4150+
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
4151+
if pre_balance >= pre_channel_reserve {
4152+
// pre OK, post not -> not
4153+
Err(post_channel_reserve)
4154+
} else {
4155+
// post not OK, but so was pre -> OK
4156+
Ok(())
4157+
}
41664158
}
4159+
}
41674160
}
41684161
}
41694162

0 commit comments

Comments
 (0)