Skip to content

Commit e6d2297

Browse files
committed
fix Pre&Post reserve check; assert no-HTLC
1 parent f406c53 commit e6d2297

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,26 +4269,21 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42694269
/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well
42704270
#[cfg(splicing)]
42714271
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> {
4272-
if post_balance == 0 {
4273-
// 0 balance is fine
4274-
return Ok(());
4275-
}
42764272
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
42774273
if post_balance >= post_channel_reserve {
42784274
return Ok(());
42794275
}
4280-
// post is not OK, check pre
4281-
if pre_balance == 0 {
4282-
// pre OK, post not -> not
4283-
return Err(post_channel_reserve);
4284-
}
42854276
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
42864277
if pre_balance >= pre_channel_reserve {
4287-
// pre OK, post not -> not
4278+
// We're not allowed to dip below the reserve once we've been above.
42884279
return Err(post_channel_reserve);
42894280
}
4290-
// post not OK, but so was pre -> OK
4291-
Ok(())
4281+
// Make sure we either remain with the same balance or move towards the reserve.
4282+
if post_balance >= pre_balance {
4283+
Ok(())
4284+
} else {
4285+
Err(post_channel_reserve)
4286+
}
42924287
}
42934288

42944289
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
@@ -8611,8 +8606,14 @@ impl<SP: Deref> FundedChannel<SP> where
86118606
}
86128607

86138608
/// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
8609+
/// Pending HTLCs are not taken into account, this method should be used when there is no such,
8610+
/// e.g. in quiscence state
86148611
#[cfg(splicing)]
86158612
fn compute_balances_less_fees(&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool) -> (u64, u64) {
8613+
// We should get here only when there are no pending HTLCs, as they are not taken into account
8614+
debug_assert!(self.context.pending_inbound_htlcs.is_empty());
8615+
debug_assert!(self.context.pending_outbound_htlcs.is_empty());
8616+
86168617
let feerate_per_kw = self.context.feerate_per_kw;
86178618

86188619
// compute 'raw' counterparty balance

0 commit comments

Comments
 (0)