-
Notifications
You must be signed in to change notification settings - Fork 404
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
base: main
Are you sure you want to change the base?
Conversation
👋 Thanks for assigning @TheBlueMatt as a reviewer! |
} | ||
|
||
Ok(()) | ||
self.persist_state(&*state_lock).map_err(|e| { |
There was a problem hiding this comment.
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| { |
There was a problem hiding this comment.
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?; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
c3420ab
to
d6051d9
Compare
90d7104
to
370d677
Compare
lightning/src/util/sweep.rs
Outdated
sweeper: OutputSweeper<B, D, E, F, K, L, O>, | ||
} | ||
|
||
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref> |
There was a problem hiding this comment.
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.
a9812ea
to
df47e8d
Compare
Codecov ReportAttention: Patch coverage is
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. 🚀 New features to boost your workflow:
|
f3e911e
to
6cd735c
Compare
Rebased after merge of #3509 |
There was a problem hiding this 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))]
lightning/src/util/sweep.rs
Outdated
// In a sync context, we can't wait for the future to complete. | ||
panic!("task not ready"); |
There was a problem hiding this comment.
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.
// 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made it unreachable!
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
🔔 2nd Reminder Hey @tnull! This PR has been waiting for your review. |
🔔 3rd Reminder Hey @tnull! This PR has been waiting for your review. |
There was a problem hiding this 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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 struct
OutputSweeper<, _, _, Arc, _, _, >found struct
OutputSweeper<, _, _, Arc<dyn lightning::chain::Filter + Send + std::marker::Sync>, Arc, _, Arc>
TestChainSourceimplements
Filterso you could change the expected type to
Box``
lightning/src/util/sweep.rs
Outdated
@@ -755,6 +782,11 @@ where | |||
} | |||
} | |||
|
|||
struct RuntimeSweeperState { |
There was a problem hiding this comment.
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?
lightning/src/sign/mod.rs
Outdated
@@ -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>>; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
lightning/src/util/sweep.rs
Outdated
// In a sync context, we can't wait for the future to complete. | ||
panic!("task not ready"); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
Changed my mind. Will do in a follow up. |
d317ae3
to
493b686
Compare
🔔 1st Reminder Hey @tnull! This PR has been waiting for your review. |
🔔 2nd Reminder Hey @tnull! This PR has been waiting for your review. |
🔔 3rd Reminder Hey @tnull! This PR has been waiting for your review. |
🔔 4th Reminder Hey @tnull! This PR has been waiting for your review. |
🔔 5th Reminder Hey @tnull! This PR has been waiting for your review. |
There was a problem hiding this 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.
lightning/src/util/sweep.rs
Outdated
|
||
use crate::sync::Arc; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
lightning/src/util/sweep.rs
Outdated
@@ -567,16 +623,15 @@ where | |||
} | |||
|
|||
fn spend_outputs( | |||
&self, sweeper_state: &SweeperState, descriptors: Vec<&SpendableOutputDescriptor>, | |||
&self, sweeper_state: &SweeperState, descriptors: &Vec<&SpendableOutputDescriptor>, |
There was a problem hiding this comment.
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]
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
lightning/src/util/sweep.rs
Outdated
}, | ||
Err(e) => { | ||
log_error!(self.logger, "Error spending outputs: {:?}", e); | ||
return Ok(()); |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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:
rust-lightning/lightning/src/util/sweep.rs
Line 507 in 5316257
Err(e) => { |
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.
There was a problem hiding this 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.
This PR converts
OutputSweeper
to take an asyncChangeDestinationSource
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