Skip to content

Allow retrieval of SpendableOutputDescriptors from relevant transactions #2609

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

Merged

Conversation

wpaulino
Copy link
Contributor

Currently, our API will only expose SpendableOutputDescriptors once after they are no longer under reorg risk (see ANTI_REORG_DELAY). Users have often requested they'd like the ability to retrieve these in some other way, either for historical purposes, or to handle replaying any in the event of a failure.

Fixes #2493.

@wpaulino wpaulino added this to the 0.0.117 milestone Sep 27, 2023
@codecov-commenter
Copy link

codecov-commenter commented Sep 27, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (1ac53ed) 88.84% compared to head (7e10b94) 88.97%.
Report is 45 commits behind head on main.

❗ Current head 7e10b94 differs from pull request most recent head ffec24b. Consider uploading reports for the commit ffec24b to get more accurate results

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2609      +/-   ##
==========================================
+ Coverage   88.84%   88.97%   +0.13%     
==========================================
  Files         113      112       -1     
  Lines       84729    86261    +1532     
  Branches    84729    86261    +1532     
==========================================
+ Hits        75275    76749    +1474     
- Misses       7240     7269      +29     
- Partials     2214     2243      +29     
Files Coverage Δ
lightning/src/chain/channelmonitor.rs 84.88% <100.00%> (+0.05%) ⬆️
lightning/src/ln/monitor_tests.rs 97.82% <100.00%> (-0.59%) ⬇️

... and 20 files with indirect coverage changes

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

@wpaulino wpaulino force-pushed the monitor-get-spendable-output branch from 7a19b6d to 67923b0 Compare September 27, 2023 18:57
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Code LGTM.

@wpaulino wpaulino force-pushed the monitor-get-spendable-output branch from 67923b0 to ff510c3 Compare September 27, 2023 23:14
TheBlueMatt
TheBlueMatt previously approved these changes Sep 28, 2023
Copy link
Contributor

@vladimirfomene vladimirfomene left a comment

Choose a reason for hiding this comment

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

ACK ff510c3

@wpaulino wpaulino force-pushed the monitor-get-spendable-output branch from 29b714c to 130abf8 Compare September 28, 2023 17:58
@wpaulino wpaulino force-pushed the monitor-get-spendable-output branch 2 times, most recently from d09d457 to 7e10b94 Compare September 28, 2023 19:03
dunxen
dunxen previously approved these changes Sep 28, 2023
Currently, our API will only expose `SpendableOutputDescriptor`s once
after they are no longer under reorg risk (see `ANTI_REORG_DELAY`).
Users have often requested they'd like the ability to retrieve these in
some other way, either for historical purposes, or to handle replaying
any in the event of a failure.
Assuming our keys haven't been compromised, and that random transactions
aren't learning of these scripts somehow and sending funds to them, it
was only possible for one spendable output to exist within a
transaction.

- `shutdown_script` can only exist in co-op close transactions.
- `counterparty_payment_script` can only exist in counterparty
  commitment transactions.
- `broadcasted_holder_revokable_script` can only exist in holder
  commitment/HTLC transactions.
- `destination_script` can exist in any other type of claim we support.

Now that we're exposing this API to users such that they can rescan any
relevant transactions, there's no harm in allowing them to claim more
funds from spendable outputs than we expected.
Copy link
Contributor

@G8XSU G8XSU left a comment

Choose a reason for hiding this comment

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

LGTM!

/// descriptors at a later time, either for historical purposes, or to replay any
/// missed/unhandled descriptors. For the purpose of gathering historical records, if the
/// channel close has fully resolved (i.e., [`ChannelMonitor::get_claimable_balances`] returns
/// an empty set), you can retrieve all spendable outputs by providing all descendant spending
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems to imply you only need to pass the one immediate descendant - the commitment tx, but actually we need to go two layers deep.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Lets consdier tweaking the wording here in a followup.

@wpaulino
Copy link
Contributor Author

Also need to make sure we don't yield a DelayedPaymentDescriptor before its to_self_delay is met.

Copy link

@ariard ariard left a comment

Choose a reason for hiding this comment

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

Code Review ACK ffec24b, though didn’t check the test changes.

See second comment, though I don’t think this is a new issue brought by this PR.

pub fn get_spendable_outputs(&self, tx: &Transaction, confirmation_height: u32) -> Vec<SpendableOutputDescriptor> {
let inner = self.inner.lock().unwrap();
let current_height = inner.best_block.height;
if current_height.saturating_sub(ANTI_REORG_DELAY) + 1 >= confirmation_height {
Copy link

Choose a reason for hiding this comment

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

One API advance could be to pass a user-selected value rather than to rely on the hardcoded ANTI_REORG_DELAY. If the spend is low-value or you wish to have the liquidity locked up in a let’s say StaticPaymentOutputDescriptor available to spend now 1-conf or 2-conf might be enough.

Here ANTI_REORG_DELAY doesn’t necessarily have the same level of severity than in other locations of channel monitor, where we wanna to be sure a HTLC-timeout cannot be reorged out by a HTLC-preimage on an outbound link before passing backward the timeout. I think a descriptor_confirmation_delay user setting could be introduced in the future.

@@ -3441,7 +3468,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
self.is_resolving_htlc_output(&tx, height, &block_hash, &logger);

self.is_paying_spendable_output(&tx, height, &block_hash, &logger);
self.check_tx_and_push_spendable_outputs(&tx, height, &block_hash, &logger);
Copy link

Choose a reason for hiding this comment

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

If a counterparty is paying to one of the spendable descriptor (e.g our payment_point as a P2WSH or P2WPKH) from let’s say a second-stage malleable HTLC-transaction on our local / holder commitment transaction, we might receive a descriptor of a “tag” value that can be latter used for e.g deanonymization attack (at the image of sending dust value to on-chain wallet to fingerprint them).

I don’t think this is a new concern and this sounds already permissible by current is_paying_spendable_output. l still think our transaction pattern matching in transactions_confirmed could be stronger to check that non-commitment-or-closing-transaction-paying-to-us-is-spent-by-us.

@TheBlueMatt TheBlueMatt merged commit 6016101 into lightningdevkit:main Sep 29, 2023
@TheBlueMatt TheBlueMatt mentioned this pull request Sep 29, 2023
@wpaulino wpaulino deleted the monitor-get-spendable-output branch September 29, 2023 16:13
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.

Option to regenerate SpendableOutputs
8 participants