Skip to content

Commit 1bb3865

Browse files
committed
Refined balance reserve check; add locking (review)
1 parent 897649c commit 1bb3865

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
@@ -4095,29 +4095,30 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
40954095
}
40964096
}
40974097

4098-
/// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
4098+
/// Check that balances meet the channel reserve requirements or violates them (below reserve).
40994099
/// The channel value is an input as opposed to using from self, so that this can be used in case of splicing
41004100
/// to checks with new channel value (before being comitted to it).
41014101
#[cfg(splicing)]
4102-
pub fn check_balance_meets_v2_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> {
4103-
if balance == 0 {
4104-
return Ok(());
4105-
}
4106-
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4107-
channel_value, self.holder_dust_limit_satoshis);
4108-
if balance < holder_selected_channel_reserve_satoshis {
4109-
return Err(ChannelError::Warn(format!(
4110-
"Balance below reserve mandated by holder, {} vs {}",
4111-
balance, holder_selected_channel_reserve_satoshis,
4112-
)));
4102+
pub fn check_balance_meets_v2_reserve_requirements(&self, self_balance: u64, counterparty_balance: u64, channel_value: u64) -> Result<(), ChannelError> {
4103+
if self_balance > 0 {
4104+
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4105+
channel_value, self.holder_dust_limit_satoshis);
4106+
if self_balance < holder_selected_channel_reserve_satoshis {
4107+
return Err(ChannelError::Warn(format!(
4108+
"Balance below reserve mandated by holder, {} vs {}",
4109+
self_balance, holder_selected_channel_reserve_satoshis,
4110+
)));
4111+
}
41134112
}
4114-
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4115-
channel_value, self.counterparty_dust_limit_satoshis);
4116-
if balance < counterparty_selected_channel_reserve_satoshis {
4117-
return Err(ChannelError::Warn(format!(
4118-
"Balance below reserve mandated by counterparty, {} vs {}",
4119-
balance, counterparty_selected_channel_reserve_satoshis,
4120-
)));
4113+
if counterparty_balance > 0 {
4114+
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4115+
channel_value, self.counterparty_dust_limit_satoshis);
4116+
if counterparty_balance < counterparty_selected_channel_reserve_satoshis {
4117+
return Err(ChannelError::Warn(format!(
4118+
"Balance below reserve mandated by counterparty, {} vs {}",
4119+
counterparty_balance, counterparty_selected_channel_reserve_satoshis,
4120+
)));
4121+
}
41214122
}
41224123
Ok(())
41234124
}
@@ -8554,10 +8555,11 @@ impl<SP: Deref> FundedChannel<SP> where
85548555

85558556
let pre_channel_value = self.funding.get_value_satoshis();
85568557
let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
8557-
let post_balance = PendingSplice::add_checked(self.funding.value_to_self_msat, our_funding_contribution);
8558-
// Pre-check for reserve requirement, assuming maximum balance of full channel value
8558+
let post_balance_self = PendingSplice::add_checked(self.funding.value_to_self_msat, our_funding_contribution);
8559+
let post_balance_counterparty = post_channel_value.saturating_sub(post_balance_self);
8560+
// Pre-check for reserve requirement
85598561
// This will also be checked later at tx_complete
8560-
let _res = self.context.check_balance_meets_v2_reserve_requirements(post_balance, post_channel_value)?;
8562+
let _res = self.context.check_balance_meets_v2_reserve_requirements(post_balance_self, post_balance_counterparty, post_channel_value)?;
85618563
Ok(())
85628564
}
85638565

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4292,6 +4292,7 @@ where
42924292
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
42934293
funding_feerate_per_kw: u32, locktime: u32,
42944294
) -> Result<(), APIError> {
4295+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
42954296
let per_peer_state = self.per_peer_state.read().unwrap();
42964297

42974298
let peer_state_mutex = per_peer_state.get(counterparty_node_id)

0 commit comments

Comments
 (0)