Skip to content

Commit 720a85f

Browse files
committed
Refined balance reserve check; add locking (review)
1 parent 454d7e9 commit 720a85f

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,29 +3973,30 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
39733973
(context.holder_selected_channel_reserve_satoshis, context.counterparty_selected_channel_reserve_satoshis)
39743974
}
39753975

3976-
/// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
3976+
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
39773977
/// The channel value is an input as opposed to using from self, so that this can be used in case of splicing
39783978
/// to checks with new channel value (before being comitted to it).
39793979
#[cfg(splicing)]
3980-
pub fn check_balance_meets_v2_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> {
3981-
if balance == 0 {
3982-
return Ok(());
3983-
}
3984-
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3985-
channel_value, self.holder_dust_limit_satoshis);
3986-
if balance < holder_selected_channel_reserve_satoshis {
3987-
return Err(ChannelError::Warn(format!(
3988-
"Balance below reserve mandated by holder, {} vs {}",
3989-
balance, holder_selected_channel_reserve_satoshis,
3990-
)));
3980+
pub fn check_balance_meets_v2_reserve_requirements(&self, self_balance: u64, counterparty_balance: u64, channel_value: u64) -> Result<(), ChannelError> {
3981+
if self_balance > 0 {
3982+
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3983+
channel_value, self.holder_dust_limit_satoshis);
3984+
if self_balance < holder_selected_channel_reserve_satoshis {
3985+
return Err(ChannelError::Warn(format!(
3986+
"Balance below reserve mandated by holder, {} vs {}",
3987+
self_balance, holder_selected_channel_reserve_satoshis,
3988+
)));
3989+
}
39913990
}
3992-
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3993-
channel_value, self.counterparty_dust_limit_satoshis);
3994-
if balance < counterparty_selected_channel_reserve_satoshis {
3995-
return Err(ChannelError::Warn(format!(
3996-
"Balance below reserve mandated by counterparty, {} vs {}",
3997-
balance, counterparty_selected_channel_reserve_satoshis,
3998-
)));
3991+
if counterparty_balance > 0 {
3992+
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
3993+
channel_value, self.counterparty_dust_limit_satoshis);
3994+
if counterparty_balance < counterparty_selected_channel_reserve_satoshis {
3995+
return Err(ChannelError::Warn(format!(
3996+
"Balance below reserve mandated by counterparty, {} vs {}",
3997+
counterparty_balance, counterparty_selected_channel_reserve_satoshis,
3998+
)));
3999+
}
39994000
}
40004001
Ok(())
40014002
}
@@ -8379,10 +8380,11 @@ impl<SP: Deref> FundedChannel<SP> where
83798380

83808381
let pre_channel_value = self.context.get_value_satoshis();
83818382
let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
8382-
let post_balance = PendingSplice::add_checked(self.context.value_to_self_msat, our_funding_contribution);
8383-
// Pre-check for reserve requirement, assuming maximum balance of full channel value
8383+
let post_balance_self = PendingSplice::add_checked(self.context.value_to_self_msat, our_funding_contribution);
8384+
let post_balance_counterparty = post_channel_value.saturating_sub(post_balance_self);
8385+
// Pre-check for reserve requirement
83848386
// This will also be checked later at tx_complete
8385-
let _res = self.context.check_balance_meets_v2_reserve_requirements(post_balance, post_channel_value)?;
8387+
let _res = self.context.check_balance_meets_v2_reserve_requirements(post_balance_self, post_balance_counterparty, post_channel_value)?;
83868388
Ok(())
83878389
}
83888390

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,7 @@ where
42454245
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
42464246
funding_feerate_per_kw: u32, locktime: u32,
42474247
) -> Result<(), APIError> {
4248+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
42484249
let per_peer_state = self.per_peer_state.read().unwrap();
42494250

42504251
let peer_state_mutex = per_peer_state.get(counterparty_node_id)

0 commit comments

Comments
 (0)