Skip to content

Commit d6c3be9

Browse files
committed
try out sync sweeper wrapper - does not compile
1 parent d6051d9 commit d6c3be9

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lightning/src/sign/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use crate::sign::taproot::TaprootChannelSigner;
6868
use crate::util::atomic_counter::AtomicCounter;
6969
use core::convert::TryInto;
7070
use core::future::Future;
71+
use core::ops::Deref;
7172
use core::pin::Pin;
7273
use core::sync::atomic::{AtomicUsize, Ordering};
7374
#[cfg(taproot)]
@@ -1005,9 +1006,16 @@ pub trait ChangeDestinationSourceSync {
10051006
}
10061007

10071008
/// A wrapper around [`ChangeDestinationSource`] to allow for async calls.
1008-
pub struct ChangeDestinationSourceSyncWrapper<T>(T) where T: ChangeDestinationSourceSync;
1009+
pub struct ChangeDestinationSourceSyncWrapper<T>(T) where T: ChangeDestinationSourceSync + Deref;
10091010

1010-
impl<T: ChangeDestinationSourceSync> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T> {
1011+
impl<T: ChangeDestinationSourceSync + Deref> ChangeDestinationSourceSyncWrapper<T> {
1012+
/// Creates a new [`ChangeDestinationSourceSyncWrapper`].
1013+
pub fn new(source: T) -> Self {
1014+
Self(source)
1015+
}
1016+
}
1017+
1018+
impl<T: ChangeDestinationSourceSync + Deref> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T> {
10111019
fn get_change_destination_script<'a>(&self) -> AsyncGetChangeDestinationScriptResult<'a, ScriptBuf> {
10121020
let script = self.0.get_change_destination_script();
10131021
Box::pin(async move { script })

lightning/src/util/sweep.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::io;
1515
use crate::ln::msgs::DecodeError;
1616
use crate::ln::types::ChannelId;
1717
use crate::prelude::*;
18-
use crate::sign::{ChangeDestinationSource, OutputSpender, SpendableOutputDescriptor};
18+
use crate::sign::{ChangeDestinationSource, ChangeDestinationSourceSync, ChangeDestinationSourceSyncWrapper, OutputSpender, SpendableOutputDescriptor};
1919
use crate::sync::Mutex;
2020
use crate::util::logger::Logger;
2121
use crate::util::persist::{
@@ -356,6 +356,60 @@ where
356356
logger: L,
357357
}
358358

359+
pub struct OutputSweeperSync<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
360+
where
361+
B::Target: BroadcasterInterface,
362+
D::Target: ChangeDestinationSource,
363+
E::Target: FeeEstimator,
364+
F::Target: Filter + Sync + Send,
365+
K::Target: KVStore,
366+
L::Target: Logger,
367+
O::Target: OutputSpender,
368+
{
369+
sweeper: OutputSweeper<B, D, E, F, K, L, O>,
370+
}
371+
372+
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
373+
OutputSweeperSync<B, D, E, F, K, L, O>
374+
where
375+
B::Target: BroadcasterInterface,
376+
D::Target: ChangeDestinationSource,
377+
E::Target: FeeEstimator,
378+
F::Target: Filter + Sync + Send,
379+
K::Target: KVStore,
380+
L::Target: Logger,
381+
O::Target: OutputSpender,
382+
{
383+
fn new<DA: ChangeDestinationSourceSync + Deref>(
384+
best_block: BestBlock, broadcaster: B, fee_estimator: E, chain_data_source: Option<F>,
385+
output_spender: O, change_destination_source: DA, kv_store: K, logger: L,
386+
) -> Self {
387+
let change_destination_source: = ChangeDestinationSourceSyncWrapper::new(change_destination_source);
388+
389+
let sweeper = OutputSweeper::new(
390+
best_block, broadcaster, fee_estimator, chain_data_source, output_spender,
391+
&change_destination_source, kv_store, logger,
392+
);
393+
Self { sweeper }
394+
}
395+
396+
/// Regenerates and broadcasts the spending transaction for any outputs that are pending
397+
pub fn regenerate_and_broadcast_spend_if_necessary(&self) -> Result<(), ()> {
398+
let mut fut = Box::pin(self.sweeper.regenerate_and_broadcast_spend_if_necessary_async());
399+
let mut waker = dummy_waker();
400+
let mut ctx = task::Context::from_waker(&mut waker);
401+
match fut.as_mut().poll(&mut ctx) {
402+
task::Poll::Ready(result) => {
403+
result
404+
},
405+
task::Poll::Pending => {
406+
// In a sync context, we can't wait for the future to complete.
407+
panic!("task not ready");
408+
},
409+
}
410+
}
411+
}
412+
359413
impl<B: Deref, D: Deref, E: Deref, F: Deref, K: Deref, L: Deref, O: Deref>
360414
OutputSweeper<B, D, E, F, K, L, O>
361415
where

0 commit comments

Comments
 (0)