Skip to content

Commit b073d8c

Browse files
committed
Consider fees and anchor in balances when checking reserve requirements
1 parent 6aaca76 commit b073d8c

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lightning/src/ln/channel.rs

+44-3
Original file line numberDiff line numberDiff line change
@@ -8618,6 +8618,43 @@ impl<SP: Deref> FundedChannel<SP> where
86188618
Ok(splice_ack_msg)
86198619
}
86208620

8621+
/// Compute the channel balances (local & remote) by taking into account fees, anchor values, and dust limits.
8622+
#[cfg(splicing)]
8623+
fn compute_balances_less_fees(&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool) -> (u64, u64) {
8624+
let feerate_per_kw = self.context.feerate_per_kw;
8625+
8626+
// compute 'raw' counterparty balance
8627+
let value_to_remote_msat: i64 = ((channel_value_sats * 1000) as i64).saturating_sub(value_to_self_msat as i64);
8628+
debug_assert!(value_to_remote_msat >= 0);
8629+
8630+
let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, 0, &self.funding.channel_transaction_parameters.channel_type_features);
8631+
let anchors_val = if self.funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 } as i64;
8632+
8633+
// consider fees and anchor values
8634+
let (mut value_to_self, mut value_to_remote) = if self.funding.is_outbound() {
8635+
((value_to_self_msat as i64) / 1000 - anchors_val - total_fee_sat as i64, value_to_remote_msat / 1000)
8636+
} else {
8637+
((value_to_self_msat as i64) / 1000, value_to_remote_msat / 1000 - anchors_val - total_fee_sat as i64)
8638+
};
8639+
8640+
// consider dust limit
8641+
let broadcaster_dust_limit_satoshis = if is_local {
8642+
self.context.holder_dust_limit_satoshis
8643+
} else {
8644+
self.context.counterparty_dust_limit_satoshis
8645+
} as i64;
8646+
if value_to_self < broadcaster_dust_limit_satoshis {
8647+
value_to_self = 0;
8648+
}
8649+
debug_assert!(value_to_self >= 0);
8650+
if value_to_remote < broadcaster_dust_limit_satoshis {
8651+
value_to_remote = 0;
8652+
}
8653+
debug_assert!(value_to_remote >= 0);
8654+
8655+
(value_to_self as u64, value_to_remote as u64)
8656+
}
8657+
86218658
/// Handle splice_ack
86228659
#[cfg(splicing)]
86238660
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
@@ -8637,11 +8674,15 @@ impl<SP: Deref> FundedChannel<SP> where
86378674
let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
86388675
let pre_balance_self = self.funding.value_to_self_msat;
86398676
let post_balance_self = PendingSplice::add_checked(pre_balance_self, our_funding_contribution);
8640-
let pre_balance_counterparty = pre_channel_value.saturating_sub(pre_balance_self);
8641-
let post_balance_counterparty = post_channel_value.saturating_sub(post_balance_self);
8677+
let (pre_balance_self_less_fees, pre_balance_counterparty_less_fees) = self.compute_balances_less_fees(pre_channel_value, pre_balance_self, true);
8678+
let (post_balance_self_less_fees, post_balance_counterparty_less_fees) = self.compute_balances_less_fees(post_channel_value, post_balance_self, true);
86428679
// Pre-check for reserve requirement
86438680
// This will also be checked later at tx_complete
8644-
let _res = self.context.check_splice_balances_meet_v2_reserve_requirements(pre_balance_self, post_balance_self, pre_balance_counterparty, post_balance_counterparty, pre_channel_value, post_channel_value)?;
8681+
let _res = self.context.check_splice_balances_meet_v2_reserve_requirements(
8682+
pre_balance_self_less_fees, post_balance_self_less_fees,
8683+
pre_balance_counterparty_less_fees, post_balance_counterparty_less_fees,
8684+
pre_channel_value, post_channel_value
8685+
)?;
86458686
Ok(())
86468687
}
86478688

0 commit comments

Comments
 (0)