Skip to content

Sweeper async change destination source fetching #3734

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

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

Conversation

joostjager
Copy link
Contributor

@joostjager joostjager commented Apr 14, 2025

This PR converts OutputSweeper to take an async ChangeDestinationSource implementation. This allows a (remote) address fetch call to run without blocking chain notifications.

Furthermore the changes demonstrates how LDK could be written in a natively async way, and still allow usage from a sync context using wrappers.

Part of #3540

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 14, 2025

👋 Thanks for assigning @TheBlueMatt 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.

}

Ok(())
self.persist_state(&*state_lock).map_err(|e| {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No more sweeping in this method, all moved to a timer.

if let Some(spending_tx) = spending_tx_opt {
self.broadcaster.broadcast_transactions(&[&spending_tx]);
}
let _ = self.persist_state(&*state_lock).map_err(|e| {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No more sweeping in this event handler.

if respend_descriptors.is_empty() {
// Nothing to do.
return None;
let change_destination_script = change_destination_script_result?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Todo: address risk of getting a tx with a new address every block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Todo: investigate what BDK does here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Traced through BDK a bit. It seems that there is only inflation if we actually use the address in a tx. It won't blindly regenerate addresses when called. But will double check this.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that there is only inflation if we actually use the address in a tx. It won't blindly regenerate addresses when called. But will double check this.

This mostly depends on the API used.

@joostjager joostjager force-pushed the async-sweep branch 2 times, most recently from c3420ab to d6051d9 Compare April 16, 2025 09:28
@joostjager joostjager force-pushed the async-sweep branch 2 times, most recently from 90d7104 to 370d677 Compare April 16, 2025 16:32
sweeper: OutputSweeper<B, D, E, F, K, L, O>,
}

impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
Copy link
Contributor Author

@joostjager joostjager Apr 16, 2025

Choose a reason for hiding this comment

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

What might be good about this wrapper is that it eliminates the possibility of someone implementing async logic in combination with future poll/ready checking. This wrapper only accepts a sync trait.

@joostjager joostjager force-pushed the async-sweep branch 4 times, most recently from a9812ea to df47e8d Compare April 18, 2025 10:23
Copy link

codecov bot commented Apr 21, 2025

Codecov Report

Attention: Patch coverage is 81.65939% with 42 lines in your changes missing coverage. Please review.

Project coverage is 90.60%. Comparing base (f507778) to head (129d0f1).
Report is 62 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/util/sweep.rs 77.16% 25 Missing and 12 partials ⚠️
lightning/src/util/async_poll.rs 57.14% 3 Missing ⚠️
lightning-background-processor/src/lib.rs 96.22% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3734      +/-   ##
==========================================
+ Coverage   89.13%   90.60%   +1.46%     
==========================================
  Files         157      157              
  Lines      123851   134752   +10901     
  Branches   123851   134752   +10901     
==========================================
+ Hits       110395   122092   +11697     
+ Misses      10779    10016     -763     
+ Partials     2677     2644      -33     

☔ 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.

@joostjager joostjager force-pushed the async-sweep branch 6 times, most recently from f3e911e to 6cd735c Compare April 21, 2025 11:35
@joostjager
Copy link
Contributor Author

Rebased after merge of #3509

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the OutputSweeper logic to support asynchronous change destination source fetching while still enabling synchronous usage via a new wrapper. Key changes include:

  • Converting OutputSweeper to use an async implementation for fetching change addresses.
  • Introducing a RuntimeSweeperState to support concurrent sweeper operations.
  • Adding an OutputSweeperSync wrapper to allow synchronous usage of the async API.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
lightning/src/util/sweep.rs Refactored OutputSweeper APIs to support async operations and state management.
lightning/src/util/async_poll.rs Updated dummy_waker implementation to work with a complete Waker vtable.
lightning/src/sign/mod.rs Introduced async alias and a synchronous wrapper (ChangeDestinationSourceSyncWrapper) for change destination fetching.
lightning/src/lib.rs Removed the forbid(unsafe_code) attribute.
lightning-background-processor/src/lib.rs Integrated the new sweeper APIs into background processing flows.
Comments suppressed due to low confidence (1)

lightning/src/lib.rs:32

  • The removal of the 'forbid(unsafe_code)' attribute now permits unsafe code in production. If this removal was not intentional, consider re-enabling it or ensuring that any introduced unsafe code is thoroughly reviewed.
#![cfg_attr(not(any(test, fuzzing, feature = "_test_utils")), forbid(unsafe_code))]

Comment on lines 974 to 975
// In a sync context, we can't wait for the future to complete.
panic!("task not ready");
Copy link
Preview

Copilot AI Apr 25, 2025

Choose a reason for hiding this comment

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

[nitpick] Using a panic in the synchronous wrapper when the future is not immediately ready may lead to abrupt termination. Consider returning an error or implementing a blocking wait so that the caller can handle the situation gracefully.

Suggested change
// In a sync context, we can't wait for the future to complete.
panic!("task not ready");
// In a sync context, block until the future is ready.
return futures::executor::block_on(fut);

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we want to be not tolerant in this case, because everything lives in the wrapper.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I'm a bit on the fence about this. But if we indeed want to panic here, using the unreachable! macro would be the most idiomatic way to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made it unreachable!

Copy link
Collaborator

Choose a reason for hiding this comment

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

We absolutely don't want to take a dependency to seek to run a future in a fresh executor in cases where its unreachable :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AI is going to AI

@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @tnull! 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

🔔 3rd Reminder

Hey @tnull! 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.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Pretty much LGTM, mod some comments.

I think the main think I'd prefer to be changed is the (rather ugly) RuntimeSweperState wrapping.

) -> Result<(), lightning::io::Error>
where
UL::Target: 'static + UtxoLookup,
CF::Target: 'static + chain::Filter,
CF::Target: 'static + chain::Filter + Sync + Send,
Copy link
Contributor

Choose a reason for hiding this comment

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

Still not quite sure why/whether these bounds are necessary now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to remove them, but that gives me a compiler error

<CF as Deref>::Target cannot be shared between threads safely
the trait std::marker::Sync is not implemented for <CF as Deref>::Target

On the OS: 'static + Deref<Target = OutputSweeper<T, D, F, CF, K, L, O>> bound.

@@ -1269,7 +1322,7 @@ mod tests {
Arc<test_utils::TestBroadcaster>,
Arc<TestWallet>,
Arc<test_utils::TestFeeEstimator>,
Arc<dyn Filter + Sync + Send>,
Arc<test_utils::TestChainSource>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was so much fighting and trying. If I revert I am getting:

type mismatch resolving <Arc<OutputSweeper<Arc, Arc, Arc, Arc<dyn Filter + Send + Sync>, Arc<...>, ..., ...>> as Deref>::Target == OutputSweeper<Arc, Arc, Arc, Arc, , Arc<...>, ...>expected structOutputSweeper<, _, _, Arc, _, _, >found structOutputSweeper<, _, _, Arc<dyn lightning::chain::Filter + Send + std::marker::Sync>, Arc, _, Arc> TestChainSourceimplementsFilterso you could change the expected type toBox``

@@ -755,6 +782,11 @@ where
}
}

struct RuntimeSweeperState {
Copy link
Contributor

Choose a reason for hiding this comment

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

Tbh. I'm not quite sure why sweep_pending can't just be an AtomicBool that lives in OutputSweeper directly? That would also allow us to check whether we're pending without acquiring (and potentially blocking on) the state mutex?

@@ -975,17 +978,56 @@ pub trait SignerProvider {
fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()>;
}

/// A type alias for a future that returns a result of type T.
pub type AsyncResult<'a, T> = Pin<Box<dyn Future<Output = Result<T, ()>> + 'a + Send>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a more general type related to future-polling. I wonder if it should rather live in utils/wakers.rs or async_poll.rs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to async_poll.rs

Comment on lines 974 to 975
// In a sync context, we can't wait for the future to complete.
panic!("task not ready");
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I'm a bit on the fence about this. But if we indeed want to panic here, using the unreachable! macro would be the most idiomatic way to do it.

pub fn sweeper_async(
&self,
) -> Arc<OutputSweeper<B, Arc<ChangeDestinationSourceSyncWrapper<D>>, E, F, K, L, O>> {
self.sweeper.clone()
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is only necessary for tests, we could just also feature-gate the Arc-wrapping in on _test_utils, no? This way we could use the approach you proposed in test, while not actually doing it in production?

To prepare for asynchronous processing of the sweep, we need to decouple
the spending from the chain notifications. These notifications run in a
sync context and wouldn't allow calls into an async trait.

Instead we now periodically call into the sweeper, to open up the
possibility to do so from an async context if desired.
@joostjager
Copy link
Contributor Author

joostjager commented May 1, 2025

Futures flag removed

Changed my mind. Will do in a follow up.

@joostjager joostjager force-pushed the async-sweep branch 2 times, most recently from d317ae3 to 493b686 Compare May 1, 2025 10:48
@joostjager joostjager requested a review from tnull May 1, 2025 12:32
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @tnull! 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 @tnull! 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

🔔 3rd Reminder

Hey @tnull! 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

🔔 4th Reminder

Hey @tnull! 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

🔔 5th Reminder

Hey @tnull! 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.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Excuse the delay here!

Mostly looks good from my side I think, just a few minor comments/nits. Will to another final thorough pass after.


use crate::sync::Arc;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Let's move this to the other crate imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's cleaner, but also I have to say I never look at these imports. But may be different for others. Moved.

@@ -567,16 +623,15 @@ where
}

fn spend_outputs(
&self, sweeper_state: &SweeperState, descriptors: Vec<&SpendableOutputDescriptor>,
&self, sweeper_state: &SweeperState, descriptors: &Vec<&SpendableOutputDescriptor>,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Mhh, &Vec<&..> looks funny. I guess the signature from spend_spendable_outputs would be preferable: &[&SpendableOutputDescriptor].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Out of curiosity, what is funny about it? Or how is &[&..] less funny? Fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Usually we try to create the least-strict API if there is no reason to. In this case, the prior API would enforce a reference to an allocated Vec, while, if we just require the slice in the API, we might (easier) be able to drop the allocation if it's not necessary.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM, mod one question and a few final nits.

},
Err(e) => {
log_error!(self.logger, "Error spending outputs: {:?}", e);
return Ok(());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we ignoring the error here instead of propagating it up? (If we'd want to ignore it for some reason, can we just have spend_outputs not return a Result and move this match / logging into it?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was pre-existing to not do anything with that error other than log:

I am not sure if the caller can do something more with the error. I do see that there non-happy flow error cases in spend_spendable_outputs, so might be better to at least give the users the choice. Added map_err.

@joostjager joostjager requested a review from tnull May 13, 2025 11:00
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

One outstanding nit, non-blocking though. If you want, feel free to address while squashing also.

synchronous wrappers for usage in a sync context.
@joostjager joostjager requested a review from TheBlueMatt May 13, 2025 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants