Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Splicing] Add reserve check to splicing #3641

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

optout21
Copy link
Contributor

@optout21 optout21 commented Mar 4, 2025

This is a continuation of #3407, adds proper channel balance/reserve check, when handling splice_ack (on initiator side).

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Mar 4, 2025

👋 Thanks for assigning @wpaulino as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@optout21
Copy link
Contributor Author

optout21 commented Mar 4, 2025

Relevant comments from #3407:

Relevant method: check_splice_balances_meet_v2_reserve_requirements

@optout21 optout21 force-pushed the splice-reserve-check branch 2 times, most recently from e34d33c to 6ec1a19 Compare March 4, 2025 19:37
@optout21
Copy link
Contributor Author

optout21 commented Mar 4, 2025

Suggestions implemented; for reserve requirement check, the balance is adjusted with fees and eventual anchor output value (see 6ec1a19).
On hold, waiting for #3407 to land first.

@optout21 optout21 force-pushed the splice-reserve-check branch from 6ec1a19 to 8ac1467 Compare March 8, 2025 07:37
@optout21
Copy link
Contributor Author

optout21 commented Mar 8, 2025

Rebased, post #3407 .

Copy link

codecov bot commented Mar 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.22%. Comparing base (00ee0ef) to head (f406c53).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3641      +/-   ##
==========================================
- Coverage   89.23%   89.22%   -0.01%     
==========================================
  Files         155      155              
  Lines      119327   119327              
  Branches   119327   119327              
==========================================
- Hits       106482   106473       -9     
- Misses      10242    10249       +7     
- Partials     2603     2605       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@optout21 optout21 force-pushed the splice-reserve-check branch from 8ac1467 to b073d8c Compare March 10, 2025 16:56
@optout21 optout21 changed the title [Draft][Splicing] Add reserve check to splicing [Splicing] Add reserve check to splicing Mar 11, 2025
@optout21 optout21 marked this pull request as ready for review March 11, 2025 08:40
@optout21 optout21 requested a review from wpaulino March 11, 2025 08:40
@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@optout21 optout21 force-pushed the splice-reserve-check branch from b073d8c to f406c53 Compare March 12, 2025 10:39
@jkczyz jkczyz self-requested a review March 12, 2025 13:37
@optout21 optout21 marked this pull request as draft March 19, 2025 23:38
@optout21
Copy link
Contributor Author

Fixes done:

  • Fix Pre&Post reserve check
  • Assert that there are no pending HTLCs
  • Move splice reserve checks to FundedChannel, because few other fields are needed (is_v2, reserves from funding)
  • Fix Msat-Sat discrepancy in balance/reserve check, which I just noticed
  • Handle originally-v1 channel pre-splice reserve specially.

@optout21 optout21 marked this pull request as ready for review March 20, 2025 09:51
@optout21 optout21 requested a review from wpaulino March 20, 2025 09:51
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @jkczyz @wpaulino! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @jkczyz @wpaulino! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

return Err(post_channel_reserve_sats);
}
} else {
if pre_balance >= self.funding.holder_selected_channel_reserve_satoshis * 1000 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to check if the channel has never been spliced before to use the v1 reserve

/// Pending HTLCs are not taken into account, this method should be used when there is no such,
/// e.g. in quiscence state
#[cfg(splicing)]
fn compute_balances_less_fees(&self, channel_value_sats: u64, value_to_self_msat: u64, is_local: bool) -> (u64, u64) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tankyleo is going to split up the existing build_commitment_transaction method into two, so that we can get the balances without building the commitment transaction. Once that lands, we can use it here in favor of this to avoid the code duplication.

/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
/// Returns the minimum channel reserve (sats)
#[cfg(splicing)]
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(&self, pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64, dust_limit: u64) -> Result<(), u64> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this below check_splice_balances_meet_v2_reserve_requirements so that the code could be read top-down?

/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
/// Returns the minimum channel reserve (sats)
#[cfg(splicing)]
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(&self, pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64, dust_limit: u64) -> Result<(), u64> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "noerr" if it can error?

}

/// Check that post-splicing balance meets reserve requirements, but only if it met it pre-splice as well.
/// Returns the minimum channel reserve (sats)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should specify on failure.

#[cfg(splicing)]
pub fn check_splice_balance_meets_v2_reserve_requirement_noerr(&self, pre_balance: u64, post_balance: u64, pre_channel_value: u64, post_channel_value: u64, dust_limit: u64) -> Result<(), u64> {
let post_channel_reserve_sats = get_v2_channel_reserve_satoshis(post_channel_value, dust_limit);
if post_balance >= post_channel_reserve_sats * 1000 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we name the function parameters with _msat?

Comment on lines +8618 to +8623
let pre_channel_value = self.funding.get_value_satoshis();
let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
let pre_balance_self = self.funding.value_to_self_msat;
let post_balance_self = PendingSplice::add_checked(pre_balance_self, our_funding_contribution);
let (pre_balance_self_less_fees, pre_balance_counterparty_less_fees) = self.compute_balances_less_fees(pre_channel_value, pre_balance_self, true);
let (post_balance_self_less_fees, post_balance_counterparty_less_fees) = self.compute_balances_less_fees(post_channel_value, post_balance_self, true);
Copy link
Contributor

@jkczyz jkczyz Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Units on these, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants