Skip to content

Commit e145119

Browse files
committed
fix Pre&Post reserve check; assert no-HTLC
1 parent 58d02b0 commit e145119

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
@@ -5126,26 +5126,21 @@ where
51265126
pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64,
51275127
dust_limit: u64,
51285128
) -> Result<(), u64> {
5129-
if post_balance == 0 {
5130-
// 0 balance is fine
5131-
return Ok(());
5132-
}
51335129
let post_channel_reserve = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
51345130
if post_balance >= post_channel_reserve {
51355131
return Ok(());
51365132
}
5137-
// post is not OK, check pre
5138-
if pre_balance == 0 {
5139-
// pre OK, post not -> not
5140-
return Err(post_channel_reserve);
5141-
}
51425133
let pre_channel_reserve = get_v2_channel_reserve_satoshis(pre_channel_value, dust_limit);
51435134
if pre_balance >= pre_channel_reserve {
5144-
// pre OK, post not -> not
5135+
// We're not allowed to dip below the reserve once we've been above.
51455136
return Err(post_channel_reserve);
51465137
}
5147-
// post not OK, but so was pre -> OK
5148-
Ok(())
5138+
// Make sure we either remain with the same balance or move towards the reserve.
5139+
if post_balance >= pre_balance {
5140+
Ok(())
5141+
} else {
5142+
Err(post_channel_reserve)
5143+
}
51495144
}
51505145

51515146
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
@@ -10420,10 +10415,16 @@ where
1042010415
}
1042110416

1042210417
/// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
10418+
/// Pending HTLCs are not taken into account, this method should be used when there is no such,
10419+
/// e.g. in quiscence state
1042310420
#[cfg(splicing)]
1042410421
fn compute_balances_less_fees(
1042510422
&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool,
1042610423
) -> (u64, u64) {
10424+
// We should get here only when there are no pending HTLCs, as they are not taken into account
10425+
debug_assert!(self.context.pending_inbound_htlcs.is_empty());
10426+
debug_assert!(self.context.pending_outbound_htlcs.is_empty());
10427+
1042710428
let feerate_per_kw = self.context.feerate_per_kw;
1042810429

1042910430
// compute 'raw' counterparty balance

0 commit comments

Comments
 (0)